|
27 | 27 | import java.io.OutputStream;
|
28 | 28 | import java.io.Reader;
|
29 | 29 | import java.io.StringReader;
|
| 30 | +import java.io.StringWriter; |
30 | 31 | import java.io.Writer;
|
31 | 32 | import java.util.concurrent.atomic.AtomicReference;
|
32 | 33 |
|
|
46 | 47 | import javax.xml.stream.events.XMLEvent;
|
47 | 48 | import javax.xml.transform.Result;
|
48 | 49 | import javax.xml.transform.Source;
|
| 50 | +import javax.xml.transform.Transformer; |
| 51 | +import javax.xml.transform.TransformerException; |
| 52 | +import javax.xml.transform.TransformerFactory; |
49 | 53 | import javax.xml.transform.dom.DOMResult;
|
50 | 54 | import javax.xml.transform.dom.DOMSource;
|
| 55 | +import javax.xml.transform.stream.StreamResult; |
51 | 56 | import javax.xml.transform.stream.StreamSource;
|
52 | 57 | import javax.xml.validation.Schema;
|
53 |
| -import javax.xml.validation.SchemaFactory; |
54 | 58 | import javax.xml.validation.Validator;
|
55 | 59 |
|
56 | 60 | import org.apache.log4j.Logger;
|
| 61 | +import org.w3c.dom.Element; |
57 | 62 | import org.w3c.dom.Document;
|
58 | 63 | import org.w3c.dom.Node;
|
| 64 | +import org.w3c.dom.NodeList; |
59 | 65 | import org.xml.sax.InputSource;
|
60 | 66 | import org.xml.sax.SAXException;
|
61 | 67 |
|
@@ -402,8 +408,29 @@ public static boolean compareXMLContent(final InputSource content1, final InputS
|
402 | 408 | return (handler1.getRootElement().equals(handler2.getRootElement()));
|
403 | 409 | }
|
404 | 410 |
|
405 |
| - private static SchemaFactory newSchemaFactory() { |
406 |
| - return SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); |
| 411 | + /** |
| 412 | + * Compare two DOM Nodes. |
| 413 | + * @param node1 The first Node. |
| 414 | + * @param node2 The second Node. |
| 415 | + * @return true if equals, false otherwise. |
| 416 | + * @throws ParserConfigurationException Parser confiuration exception |
| 417 | + * @throws TransformerException Transformer exception |
| 418 | + * @throws SAXException SAX exception |
| 419 | + * @throws IOException If unable to read the stream |
| 420 | + */ |
| 421 | + public static boolean compareXMLContent(final Node node1, final Node node2) |
| 422 | + throws ParserConfigurationException, TransformerException, SAXException, IOException { |
| 423 | + TransformerFactory transFactory = TransformerFactory.newInstance(); |
| 424 | + Transformer transformer = transFactory.newTransformer(); |
| 425 | + StringWriter writer1 = new StringWriter(); |
| 426 | + StringWriter writer2 = new StringWriter(); |
| 427 | + DOMSource source = new DOMSource(node1); |
| 428 | + StreamResult result = new StreamResult(writer1); |
| 429 | + transformer.transform(source, result); |
| 430 | + source = new DOMSource(node2); |
| 431 | + result = new StreamResult(writer2); |
| 432 | + transformer.transform(source, result); |
| 433 | + return compareXMLContent(writer1.toString(), writer2.toString()); |
407 | 434 | }
|
408 | 435 |
|
409 | 436 | /**
|
@@ -447,6 +474,48 @@ private static Document getNewDocument()
|
447 | 474 | return builder.newDocument();
|
448 | 475 | }
|
449 | 476 | }
|
| 477 | + |
| 478 | + /** |
| 479 | + * Transform a DOM Node to String. |
| 480 | + * @param node The Node to be transformed. |
| 481 | + * @return a String representation. |
| 482 | + * @throws ParserConfigurationException Parser confiuration exception |
| 483 | + * @throws TransformerException Transformer exception |
| 484 | + */ |
| 485 | + public static String toString(final Node node) |
| 486 | + throws ParserConfigurationException, TransformerException { |
| 487 | + TransformerFactory transFactory = TransformerFactory.newInstance(); |
| 488 | + Transformer transformer = transFactory.newTransformer(); |
| 489 | + StringWriter writer = new StringWriter(); |
| 490 | + DOMSource source = new DOMSource(node); |
| 491 | + StreamResult result = new StreamResult(writer); |
| 492 | + transformer.transform(source, result); |
| 493 | + return writer.toString(); |
| 494 | + } |
| 495 | + |
| 496 | + /** |
| 497 | + * Get the first child Element of the supplied node that matches a given tag name. |
| 498 | + * |
| 499 | + * @param node The DOM Node. |
| 500 | + * @param name The name of the child node to search for. |
| 501 | + * @return The first child element with the matching tag name. |
| 502 | + */ |
| 503 | + public static Element getFirstChildElementByName(Node node, String name) { |
| 504 | + NodeList children = node.getChildNodes(); |
| 505 | + int childCount = children.getLength(); |
| 506 | + |
| 507 | + for (int i = 0; i < childCount; i++) { |
| 508 | + Node child = children.item(i); |
| 509 | + if (child != null |
| 510 | + && child.getNodeType() == Node.ELEMENT_NODE |
| 511 | + && child.getNodeName() != null |
| 512 | + && child.getNodeName().equals(name)) { |
| 513 | + return (Element) child; |
| 514 | + } |
| 515 | + } |
| 516 | + |
| 517 | + return null; |
| 518 | + } |
450 | 519 |
|
451 | 520 | /**
|
452 | 521 | * Get the document builder for creation
|
|
0 commit comments