/
XMLUtil.java
534 lines (457 loc) · 15.4 KB
/
XMLUtil.java
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
* Copyright 2009 Red Hat, Inc.
* Red Hat licenses this file to you under the Apache License, version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package org.hornetq.utils;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.hornetq.core.logging.Logger;
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.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
* @author <a href="mailto:ovidiu@feodorov.com">Ovidiu Feodorov</a>
* @author <a href="mailto:tim.fox@jboss.com">Tim Fox</a>
* @version <tt>$Revision$</tt>
* $Id$
*/
public class XMLUtil
{
// Constants ------------------------------------------------------------------------------------
private static final Logger log = Logger.getLogger(XMLUtil.class);
// Static ---------------------------------------------------------------------------------------
public static Element stringToElement(final String s) throws Exception
{
return XMLUtil.readerToElement(new StringReader(s));
}
public static Element urlToElement(final URL url) throws Exception
{
return XMLUtil.readerToElement(new InputStreamReader(url.openStream()));
}
public static String readerToString(final Reader r) throws Exception
{
// Read into string
StringBuffer buff = new StringBuffer();
int c;
while ((c = r.read()) != -1)
{
buff.append((char)c);
}
return buff.toString();
}
public static Element readerToElement(final Reader r) throws Exception
{
// Read into string
StringBuffer buff = new StringBuffer();
int c;
while ((c = r.read()) != -1)
{
buff.append((char)c);
}
// Quick hardcoded replace, FIXME this is a kludge - use regexp to match properly
String s = buff.toString();
StringReader sreader = new StringReader(s);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6529766
factory.setNamespaceAware(true);
DocumentBuilder parser = factory.newDocumentBuilder();
Document doc = parser.parse(new InputSource(sreader));
return doc.getDocumentElement();
}
public static String elementToString(final Node n)
{
String name = n.getNodeName();
short type = n.getNodeType();
if (Node.CDATA_SECTION_NODE == type)
{
return "<![CDATA[" + n.getNodeValue() + "]]>";
}
if (name.startsWith("#"))
{
return "";
}
StringBuffer sb = new StringBuffer();
sb.append('<').append(name);
NamedNodeMap attrs = n.getAttributes();
if (attrs != null)
{
for (int i = 0; i < attrs.getLength(); i++)
{
Node attr = attrs.item(i);
sb.append(' ').append(attr.getNodeName()).append("=\"").append(attr.getNodeValue()).append("\"");
}
}
String textContent = null;
NodeList children = n.getChildNodes();
if (children.getLength() == 0)
{
if ((textContent = XMLUtil.getTextContent(n)) != null && !"".equals(textContent))
{
sb.append(textContent).append("</").append(name).append('>');;
}
else
{
sb.append("/>").append('\n');
}
}
else
{
sb.append('>').append('\n');
boolean hasValidChildren = false;
for (int i = 0; i < children.getLength(); i++)
{
String childToString = XMLUtil.elementToString(children.item(i));
if (!"".equals(childToString))
{
sb.append(childToString);
hasValidChildren = true;
}
}
if (!hasValidChildren && (textContent = XMLUtil.getTextContent(n)) != null)
{
sb.append(textContent);
}
sb.append("</").append(name).append('>');
}
return sb.toString();
}
private static final Object[] EMPTY_ARRAY = new Object[0];
/**
* This metod is here because Node.getTextContent() is not available in JDK 1.4 and I would like
* to have an uniform access to this functionality.
*
* Note: if the content is another element or set of elements, it returns a string representation
* of the hierarchy.
*
* TODO implementation of this method is a hack. Implement it properly.
*/
public static String getTextContent(final Node n)
{
if (n.hasChildNodes())
{
StringBuffer sb = new StringBuffer();
NodeList nl = n.getChildNodes();
for (int i = 0; i < nl.getLength(); i++)
{
sb.append(XMLUtil.elementToString(nl.item(i)));
if (i < nl.getLength() - 1)
{
sb.append('\n');
}
}
String s = sb.toString();
if (s.length() != 0)
{
return s;
}
}
Method[] methods = Node.class.getMethods();
for (Method getTextContext : methods)
{
if ("getTextContent".equals(getTextContext.getName()))
{
try
{
return (String)getTextContext.invoke(n, XMLUtil.EMPTY_ARRAY);
}
catch (Exception e)
{
XMLUtil.log.error("Failed to invoke getTextContent() on node " + n, e);
return null;
}
}
}
String textContent = null;
if (n.hasChildNodes())
{
NodeList nl = n.getChildNodes();
for (int i = 0; i < nl.getLength(); i++)
{
Node c = nl.item(i);
if (c.getNodeType() == Node.TEXT_NODE)
{
textContent = n.getNodeValue();
if (textContent == null)
{
// TODO This is a hack. Get rid of it and implement this properly
String s = c.toString();
int idx = s.indexOf("#text:");
if (idx != -1)
{
textContent = s.substring(idx + 6).trim();
if (textContent.endsWith("]"))
{
textContent = textContent.substring(0, textContent.length() - 1);
}
}
}
if (textContent == null)
{
break;
}
}
}
// TODO This is a hack. Get rid of it and implement this properly
String s = n.toString();
int i = s.indexOf('>');
int i2 = s.indexOf("</");
if (i != -1 && i2 != -1)
{
textContent = s.substring(i + 1, i2);
}
}
return textContent;
}
public static void assertEquivalent(final Node node, final Node node2)
{
if (node == null)
{
throw new IllegalArgumentException("the first node to be compared is null");
}
if (node2 == null)
{
throw new IllegalArgumentException("the second node to be compared is null");
}
if (!node.getNodeName().equals(node2.getNodeName()))
{
throw new IllegalArgumentException("nodes have different node names");
}
int attrCount = 0;
NamedNodeMap attrs = node.getAttributes();
if (attrs != null)
{
attrCount = attrs.getLength();
}
int attrCount2 = 0;
NamedNodeMap attrs2 = node2.getAttributes();
if (attrs2 != null)
{
attrCount2 = attrs2.getLength();
}
if (attrCount != attrCount2)
{
throw new IllegalArgumentException("nodes hava a different number of attributes");
}
outer: for (int i = 0; i < attrCount; i++)
{
Node n = attrs.item(i);
String name = n.getNodeName();
String value = n.getNodeValue();
for (int j = 0; j < attrCount; j++)
{
Node n2 = attrs2.item(j);
String name2 = n2.getNodeName();
String value2 = n2.getNodeValue();
if (name.equals(name2) && value.equals(value2))
{
continue outer;
}
}
throw new IllegalArgumentException("attribute " + name + "=" + value + " doesn't match");
}
boolean hasChildren = node.hasChildNodes();
if (hasChildren != node2.hasChildNodes())
{
throw new IllegalArgumentException("one node has children and the other doesn't");
}
if (hasChildren)
{
NodeList nl = node.getChildNodes();
NodeList nl2 = node2.getChildNodes();
short[] toFilter = new short[] { Node.TEXT_NODE, Node.ATTRIBUTE_NODE, Node.COMMENT_NODE };
List nodes = XMLUtil.filter(nl, toFilter);
List nodes2 = XMLUtil.filter(nl2, toFilter);
int length = nodes.size();
if (length != nodes2.size())
{
throw new IllegalArgumentException("nodes hava a different number of children");
}
for (int i = 0; i < length; i++)
{
Node n = (Node)nodes.get(i);
Node n2 = (Node)nodes2.get(i);
XMLUtil.assertEquivalent(n, n2);
}
}
}
public static String stripCDATA(String s)
{
s = s.trim();
if (s.startsWith("<![CDATA["))
{
s = s.substring(9);
int i = s.indexOf("]]>");
if (i == -1)
{
throw new IllegalStateException("argument starts with <![CDATA[ but cannot find pairing ]]>");
}
s = s.substring(0, i);
}
return s;
}
/* public static String replaceSystemProps(String xml)
{
Properties properties = System.getProperties();
Enumeration e = properties.propertyNames();
while (e.hasMoreElements())
{
String key = (String)e.nextElement();
String s = "${" + key + "}";
if (xml.contains(s))
{
xml = xml.replace(s, properties.getProperty(key));
}
}
return xml;
}*/
public static String replaceSystemProps(String xml)
{
while (xml.contains("${"))
{
int start = xml.indexOf("${");
int end = xml.indexOf("}") + 1;
if (end < 0)
{
break;
}
String subString = xml.substring(start, end);
String prop = subString.substring(2, subString.length() - 1).trim();
String val = "";
if (prop.contains(":"))
{
String[] parts = prop.split(":", 2);
prop = parts[0].trim();
val = parts[1].trim();
}
String sysProp = System.getProperty(prop, val);
XMLUtil.log.debug("replacing " + subString + " with " + sysProp);
xml = xml.replace(subString, sysProp);
}
return xml;
}
public static long parseLong(final Node elem)
{
String value = elem.getTextContent().trim();
try
{
return Long.parseLong(value);
}
catch (NumberFormatException e)
{
throw new IllegalArgumentException("Element " + elem +
" requires a valid Long value, but '" +
value +
"' cannot be parsed as a Long");
}
}
public static int parseInt(final Node elem)
{
String value = elem.getTextContent().trim();
try
{
return Integer.parseInt(value);
}
catch (NumberFormatException e)
{
throw new IllegalArgumentException("Element " + elem +
" requires a valid Integer value, but '" +
value +
"' cannot be parsed as an Integer");
}
}
public static boolean parseBoolean(final Node elem)
{
String value = elem.getTextContent().trim();
try
{
return Boolean.parseBoolean(value);
}
catch (NumberFormatException e)
{
throw new IllegalArgumentException("Element " + elem +
" requires a valid Boolean value, but '" +
value +
"' cannot be parsed as a Boolean");
}
}
public static double parseDouble(final Node elem)
{
String value = elem.getTextContent().trim();
try
{
return Double.parseDouble(value);
}
catch (NumberFormatException e)
{
throw new IllegalArgumentException("Element " + elem +
" requires a valid Double value, but '" +
value +
"' cannot be parsed as a Double");
}
}
public static void validate(final Node node, final String schemaFile) throws Exception
{
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(ClassloadingUtil.findResource(schemaFile));
Validator validator = schema.newValidator();
// validate the DOM tree
try
{
validator.validate(new DOMSource(node));
}
catch (SAXException e)
{
XMLUtil.log.error("Invalid configuration", e);
throw new IllegalStateException("Invalid configuration", e);
}
}
// Attributes -----------------------------------------------------------------------------------
// Constructors ---------------------------------------------------------------------------------
// Public ---------------------------------------------------------------------------------------
// Package protected ----------------------------------------------------------------------------
// Protected ------------------------------------------------------------------------------------
// Private --------------------------------------------------------------------------------------
private static List filter(final NodeList nl, final short[] typesToFilter)
{
List nodes = new ArrayList();
outer: for (int i = 0; i < nl.getLength(); i++)
{
Node n = nl.item(i);
short type = n.getNodeType();
for (int j = 0; j < typesToFilter.length; j++)
{
if (typesToFilter[j] == type)
{
continue outer;
}
}
nodes.add(n);
}
return nodes;
}
// Inner classes --------------------------------------------------------------------------------
}