KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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