DataUtils.java
3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.icc.dbforms.exceptions.ICCException
*/
package com.adobe.fd.adaddon.internal.utils;
import com.adobe.icc.dbforms.exceptions.ICCException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class DataUtils {
public static final String DSOM_PREFIX = "xfa[0].datasets[0].data[0].";
public static Node resolveDataSom(Element rootElement, String dsom, boolean peek) {
String[] components;
if (dsom == null || rootElement == null) {
return null;
}
String rootName = DataUtils.getRootElementName(dsom);
String dataSom = dsom.substring("xfa[0].datasets[0].data[0].".length() + rootName.length() + 4);
Node resolvedNode = null;
Element parentElement = rootElement;
for (String comp : components = dataSom.split("\\.")) {
int elemOccur;
Pattern pattern = Pattern.compile("(.+)\\[(\\d+)\\]");
Matcher m = pattern.matcher(comp);
if (!m.find()) {
throw new ICCException("invalid component in DSOM {" + dsom + "} (expected '{name}[{occur}]'): " + comp);
}
String elemName = m.group(1);
resolvedNode = DataUtils.getChildOccurrence(parentElement, elemName, elemOccur = Integer.parseInt(m.group(2)), !peek);
if (resolvedNode == null || !(resolvedNode instanceof Element)) break;
parentElement = (Element)resolvedNode;
}
return resolvedNode;
}
private static String getRootElementName(String dsom) {
if (dsom == null) {
return null;
}
if (dsom.indexOf("xfa[0].datasets[0].data[0].") != 0) {
throw new ICCException("invalid DSOM expression: " + dsom);
}
String dataSom = dsom.substring("xfa[0].datasets[0].data[0].".length());
String rootName = null;
int rootPos = dataSom.indexOf("[0]");
if (rootPos <= 0) {
throw new ICCException("missing root subform/data element in DSOM: " + dsom);
}
rootName = dataSom.substring(0, rootPos);
return rootName;
}
private static Node getChildOccurrence(Element parentElement, String elementName, int occurrence, boolean create) {
if (occurrence < 0) {
return null;
}
Node targetNode = null;
int occur = -1;
NodeList children = parentElement.getChildNodes();
for (int i = 0; i < children.getLength(); ++i) {
Node childNode = children.item(i);
if (!(childNode instanceof Element) || !childNode.getNodeName().equals(elementName) || ++occur != occurrence) continue;
targetNode = childNode;
break;
}
if (targetNode == null && create) {
for (int j = 0; j < occurrence - occur; ++j) {
targetNode = DataUtils.addChildElement(parentElement, elementName, null);
}
}
return targetNode;
}
private static Node addChildElement(Element parentElement, String elementName, String nameAttrValue) {
if (parentElement == null || elementName == null) {
return null;
}
Element childElement = parentElement.getOwnerDocument().createElement(elementName);
parentElement.appendChild(childElement);
if (nameAttrValue != null) {
childElement.setTextContent(nameAttrValue);
}
return childElement;
}
}