Example usage for org.w3c.dom NamedNodeMap item

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

Introduction

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

Prototype

public Node item(int index);

Source Link

Document

Returns the indexth item in the map.

Usage

From source file:Main.java

@Nullable
public static Map<String, String> getAllAttributesAsMap(@Nullable final Element aSrcNode) {
    if (aSrcNode != null) {
        final NamedNodeMap aNNM = aSrcNode.getAttributes();
        if (aNNM != null) {
            final Map<String, String> aMap = new LinkedHashMap<String, String>(aNNM.getLength());
            final int nMax = aNNM.getLength();
            for (int i = 0; i < nMax; ++i) {
                final Attr aAttr = (Attr) aNNM.item(i);
                aMap.put(aAttr.getName(), aAttr.getValue());
            }//  w ww  . j  a v a2s . c o m
            return aMap;
        }
    }
    return null;
}

From source file:no.digipost.api.xml.Marshalling.java

public static void trimNamespaces(final Document doc) {
    NamedNodeMap attributes = doc.getDocumentElement().getAttributes();
    List<Attr> attrsToRemove = new ArrayList<Attr>();
    for (int i = 0; i < attributes.getLength(); i++) {
        if (doc.getElementsByTagNameNS(attributes.item(i).getNodeValue(), "*").getLength() == 0) {
            attrsToRemove.add((Attr) attributes.item(i));
        }// ww w  . j  a  v a2 s.co m
    }
    for (Attr a : attrsToRemove) {
        doc.getDocumentElement().removeAttributeNode(a);
    }
}

From source file:Main.java

/**
 * Checks whether the expected XML element is contained in the actual
 * element. I.e., every attribute of the expected element must occur in the
 * actual element, with the same value, and all child nodes in the expected
 * element must occur in the actual element; if multiple child nodes with
 * the same name are found in the expected element, the first found
 * identifying attribute in the expected child element is used to identify
 * the child in the actual element.//  www.j a va 2 s  .co m
 * 
 * @param expected
 * @param actual
 * @return
 */
public static void assertContains(Element expected, Element actual, String messagePrefix,
        String... identifyingAttributes) {
    if (!expected.getTagName().equals(actual.getTagName())) {
        throw new AssertionError(messagePrefix + "\nExpected tagname differs from actual tagname (expected '"
                + printNodeOnly(expected) + "', found " + printNodeOnly(actual) + ")");
    }

    // Compare attributes
    NamedNodeMap expectedAttributes = expected.getAttributes();
    for (int i = expectedAttributes.getLength() - 1; i >= 0; i--) {
        String expectedAttributeName = expectedAttributes.item(i).getNodeName();
        String expectedAttributeValue = expectedAttributes.item(i).getNodeValue();

        String actualValue = actual.getAttribute(expectedAttributeName);
        if (actualValue == null || actualValue.isEmpty()) {
            throw new AssertionError(messagePrefix + "\nThe attribute '" + expectedAttributeName
                    + "' with value '" + expectedAttributeValue + "' was not found in the result "
                    + printNodeOnly(actual));
        }
        if (!expectedAttributeValue.equals(actualValue)) {
            throw new AssertionError(messagePrefix + "\nThe attribute '" + expectedAttributeName
                    + "' has value '" + actualValue + "' instead of '" + expectedAttributeValue
                    + "' in the result " + printNodeOnly(actual));
        }
    }

    // Compare child elements
    Node child = expected.getFirstChild();
    while (child != null) {
        if (child instanceof Element) {
            assertContainsChildElement((Element) child, actual, messagePrefix, identifyingAttributes);
        }
        child = child.getNextSibling();
    }
}

From source file:Main.java

/**
 * @param n1 first Node to test//w  w  w.  ja  v  a 2 s.c  om
 * @param n2 second Node to test
 * @return true if a deep compare show the same children and attributes in
 * the same order
 */
public static boolean equals(Node n1, Node n2) {
    // compare type
    if (!n1.getNodeName().equals(n2.getNodeName())) {
        return false;
    }
    // compare attributes
    NamedNodeMap nnm1 = n1.getAttributes();
    NamedNodeMap nnm2 = n2.getAttributes();
    if (nnm1.getLength() != nnm2.getLength()) {
        return false;
    }
    for (int i = 0; i < nnm1.getLength(); i++) {
        Node attr1 = nnm1.item(i);
        if (!getAttribute(n1, attr1.getNodeName()).equals(getAttribute(n2, attr1.getNodeName()))) {
            return false;
        }
    }
    // compare children
    Node c1 = n1.getFirstChild();
    Node c2 = n2.getFirstChild();
    for (;;) {
        while ((c1 != null) && c1.getNodeName().startsWith("#")) {
            c1 = c1.getNextSibling();
        }
        while ((c2 != null) && c2.getNodeName().startsWith("#")) {
            c2 = c2.getNextSibling();
        }
        if ((c1 == null) && (c2 == null)) {
            break;
        }
        if ((c1 == null) || (c2 == null)) {
            return false;
        }
        if (!equals(c1, c2)) {
            return false;
        }
        c1 = c1.getNextSibling();
        c2 = c2.getNextSibling();
    }
    return true;
}

From source file:org.jasypt.spring31.xml.encryption.EncryptorConfigBeanDefinitionParser.java

private static Class<?> computeConfigClass(final Element element) {

    boolean isSimpleConfig = false;
    boolean isStringConfig = false;
    boolean isEnvironmentConfig = false;
    boolean isStringEnvironmentConfig = false;

    final NamedNodeMap attributesMap = element.getAttributes();
    final int attributesLen = attributesMap.getLength();
    for (int i = 0; i < attributesLen; i++) {
        final Node attribute = attributesMap.item(i);
        final String attributeName = attribute.getNodeName();
        if (!isSimpleConfig && PARAMS_SIMPLE.contains(attributeName)) {
            isSimpleConfig = true;/*from w w  w. j av  a  2s .c  o m*/
        }
        if (!isStringConfig && PARAMS_STRING.contains(attributeName)) {
            isStringConfig = true;
        }
        if (!isEnvironmentConfig && PARAMS_ENVIRONMENT.contains(attributeName)) {
            isEnvironmentConfig = true;
        }
        if (!isStringEnvironmentConfig && PARAMS_STRING_ENVIRONMENT.contains(attributeName)) {
            isStringEnvironmentConfig = true;
        }
    }

    if (isStringEnvironmentConfig || (isEnvironmentConfig && isStringConfig)) {
        return EnvironmentStringPBEConfig.class;
    }
    if (isEnvironmentConfig) {
        return EnvironmentPBEConfig.class;
    }
    if (isStringConfig) {
        return SimpleStringPBEConfig.class;
    }
    return SimplePBEConfig.class;

}

From source file:Main.java

/**
 * Print a Node tree recursively.//from w  w  w . j  a v  a  2  s . c o m
 * @param node A DOM tree Node
 * @return An xml String representation of the DOM tree.
 */
public static String print(Node node) {
    if (node == null) {
        return null;
    }

    StringBuffer xml = new StringBuffer(100);
    int type = node.getNodeType();

    switch (type) {
    // print element with attributes
    case Node.ELEMENT_NODE: {
        xml.append('<');
        xml.append(node.getNodeName());

        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)));
            }
        }

        break;
    }

    // handle entity reference nodes
    case Node.ENTITY_REFERENCE_NODE: {
        NodeList children = node.getChildNodes();

        if (children != null) {
            int len = children.getLength();

            for (int i = 0; i < len; i++) {
                xml.append(print(children.item(i)));
            }
        }

        break;
    }

    // print cdata sections
    case Node.CDATA_SECTION_NODE: {
        xml.append("<![CDATA[");
        xml.append(node.getNodeValue());
        xml.append("]]>");

        break;
    }

    // print text
    case Node.TEXT_NODE: {
        //xml.append(normalize(node.getNodeValue()));
        xml.append(node.getNodeValue());

        break;
    }

    // print processing instruction
    case Node.PROCESSING_INSTRUCTION_NODE: {
        xml.append("<?");
        xml.append(node.getNodeName());

        String data = node.getNodeValue();

        if ((data != null) && (data.length() > 0)) {
            xml.append(' ');
            xml.append(data);
        }

        xml.append("?>");

        break;
    }
    }

    if (type == Node.ELEMENT_NODE) {
        xml.append("</");
        xml.append(node.getNodeName());
        xml.append('>');
    }

    return xml.toString();
}

From source file:com.tascape.qa.th.android.model.UIA.java

public static UIANode parseNode(Node node) {
    if (!node.getNodeName().equals(UIANode.TAG_NAME)) {
        return null;
    }/*from   w w w. j  av  a  2  s  .  c om*/

    NamedNodeMap map = node.getAttributes();
    String klass = map.getNamedItem("class").getNodeValue();
    UIANode uiNode = newNode(klass);

    for (int i = 0, j = map.getLength(); i < j; i++) {
        Node attr = map.item(i);
        uiNode.setAttribute(attr.getNodeName(), attr.getNodeValue());
    }

    NodeList nl = node.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        UIANode n = parseNode(nl.item(i));
        if (n == null) {
            continue;
        }
        uiNode.addNode(n);
    }

    return uiNode;
}

From source file:org.jasypt.spring31.xml.encryption.DigesterConfigBeanDefinitionParser.java

private static Class<?> computeConfigClass(final Element element) {

    boolean isSimpleConfig = false;
    boolean isStringConfig = false;
    boolean isEnvironmentConfig = false;
    boolean isStringEnvironmentConfig = false;

    final NamedNodeMap attributesMap = element.getAttributes();
    final int attributesLen = attributesMap.getLength();
    for (int i = 0; i < attributesLen; i++) {
        final Node attribute = attributesMap.item(i);
        final String attributeName = attribute.getNodeName();
        if (!isSimpleConfig && PARAMS_SIMPLE.contains(attributeName)) {
            isSimpleConfig = true;/*from  www.  j av  a2s  .  c  om*/
        }
        if (!isStringConfig && PARAMS_STRING.contains(attributeName)) {
            isStringConfig = true;
        }
        if (!isEnvironmentConfig && PARAMS_ENVIRONMENT.contains(attributeName)) {
            isEnvironmentConfig = true;
        }
        if (!isStringEnvironmentConfig && PARAMS_STRING_ENVIRONMENT.contains(attributeName)) {
            isStringEnvironmentConfig = true;
        }
    }

    if (isStringEnvironmentConfig || (isEnvironmentConfig && isStringConfig)) {
        return EnvironmentStringDigesterConfig.class;
    }
    if (isEnvironmentConfig) {
        return EnvironmentDigesterConfig.class;
    }
    if (isStringConfig) {
        return SimpleStringDigesterConfig.class;
    }
    return SimpleDigesterConfig.class;

}

From source file:Main.java

/**
 * Performs the actual recursive dumping of a DOM tree to a given
 * <CODE>PrintStream</CODE>. Note that dump is intended to be a detailed
 * debugging aid rather than pretty to look at. 
 * //from   w w  w  .  j  a v a 2 s.  c o m
 * @param    out            The <CODE>PrintStream</CODE> to write to.
 * @param    node         The <CODE>Node</CODE> under consideration.
 * @param    indent         The level of indentation.
 * @see      #dump(Node)
 * @see      #dump(PrintStream, Node)
 * @since   TFP 1.0
 */
private static void doDump(PrintStream out, final Node node, int indent) {
    if (node != null) {
        for (int index = 0; index < indent; ++index)
            out.write(' ');

        switch (node.getNodeType()) {
        case Node.DOCUMENT_NODE: {
            Document document = (Document) node;

            out.println("DOCUMENT:");

            doDump(out, document.getDoctype(), indent + 1);
            doDump(out, document.getDocumentElement(), indent + 1);
            break;
        }

        case Node.DOCUMENT_TYPE_NODE: {
            DocumentType type = (DocumentType) node;

            out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId="
                    + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]");
            break;
        }

        case Node.ELEMENT_NODE: {
            Element element = (Element) node;

            out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name="
                    + format(element.getLocalName()) + "]");

            NamedNodeMap attrs = element.getAttributes();

            for (int index = 0; index < attrs.getLength(); ++index)
                doDump(out, attrs.item(index), indent + 1);

            for (Node child = element.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }
        case Node.ATTRIBUTE_NODE: {
            Attr attr = (Attr) node;

            out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix="
                    + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value="
                    + format(attr.getNodeValue()) + "]");
            break;
        }

        case Node.TEXT_NODE: {
            Text text = (Text) node;

            out.println("TEXT: [" + format(text.getNodeValue()) + "]");

            for (Node child = text.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }

        case Node.CDATA_SECTION_NODE: {
            CDATASection data = (CDATASection) node;

            out.println("CDATA: [" + format(data.getNodeValue()) + "]");
            break;
        }

        case Node.COMMENT_NODE: {
            Comment comm = (Comment) node;

            out.println("COMMENT: [" + format(comm.getNodeValue()) + "]");
            break;
        }

        default:
            out.println("UNKNOWN: [type=" + node.getNodeType() + "]");
            break;
        }
    }
}

From source file:Main.java

/**
 * Performs the actual recursive dumping of a DOM tree to a given
 * <CODE>PrintWriter</CODE>. Note that dump is intended to be a detailed
 * debugging aid rather than pretty to look at. 
 * /*from   ww w.j av a  2s.  c  o  m*/
 * @param    out            The <CODE>PrintWriter</CODE> to write to.
 * @param    node         The <CODE>Node</CODE> under consideration.
 * @param    indent         The level of indentation.
 * @see      #dump(PrintWriter, Node)
 * @since   TFP 1.0
 */
private static void doDump(PrintWriter out, final Node node, int indent) {
    if (node != null) {
        for (int index = 0; index < indent; ++index)
            out.write(' ');

        switch (node.getNodeType()) {
        case Node.DOCUMENT_NODE: {
            Document document = (Document) node;

            out.println("DOCUMENT:");

            doDump(out, document.getDoctype(), indent + 1);
            doDump(out, document.getDocumentElement(), indent + 1);
            break;
        }

        case Node.DOCUMENT_TYPE_NODE: {
            DocumentType type = (DocumentType) node;

            out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId="
                    + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]");
            break;
        }

        case Node.ELEMENT_NODE: {
            Element element = (Element) node;

            out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name="
                    + format(element.getLocalName()) + "]");

            NamedNodeMap attrs = element.getAttributes();

            for (int index = 0; index < attrs.getLength(); ++index)
                doDump(out, attrs.item(index), indent + 1);

            for (Node child = element.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }
        case Node.ATTRIBUTE_NODE: {
            Attr attr = (Attr) node;

            out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix="
                    + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value="
                    + format(attr.getNodeValue()) + "]");
            break;
        }

        case Node.TEXT_NODE: {
            Text text = (Text) node;

            out.println("TEXT: [" + format(text.getNodeValue()) + "]");

            for (Node child = text.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }

        case Node.CDATA_SECTION_NODE: {
            CDATASection data = (CDATASection) node;

            out.println("CDATA: [" + format(data.getNodeValue()) + "]");
            break;
        }

        case Node.COMMENT_NODE: {
            Comment comm = (Comment) node;

            out.println("COMMENT: [" + format(comm.getNodeValue()) + "]");
            break;
        }

        default:
            out.println("UNKNOWN: [type=" + node.getNodeType() + "]");
            break;
        }
    }
}