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:MainClass.java

public void processNode(Node node, String spacer) throws IOException {
    if (node == null)
        return;/*from  www .  ja  va  2s .  c  o m*/
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        String name = node.getNodeName();
        System.out.print(spacer + "<" + name);
        NamedNodeMap nnm = node.getAttributes();
        for (int i = 0; i < nnm.getLength(); i++) {
            Node current = nnm.item(i);
            System.out.print(" " + current.getNodeName() + "= " + current.getNodeValue());
        }
        System.out.print(">");
        NodeList nl = node.getChildNodes();
        if (nl != null) {
            for (int i = 0; i < nl.getLength(); i++) {
                processNode(nl.item(i), "");
            }
        }
        System.out.println(spacer + "</" + name + ">");
        break;
    case Node.TEXT_NODE:
        System.out.print(node.getNodeValue());
        break;
    case Node.CDATA_SECTION_NODE:
        System.out.print("" + node.getNodeValue() + "");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        System.out.print("&" + node.getNodeName() + ";");
        break;
    case Node.ENTITY_NODE:
        System.out.print("<ENTITY: " + node.getNodeName() + "> </" + node.getNodeName() + "/>");
        break;
    case Node.DOCUMENT_NODE:
        NodeList nodes = node.getChildNodes();
        if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {
                processNode(nodes.item(i), "");
            }
        }
        break;
    case Node.DOCUMENT_TYPE_NODE:
        DocumentType docType = (DocumentType) node;
        System.out.print("<!DOCTYPE " + docType.getName());
        if (docType.getPublicId() != null) {
            System.out.print(" PUBLIC " + docType.getPublicId() + " ");
        } else {
            System.out.print(" SYSTEM ");
        }
        System.out.println(" " + docType.getSystemId() + ">");
        break;
    default:
        break;
    }
}

From source file:com.nickandjerry.dynamiclayoutinflator.DynamicLayoutInflator.java

private static HashMap<String, String> getAttributesMap(Node currentNode) {
    NamedNodeMap attributeMap = currentNode.getAttributes();
    int attributeCount = attributeMap.getLength();
    HashMap<String, String> attributes = new HashMap<>(attributeCount);
    for (int j = 0; j < attributeCount; j++) {
        Node attr = attributeMap.item(j);
        String nodeName = attr.getNodeName();
        if (nodeName.startsWith("android:"))
            nodeName = nodeName.substring(8);
        attributes.put(nodeName, attr.getNodeValue());
    }/*w  ww.j a  va 2  s  .  co m*/
    return attributes;
}

From source file:eu.elf.license.LicenseParser.java

/**
 * Creates list of license model parameters
 *
 * @param declarationListElement - XML element <x:declarationList>
 * @return List of LicenseParam objects/*from   w  w w.  j a va  2  s  .  c  o m*/
 */
private static List<LicenseParam> createLicenseModelParamList(Element declarationListElement) {
    List<LicenseParam> paramList = new ArrayList<LicenseParam>();

    // Predefined Parameters - create as LicenseParamDisplay objects
    Element predefinedParametersElement = (Element) declarationListElement
            .getElementsByTagNameNS("http://www.conterra.de/xcpf/1.1", "predefinedParameters").item(0);

    if (predefinedParametersElement != null) {
        NodeList predefinedParametersParameterElementList = predefinedParametersElement
                .getElementsByTagNameNS("http://www.conterra.de/xcpf/1.1", "parameter");

        for (int n = 0; n < predefinedParametersParameterElementList.getLength(); n++) {
            Element parameterElement = (Element) predefinedParametersParameterElementList.item(n);

            LicenseParamDisplay displayParam = createLicenseParamDisplay(parameterElement,
                    "predefinedParameter");
            paramList.add(displayParam);

        }
    }

    // Configuration Parameters - might be LicenseParamDisplay || LicenseParamBln || LicenseParamEnum || LicenseParamInt || LicenseParamText
    Element configurationParametersElement = (Element) declarationListElement
            .getElementsByTagNameNS("http://www.conterra.de/xcpf/1.1", "configurationParameters").item(0);

    if (configurationParametersElement != null) {
        NodeList configurationParametersParameterElementList = configurationParametersElement
                .getElementsByTagNameNS("http://www.conterra.de/xcpf/1.1", "parameter");

        for (int n = 0; n < configurationParametersParameterElementList.getLength(); n++) {
            Element parameterElement = (Element) configurationParametersParameterElementList.item(n);

            LicenseParam lp = null;
            Boolean isStringType = false;
            Boolean multiAttributeExists = false; // true for enumration type parameters
            //Boolean multiAttributeValue = false; // value of the multi attribute 
            //Boolean optionalAttribute = false;  // What does optional attribute signify???

            NamedNodeMap parameterElementAttributeMap = parameterElement.getAttributes();

            // Create appropriate subclass object based on the "type" and "multi" attributes
            for (int o = 0; o < parameterElementAttributeMap.getLength(); o++) {
                Attr attrs = (Attr) parameterElementAttributeMap.item(o);

                if (attrs.getNodeName().equals("type")) {
                    if (attrs.getNodeValue().equals("real")) {
                        lp = createLicenseParamInt(parameterElement, "configurationParameter");
                        paramList.add(lp);
                        //System.out.println("lp - name "+lpInt.getName());
                    } else if (attrs.getNodeValue().equals("string")) {
                        isStringType = true;
                    } else if (attrs.getNodeValue().equals("boolean")) {
                        lp = createLicenseParamBln(parameterElement, "configurationParameter");
                        paramList.add(lp);
                    }

                }
                if (attrs.getNodeName().equals("multi")) {
                    multiAttributeExists = true;
                    //if (attrs.getNodeValue().equals("true")) {

                    //multiAttributeValue = true;
                    //}
                    //else {
                    //multiAttributeExists = false;
                    //multiAttributeValue = false;
                    //}
                }
                // if (attrs.getNodeName().equals("optional")) {
                //    if (attrs.getNodeName().equals("true")) {
                //       optionalAttribute = true;
                //    }
                //    else {
                //       optionalAttribute = false;
                //    }
                // }

            }

            if (isStringType == true) {
                if (multiAttributeExists == true) {
                    lp = createLicenseParamEnum(parameterElement, "configurationParameter");
                    paramList.add(lp);

                    //if (optionalAttribute == true) {
                    //   
                    //    lp = createLicenseParamEnum(parameterElement, "configurationParameter");
                    //     paramList.add(lp);
                    //} 
                } else {
                    lp = createLicenseParamText(parameterElement, "configurationParameter");
                    paramList.add(lp);
                }
            }

        }
    }

    // Precalculated Parameters - create as LicenseParamDisplay objects
    Element precalculatedParametersElement = (Element) declarationListElement
            .getElementsByTagNameNS("http://www.conterra.de/xcpf/1.1", "precalculatedParameters").item(0);

    if (precalculatedParametersElement != null) {
        NodeList precalculatedParametersParameterElementList = precalculatedParametersElement
                .getElementsByTagNameNS("http://www.conterra.de/xcpf/1.1", "parameter");

        for (int n = 0; n < precalculatedParametersParameterElementList.getLength(); n++) {
            Element parameterElement = (Element) precalculatedParametersParameterElementList.item(n);

            LicenseParamDisplay displayParam = createLicenseParamDisplay(parameterElement,
                    "precalculatedParameter");
            paramList.add(displayParam);

        }
    }

    // Result Parameters - create as LicenseParamDisplay objects
    Element resultParametersElement = (Element) declarationListElement
            .getElementsByTagNameNS("http://www.conterra.de/xcpf/1.1", "resultParameters").item(0);

    if (resultParametersElement != null) {
        NodeList resultParametersParameterElementList = resultParametersElement
                .getElementsByTagNameNS("http://www.conterra.de/xcpf/1.1", "parameter");

        for (int n = 0; n < resultParametersParameterElementList.getLength(); n++) {
            Element parameterElement = (Element) resultParametersParameterElementList.item(n);

            LicenseParamDisplay displayParam = createLicenseParamDisplay(parameterElement, "resultParameter");
            paramList.add(displayParam);

        }
    }

    return paramList;
}

From source file:org.carewebframework.shell.BaseXmlParser.java

/**
 * Adds all attributes of the specified elements as properties in the current builder.
 * //from www  .j  a  v  a 2 s . c  o  m
 * @param element Element whose attributes are to be added.
 * @param builder Target builder.
 */
protected void addProperties(Element element, BeanDefinitionBuilder builder) {
    NamedNodeMap attributes = element.getAttributes();

    for (int i = 0; i < attributes.getLength(); i++) {
        Node node = attributes.item(i);
        String attrName = getNodeName(node);
        attrName = "class".equals(attrName) ? "clazz" : attrName;
        builder.addPropertyValue(attrName, node.getNodeValue());
    }
}

From source file:com.gargoylesoftware.htmlunit.xml.XmlUtil.java

private static DomNode createFrom(final SgmlPage page, final Node source, final boolean handleXHTMLAsHTML) {
    if (source.getNodeType() == Node.TEXT_NODE) {
        return new DomText(page, source.getNodeValue());
    }/*from   ww  w  . j av a  2  s  .  c  o  m*/
    if (source.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
        return new DomProcessingInstruction(page, source.getNodeName(), source.getNodeValue());
    }
    if (source.getNodeType() == Node.COMMENT_NODE) {
        return new DomComment(page, source.getNodeValue());
    }
    if (source.getNodeType() == Node.DOCUMENT_TYPE_NODE) {
        final DocumentType documentType = (DocumentType) source;
        return new DomDocumentType(page, documentType.getName(), documentType.getPublicId(),
                documentType.getSystemId());
    }
    final String ns = source.getNamespaceURI();
    String localName = source.getLocalName();
    if (handleXHTMLAsHTML && HTMLParser.XHTML_NAMESPACE.equals(ns)) {
        final ElementFactory factory = HTMLParser.getFactory(localName);
        return factory.createElementNS(page, ns, localName,
                namedNodeMapToSaxAttributes(source.getAttributes()));
    }
    final NamedNodeMap nodeAttributes = source.getAttributes();
    if (page != null && page.isHtmlPage()) {
        localName = localName.toUpperCase(Locale.ROOT);
    }
    final String qualifiedName;
    if (source.getPrefix() == null) {
        qualifiedName = localName;
    } else {
        qualifiedName = source.getPrefix() + ':' + localName;
    }

    final String namespaceURI = source.getNamespaceURI();
    if (HTMLParser.SVG_NAMESPACE.equals(namespaceURI)) {
        return HTMLParser.SVG_FACTORY.createElementNS(page, namespaceURI, qualifiedName,
                namedNodeMapToSaxAttributes(nodeAttributes));
    }

    final Map<String, DomAttr> attributes = new LinkedHashMap<>();
    for (int i = 0; i < nodeAttributes.getLength(); i++) {
        final Attr attribute = (Attr) nodeAttributes.item(i);
        final String attributeNamespaceURI = attribute.getNamespaceURI();
        final String attributeQualifiedName;
        if (attribute.getPrefix() != null) {
            attributeQualifiedName = attribute.getPrefix() + ':' + attribute.getLocalName();
        } else {
            attributeQualifiedName = attribute.getLocalName();
        }
        final String value = attribute.getNodeValue();
        final boolean specified = attribute.getSpecified();
        final DomAttr xmlAttribute = new DomAttr(page, attributeNamespaceURI, attributeQualifiedName, value,
                specified);
        attributes.put(attribute.getNodeName(), xmlAttribute);
    }
    return new DomElement(namespaceURI, qualifiedName, page, attributes);
}

From source file:org.jetbrains.webdemo.help.HelpLoader.java

private String getTagValueWithTagName(Node node) {
    StringBuilder result = new StringBuilder();

    if (node.getNodeType() == 3) {
        result.append(node.getNodeValue());
    } else {//from   w  w w  . j  a  va 2s  .  co  m
        result.append("<");
        result.append(node.getNodeName());
        if (node.getNodeName().equals("a")) {
            result.append(" target=\"_blank\" ");
        }
        if (node.hasAttributes()) {
            result.append(" ");
            NamedNodeMap map = node.getAttributes();
            for (int i = 0; i < map.getLength(); i++) {
                result.append(map.item(i).getNodeName());
                result.append("=\"");
                result.append(map.item(i).getTextContent());
                result.append("\" ");
            }
        }
        result.append(">");
        if (node.hasChildNodes()) {
            NodeList nodeList = node.getChildNodes();
            for (int i = 0; i < nodeList.getLength(); i++) {
                result.append(getTagValueWithTagName(nodeList.item(i)));
            }
        }
        result.append("</");
        result.append(node.getNodeName());
        result.append(">");
    }
    return result.toString();
}

From source file:com.intuit.karate.Script.java

public static void evalXmlEmbeddedExpressions(Node node, ScriptContext context) {
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        node = node.getFirstChild();/* w w  w  . j  av  a  2 s . com*/
    }
    NamedNodeMap attribs = node.getAttributes();
    int attribCount = attribs.getLength();
    for (int i = 0; i < attribCount; i++) {
        Attr attrib = (Attr) attribs.item(i);
        String value = attrib.getValue();
        value = StringUtils.trimToEmpty(value);
        if (isEmbeddedExpression(value)) {
            try {
                ScriptValue sv = evalInNashorn(value.substring(1), context);
                attrib.setValue(sv.getAsString());
            } catch (Exception e) {
                logger.warn("embedded xml-attribute script eval failed: {}", e.getMessage());
            }
        }
    }
    NodeList nodes = node.getChildNodes();
    int childCount = nodes.getLength();
    for (int i = 0; i < childCount; i++) {
        Node child = nodes.item(i);
        String value = child.getNodeValue();
        if (value != null) {
            value = StringUtils.trimToEmpty(value);
            if (isEmbeddedExpression(value)) {
                try {
                    ScriptValue sv = evalInNashorn(value.substring(1), context);
                    child.setNodeValue(sv.getAsString());
                } catch (Exception e) {
                    logger.warn("embedded xml-text script eval failed: {}", e.getMessage());
                }
            }
        } else if (child.hasChildNodes()) {
            evalXmlEmbeddedExpressions(child, context);
        }
    }
}

From source file:edu.mayo.cts2.framework.core.xml.PatchedCastorMarshaller.java

protected Object unmarshalDomSource(DOMSource domSource) throws XmlMappingException {

    Node node = domSource.getNode();
    if (node != null && node.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) node;

        Node parent = node.getParentNode();
        while (parent != null) {
            NamedNodeMap atts = parent.getAttributes();
            if (atts != null) {
                for (int i = 0, j = atts.getLength(); i < j; i++) {

                    Attr att = (Attr) atts.item(i);
                    if (XMLNS_NS.equals(att.getNamespaceURI())) {
                        String name = att.getName();
                        String value = att.getValue();
                        if (!element.hasAttributeNS(XMLNS_NS, name)) {
                            element.setAttributeNS(XMLNS_NS, name, value);
                        }/* ww  w . j  a  va2  s . c  o m*/
                    }

                }
            }
            parent = parent.getParentNode();
        }
    }

    return super.unmarshalDomSource(domSource);
}

From source file:DOMDump.java

private void dump(Node root, String prefix) {
    if (root instanceof Element) {
        System.out.println(prefix + ((Element) root).getTagName() + " / " + root.getClass().getName());
    } else if (root instanceof CharacterData) {
        String data = ((CharacterData) root).getData().trim();
        if (!data.equals("")) {
            System.out.println(prefix + "CharacterData: " + data);
        }/* w w  w.j av  a2  s .  c  o m*/
    } else {
        System.out.println(prefix + root.getClass().getName());
    }
    NamedNodeMap attrs = root.getAttributes();
    if (attrs != null) {
        int len = attrs.getLength();
        for (int i = 0; i < len; i++) {
            Node attr = attrs.item(i);
            System.out.print(prefix + HALF_INDENT + "attribute " + i + ": " + attr.getNodeName());
            if (attr instanceof Attr) {
                System.out.print(" = " + ((Attr) attr).getValue());
            }
            System.out.println();
        }
    }

    if (root.hasChildNodes()) {
        NodeList children = root.getChildNodes();
        if (children != null) {
            int len = children.getLength();
            for (int i = 0; i < len; i++) {
                dump(children.item(i), prefix + INDENT);
            }
        }
    }
}

From source file:de.micromata.genome.gwiki.page.impl.wiki.rte.els.RteTableDomElementListener.java

protected void copyAttributes(MacroAttributes target, DomElementEvent event) {
    NamedNodeMap attrs = event.element.getAttributes();
    for (int i = 0; i < attrs.getLength(); ++i) {
        String k = attrs.item(i).getLocalName();
        String v = attrs.item(i).getNodeValue();
        target.getArgs().setStringValue(k, v);
    }/*from  w ww. j  av  a2  s . c  om*/
}