Example usage for org.w3c.dom NamedNodeMap getLength

List of usage examples for org.w3c.dom NamedNodeMap getLength

Introduction

In this page you can find the example usage for org.w3c.dom NamedNodeMap getLength.

Prototype

public int getLength();

Source Link

Document

The number of nodes in this map.

Usage

From source file:Main.java

public static String getNodeAttr(String attrName, Node node) {
    NamedNodeMap attrs = node.getAttributes();
    for (int y = 0; y < attrs.getLength(); y++) {
        Node attr = attrs.item(y);
        if (attr.getNodeName().equalsIgnoreCase(attrName)) {
            return attr.getNodeValue();
        }/*w  w w.j a va 2  s.c  o m*/
    }
    return "";
}

From source file:Main.java

/**
 * Rename an element, replacing it in its document.
 * @param element the element to rename.
 * @param name the new element name.// ww w.j  a  v  a 2 s. c  o  m
 * @return the renamed element.
 */
public static Element renameElement(Element element, String name) {
    if (element.getNodeName().equals(name))
        return element;
    Element el = element.getOwnerDocument().createElement(name);

    //Copy the attributes
    NamedNodeMap attributes = element.getAttributes();
    int nAttrs = attributes.getLength();
    for (int i = 0; i < nAttrs; i++) {
        Node attr = attributes.item(i);
        el.setAttribute(attr.getNodeName(), attr.getNodeValue());
    }

    //Copy the children
    Node node = element.getFirstChild();
    while (node != null) {
        Node clone = node.cloneNode(true);
        el.appendChild(clone);
        node = node.getNextSibling();
    }

    //Replace the element
    element.getParentNode().replaceChild(el, element);
    return el;
}

From source file:Main.java

public static void removeInvalidAttributes(Element element, String... validAttributeNames) {
    Set<String> validNames = new HashSet<String>();
    for (String name : validAttributeNames) {
        validNames.add(name);//from w ww .j a va2 s .com
    }
    boolean elementModified = false;
    NamedNodeMap nnm = element.getAttributes();
    for (int i = 0; i < nnm.getLength(); i++) {
        String attributeName = nnm.item(i).getNodeName();
        if (!validNames.contains(attributeName)) {
            element.removeAttribute(attributeName);
            elementModified = true;
        }
    }
    if (elementModified) {
        flagDocumentAsCorrected(element);
    }
}

From source file:Main.java

/**
 * Converts {@link NamedNodeMap} to a {@link LinkedHashMap}&lt;String,String&gt;.
 * @param _nodeMap node map/*w w  w .jav a 2s  . co m*/
 * @return {@link LinkedHashMap}, maybe empty but never null
 */
public static Map<String, String> convertToAttributeMap(NamedNodeMap _nodeMap) {
    Map<String, String> map = new LinkedHashMap<>();
    for (int i = 0; i < _nodeMap.getLength(); i++) {
        Node node = _nodeMap.item(i);
        map.put(node.getNodeName(), node.getNodeValue());
    }
    return map;
}

From source file:Main.java

/**
 * A method to get attribute value from provided xml node and attribute name
 * @param Node parent, a node where the attribute residing
 * @param String attr, attribute name to get
 * @return String , a value from request attribute 
 *///from   w  w  w.j a va2  s  .  c o  m
static public String getAttribute(Node parent, String Attr) {
    String ret = "";

    if (!parent.hasAttributes())
        return ("");
    NamedNodeMap nmap = parent.getAttributes();

    for (int i = 0; i < nmap.getLength(); ++i) {
        Node n = nmap.item(i);
        if (n.getNodeName().trim().equals(Attr)) {
            ret = n.getNodeValue().trim();
        }
        //System.out.println("Attribute: "+n.getNodeType()+" - "+n.getNodeName()+" "+n.getNodeValue());

    }
    return (ret);
}

From source file:Main.java

static public String getNodeAttr(String attrName, Node node) {
    NamedNodeMap attrs = node.getAttributes();
    for (int y = 0; y < attrs.getLength(); y++) {
        Node attr = attrs.item(y);
        if (attr.getNodeName().equalsIgnoreCase(attrName)) {
            return attr.getNodeValue();
        }//from   www.j a  va2  s . c o m
    }
    return "";
}

From source file:Main.java

/**
 * Compare necessary namespace declarations between original and proposed
 * document, if namespaces in the original are missing compared to the
 * proposed, we add them to the original.
 * /*from w w w .  ja  v  a  2 s.co m*/
 * @param original document as read from the file system
 * @param proposed document as determined by the JspViewManager
 * @return true if the document was adjusted, otherwise false
 */
private static boolean checkNamespaces(final Document original, final Document proposed) {
    boolean originalDocumentChanged = false;
    final NamedNodeMap nsNodes = proposed.getDocumentElement().getAttributes();
    for (int i = 0; i < nsNodes.getLength(); i++) {
        if (0 == original.getDocumentElement().getAttribute(nsNodes.item(i).getNodeName()).length()) {
            original.getDocumentElement().setAttribute(nsNodes.item(i).getNodeName(),
                    nsNodes.item(i).getNodeValue());
            originalDocumentChanged = true;
        }
    }
    return originalDocumentChanged;
}

From source file:Main.java

private static void attributize(Element root) {
    NamedNodeMap attributeMap = root.getAttributes();
    for (int i = 0; i < attributeMap.getLength(); i++) {
        org.w3c.dom.Attr attr = (Attr) attributeMap.item(i);

        Element attrElement = root.getOwnerDocument().createElement(attr.getName());
        attrElement.setTextContent(attr.getValue());
        root.appendChild(attrElement);/*from  w ww .  ja  v a  2s.  c o  m*/
    }

    NodeList children = root.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i) instanceof Element) {
            attributize((Element) children.item(i));
        }
    }
}

From source file:Main.java

public static Properties extractProperties(Node node) {
    Properties props = new Properties();
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            Node item = attributes.item(i);
            props.put(item.getNodeName(), item.getNodeValue());
        }/*from   w w  w  .j a  va2s . co  m*/
    }
    return props;
}

From source file:Main.java

private static String readXsdVersionFromFile(Document doc) {
    final String JBOSS_ESB = "jbossesb";
    NodeList nodes = doc.getChildNodes();
    if (nodes != null) {
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            if (JBOSS_ESB.equals(node.getNodeName())) {
                NamedNodeMap attributes = node.getAttributes();
                for (int j = 0; j < attributes.getLength(); j++) {
                    Node attribute = attributes.item(j);
                    if ("xmlns".equals(attribute.getNodeName())) {
                        String value = attribute.getNodeValue();
                        if (value.contains(JBOSS_ESB) && value.endsWith(".xsd"))
                            return value.substring(value.lastIndexOf('/') + 1, value.length());
                        else
                            throw new IllegalStateException(
                                    "The ESB descriptor points to an invalid XSD" + value);
                    }/*from   www . j  av a  2  s .  c o m*/
                }
            }
        }
        throw new IllegalArgumentException("No root node " + JBOSS_ESB + " found.");
    } else
        throw new IllegalArgumentException("Descriptor has no root element !");
}