1 19 20 package org.netbeans.modules.db.test; 21 22 import java.util.regex.Pattern ; 23 import org.w3c.dom.Attr ; 24 import org.w3c.dom.Document ; 25 import org.w3c.dom.Element ; 26 import org.w3c.dom.NamedNodeMap ; 27 import org.w3c.dom.Node ; 28 import org.w3c.dom.NodeList ; 29 30 37 public class DOMCompare { 38 39 private DOMCompare() { 40 } 41 42 public static boolean compareDocuments(Document doc1, Document doc2) { 43 Element e1 = doc1.getDocumentElement(); 44 Element e2 = doc2.getDocumentElement(); 45 46 return compareElements(e1, e2); 47 } 48 49 private static boolean compareElements(Element e1, Element e2) { 50 if (!e1.getLocalName().equals(e2.getLocalName())) { 51 System.out.println("Different local names " + e1.getLocalName() + " and " + e2.getLocalName()); 52 return false; 53 } 54 if (!compareStrings(e1.getNamespaceURI(), e2.getNamespaceURI())) { 55 System.out.println("Different namespaces " + e1.getNamespaceURI() + " and " + e2.getNamespaceURI()); 56 return false; 57 } 58 if (!compareElementAttrs(e1, e2)) { 59 return false; 60 } 61 if (!compareElementChildren(e1, e2)) { 62 return false; 63 } 64 return true; 65 } 66 67 private static boolean compareElementAttrs(Element e1, Element e2) { 68 NamedNodeMap at1 = e1.getAttributes(); 69 NamedNodeMap at2 = e2.getAttributes(); 70 if (at1.getLength() != at2.getLength()) { 71 System.out.println("Different number of attributes"); 72 } 73 for (int i = 0; i < at1.getLength(); i++) { 74 Attr attr1 = (Attr )at1.item(i); 75 Attr attr2 = (Attr )at2.getNamedItemNS(attr1.getNamespaceURI(), attr1.getLocalName()); 76 if (attr2 == null) { 77 System.out.println("Attribute " + attr1.getNodeName() + " not found"); 78 return false; 79 } 80 if (!compareStrings(attr1.getNodeValue(), attr2.getNodeValue())) { 81 System.out.println("Different attributes " + attr1.getNodeName() + " and " + attr2.getNodeName()); 82 return false; 83 } 84 } 85 return true; 86 } 87 88 private static boolean compareElementChildren(Element e1, Element e2) { 89 NodeList ch1 = e1.getChildNodes(); 90 NodeList ch2 = e2.getChildNodes(); 91 int i1 = 0; 92 int i2 = 0; 93 for (;;) { 94 while (i1 < ch1.getLength()) { 95 Node node = ch1.item(i1); 96 int type = node.getNodeType(); 97 if (type == Node.ELEMENT_NODE) { 98 break; 99 } else if (type == Node.TEXT_NODE) { 100 if (!isWhitespace(node.getNodeValue())) { 101 break; 102 } 103 } else { 104 System.out.println("Unsupported node type " + type); 105 return false; 106 } 107 i1++; 108 } 109 while (i2 < ch2.getLength()) { 110 Node node = ch2.item(i2); 111 int type = node.getNodeType(); 112 if (type == Node.ELEMENT_NODE) { 113 break; 114 } else if (type == Node.TEXT_NODE) { 115 if (!isWhitespace(node.getNodeValue())) { 116 break; 117 } 118 } else { 119 System.out.println("Unsupported node type " + type); 120 return false; 121 } 122 i2++; 123 } 124 if (i1 < ch1.getLength() && i2 < ch2.getLength()) { 125 if (ch1.item(i1).getNodeType() != ch2.item(i2).getNodeType()) { 126 System.out.println("Different element types: " + ch1.item(i1).getNodeType() + " and " + ch2.item(i2).getNodeType()); 127 return false; 128 } 129 switch (ch1.item(i1).getNodeType()) { 130 case Node.ELEMENT_NODE: 131 if (!compareElements((Element )ch1.item(i1), (Element )ch2.item(i2))) { 132 System.out.println("Different elements " + getElementStringRep((Element )ch1.item(i1)) + " and " + getElementStringRep((Element )ch2.item(i2))); 133 return false; 134 } 135 break; 136 case Node.TEXT_NODE: 137 if (!compareStrings(ch1.item(i1).getNodeValue(), ch2.item(i2).getNodeValue())) { 138 System.out.println("Different text content '" + ch1.item(i1).getNodeValue() + "' and '" + ch2.item(i2).getNodeValue() + "'"); 139 return false; 140 } 141 break; 142 default: 143 assert false; 144 } 145 i1++; 146 i2++; 147 } else { 148 if (i1 >= ch1.getLength() && i2 >= ch2.getLength()) { 149 return true; 150 } else { 151 System.out.println("More children in " + getElementStringRep((Element )e1)); 152 return false; 153 } 154 } 155 } 156 } 157 158 private static String getElementStringRep(Element el) { 159 String result = el.getLocalName(); 160 if (el.getNamespaceURI() != null) { 161 result = el.getNamespaceURI() + ":" + result; 162 } 163 return result; 164 } 165 166 private static boolean compareStrings(String str1, String str2) { 167 return (str1 == null) ? str2 == null : str1.equals(str2); 168 } 169 170 private static boolean isWhitespace(String s) { 171 return Pattern.matches("^[ \t\r\n]+$", s); 172 } 173 } 174 | Popular Tags |