You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by la...@apache.org on 2011/10/23 22:25:13 UTC

svn commit: r1187967 - /incubator/airavata/trunk/modules/commons/utils/src/main/java/org/apache/airavata/common/utils/XMLUtil.java

Author: lahiru
Date: Sun Oct 23 20:25:13 2011
New Revision: 1187967

URL: http://svn.apache.org/viewvc?rev=1187967&view=rev
Log:
fixing xml comparison function.

Modified:
    incubator/airavata/trunk/modules/commons/utils/src/main/java/org/apache/airavata/common/utils/XMLUtil.java

Modified: incubator/airavata/trunk/modules/commons/utils/src/main/java/org/apache/airavata/common/utils/XMLUtil.java
URL: http://svn.apache.org/viewvc/incubator/airavata/trunk/modules/commons/utils/src/main/java/org/apache/airavata/common/utils/XMLUtil.java?rev=1187967&r1=1187966&r2=1187967&view=diff
==============================================================================
--- incubator/airavata/trunk/modules/commons/utils/src/main/java/org/apache/airavata/common/utils/XMLUtil.java (original)
+++ incubator/airavata/trunk/modules/commons/utils/src/main/java/org/apache/airavata/common/utils/XMLUtil.java Sun Oct 23 20:25:13 2011
@@ -31,6 +31,7 @@ import java.net.URL;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.LinkedList;
+import java.util.List;
 
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
@@ -492,74 +493,87 @@ public class XMLUtil {
         return null;
     }
 
-    /**
-     * Check two XmlElements are equal with the Xml content or not
-     * 
-     * @param elem1
-     * @param elem2
-     * @return
-     */
     public static boolean isEqual(XmlElement elem1, XmlElement elem2) throws Exception {
 
-        if (elem1 == null && elem2 == null) {
-            return true;
-        } else if (elem1 == null) {
-            return false;
-        } else if (elem2 == null) {
-            return false;
-        }
+            if (elem1 == null && elem2 == null) {
+                return true;
+            } else if (elem1 == null) {
+                return false;
+            } else if (elem2 == null) {
+                return false;
+            }
 
-        if (!elem1.getName().equals(elem2.getName())) {
-            return false;
-        } else {
-            // now check if children are the same
-            Iterator children1 = elem1.children().iterator();
-            Iterator children2 = elem2.children().iterator();
-            while (children1.hasNext()) {
-                Object child = children1.next();
-
-                if (child instanceof String) {
-                    // check whether this is the only string
-                    // if yes this is text val otherwise its a text
-                    // among the elements so ignore
-                    if (children1.hasNext()) {
-                        continue;
+            if (!elem1.getName().equals(elem2.getName())) {
+                return false;
+            } else {
+                // now check if children are the same
+                Iterator children1 = elem1.children().iterator();
+                Iterator children2 = elem2.children().iterator();
+
+                //check first ones for string
+                Object child1 = null;
+                Object child2 = null;
+                if (children1.hasNext() && children2.hasNext()) {
+                    child1 = children1.next();
+                    child2 = children2.next();
+
+                    if (!children1.hasNext() && !children2.hasNext()) {
+                        //only one node could be string could be xmlelement
+                        return compareObjs(child1, child2);
                     } else {
-                        // this is a text node
-                        if (children2.hasNext()) {
-                            Object child2Obj = children2.next();
-                            if (child2Obj instanceof String) {
-                                return child.equals(child2Obj);
-                            }
-                        }
-                        return false;
-                    }
+                          //get new iterators
+
+                        List<XmlElement> elemSet1 = getXmlElementsOnly(elem1.children().iterator());
+                        List<XmlElement> elemSet2 = getXmlElementsOnly(elem2.children().iterator());
 
-                } else if (child instanceof XmlElement) {
-                    if (children2.hasNext()) {
-                        Object child2Obj = children2.next();
-                        if (child2Obj instanceof String) {
-                            child2Obj = children2.next();
+                        if(elemSet1.size() != elemSet2.size()){
+                            return false;
                         }
-                        if (child2Obj instanceof XmlElement) {
-                            if (!isEqual((XmlElement) child, (XmlElement) child2Obj)) {
+                        for(int i =0; i< elemSet1.size(); ++i){
+                            if(!isEqual(elemSet1.get(i), elemSet2.get(i))){
                                 return false;
                             }
-                        } else {
-                            return false;
                         }
-                    } else {
-                        // childrens2 ran out of elements
-                        return false;
+                        return true;
                     }
 
-                } else {
-                    throw new Exception("Unhandled element type found" + child);
+
+                }else {
+                    //no internal element
+
+                    return true;
                 }
             }
-            return true;
+
+
+        }
+
+
+
+        private static List<XmlElement> getXmlElementsOnly(Iterator itr){
+            LinkedList<XmlElement> list = new LinkedList<XmlElement>();
+            while(itr.hasNext()){
+                Object obj = itr.next();
+                if(obj instanceof XmlElement){
+                    list.add((XmlElement) obj);
+                }
+            }
+            return  list;
+        }
+
+
+
+        private static boolean compareObjs(Object child1, Object child2) throws Exception {
+            if (child1 instanceof String && child2 instanceof String) {
+                return child1.equals(child2);
+
+
+            } else if (child1 instanceof XmlElement && child2 instanceof XmlElement) {
+                return isEqual((XmlElement) child1, (XmlElement) child2);
+            } else {
+                return false;
+            }
         }
 
-    }
 
 }
\ No newline at end of file