KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > test > DOMCompare


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
20 package org.netbeans.modules.db.test;
21
22 import java.util.regex.Pattern JavaDoc;
23 import org.w3c.dom.Attr JavaDoc;
24 import org.w3c.dom.Document JavaDoc;
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.NamedNodeMap JavaDoc;
27 import org.w3c.dom.Node JavaDoc;
28 import org.w3c.dom.NodeList JavaDoc;
29
30 /**
31  * Utility class for comparing DOM documents. It is namespace aware (hopefully),
32  * but can only compare text and elements nodes and the attributes list of
33  * element nodes.
34  *
35  * @author Andrei Badea
36  */

37 public class DOMCompare {
38
39     private DOMCompare() {
40     }
41
42     public static boolean compareDocuments(Document JavaDoc doc1, Document JavaDoc doc2) {
43         Element JavaDoc e1 = doc1.getDocumentElement();
44         Element JavaDoc e2 = doc2.getDocumentElement();
45         
46         return compareElements(e1, e2);
47     }
48     
49     private static boolean compareElements(Element JavaDoc e1, Element JavaDoc 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 JavaDoc e1, Element JavaDoc e2) {
68         NamedNodeMap JavaDoc at1 = e1.getAttributes();
69         NamedNodeMap JavaDoc 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 JavaDoc attr1 = (Attr JavaDoc)at1.item(i);
75             Attr JavaDoc attr2 = (Attr JavaDoc)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 JavaDoc e1, Element JavaDoc e2) {
89         NodeList JavaDoc ch1 = e1.getChildNodes();
90         NodeList JavaDoc ch2 = e2.getChildNodes();
91         int i1 = 0;
92         int i2 = 0;
93         for (;;) {
94             while (i1 < ch1.getLength()) {
95                 Node JavaDoc 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 JavaDoc 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 JavaDoc)ch1.item(i1), (Element JavaDoc)ch2.item(i2))) {
132                             System.out.println("Different elements " + getElementStringRep((Element JavaDoc)ch1.item(i1)) + " and " + getElementStringRep((Element JavaDoc)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 JavaDoc)e1));
152                     return false;
153                 }
154             }
155         }
156     }
157     
158     private static String JavaDoc getElementStringRep(Element JavaDoc el) {
159         String JavaDoc 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 JavaDoc str1, String JavaDoc str2) {
167         return (str1 == null) ? str2 == null : str1.equals(str2);
168     }
169     
170     private static boolean isWhitespace(String JavaDoc s) {
171         return Pattern.matches("^[ \t\r\n]+$", s);
172     }
173 }
174
Popular Tags