Merge "Bug 164"
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / xml / XmlElement.java
index 5a7fde4e3ddd3e0f29720ebb914cf1a447817d8b..212214cd2ab516e8f7c8e1226b63bb16d886e927 100644 (file)
@@ -8,20 +8,28 @@
 
 package org.opendaylight.controller.netconf.util.xml;
 
-import java.io.IOException;
-import java.util.*;
-
-import javax.annotation.Nullable;
-
-import org.w3c.dom.*;
-import org.xml.sax.SAXException;
-
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Predicate;
 import com.google.common.collect.Collections2;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
+import org.xml.sax.SAXException;
+
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 public class XmlElement {
 
@@ -95,6 +103,9 @@ public class XmlElement {
     }
 
     public String getName() {
+        if (element.getLocalName()!=null && !element.getLocalName().equals("")){
+            return element.getLocalName();
+        }
         return element.getTagName();
     }
 
@@ -106,6 +117,10 @@ public class XmlElement {
         return element.getAttributeNS(namespace, attributeName);
     }
 
+    public NodeList getElementsByTagName(String name) {
+        return element.getElementsByTagName(name);
+    }
+
     public void appendChild(Element element) {
         this.element.appendChild(element);
         // Element newElement = (Element) element.cloneNode(true);
@@ -265,8 +280,8 @@ public class XmlElement {
 
     public String getNamespaceAttribute() {
         String attribute = element.getAttribute(XmlUtil.XMLNS_ATTRIBUTE_KEY);
-        Preconditions.checkState(attribute != null && !attribute.equals(""), "Element %s must specify a %s attribute",
-                toString(), XmlUtil.XMLNS_ATTRIBUTE_KEY);
+        Preconditions.checkState(attribute != null && !attribute.equals(""), "Element %s must specify namespace",
+                toString());
         return attribute;
     }
 
@@ -311,7 +326,7 @@ public class XmlElement {
             prefix = "";
         }
         if (namespaces.containsKey(prefix) == false) {
-            throw new IllegalArgumentException("Cannot find namespace for " + element + ". Prefix from content is "
+            throw new IllegalArgumentException("Cannot find namespace for " + XmlUtil.toString(element) + ". Prefix from content is "
                     + prefix + ". Found namespaces " + namespaces);
         }
         return Maps.immutableEntry(prefix, namespaces.get(prefix));
@@ -361,6 +376,20 @@ public class XmlElement {
         return element.hashCode();
     }
 
+    public boolean hasNamespace() {
+        try {
+            getNamespaceAttribute();
+        } catch (IllegalStateException e) {
+            try {
+                getNamespace();
+            } catch (IllegalStateException e1) {
+                return false;
+            }
+            return true;
+        }
+        return true;
+    }
+
     private static interface ElementFilteringStrategy {
         boolean accept(Element e);
     }