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:eu.europa.esig.dss.DSSXMLUtils.java

/**
 * If this method finds an attribute with names ID (case-insensitive) then declares it to be a user-determined ID attribute.
 *
 * @param childElement//from   w ww. j  ava 2 s  . c  om
 */
public static void setIDIdentifier(final DOMValidateContext context, final Element childElement) {

    final NamedNodeMap attributes = childElement.getAttributes();
    for (int jj = 0; jj < attributes.getLength(); jj++) {

        final Node item = attributes.item(jj);
        final String localName = item.getNodeName();
        if (localName != null) {
            final String id = localName.toLowerCase();
            if (ID_ATTRIBUTE_NAME.equals(id)) {

                context.setIdAttributeNS(childElement, null, localName);
                break;
            }
        }
    }
}

From source file:eu.europa.esig.dss.DSSXMLUtils.java

/**
 * If this method finds an attribute with names ID (case-insensitive) then declares it to be a user-determined ID attribute.
 *
 * @param childElement/*  ww w . j a  v  a2  s. c o  m*/
 */
public static void setIDIdentifier(final Element childElement) {

    final NamedNodeMap attributes = childElement.getAttributes();
    for (int jj = 0; jj < attributes.getLength(); jj++) {

        final Node item = attributes.item(jj);
        final String localName = item.getNodeName();
        if (localName != null) {
            final String id = localName.toLowerCase();
            if (ID_ATTRIBUTE_NAME.equals(id)) {

                childElement.setIdAttribute(localName, true);
                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());
    }/*from w  w w.  j  a va2s . c o  m*/
    return attributes;
}

From source file:com.esri.geoevent.solutions.adapter.cot.CoTAdapterInbound.java

public static String elementToString(Node n) throws Exception {
    try {// w w w.j a v  a 2  s.  c  om
        String name = n.getNodeName();
        short type = n.getNodeType();

        if (Node.CDATA_SECTION_NODE == type) {
            return "<![CDATA[" + n.getNodeValue() + "]]&gt;";
        }

        if (name.startsWith("#")) {
            return "";
        }

        StringBuffer sb = new StringBuffer();
        sb.append('<').append(name);

        NamedNodeMap attrs = n.getAttributes();
        if (attrs != null) {
            for (int i = 0; i < attrs.getLength(); i++) {
                Node attr = attrs.item(i);
                sb.append(' ').append(attr.getNodeName()).append("=\"").append(attr.getNodeValue())
                        .append("\"");
            }
        }

        String textContent = null;
        NodeList children = n.getChildNodes();

        if (children.getLength() == 0) {
            if ((textContent = n.getTextContent()) != null && !"".equals(textContent)) {
                sb.append(textContent).append("</").append(name).append('>');
            } else {
                sb.append("/>").append('\n');
            }
        } else {
            sb.append('>').append('\n');
            boolean hasValidChildren = false;
            for (int i = 0; i < children.getLength(); i++) {
                String childToString = elementToString(children.item(i));
                if (!"".equals(childToString)) {
                    sb.append(childToString);
                    hasValidChildren = true;
                }
            }

            if (!hasValidChildren && ((textContent = n.getTextContent()) != null)) {
                sb.append(textContent);
            }

            sb.append("</").append(name).append('>');
        }

        return sb.toString();
    } catch (Exception e) {
        log.error(e);
        log.error(e.getStackTrace());
        throw (e);
    }
}

From source file:MainClass.java

public void processNode(Node node, String spacer) throws IOException {
    if (node == null)
        return;/*  w  w  w.  j av a2s .  com*/
    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: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. ja  v a 2s  .co  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:net.di2e.ecdr.search.transform.atom.security.impl.XmlMetadataSecurityMarkingHandler.java

@Override
public SecurityData getSecurityData(Metacard metacard) {
    String metadata = metacard.getMetadata();
    if (StringUtils.isNotBlank(metadata)) {
        XPathHelper helper = new XPathHelper(metacard.getMetadata());
        Document document = helper.getDocument();
        NodeList nodeList = document.getElementsByTagNameNS("*", "security");
        if (nodeList != null && nodeList.getLength() > 0) {
            Element element = (Element) nodeList.item(0);
            NamedNodeMap nodeNameMap = element.getAttributes();
            int length = nodeNameMap.getLength();

            Map<String, List<String>> securityProps = new HashMap<String, List<String>>();
            String securityNamespace = null;
            for (int i = 0; i < length; i++) {
                Attr attr = (Attr) nodeNameMap.item(i);
                String value = attr.getValue();
                if (!attr.getName().startsWith(XMLNS_PREFIX) && StringUtils.isNotBlank(value)) {
                    securityProps.put(attr.getLocalName(), SecurityMarkingParser.getValues(value));
                    if (securityNamespace == null) {
                        securityNamespace = attr.getNamespaceURI();
                    }/* ww  w . jav  a2s .  co  m*/
                }
            }
            if (!securityProps.isEmpty()) {
                return new SecurityDataImpl(securityProps, securityNamespace);
            }
        }
    }
    return null;
}

From source file:org.jolokia.jvmagent.spring.config.ConfigBeanDefinitionParser.java

private Map<Object, Object> createConfigMap(NamedNodeMap attributes) {
    ManagedMap<Object, Object> map = new ManagedMap<Object, Object>(attributes.getLength());
    map.setKeyTypeName("java.lang.String");
    map.setValueTypeName("java.lang.String");

    for (int i = 0; i < attributes.getLength(); i++) {
        Attr attr = (Attr) attributes.item(i);
        String name = attr.getName();
        if (skipMap.contains(name)) {
            continue;
        }//from  w  ww. j  av a 2  s. c  om
        Object key = new TypedStringValue(name, String.class);
        Object value = new TypedStringValue(attr.getValue(), String.class);
        map.put(key, value);
    }
    return map;
}

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();//from  ww w .  ja  va  2s.  c  o  m
    }
    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: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  w  w w.ja  v  a  2s .  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);
}