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 ; 11 12 28 public class XMLComparator { 29 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 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 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 failureNotice, String one, String two) throws XMLComparisonException { 106 if(!one.equals(two)){ 107 throw new XMLComparisonException(failureNotice+ one + " != " + two); 108 } 109 } 110 111 private void compare(String 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 } 126 } 127 | Popular Tags |