1 package org.xmldb.xupdate.unittests; 2 3 11 12 import junit.framework.Assert; 13 import org.w3c.dom.*; 14 15 public class XhiveNodeMatcher { 16 17 public XhiveNodeMatcher() { 18 } 19 20 protected static String nodeTypeToString(int type) { 21 switch (type) { 22 case Node.ELEMENT_NODE: 23 return "ELEMENT_NODE"; 24 case Node.ATTRIBUTE_NODE: 25 return "ATTRIBUTE_NODE"; 26 case Node.TEXT_NODE: 27 return "TEXT_NODE"; 28 case Node.CDATA_SECTION_NODE: 29 return "CDATA_SECTION_NODE"; 30 case Node.ENTITY_REFERENCE_NODE: 31 return "ENTITY_REFERENCE_NODE"; 32 case Node.ENTITY_NODE: 33 return "ENTITY_NODE"; 34 case Node.PROCESSING_INSTRUCTION_NODE: 35 return "PROCESSING_INSTRUCTION_NODE"; 36 case Node.COMMENT_NODE: 37 return "COMMENT_NODE"; 38 case Node.DOCUMENT_NODE: 39 return "DOCUMENT_NODE"; 40 case Node.DOCUMENT_TYPE_NODE: 41 return "DOCUMENT_TYPE_NODE"; 42 case Node.DOCUMENT_FRAGMENT_NODE: 43 return "DOCUMENT_FRAGMENT_NODE"; 44 case Node.NOTATION_NODE: 45 return "NOTATION_NODE"; 46 } 47 return "UNKNOWN NODE TYPE"; 48 } 49 50 public static void compareNodes(Node origin, Node target, boolean clone, boolean deep) { 51 if (!(origin == null && target == null)) { 53 if (origin != null) { 54 Assert.assertNotNull("NodeMatcher : Missing node, origin tagname : " + origin.getNodeName(), target); 55 } else { 56 Assert.assertNull("NodeMatcher : extra node: " + (target != null ? target.getNodeName() : ""), target); 57 } 58 Assert.assertEquals("NodeMatcher : node types do not match", nodeTypeToString(origin.getNodeType()), 60 nodeTypeToString(target.getNodeType())); 61 Assert.assertEquals("NodeMatcher : node names do not match.", origin.getNodeName(), target.getNodeName()); 63 Assert.assertEquals("NodeMatcher : node values do not match.", origin.getNodeValue(), target.getNodeValue()); 65 Assert.assertEquals("NodeMatcher : local names do not match.", origin.getLocalName(), target.getLocalName()); 68 Assert.assertEquals("NodeMatcher : namespace URI's do not match (localName=" + origin.getLocalName() + ")." 70 , origin.getNamespaceURI(), target.getNamespaceURI()); 71 Assert.assertEquals("NodeMatcher : prefixes do not match.", origin.getPrefix(), target.getPrefix()); 73 if (origin.hasAttributes()) { 75 Assert.assertTrue("NodeMatcher : hasAttributes() should return true for node with name " + origin.getNodeName() 76 , target.hasAttributes()); 77 } 78 if (clone || (origin.getNodeType() != Node.ENTITY_REFERENCE_NODE)) { 80 if (origin.hasChildNodes() && deep) { 81 Assert.assertTrue("NodeMatcher : hasChildren() should return true for node with name " + origin.getNodeName() 82 , target.hasChildNodes()); 83 } 84 } 85 if (origin.getNodeType() == Node.ELEMENT_NODE) { 87 compareNamedNodeMaps(origin.getAttributes(), target.getAttributes(), clone, deep); 89 } 90 if (origin.getNodeType() == Node.ATTRIBUTE_NODE) { 91 compareAttrs((Attr) origin, (Attr) target, clone, deep); 93 } 94 if (origin.getNodeType() == Node.ENTITY_NODE) { 95 compareEntities((Entity) origin, (Entity) target); 97 } 98 if (origin.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) { 99 comparePIs((ProcessingInstruction) origin, (ProcessingInstruction) target); 101 } 102 if (origin.getNodeType() == Node.NOTATION_NODE) { 103 compareNotations((Notation) origin, (Notation) target); 105 } 106 if (origin.getNodeType() == Node.DOCUMENT_NODE) { 107 compareDocuments((Document) origin, (Document) target, clone, deep); 109 } 110 if ((deep) && (clone || (origin.getNodeType() != Node.ENTITY_REFERENCE_NODE))) { 112 Node originPointer = origin.getFirstChild(); 115 Node targetPointer = target.getFirstChild(); 116 while (originPointer != null) { 117 Assert.assertNotNull("NodeMatcher : Child list for node does not match, missing node " 119 + originPointer.getNodeName(), targetPointer); 120 compareNodes(originPointer, targetPointer, clone, deep); 121 originPointer = originPointer.getNextSibling(); 122 targetPointer = targetPointer.getNextSibling(); 123 } 124 if (targetPointer != null && originPointer == null) { 125 Assert.fail("NodeMatcher : Child list does not match, node should not be there :" 126 + targetPointer.getNodeName() + ", " + targetPointer.getNodeValue()); 127 } 128 } 129 } 130 } 131 132 public static void compareNodeLists(NodeList origin, NodeList target, boolean clone, boolean deep) { 133 if (origin != null) { 134 Assert.assertNotNull("NodeMatcher : expected nodelist not found", target); 135 Assert.assertEquals("NodeMatcher : nodelist item count does not match.", origin.getLength(), target.getLength()); 136 137 for (int i = 0; i < origin.getLength(); i++) { 138 Node originNode = origin.item(i); 139 Node targetNode = target.item(i); 140 compareNodes(originNode, targetNode, clone, deep); 141 } 142 } else { 143 Assert.assertNull("NodeMatcher : unexpected nodelist found", target); 144 } 145 } 146 147 public static void compareNamedNodeMaps(NamedNodeMap origin, NamedNodeMap target, boolean clone, boolean deep) { 148 if (origin != null) { 149 Assert.assertNotNull("NodeMatcher : expected namednodemap not found", target); 150 Assert.assertEquals("NodeMatcher : nodemap item count does not match.", origin.getLength(), target.getLength()); 151 152 for (int i = 0; i < origin.getLength(); i++) { 153 Node originNode = origin.item(i); 154 Node targetNode = lookupTargetNode(target, originNode); 156 compareNodes(originNode, targetNode, clone, deep); 157 } 158 } else { 159 Assert.assertNull("NodeMatcher : unexpected namednodemap found", target); 160 } 161 } 162 163 167 private static Node lookupTargetNode(NamedNodeMap target, Node originNode) { 168 if (originNode.getLocalName() == null) { 170 return target.getNamedItem(originNode.getNodeName()); 171 } else { 172 return target.getNamedItemNS(originNode.getNamespaceURI(), originNode.getLocalName()); 173 } 174 } 175 176 protected static void compareAttrs(Attr origin, Attr target, boolean clone, boolean deep) { 177 if (clone) { 178 if (origin.getSpecified()) { 179 Assert.assertTrue("NodeMatcher : attribute specified does not match", target.getSpecified()); 180 } 181 } 182 } 183 184 protected static void compareEntities(Entity origin, Entity target) { 185 Assert.assertEquals("NodeMatcher : entity public id's do not match.", origin.getPublicId(), target.getPublicId()); 186 Assert.assertEquals("NodeMatcher : entity system id's do not match.", origin.getSystemId(), target.getSystemId()); 187 Assert.assertEquals("NodeMatcher : entity notation names do not match.", origin.getNotationName() 188 , target.getNotationName()); 189 } 190 191 protected static void comparePIs(ProcessingInstruction origin, ProcessingInstruction target) { 192 Assert.assertEquals("NodeMatcher : processing instruction targets do not match." 193 , origin.getTarget(), target.getTarget()); 194 Assert.assertEquals("NodeMatcher : processing instruction data does not match." 195 , origin.getData(), target.getData()); 196 } 197 198 protected static void compareNotations(Notation origin, Notation target) { 199 Assert.assertEquals("NodeMatcher : notation public id's do not match.", origin.getPublicId(), target.getPublicId()); 200 Assert.assertEquals("NodeMatcher : notation system id's do not match.", origin.getSystemId(), target.getSystemId()); 201 } 202 203 209 public static void compareDocTypes(DocumentType origin, DocumentType target, boolean clone, boolean deep) { 210 if (origin == null) { 211 Assert.assertNull("NodeMatcher: One doctype is null and one is not", target); 212 } else { 213 Assert.assertNotNull("NodeMatcher: One doctype is null and one is not", target); 214 } 215 Assert.assertEquals("NodeMatcher : document type public id's do not match." 216 , origin.getPublicId(), target.getPublicId()); 217 Assert.assertEquals("NodeMatcher : document type system id's do not match." 218 , origin.getSystemId(), target.getSystemId()); 219 Assert.assertEquals("NodeMatcher : document type names do not match." 220 , origin.getName(), target.getName()); 221 compareNamedNodeMaps(origin.getEntities(), target.getEntities(), clone, deep); 222 compareNamedNodeMaps(origin.getNotations(), target.getNotations(), clone, deep); 223 } 224 225 protected static void compareDocuments(Document origin, Document target, boolean clone, boolean deep) { 226 DocumentType originDocumentType = origin.getDoctype(); 227 DocumentType targetDocumentType = target.getDoctype(); 228 compareNodes(originDocumentType, targetDocumentType, clone, deep); 229 } 230 231 } 232 | Popular Tags |