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 ; 12 13 29 public class XMLComparator { 30 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 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 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 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 failureNotice, String one, String two) throws XMLComparisonException { 124 if (!one.equals(two)) { 125 throw new XMLComparisonException(failureNotice + one + " != " + two); 126 } 127 } 128 129 private void compare(String 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 } 144 } 145 | Popular Tags |