1 package org.xmldb.xupdate.unittests; 2 3 55 56 import org.w3c.dom.Document ; 57 import org.w3c.dom.NamedNodeMap ; 58 import org.w3c.dom.Node ; 59 import org.w3c.dom.NodeList ; 60 61 63 public class CompareDocuments { 64 65 68 public CompareDocuments() { 69 } 70 71 public void compare(Node node1, Node node2) throws Exception { 72 compare(node1, node2, "", true); 73 } 74 75 public void compare(Node node1, Node node2, String space, boolean show) throws Exception { 76 if (node1.getNodeType() == Node.DOCUMENT_NODE && node2.getNodeType() == Node.DOCUMENT_NODE) { 77 compare(((Document ) node1).getDocumentElement(), ((Document ) node2).getDocumentElement(), space, show); 78 return; 79 } 80 81 if (show) { 82 System.err.print(space); 83 switch (node1.getNodeType()) { 84 case Node.ATTRIBUTE_NODE: 85 System.err.print("@"); 86 default: 87 } 88 System.err.print(node1 + "[" + node1.getNodeType() + "] <==> "); 89 switch (node2.getNodeType()) { 90 case Node.ATTRIBUTE_NODE: 91 System.err.print("@"); 92 default: 93 } 94 System.err.println(node2 + "[" + node2.getNodeType() + "]"); 95 } 96 if (node1.getNodeType() != node2.getNodeType()) { 97 throw new Exception ("different node types (" 98 + node1.getNodeType() + "!=" + node2.getNodeType() + ")..."); 99 } 100 if (node1.getNamespaceURI() == null ^ node2.getNamespaceURI() == null) { 101 throw new Exception ("only one node has a Namespace"); 102 } 103 if (node1.getNamespaceURI() != null && node2.getNamespaceURI() != null 104 && !node1.getNamespaceURI().equals(node2.getNamespaceURI())) { 105 throw new Exception ("different NamespaceURI's (" 106 + node1.getNamespaceURI() + "!=" + node2.getNamespaceURI() + ")..."); 107 } 108 if (!node1.getNodeName().equals(node2.getNodeName())) { 109 throw new Exception ("different node names (" 110 + node1.getNodeName() + "!=" + node2.getNodeName() + "..."); 111 } 112 if (node1.getNodeValue() != null && node2.getNodeValue() != null 113 && !node1.getNodeValue().equals(node2.getNodeValue())) { 114 throw new Exception ("different node values (" 115 + node1.getNodeValue() + "!=" + node2.getNodeValue() + ")..."); 116 } 117 NamedNodeMap attr1 = node1.getAttributes(); 118 NamedNodeMap attr2 = node2.getAttributes(); 119 if (attr1 != null && attr2 != null) { 120 if (attr1.getLength() != attr2.getLength()) { 121 throw new Exception ("different attribute counts..."); 122 } 123 for (int i = 0; i < attr1.getLength(); i++) { 124 compare(attr1.item(i), attr2.item(i), space, show); 125 } 126 } 127 NodeList list1 = node1.getChildNodes(); 128 NodeList list2 = node2.getChildNodes(); 129 if (list1.getLength() != list2.getLength()) { 130 throw new Exception ("different child node counts(" 131 + list1.getLength() + "!=" + list2.getLength() + ")..."); 132 } 133 153 Node child1 = node1.getFirstChild(); 154 Node child2 = node2.getFirstChild(); 155 space += " "; 156 while (child1 != null) { 157 compare(child1, child2, space, show); 158 child1 = child1.getNextSibling(); 159 child2 = child2.getNextSibling(); 160 } 161 } 162 163 } 164 | Popular Tags |