XMLUtils.java
12.4 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.fd.forms.internal.utils;
import com.adobe.fd.forms.internal.logging.FormsServiceLogger;
import com.adobe.fd.forms.internal.utils.CustomExternalEntityResolver;
import com.adobe.fd.forms.internal.utils.StringUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public final class XMLUtils {
private static FormsServiceLogger logger = new FormsServiceLogger(XMLUtils.class);
private static ParserPool m_DocumentBuilderPool = new ParserPool();
private static TransformerPool m_TransformerPool = new TransformerPool();
private static final SAXParserFactory m_SAXParserFactory = SAXParserFactory.newInstance();
private XMLUtils() {
}
public static SAXParserFactory getSAXParserFactoryInstance() {
return m_SAXParserFactory;
}
public static Node selectNode(Element oParentElement, String sTagName, int nItemNum) {
if (oParentElement == null) {
return null;
}
StringTokenizer toks = new StringTokenizer(sTagName, "/");
int iToks = toks.countTokens();
if (iToks > 0) {
String tok = toks.nextToken();
Vector<Node> oNodeList = new Vector<Node>();
for (Node oCh = oParentElement.getFirstChild(); oCh != null; oCh = oCh.getNextSibling()) {
if (oCh.getNodeType() != 1 || !oCh.getNodeName().equals(tok)) continue;
oNodeList.add(oCh);
}
int nodeCnt = oNodeList.size();
if (iToks == 1) {
if (nodeCnt > nItemNum) {
return (Node)oNodeList.elementAt(nItemNum);
}
return null;
}
for (int i = 0; i < nodeCnt; ++i) {
Node oNode = XMLUtils.selectNode((Element)oNodeList.elementAt(i), sTagName.substring(sTagName.indexOf(tok) + tok.length() + 1), nItemNum);
if (oNode == null) continue;
return oNode;
}
return null;
}
return null;
}
public static String getNodeTextValue(Node oNode) {
return XMLUtils.getNodeTextValue(oNode, false);
}
public static String getNodeTextValue(Node oNode, boolean bDeep) {
StringBuffer tb = new StringBuffer();
NodeList oNodeList = oNode.getChildNodes();
for (int i = 0; i < oNodeList.getLength(); ++i) {
Node oItem = oNodeList.item(i);
if (oItem.getNodeType() == 3) {
tb.append(oItem.getNodeValue());
}
if (oItem.getNodeType() != 1 || !bDeep) continue;
tb.append(XMLUtils.getNodeTextValue(oItem, bDeep));
}
return tb.toString();
}
public static Node copyNodes(Node oSrcNode, Node oDstNode) {
return XMLUtils.copyNodes(oSrcNode, oDstNode, null);
}
public static Node copyNodes(Node oSrcNode, Node oDstNode, String nodeName) {
Document oDstDocument = null;
oDstDocument = oDstNode instanceof Document ? (Document)oDstNode : oDstNode.getOwnerDocument();
Node oNewNode = null;
short nNodeType = oSrcNode.getNodeType();
if (nNodeType == 1) {
if (nodeName == null || nodeName.length() == 0) {
nodeName = oSrcNode.getNodeName();
}
oNewNode = oDstDocument.createElement(nodeName);
oDstNode.appendChild(oNewNode);
NamedNodeMap oMap = oSrcNode.getAttributes();
for (int i = 0; i < oMap.getLength(); ++i) {
Node oAttr = oMap.item(i);
Element oNewElement = (Element)oNewNode;
String sAttributeName = oAttr.getNodeName();
String sAttributeValue = oAttr.getNodeValue();
oNewElement.setAttribute(sAttributeName, sAttributeValue);
}
NodeList oNodeList = oSrcNode.getChildNodes();
for (int i2 = 0; i2 < oNodeList.getLength(); ++i2) {
XMLUtils.copyNodes(oNodeList.item(i2), oNewNode);
}
} else if (nNodeType == 3) {
oNewNode = oDstDocument.createTextNode(oSrcNode.getNodeValue());
oDstNode.appendChild(oNewNode);
} else if (nNodeType == 8) {
oNewNode = oDstDocument.createComment(oSrcNode.getNodeValue());
oDstNode.appendChild(oNewNode);
}
return oNewNode;
}
public static Element getChildElement(Element oParent, String sTagName) {
if (oParent == null) {
return null;
}
Element elem = null;
NodeList oNodes = oParent.getChildNodes();
for (int i = 0; i < oNodes.getLength(); ++i) {
if (oNodes.item(i).getNodeType() != 1) continue;
if (sTagName != null) {
if (!oNodes.item(i).getNodeName().equalsIgnoreCase(sTagName)) continue;
elem = (Element)oNodes.item(i);
break;
}
elem = (Element)oNodes.item(i);
break;
}
return elem;
}
public static byte[] toXML(DOMSource oSource) throws TransformerException {
return XMLUtils.toXML(oSource, false);
}
public static byte[] toXML(DOMSource oSource, boolean canonicalize) throws TransformerException {
Properties props = new Properties();
props.put("method", "xml");
props.put("encoding", "UTF-8");
props.put("omit-xml-declaration", "no");
props.put("indent", "yes");
return XMLUtils.transformToBytes(oSource, props, canonicalize);
}
public static byte[] transformToBytes(DOMSource oSource, Properties props, boolean canonicalize) throws TransformerException {
Transformer xml_out = m_TransformerPool.get();
xml_out.setOutputProperties(props);
ByteArrayOutputStream out = new ByteArrayOutputStream();
xml_out.transform(oSource, new StreamResult(out));
m_TransformerPool.put(xml_out);
return out.toByteArray();
}
public static String getElementFast(byte[] content, String sElement) {
return XMLUtils.getElementFast(content, 0, sElement);
}
public static String getElementFast(byte[] content, int startIndex, String sElement) {
if (content == null) {
return null;
}
sElement = "<" + sElement;
int iBegin = StringUtils.findArraySegment(content, startIndex, sElement.getBytes());
if (iBegin < 0) {
return null;
}
int iEnd = StringUtils.findArraySegment(content, iBegin, ">".getBytes());
if (iEnd < 0) {
return null;
}
return new String(content, iBegin, iEnd - iBegin + 1);
}
public static String getPreprocessorInstructionValue(byte[] content, String sProcessor, String sInstruction) {
String sValue = null;
String sPI = XMLUtils.getElementFast(content, "?" + sProcessor + " " + sInstruction);
if (sPI != null) {
int nInst = sPI.indexOf(sInstruction);
int nStart = sPI.indexOf("=", nInst) + 1;
if (nStart <= 0) {
nStart = sPI.indexOf(" ", nInst) + 1;
}
int nEnd = sPI.indexOf("?", nStart);
if (nStart > 0 && nEnd > nStart) {
sValue = sPI.substring(nStart, nEnd);
}
}
return sValue;
}
public static Document loadXMLbytes(byte[] XMLBytes, boolean resolveExtEntityFlag) throws SAXException, IOException, ParserConfigurationException {
InputSource oInputSource = new InputSource(new ByteArrayInputStream(XMLBytes));
return XMLUtils.initDocument(null, oInputSource, resolveExtEntityFlag);
}
public static Document initDocument(Document oDocument, InputSource oInputSource, boolean resolveExtEntityFlag) throws SAXException, IOException, ParserConfigurationException {
DocumentBuilder db = m_DocumentBuilderPool.get(resolveExtEntityFlag);
oDocument = oDocument == null ? (oInputSource != null ? db.parse(oInputSource) : db.newDocument()) : db.newDocument();
m_DocumentBuilderPool.put(db);
return oDocument;
}
public static String selectNodeValue(Element oParentElement, String sTagName, int nItemNum) {
if (oParentElement == null) {
return "";
}
Node oNode = XMLUtils.selectNode(oParentElement, sTagName, nItemNum);
if (oNode != null) {
return XMLUtils.getNodeTextValue(oNode);
}
return "";
}
public static void removeChildNodes(Element oParent) {
if (oParent != null && oParent.hasChildNodes()) {
NodeList oNodes = oParent.getChildNodes();
for (int i = 0; i < oNodes.getLength(); ++i) {
oParent.removeChild(oNodes.item(i));
}
}
}
public static void appendText(Element oParent, String sText) {
if (oParent != null && sText != null) {
oParent.appendChild(oParent.getOwnerDocument().createTextNode(sText));
}
}
public static Element createElement(Document oDoc, String name, String val) {
Element oOptionElem = oDoc.createElement(name);
if (val != null && val.length() > 0) {
Text oText = oDoc.createTextNode(val);
oOptionElem.appendChild(oText);
}
return oOptionElem;
}
private static class TransformerPool {
private static final Stack<Transformer> stack = new Stack();
private static final TransformerFactory tf = TransformerFactory.newInstance();
private TransformerPool() {
}
public synchronized Transformer get() throws TransformerException {
if (stack.isEmpty()) {
return tf.newTransformer();
}
return stack.pop();
}
public synchronized void put(Transformer t) {
stack.push(t);
}
}
private static class ParserPool {
private static final Stack<DocumentBuilder> stack = new Stack();
private static final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
private ParserPool() {
}
public synchronized DocumentBuilder get(boolean resolveExtEntityFlag) throws ParserConfigurationException {
try {
DocumentBuilder docBuilder = null;
docBuilder = stack.isEmpty() ? dbf.newDocumentBuilder() : stack.pop();
CustomExternalEntityResolver entityResolver = CustomExternalEntityResolver.valueOf(resolveExtEntityFlag);
docBuilder.setEntityResolver(entityResolver);
return docBuilder;
}
catch (Exception e) {
logger.debug("AEM_FRM_001_004", new String[]{e.getMessage()}, e);
throw new ParserConfigurationException(e.getMessage());
}
}
public synchronized void put(DocumentBuilder db) {
stack.push(db);
}
static {
dbf.setNamespaceAware(true);
dbf.setValidating(false);
try {
dbf.setFeature("http://javax.xml.XMLConstants/feature/secure-processing", true);
}
catch (ParserConfigurationException pse) {
logger.debug("AEM_FRM_001_004", new String[]{pse.getMessage()}, pse);
}
dbf.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", Boolean.valueOf("false"));
dbf.setAttribute("http://apache.org/xml/features/allow-java-encodings", Boolean.valueOf("true"));
}
}
}