1 19 package org.netbeans.modules.xml.axi.util; 20 21 import java.io.FileReader ; 22 import java.net.URL ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import javax.xml.xpath.XPath ; 27 import javax.xml.xpath.XPathConstants ; 28 import javax.xml.xpath.XPathFactory ; 29 import org.netbeans.modules.xml.axi.AXIComponent; 30 import org.netbeans.modules.xml.axi.AXIDocument; 31 import org.netbeans.modules.xml.axi.AnyAttribute; 32 import org.netbeans.modules.xml.axi.Attribute; 33 import org.netbeans.modules.xml.axi.Element; 34 import org.netbeans.modules.xml.axi.visitor.PrintAXITreeVisitor; 35 import org.w3c.dom.Node ; 36 import org.w3c.dom.NodeList ; 37 import org.xml.sax.InputSource ; 38 39 43 public class ModelValidator extends PrintAXITreeVisitor { 44 45 private String errorMessage; 46 private boolean success; 47 private URL expectedFileURL; 48 private FileReader expectedFileReader; 49 50 53 public ModelValidator(URL url) { 54 this.success = true; 55 this.expectedFileURL = url; 56 } 57 58 public boolean visitAndCompareAgainstDOMElement(Element element) { 59 this.visit(element); 60 return success; 61 } 62 63 public void visit(Attribute attr) { 64 } 66 67 public void visit(AnyAttribute attr) { 68 } 70 71 protected void visitChildren(AXIComponent component) { 72 if(canCompare()) { 73 if(!compare(component)) 74 return; 75 } 76 super.visitChildren(component); 77 } 78 79 public String getErrorMessage() { 80 return errorMessage; 81 } 82 83 private boolean canCompare() { 84 if(expectedFileURL == null) 85 return false; 86 87 return true; 88 } 89 90 93 private boolean compare(AXIComponent axiNode) { 94 FileUtil util = FileUtil.getInstance(); 95 try { 96 InputSource inputSource = util.openFile(expectedFileURL); 97 if(inputSource == null) { 98 success = false; 99 return false; 100 } 101 String expression = getExpression(axiNode); 102 XPath xpath = XPathFactory.newInstance().newXPath(); 103 Node domNode = (Node ) xpath.evaluate(expression, inputSource, XPathConstants.NODE); 104 if(!axiNode.toString().equals(domNode.getNodeName())) { 105 success = false; 106 errorMessage = "Expected AXI node " + axiNode + ", but found DOM node " + domNode.getNodeName(); 107 return false; 108 } 109 110 if(!compareChildren(axiNode, domNode)) { 111 return false; 112 } 113 114 } catch(Exception ex) { 115 ex.printStackTrace(); 116 success = false; 117 errorMessage = "Exception: " + ex.getMessage(); 118 return false; 119 } finally { 120 util.closeFile(); 121 } 122 123 return true; 124 } 125 126 130 private boolean compareChildren(AXIComponent axiNode, Node domNode) { 131 Collection <AXIComponent> axiChildren = getChildren(axiNode); 133 Collection <Node > domChildren = getChildren(domNode); 134 if( axiChildren.size() != domChildren.size() ) { 135 success = false; 136 errorMessage = "For AXI node " + axiNode + 137 ", expected child count is " + axiChildren.size() + 138 " where as, actual count is " + domChildren.size(); 139 return success; 140 } 141 142 Collection <Attribute> attrs = axiNode.getChildren(Attribute.class); 143 if( attrs.size() != domNode.getAttributes().getLength() ) { 144 success = false; 145 errorMessage = "For AXI node " + axiNode + 146 ", expected attribute count is " + attrs.size() + 147 " where as, actual count is " + domNode.getAttributes().getLength(); 148 return success; 149 } 150 151 Iterator <AXIComponent> axiIterator = axiChildren.iterator(); 152 Iterator <Node > domIterator = domChildren.iterator(); 153 while(axiIterator.hasNext() && domIterator.hasNext()) { 154 AXIComponent axiChild = axiIterator.next(); 155 Node domChild = domIterator.next(); 156 if(!axiChild.toString().equals(domChild.getNodeName())) { 157 success = false; 158 errorMessage = "For AXI node " + axiNode + 159 ", expecting child " + axiChild + 160 " where as, found child " + domChild.getNodeName(); 161 return success; 162 } 163 } 164 165 return true; 166 } 167 168 private String getExpression(AXIComponent component) { 169 if(component.getParent() instanceof AXIDocument) 170 return "/" + component; 171 172 return getExpression(component.getParent()) + "/" + component; 173 } 174 175 private Collection <Node > getChildren(Node node) { 176 Collection <Node > nodes = new ArrayList <Node >(); 177 NodeList children = node.getChildNodes(); 178 for(int i=0; i<children.getLength(); i++) { 179 Node child = children.item(i); 180 if(child.getNodeType() != Node.ELEMENT_NODE) 181 continue; 182 nodes.add(child); 183 } 184 return nodes; 185 } 186 187 private Collection <AXIComponent> getChildren(AXIComponent component) { 188 Collection <AXIComponent> children = new ArrayList <AXIComponent>(); 189 for(AXIComponent child : component.getChildren()) { 190 if(child instanceof Attribute) 191 continue; 192 children.add(child); 193 } 194 return children; 195 } 196 197 } 198 | Popular Tags |