KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > om > impl > llom > util > XMLComparator


1 package org.apache.axis2.om.impl.llom.util;
2
3 import org.apache.axis2.om.OMAttribute;
4 import org.apache.axis2.om.OMElement;
5 import org.apache.axis2.om.OMNamespace;
6 import org.apache.axis2.om.OMNode;
7 import org.apache.axis2.om.impl.llom.exception.XMLComparisonException;
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import java.util.Iterator JavaDoc;
12
13 /**
14  * Copyright 2001-2004 The Apache Software Foundation.
15  * <p/>
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
17  * use this file except in compliance with the License. You may obtain a copy of
18  * the License at
19  * <p/>
20  * http://www.apache.org/licenses/LICENSE-2.0
21  * <p/>
22  * Unless required by applicable law or agreed to in writing, software
23  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
25  * License for the specific language governing permissions and limitations under
26  * the License.
27  * <p/>
28  */

29 public class XMLComparator {
30     /**
31      * Eran Chinthaka (chinthaka@apache.org)
32      */

33     private Log log = LogFactory.getLog(getClass());
34
35
36     public boolean compare(OMElement elementOne, OMElement elementTwo) throws XMLComparisonException {
37         if (elementOne == null && elementTwo == null) {
38             log.info("Both Elements are null.");
39             return true;
40         }
41         if (elementOne == null && elementTwo != null) {
42             throw new XMLComparisonException("Element One is null and Element Two is not null");
43         }
44         if (elementOne != null && elementTwo == null) {
45             throw new XMLComparisonException("Element Two is null and Element One is not null");
46         }
47
48         log.info("Now Checking " + elementOne.getLocalName() + " and " + elementTwo.getLocalName() + "=============================");
49
50         log.info("Comparing Element Names .......");
51         compare("Elements names are not equal. ", elementOne.getLocalName(), elementTwo.getLocalName());
52
53         log.info("Comparing Namespaces .........");
54         compare("Element namespaces are not equal", elementOne.getNamespace(), elementTwo.getNamespace());
55
56         log.info("Comparing attributes .....");
57         compareAllAttributes(elementOne, elementTwo);
58
59         log.info("Comparing texts .....");
60         compare("Elements texts are not equal ", elementOne.getText(), elementTwo.getText());
61
62         log.info("Comparing Children ......");
63         compareAllChildren(elementOne, elementTwo);
64
65
66         return true;
67     }
68
69     private void compareAllAttributes(OMElement elementOne, OMElement elementTwo) throws XMLComparisonException {
70         compareAttibutes(elementOne, elementTwo);
71         compareAttibutes(elementTwo, elementOne);
72     }
73
74     private void compareAllChildren(OMElement elementOne, OMElement elementTwo) throws XMLComparisonException {
75         compareChildren(elementOne, elementTwo);
76         compareChildren(elementTwo, elementOne);
77     }
78
79     private void compareChildren(OMElement elementOne, OMElement elementTwo) throws XMLComparisonException {
80         Iterator JavaDoc elementOneChildren = elementOne.getChildren();
81         while (elementOneChildren.hasNext()) {
82             OMNode omNode = (OMNode) elementOneChildren.next();
83             if (omNode instanceof OMElement) {
84                 OMElement elementOneChild = (OMElement) omNode;
85                 if("Reference4".equals(elementOneChild.getLocalName())){
86                     System.out.println("Reference4");
87                 }
88                 OMElement elementTwoChild = elementTwo.getFirstChildWithName(elementOneChild.getQName());
89                 if (elementTwoChild == null) {
90                     throw new XMLComparisonException(" There is no " + elementOneChild.getLocalName() + " element under " + elementTwo.getLocalName());
91                 }
92                 compare(elementOneChild, elementTwoChild);
93             }
94         }
95     }
96
97
98     private void compareAttibutes(OMElement elementOne, OMElement elementTwo) throws XMLComparisonException {
99         int elementOneAtribCount = 0;
100         int elementTwoAtribCount = 0;
101         Iterator JavaDoc attributes = elementOne.getAttributes();
102         while (attributes.hasNext()) {
103             OMAttribute omAttribute = (OMAttribute) attributes.next();
104             OMAttribute attr = elementTwo.getFirstAttribute(omAttribute.getQName());
105             if (attr == null) {
106                 throw new XMLComparisonException("Attributes are not the same in two elements. Attribute " + omAttribute.getLocalName() + " != ");
107             }
108             elementOneAtribCount++;
109         }
110
111         Iterator JavaDoc elementTwoIter = elementTwo.getAttributes();
112         while (elementTwoIter.hasNext()) {
113             elementTwoIter.next();
114             elementTwoAtribCount++;
115
116         }
117
118         if (elementOneAtribCount != elementTwoAtribCount) {
119             throw new XMLComparisonException("Attributes are not the same in two elements.");
120         }
121     }
122
123     private void compare(String JavaDoc failureNotice, String JavaDoc one, String JavaDoc two) throws XMLComparisonException {
124         if (!one.equals(two)) {
125             throw new XMLComparisonException(failureNotice + one + " != " + two);
126         }
127     }
128
129     private void compare(String JavaDoc failureNotice, OMNamespace one, OMNamespace two) throws XMLComparisonException {
130         if (one == null && two == null) {
131             return;
132         } else if (one != null && two == null) {
133             throw new XMLComparisonException("First Namespace is NOT null. But the second is null");
134         } else if (one == null && two != null) {
135             throw new XMLComparisonException("First Namespace is null. But the second is NOT null");
136         }
137
138         if (!one.getName().equals(two.getName())) {
139             throw new XMLComparisonException(failureNotice + one + " != " + two);
140         }
141
142         // Do we need to compare prefixes as well
143
}
144 }
145
Popular Tags