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

/**
 *  Copy the attributes from n2 to n1./* w w w  .j  a  v a2s.  co m*/
 *
 *  @param n1 The source of the attributes.
 *  @param n2 What to copy into.
 */
public static void mergeAttributes(Element n1, Element n2) {
    if ((n1 == null) || (n2 == null)) {
        return;
    }
    NamedNodeMap nnm = n2.getAttributes();
    if (nnm == null) {
        return;
    }
    for (int i = 0; i < nnm.getLength(); i++) {
        Attr attr = (Attr) nnm.item(i);
        n1.setAttribute(attr.getNodeName(), attr.getNodeValue());
    }

}

From source file:Main.java

@SuppressWarnings("fallthrough")
static final void getSetRec(final Node rootNode, final Set<Node> result, final Node exclude,
        final boolean com) {
    //Set result = new HashSet();
    if (rootNode == exclude) {
        return;//ww  w  .  j a  v a  2s .c  o m
    }
    switch (rootNode.getNodeType()) {
    case Node.ELEMENT_NODE:
        result.add(rootNode);
        Element el = (Element) rootNode;
        if (el.hasAttributes()) {
            NamedNodeMap nl = ((Element) rootNode).getAttributes();
            for (int i = 0; i < nl.getLength(); i++) {
                result.add(nl.item(i));
            }
        }
        //no return keep working - ignore fallthrough warning
    case Node.DOCUMENT_NODE:
        for (Node r = rootNode.getFirstChild(); r != null; r = r.getNextSibling()) {
            if (r.getNodeType() == Node.TEXT_NODE) {
                result.add(r);
                while ((r != null) && (r.getNodeType() == Node.TEXT_NODE)) {
                    r = r.getNextSibling();
                }
                if (r == null)
                    return;
            }
            getSetRec(r, result, exclude, com);
        }
        return;
    case Node.COMMENT_NODE:
        if (com) {
            result.add(rootNode);
        }
        return;
    case Node.DOCUMENT_TYPE_NODE:
        return;
    default:
        result.add(rootNode);
    }
    return;
}

From source file:Main.java

/**
 * Print SAML Attribute Element and replace its prefix with the input
 * prefix./*from ww  w  . ja  va2 s . c  om*/
 * 
 * @param node
 *            A DOM tree Node
 * @param prefix
 *            A String representing the new prefix
 * @return An xml String representation of the DOM tree.
 */
public static String printAttributeValue(Element node, String prefix) {
    if (node == null) {
        return null;
    }

    StringBuffer xml = new StringBuffer(100);
    xml.append('<');
    xml.append(prefix).append(node.getLocalName());
    NamedNodeMap attrs = node.getAttributes();
    int length = attrs.getLength();
    for (int i = 0; i < length; i++) {
        Attr attr = (Attr) attrs.item(i);
        xml.append(' ');
        xml.append(attr.getNodeName());
        xml.append("=\"");
        // xml.append(normalize(attr.getNodeValue()));
        xml.append(attr.getNodeValue());
        xml.append('"');
    }
    xml.append('>');
    NodeList children = node.getChildNodes();
    if (children != null) {
        int len = children.getLength();
        for (int i = 0; i < len; i++) {
            xml.append(print(children.item(i)));
        }
    }
    xml.append("</");
    xml.append(prefix).append(node.getLocalName());
    xml.append('>');

    return xml.toString();

}

From source file:Main.java

/**
 * Prints elements of the specified node.
 * //w  w  w .ja  va 2  s  .c  o  m
 * @param node the specified node.
 * @param level a number of indent.
 */
private static void printElement(Node node, int level) {
    for (int i = 0; i < level; i++) {
        System.out.print("  ");
    }
    System.out.print("{" + node.getNamespaceURI() + "}");
    System.out.println(node.getNodeName());
    NamedNodeMap attrs = node.getAttributes();
    if (attrs != null) {
        for (int i = 0; i < attrs.getLength(); i++) {
            printNode(attrs.item(i), level + 1);
        }
    }

    NodeList children = node.getChildNodes();
    int n = children.getLength();
    for (int i = 0; i < n; i++) {
        Node child = children.item(i);
        printNode(child, level + 1);
    }
}

From source file:com.mingo.parser.xml.dom.DomUtil.java

/**
 * Transform node attributes to map./*from ww  w  . ja  v  a  2s. co m*/
 *
 * @param node {@link Node}
 * @return map : key - attribute name; value - attribute value
 */
public static Map<String, String> getAttributes(Node node) {
    Map<String, String> attributes = ImmutableMap.of();
    if (node.hasAttributes()) {
        ImmutableMap.Builder builder = ImmutableMap.builder();
        // get attributes names and values
        NamedNodeMap nodeMap = node.getAttributes();
        for (int i = 0; i < nodeMap.getLength(); i++) {
            Node currentNode = nodeMap.item(i);
            builder.put(currentNode.getNodeName(), currentNode.getNodeValue());
        }
        attributes = builder.build();
    }
    return attributes;
}

From source file:Main.java

/**
 * Searches throgh the passed NamedNodeMap for an attribute. If it is found, it will try to convert it to a boolean.
 * @param nnm NamedNodeMap/*  w ww  .ja  va  2 s  .  c o  m*/
 * @param name String
 * @throws RuntimeException on any failure to parse a boolean
 * @return boolean
 */
public static boolean getAttributeBooleanByName(NamedNodeMap nnm, String name) throws RuntimeException {
    for (int i = 0; i < nnm.getLength(); i++) {
        Attr attr = (Attr) nnm.item(i);
        if (attr.getName().equalsIgnoreCase(name)) {
            String tmp = attr.getValue().toLowerCase();
            if (tmp.equalsIgnoreCase("true"))
                return true;
            if (tmp.equalsIgnoreCase("false"))
                return false;
            throw new RuntimeException("Attribute " + name + " value not boolean:" + tmp);
        }
    }
    throw new RuntimeException("Attribute " + name + " not found.");
}

From source file:Main.java

public static void collectNamespaces(Node node, Map namespaces, Map prefixes) {
    if (node == null) {
        throw new IllegalArgumentException("nullArgument: node");
        //                    i18n.getMessage("nullArgument", "node"));
    }/*from  w  w w .  j av a 2  s.  c o  m*/
    if (namespaces == null) {
        throw new IllegalArgumentException("nullArgument: namespace");
        //                    i18n.getMessage("nullArgument", "namespaces"));
    }
    if (prefixes == null) {
        prefixes = new HashMap();
    }
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            Attr attr = (Attr) attributes.item(i);
            String name = attr.getName();
            String value = attr.getValue();
            if (name.startsWith("xmlns:")) {
                if (namespaces.get(value) == null) {
                    // ns not defined
                    if (prefixes.get(name) != null) {
                        // find unique prefix
                        int j = 1;
                        do {
                            name = "xmlns:ns" + j++;
                        } while (prefixes.get(name) != null);
                    }
                    prefixes.put(name, value);
                    namespaces.put(value, name);
                }
            }
        }
    }
    NodeList children = node.getChildNodes();
    if (children != null) {
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                collectNamespaces(child, namespaces, prefixes);
            }
        }
    }
}

From source file:Main.java

/**
 * Used for debuging//from ww w .  jav  a 2 s.  co  m
 * 
 * @param parent
 *            Element
 * @param out
 *            PrintStream
 * @param deep
 *            boolean
 * @param prefix
 *            String
 */
public static void printChildElements(Element parent, PrintStream out, boolean deep, String prefix) {
    out.print(prefix + "<" + parent.getNodeName());
    NamedNodeMap attrs = parent.getAttributes();
    Node node;
    for (int i = 0; i < attrs.getLength(); i++) {
        node = attrs.item(i);
        out.print(" " + node.getNodeName() + "=\"" + node.getNodeValue() + "\"");
    }
    out.println(">");

    // String data = getElementTextValueDeprecated(parent);
    String data = parent.getNodeValue();
    if (data != null && data.trim().length() > 0) {
        out.println(prefix + "\t" + data);
    }

    data = getElementCDataValue(parent);
    if (data != null && data.trim().length() > 0) {
        out.println(prefix + "\t<![CDATA[" + data + "]]>");
    }

    NodeList nodes = parent.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (deep) {
                printChildElements((Element) node, out, deep, prefix + "\t");
            } else {
                out.println(prefix + node.getNodeName());
            }
        }
    }

    out.println(prefix + "</" + parent.getNodeName() + ">");
}

From source file:XmlUtil.java

/**
 * Returns a map of all node's attributes. All non-attribute nodes are ignored.
 *///from   w ww.j  a v a 2 s .  c o  m
public static Map<String, String> getAllAttributes(Node node) {
    HashMap<String, String> attrs = new HashMap<String, String>();
    NamedNodeMap nmm = node.getAttributes();
    for (int j = 0; j < nmm.getLength(); j++) {
        Node attribute = nmm.item(j);
        if (attribute.getNodeType() != Node.ATTRIBUTE_NODE) {
            continue;
        }
        attrs.put(attribute.getNodeName(), attribute.getNodeValue());
    }
    return attrs;
}

From source file:Main.java

static final void getSetRec(final Node rootNode, final Set result, final Node exclude, final boolean com) {
    //Set result = new HashSet();
    if (rootNode == exclude) {
        return;//w w  w. j a  va 2 s  .  c om
    }
    switch (rootNode.getNodeType()) {
    case Node.ELEMENT_NODE:
        result.add(rootNode);
        Element el = (Element) rootNode;
        if (el.hasAttributes()) {
            NamedNodeMap nl = ((Element) rootNode).getAttributes();
            for (int i = 0; i < nl.getLength(); i++) {
                result.add(nl.item(i));
            }
        }
        //no return keep working
    case Node.DOCUMENT_NODE:
        for (Node r = rootNode.getFirstChild(); r != null; r = r.getNextSibling()) {
            if (r.getNodeType() == Node.TEXT_NODE) {
                result.add(r);
                while ((r != null) && (r.getNodeType() == Node.TEXT_NODE)) {
                    r = r.getNextSibling();
                }
                if (r == null)
                    return;
            }
            getSetRec(r, result, exclude, com);
        }
        return;
    case Node.COMMENT_NODE:
        if (com) {
            result.add(rootNode);
        }
        return;
    case Node.DOCUMENT_TYPE_NODE:
        return;
    default:
        result.add(rootNode);
    }
    return;
}