KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > platform > xml > XMLComparer


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.platform.xml;
23
24 import org.w3c.dom.Attr JavaDoc;
25 import org.w3c.dom.Comment JavaDoc;
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.DocumentFragment JavaDoc;
28 import org.w3c.dom.DocumentType JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30 import org.w3c.dom.EntityReference JavaDoc;
31 import org.w3c.dom.NamedNodeMap JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33 import org.w3c.dom.NodeList JavaDoc;
34 import org.w3c.dom.ProcessingInstruction JavaDoc;
35 import org.w3c.dom.Text JavaDoc;
36
37 /**
38  * This class is used to compare if two DOM nodes are equal.
39  */

40 public class XMLComparer {
41     public XMLComparer() {
42         super();
43     }
44
45     /**
46      * Compare two DOM nodes.
47      * @param control The first node in the comparison.
48      * @param test The second node in the comparison.
49      * @return Return true if the nodes are equal, else false.
50      */

51     public boolean isNodeEqual(Node JavaDoc control, Node JavaDoc test) {
52         if (control == test) {
53             return true;
54         } else if ((null == control) || (null == test)) {
55             return false;
56         } else if (control.getNodeType() != test.getNodeType()) {
57             return false;
58         }
59         switch (control.getNodeType()) {
60         case (Node.ATTRIBUTE_NODE):
61             return isAttributeEqual((Attr JavaDoc)control, (Attr JavaDoc)test);
62         case (Node.CDATA_SECTION_NODE):
63             return isTextEqual((Text JavaDoc)control, (Text JavaDoc)test);
64         case (Node.COMMENT_NODE):
65             return isCommentEqual((Comment JavaDoc)control, (Comment JavaDoc)test);
66         case (Node.DOCUMENT_FRAGMENT_NODE):
67             return isDocumentFragmentEqual((DocumentFragment JavaDoc)control, (DocumentFragment JavaDoc)test);
68         case (Node.DOCUMENT_NODE):
69             return isDocumentEqual((Document JavaDoc)control, (Document JavaDoc)test);
70         case (Node.DOCUMENT_TYPE_NODE):
71             return isDocumentTypeEqual((DocumentType JavaDoc)control, (DocumentType JavaDoc)test);
72         case (Node.ELEMENT_NODE):
73             return isElementEqual((Element JavaDoc)control, (Element JavaDoc)test);
74         case (Node.ENTITY_NODE):
75             return false;
76         case (Node.ENTITY_REFERENCE_NODE):
77             return isEntityReferenceEqual((EntityReference JavaDoc)control, (EntityReference JavaDoc)test);
78         case (Node.NOTATION_NODE):
79             return false;
80         case (Node.PROCESSING_INSTRUCTION_NODE):
81             return isProcessingInstructionEqual((ProcessingInstruction JavaDoc)control, (ProcessingInstruction JavaDoc)test);
82         case (Node.TEXT_NODE):
83             return isTextEqual((Text JavaDoc)control, (Text JavaDoc)test);
84         default:
85             return true;
86         }
87     }
88
89     private boolean isAttributeEqual(Attr JavaDoc control, Attr JavaDoc test) {
90         if (!isStringEqual(control.getNamespaceURI(), test.getNamespaceURI())) {
91             return false;
92         }
93         if (!isStringEqual(control.getName(), test.getName())) {
94             return false;
95         }
96         if (!isStringEqual(control.getNodeValue(), test.getNodeValue())) {
97             return false;
98         }
99         return true;
100     }
101
102     private boolean isCommentEqual(Comment JavaDoc control, Comment JavaDoc test) {
103         if (!isStringEqual(control.getNodeValue(), test.getNodeValue())) {
104             return false;
105         }
106         return true;
107     }
108
109     private boolean isDocumentEqual(Document JavaDoc control, Document JavaDoc test) {
110         if (!isDocumentTypeEqual(control.getDoctype(), test.getDoctype())) {
111             return false;
112         }
113
114         Element JavaDoc controlRootElement = control.getDocumentElement();
115         Element JavaDoc testRootElement = test.getDocumentElement();
116         if (controlRootElement == testRootElement) {
117             return true;
118         } else if ((null == controlRootElement) || (null == testRootElement)) {
119             return false;
120         }
121         return isElementEqual(controlRootElement, testRootElement);
122     }
123
124     private boolean isDocumentFragmentEqual(DocumentFragment JavaDoc control, DocumentFragment JavaDoc test) {
125         return isNodeListEqual(control.getChildNodes(), test.getChildNodes());
126     }
127
128     private boolean isDocumentTypeEqual(DocumentType JavaDoc control, DocumentType JavaDoc test) {
129         if (control == test) {
130             return true;
131         } else if ((null == control) || (null == test)) {
132             return false;
133         }
134
135         if (!isStringEqual(control.getName(), test.getName())) {
136             return false;
137         }
138         if (!isStringEqual(control.getPublicId(), test.getPublicId())) {
139             return false;
140         }
141         if (!isStringEqual(control.getSystemId(), test.getSystemId())) {
142             return false;
143         }
144
145         return true;
146     }
147
148     private boolean isElementEqual(Element JavaDoc control, Element JavaDoc test) {
149         if (!isStringEqual(control.getNamespaceURI(), test.getNamespaceURI())) {
150             return false;
151         }
152         if (!isStringEqual(control.getTagName(), test.getTagName())) {
153             return false;
154         }
155
156         // COMPARE ATTRIBUTES
157
NamedNodeMap JavaDoc controlAttributes = control.getAttributes();
158         NamedNodeMap JavaDoc testAttributes = test.getAttributes();
159         int numberOfControlAttributes = controlAttributes.getLength();
160         if (numberOfControlAttributes != testAttributes.getLength()) {
161             return false;
162         }
163         Attr JavaDoc controlAttribute;
164         Attr JavaDoc testAttribute;
165         for (int x = 0; x < numberOfControlAttributes; x++) {
166             controlAttribute = (Attr JavaDoc)controlAttributes.item(x);
167             if (null == controlAttribute.getNamespaceURI()) {
168                 testAttribute = (Attr JavaDoc)testAttributes.getNamedItem(controlAttribute.getNodeName());
169             } else {
170                 testAttribute = (Attr JavaDoc)testAttributes.getNamedItemNS(controlAttribute.getNamespaceURI(), controlAttribute.getLocalName());
171             }
172             if (null == testAttribute) {
173                 return false;
174             } else if (!isAttributeEqual(controlAttribute, testAttribute)) {
175                 return false;
176             }
177         }
178
179         // COMPARE CHILD NODES
180
return isNodeListEqual(control.getChildNodes(), test.getChildNodes());
181     }
182
183     private boolean isEntityReferenceEqual(EntityReference JavaDoc control, EntityReference JavaDoc test) {
184         if (!isStringEqual(control.getNodeName(), test.getNodeName())) {
185             return false;
186         }
187         return true;
188     }
189
190     private boolean isProcessingInstructionEqual(ProcessingInstruction JavaDoc control, ProcessingInstruction JavaDoc test) {
191         if (!isStringEqual(control.getTarget(), test.getTarget())) {
192             return false;
193         }
194         if (!isStringEqual(control.getData(), test.getData())) {
195             return false;
196         }
197         return true;
198     }
199
200     private boolean isTextEqual(Text JavaDoc control, Text JavaDoc test) {
201         return isStringEqual(control.getNodeValue(), test.getNodeValue());
202     }
203
204     private boolean isNodeListEqual(NodeList JavaDoc control, NodeList JavaDoc test) {
205         int numberOfControlNodes = control.getLength();
206         if (numberOfControlNodes != test.getLength()) {
207             return false;
208         }
209         for (int x = 0; x < numberOfControlNodes; x++) {
210             if (!isNodeEqual(control.item(x), test.item(x))) {
211                 return false;
212             }
213         }
214         return true;
215     }
216
217     private boolean isStringEqual(String JavaDoc control, String JavaDoc test) {
218         if (control == test) {
219             return true;
220         } else if (null == control) {
221             return false;
222         } else {
223             return control.equals(test);
224         }
225     }
226 }
227
Popular Tags