package com.microsoft.windowsazure.core.utils;

import java.util.ArrayList;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public abstract class XmlUtility {
    public static Element getElementByTagNameNS(Node element, String namespace,
            String name) {
        NodeList elements = element.getChildNodes();
        CharSequence colon = ":";
        if (elements != null) {
            for (int i = 0; i < elements.getLength(); i++) {
                if (elements.item(i).getNodeType() == Node.ELEMENT_NODE &&
                    (elements.item(i).getAttributes().getNamedItemNS("http://www.w3.org/2001/XMLSchema-instance", "nil") == null ||
                     !"true".equals(elements.item(i).getAttributes().getNamedItemNS("http://www.w3.org/2001/XMLSchema-instance", "nil")))) {
                    Element currentElement = (Element) elements.item(i);
                    String nodeName = currentElement.getNodeName();
                    String nodeNameOnly = nodeName;
                    if (nodeName.contains(colon))
                    {
                    	String[] nodeNameSplit = nodeName.split(":");
                    	nodeNameOnly = nodeNameSplit[1];
                    }
                    
                    if (currentElement.getNamespaceURI().equals(namespace)
                      && nodeNameOnly.equals(name)) {
                        return currentElement;
                    }
                }
            }
        }

        return null;
    }

    public static ArrayList<Element> getElementsByTagNameNS(Node element,
            String namespace, String name) {
        ArrayList<Element> childElements = new ArrayList<Element>();

        NodeList elements = element.getChildNodes();
        if (elements != null) {
            for (int i = 0; i < elements.getLength(); i++) {
                if (elements.item(i).getNodeType() == Node.ELEMENT_NODE) {
                    Element currentElement = (Element) elements.item(i);
                    if (currentElement.getNamespaceURI().equals(namespace)
                            && currentElement.getNodeName().equals(name)) {

                        childElements.add(currentElement);
                    }
                }
            }
        }

        return childElements;
    }

    public static Element getElementByTagName(Node element, String name) {
        NodeList elements = element.getChildNodes();
        if (elements != null) {
            for (int i = 0; i < elements.getLength(); i++) {
                if (elements.item(i).getNodeType() == Node.ELEMENT_NODE) {
                    Element currentElement = (Element) elements.item(i);
                    if (currentElement.getNodeName().equals(name)) {

                        return currentElement;
                    }
                }
            }
        }

        return null;
    }
}

@@ -546,19 +546,16 @@
 ENT_NODE
- &&
 %0A       
@@ -566,16 +566,19 @@
         
+ &&
  (elemen
@@ -682,11 +682,8 @@
 null
- %7C%7C
 %0A   
@@ -699,16 +699,18 @@
         
+%7C%7C
  !%22true%22
@@ -1064,36 +1064,16 @@
 (colon))
-%0A                   
  %7B%0A     
@@ -1087,17 +1087,20 @@
         
-%09
+    
 String%5B%5D
@@ -1161,9 +1161,12 @@
     
-%09
+    
 node

Fix checkstyle issues