KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > axi > util > ModelValidator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.axi.util;
20
21 import java.io.FileReader JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import javax.xml.xpath.XPath JavaDoc;
27 import javax.xml.xpath.XPathConstants JavaDoc;
28 import javax.xml.xpath.XPathFactory JavaDoc;
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 JavaDoc;
36 import org.w3c.dom.NodeList JavaDoc;
37 import org.xml.sax.InputSource JavaDoc;
38
39 /**
40  * Test visitor to visit all children in the AXI tree.
41  * @author Samaresh
42  */

43 public class ModelValidator extends PrintAXITreeVisitor {
44     
45     private String JavaDoc errorMessage;
46     private boolean success;
47     private URL JavaDoc expectedFileURL;
48     private FileReader JavaDoc expectedFileReader;
49         
50     /**
51      * Creates a new instance of AXITreeVisitor
52      */

53     public ModelValidator(URL JavaDoc 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         //do nothing
65
}
66     
67     public void visit(AnyAttribute attr) {
68         //do nothing
69
}
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 JavaDoc getErrorMessage() {
80         return errorMessage;
81     }
82     
83     private boolean canCompare() {
84         if(expectedFileURL == null)
85             return false;
86         
87         return true;
88     }
89
90     /**
91      * Compares AXI component against corresponding DOM element.
92      */

93     private boolean compare(AXIComponent axiNode) {
94         FileUtil util = FileUtil.getInstance();
95         try {
96             InputSource JavaDoc inputSource = util.openFile(expectedFileURL);
97             if(inputSource == null) {
98                 success = false;
99                 return false;
100             }
101             String JavaDoc expression = getExpression(axiNode);
102             XPath JavaDoc xpath = XPathFactory.newInstance().newXPath();
103             Node JavaDoc domNode = (Node JavaDoc) 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 JavaDoc 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     /**
127      * Compares each child of an AXI component against
128      * corresponding DOM element's child.
129      */

130     private boolean compareChildren(AXIComponent axiNode, Node JavaDoc domNode) {
131         //compare sizes
132
Collection JavaDoc<AXIComponent> axiChildren = getChildren(axiNode);
133         Collection JavaDoc<Node JavaDoc> 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 JavaDoc<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 JavaDoc<AXIComponent> axiIterator = axiChildren.iterator();
152         Iterator JavaDoc<Node JavaDoc> domIterator = domChildren.iterator();
153         while(axiIterator.hasNext() && domIterator.hasNext()) {
154             AXIComponent axiChild = axiIterator.next();
155             Node JavaDoc 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 JavaDoc getExpression(AXIComponent component) {
169         if(component.getParent() instanceof AXIDocument)
170             return "/" + component;
171         
172         return getExpression(component.getParent()) + "/" + component;
173     }
174     
175     private Collection JavaDoc<Node JavaDoc> getChildren(Node JavaDoc node) {
176         Collection JavaDoc<Node JavaDoc> nodes = new ArrayList JavaDoc<Node JavaDoc>();
177         NodeList JavaDoc children = node.getChildNodes();
178         for(int i=0; i<children.getLength(); i++) {
179             Node JavaDoc 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 JavaDoc<AXIComponent> getChildren(AXIComponent component) {
188         Collection JavaDoc<AXIComponent> children = new ArrayList JavaDoc<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