Refactor shutdown-impl: add parameter to RPC, remove secret validation and masking.
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / xml / XmlElement.java
index 5a7fde4e3ddd3e0f29720ebb914cf1a447817d8b..18a94c6d07ff7cf74289bfb5d3e057967a61e86c 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);
@@ -257,7 +272,9 @@ public class XmlElement {
 
     public String getTextContent() {
         Node textChild = element.getFirstChild();
-        Preconditions.checkState(textChild instanceof Text, getName() + " should contain text");
+        Preconditions.checkNotNull(textChild, "Child node expected, got null for " + getName() + " : " + element);
+        Preconditions.checkState(textChild instanceof Text, getName() + " should contain text." +
+                Text.class.getName() + " expected, got " + textChild);
         String content = textChild.getTextContent();
         // Trim needed
         return content.trim();
@@ -265,8 +282,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 +328,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 +378,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);
     }