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:org.solmix.runtime.support.spring.AbstractRootBeanDefinitionParser.java

protected void parseAttributes(Element element, ParserContext parserContext, String id,
        RootBeanDefinition beanDefinition) {
    Set<String> props = new HashSet<String>();
    ManagedMap<String, Object> parameters = null;
    for (Method setter : this.type.getMethods()) {
        String name = setter.getName();
        if (name.length() > 3 && name.startsWith("set") && Modifier.isPublic(setter.getModifiers())
                && setter.getParameterTypes().length == 1) {
            Class<?> type = setter.getParameterTypes()[0];
            String property = StringUtils
                    .camelToSplitName(name.substring(3, 4).toLowerCase() + name.substring(4), "-");
            props.add(property);// w  w  w .  j  a  va2s .co  m
            Method getter = null;
            try {
                getter = this.type.getMethod("get" + name.substring(3), new Class<?>[0]);
            } catch (NoSuchMethodException e) {
                try {
                    getter = this.type.getMethod("is" + name.substring(3), new Class<?>[0]);
                } catch (NoSuchMethodException e2) {
                }
            }
            if (getter == null || !Modifier.isPublic(getter.getModifiers())
                    || !type.equals(getter.getReturnType())) {
                continue;
            }
            if ("properties".equals(property)) {
                parameters = parseProperties(element.getChildNodes(), beanDefinition);
            } else if ("arguments".equals(property)) {
                parseArguments(id, element.getChildNodes(), beanDefinition, parserContext);
            } else {
                String value = element.getAttribute(property);
                if (value != null && value.trim().length() > 0) {
                    value = value.trim();
                    parserValue(beanDefinition, property, type, value, parserContext);
                }
            }
        }
    }
    NamedNodeMap attributes = element.getAttributes();
    int len = attributes.getLength();
    for (int i = 0; i < len; i++) {
        Node node = attributes.item(i);
        String name = node.getLocalName();
        if (!props.contains(name)) {
            if (parameters == null) {
                parameters = new ManagedMap<String, Object>();
            }
            String value = node.getNodeValue();
            parameters.put(name, new TypedStringValue(value, String.class));
        }
    }
    if (parameters != null) {
        beanDefinition.getPropertyValues().addPropertyValue("properties", parameters);
    }
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.xml.XMLSerializer.java

private void toXml(final int indent, final DomNode node, final StringBuilder buffer,
        final String foredNamespace) {
    final String nodeName = node.getNodeName();
    buffer.append('<').append(nodeName);

    String optionalPrefix = "";
    final String namespaceURI = node.getNamespaceURI();
    final String prefix = node.getPrefix();
    if (namespaceURI != null && prefix != null) {
        boolean sameNamespace = false;
        for (DomNode parentNode = node
                .getParentNode(); parentNode instanceof DomElement; parentNode = parentNode.getParentNode()) {
            if (namespaceURI.equals(parentNode.getNamespaceURI())) {
                sameNamespace = true;/*  www  . j  a v  a 2 s.  co m*/
            }
        }
        if (node.getParentNode() == null || !sameNamespace) {
            ((DomElement) node).setAttribute("xmlns:" + prefix, namespaceURI);
        }
    } else if (foredNamespace != null) {
        buffer.append(" xmlns=\"").append(foredNamespace).append('"');
        optionalPrefix = " ";
    }

    final NamedNodeMap attributesMap = node.getAttributes();
    for (int i = 0; i < attributesMap.getLength(); i++) {
        final DomAttr attrib = (DomAttr) attributesMap.item(i);
        buffer.append(' ').append(attrib.getQualifiedName()).append('=').append('"').append(attrib.getValue())
                .append('"');
    }
    boolean startTagClosed = false;
    for (final DomNode child : node.getChildren()) {
        if (!startTagClosed) {
            buffer.append(optionalPrefix).append('>');
            startTagClosed = true;
        }
        switch (child.getNodeType()) {
        case Node.ELEMENT_NODE:
            toXml(indent + 1, child, buffer, null);
            break;

        case Node.TEXT_NODE:
            String value = child.getNodeValue();
            value = StringUtils.escapeXmlChars(value);
            buffer.append(value);
            break;

        case Node.CDATA_SECTION_NODE:
        case Node.COMMENT_NODE:
            buffer.append(child.asXml());
            break;

        default:

        }
    }
    if (!startTagClosed) {
        final String tagName = nodeName.toLowerCase(Locale.ROOT);
        final boolean nonEmptyTagsSupported = getBrowserVersion().hasFeature(JS_XML_SERIALIZER_NON_EMPTY_TAGS);
        if (nonEmptyTagsSupported && NON_EMPTY_TAGS.contains(tagName)) {
            buffer.append('>');
            buffer.append("</").append(nodeName).append('>');
        } else {
            buffer.append(optionalPrefix);
            if (buffer.charAt(buffer.length() - 1) != ' '
                    && getBrowserVersion().hasFeature(JS_XML_SERIALIZER_BLANK_BEFORE_SELF_CLOSING)) {
                buffer.append(" ");
            }
            buffer.append("/>");
        }
    } else {
        buffer.append('<').append('/').append(nodeName).append('>');
    }
}

From source file:ca.mcgill.music.ddmal.mei.MeiXmlReader.java

private MeiElement makeMeiElement(Node element) {
    // TODO: CDATA
    // Comments get a name #comment
    String nshref = element.getNamespaceURI();
    String nsprefix = element.getPrefix();
    MeiNamespace elns = new MeiNamespace(nshref, nsprefix);
    MeiElement e = new MeiElement(elns, element.getNodeName());
    if (element.getNodeType() == Node.COMMENT_NODE) {
        e.setValue(element.getNodeValue());
    }//from w ww . j a v  a  2  s  .c  o m

    NamedNodeMap attributes = element.getAttributes();
    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            Node item = attributes.item(i);
            if (XML_ID_ATTRIBUTE.equals(item.getNodeName())) {
                e.setId(item.getNodeValue());
            } else {
                String attrns = item.getNamespaceURI();
                String attrpre = item.getPrefix();
                MeiNamespace atns = new MeiNamespace(attrns, attrpre);
                MeiAttribute a = new MeiAttribute(atns, item.getNodeName(), item.getNodeValue());
                e.addAttribute(a);
            }
        }
    }

    NodeList childNodes = element.getChildNodes();
    MeiElement lastElement = null;
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node item = childNodes.item(i);
        if (item.getNodeType() == Node.TEXT_NODE) {
            if (lastElement == null) {
                e.setValue(item.getNodeValue());
            } else {
                lastElement.setTail(item.getNodeValue());
            }
        } else {
            MeiElement child = makeMeiElement(item);
            e.addChild(child);
            lastElement = child;
        }
    }
    return e;
}

From source file:com.jaspersoft.studio.data.xml.XMLDataAdapterDescriptor.java

private void findDirectChildrenAttributes(Node node, LinkedHashMap<String, JRDesignField> fieldsMap,
        String prefix) {/* w ww .ja v a2  s.  c  o m*/
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            Node item = attributes.item(i);
            if (item.getNodeType() == Node.ATTRIBUTE_NODE) {
                addNewField(item.getNodeName(), fieldsMap, item, prefix);
            }
        }
    }
}

From source file:com.founder.fix.fixwpe.wpeformdesigner.jst.pagedesigner.properties.FixPropertySourceProvider.java

/**
 *   tag//from w w w  .  jav a2s. c  o m
 */
public void refleshTagProperty() {
    NamedNodeMap attributes = impl.getAttributes();
    // ?
    JSONObject attributeJsonTag = new JSONObject();
    for (int i = 0; i < attributes.getLength(); i++) {
        try {
            attributeJsonTag.put(attributes.item(i).getNodeName(), attributes.item(i).getNodeValue());

            if (fixImpl.getObjectJson() == null) {
                fixImpl.setObjectJson(new JSONObject());
                fixImpl.getObjectJson().put(ConstantVariable.childJsonAttributeTag, attributeJsonTag);
            } else {
                fixImpl.getObjectJson().put(ConstantVariable.childJsonAttributeTag, attributeJsonTag);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:com.sun.faces.config.rules.DescriptionTextRule.java

/**
 * <p>Append the serialized version of the specified node to the
 * string buffer being accumulated.</p>
 *
 * @param sb StringBuffer to which serialized text is appended
 * @param node Node to be serialized/*from w ww  .jav  a  2 s  . com*/
 *
 * @exception Exception if any processing exception occurs
 */
private void serialize(StringBuffer sb, Node node) throws Exception {

    // Processing depends on the node type
    switch (node.getNodeType()) {

    case Node.ELEMENT_NODE:

        if (digester.getLogger().isDebugEnabled()) {
            digester.getLogger().debug("  Processing element node '" + node.getNodeName() + "'");
        }

        // Open the element and echo the attributes
        sb.append("<");
        sb.append(node.getNodeName());
        NamedNodeMap attrs = node.getAttributes();
        int n = attrs.getLength();
        for (int i = 0; i < n; i++) {
            Node attr = attrs.item(i);
            sb.append(" ");
            sb.append(attr.getNodeName());
            sb.append("=\"");
            sb.append(attr.getNodeValue());
            sb.append("\"");
        }

        // Does this element have any children?
        NodeList kids = node.getChildNodes();
        int m = kids.getLength();
        if (m > 0) {

            // Yes -- serialize child elements and close parent element
            sb.append(">");
            for (int j = 0; j < m; j++) {
                serialize(sb, kids.item(j));
            }
            sb.append("</");
            sb.append(node.getNodeName());
            sb.append(">");

        } else {

            // No -- shorthand close of the parent element
            sb.append(" />");

        }
        break;

    case Node.TEXT_NODE:

        if (digester.getLogger().isDebugEnabled()) {
            digester.getLogger().debug("  Processing text node '" + node.getNodeValue() + "'");
        }

        // Append the text to our accumulating buffer
        sb.append(node.getNodeValue());
        break;

    default:
        throw new IllegalArgumentException(
                "Cannot process node '" + node.getNodeName() + "' of type '" + node.getNodeType());

    }

}

From source file:com.doculibre.constellio.lang.html.HtmlLangDetector.java

private List<String> parse(Node node) {
    List<String> langs = new ArrayList<String>();
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        // Check for the lang HTML attribute
        final String htmlLang = parseLanguage(((Element) node).getAttribute("lang"));
        if (StringUtils.isNotBlank(htmlLang)) {
            langs.add(htmlLang);/* ww w . j ava 2s. c  om*/
        }

        // Check for Meta
        if ("meta".equalsIgnoreCase(node.getNodeName())) {
            NamedNodeMap attrs = node.getAttributes();

            // Check for the dc.language Meta
            for (int i = 0; i < attrs.getLength(); i++) {
                Node attrnode = attrs.item(i);
                if ("name".equalsIgnoreCase(attrnode.getNodeName())) {
                    if ("dc.language".equalsIgnoreCase(attrnode.getNodeValue())) {
                        Node valueattr = attrs.getNamedItem("content");
                        if (valueattr != null) {
                            final String dublinCore = parseLanguage(valueattr.getNodeValue());
                            if (StringUtils.isNotBlank(dublinCore)) {
                                langs.add(dublinCore);
                            }
                        }
                    }
                }
            }

            // Check for the http-equiv content-language
            for (int i = 0; i < attrs.getLength(); i++) {
                Node attrnode = attrs.item(i);
                if ("http-equiv".equalsIgnoreCase(attrnode.getNodeName())) {
                    if ("content-language".equalsIgnoreCase(attrnode.getNodeValue().toLowerCase())) {
                        Node valueattr = attrs.getNamedItem("content");
                        if (valueattr != null) {
                            final String httpEquiv = parseLanguage(valueattr.getNodeValue());
                            if (StringUtils.isNotBlank(httpEquiv)) {
                                langs.add(httpEquiv);
                            }
                        }
                    }
                }
            }

            // Check for the <meta name="language" content="xyz" /> tag
            for (int i = 0; i < attrs.getLength(); i++) {
                Node attrnode = attrs.item(i);
                if ("name".equalsIgnoreCase(attrnode.getNodeName())) {
                    if ("language".equalsIgnoreCase(attrnode.getNodeValue().toLowerCase())) {
                        Node valueattr = attrs.getNamedItem("content");
                        if (valueattr != null) {
                            final String metaLanguage = parseLanguage(valueattr.getNodeValue());
                            if (StringUtils.isNotBlank(metaLanguage)) {
                                langs.add(metaLanguage);
                            }
                        }
                    }
                }
            }
        }
    }

    if (langs.isEmpty()) {
        // Recurse
        NodeList children = node.getChildNodes();
        for (int i = 0; children != null && i < children.getLength(); i++) {
            langs.addAll(parse(children.item(i)));
            if (!langs.isEmpty()) {
                break;
            }
        }
    }

    return langs;
}

From source file:com.nridge.core.base.io.xml.RelationshipXML.java

/**
 * Parses an XML DOM element and loads it into a relationship.
 *
 * @param anElement DOM element./*from www.j  ava 2  s  . c om*/
 *
 * @throws java.io.IOException I/O related exception.
 */
@Override
public void load(Element anElement) throws IOException {
    Node nodeItem;
    Attr nodeAttr;
    Element nodeElement;
    DataBagXML dataBagXML;
    DocumentXML documentXML;
    String nodeName, nodeValue;

    mRelationship.reset();
    String attrValue = anElement.getAttribute("type");
    if (StringUtils.isEmpty(attrValue))
        throw new IOException("Relationship is missing type attribute.");
    mRelationship.setType(attrValue);

    NamedNodeMap namedNodeMap = anElement.getAttributes();
    int attrCount = namedNodeMap.getLength();
    for (int attrOffset = 0; attrOffset < attrCount; attrOffset++) {
        nodeAttr = (Attr) namedNodeMap.item(attrOffset);
        nodeName = nodeAttr.getNodeName();
        nodeValue = nodeAttr.getNodeValue();

        if (StringUtils.isNotEmpty(nodeValue)) {
            if ((!StringUtils.equalsIgnoreCase(nodeName, "type")))
                mRelationship.addFeature(nodeName, nodeValue);
        }
    }
    NodeList nodeList = anElement.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        nodeItem = nodeList.item(i);

        if (nodeItem.getNodeType() != Node.ELEMENT_NODE)
            continue;

        nodeName = nodeItem.getNodeName();
        if (nodeName.equalsIgnoreCase(IO.XML_PROPERTIES_NODE_NAME)) {
            nodeElement = (Element) nodeItem;
            dataBagXML = new DataBagXML();
            dataBagXML.load(nodeElement);
            mRelationship.setBag(dataBagXML.getBag());
        } else {
            nodeElement = (Element) nodeItem;
            documentXML = new DocumentXML();
            documentXML.load(nodeElement);
            mRelationship.add(documentXML.getDocument());
        }
    }
}

From source file:com.predic8.membrane.annot.parser.BlueprintElementParser.java

protected void setProperties(ParserContext context, String springPropertyName, Element element,
        MutableBeanMetadata mcm) {//from  w  ww  .  j a va  2  s .  c o  m
    NamedNodeMap attributes = element.getAttributes();
    HashMap<String, String> attrs = new HashMap<String, String>();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr item = (Attr) attributes.item(i);
        if (item.getLocalName() != null)
            attrs.put(item.getLocalName(), item.getValue());
    }
    MutablePassThroughMetadata pt = context.createMetadata(MutablePassThroughMetadata.class);
    pt.setObject(attrs);
    mcm.addProperty(springPropertyName, pt);
}

From source file:edu.illinois.cs.cogcomp.temporal.normalizer.main.TemporalNormalizerBenchmark.java

/**
 * Setting up TemporalChunkerAnnotator, prepare dataset
 * @param fullFolderName folder name of the dataset
 * @param useHeidelTime boolean whether use HeidelTime for normalization or not
 * @throws IOException/*from  w w w .j a v a  2  s. c  om*/
 * @throws ParserConfigurationException
 * @throws SAXException
 */
public void setUp(String fullFolderName, boolean useHeidelTime)
        throws IOException, ParserConfigurationException, SAXException {
    testText = new ArrayList<>();
    DCTs = new ArrayList<>();
    docIDs = new ArrayList<>();
    te3inputText = new ArrayList<>();

    Properties rmProps = new TemporalChunkerConfigurator().getDefaultConfig().getProperties();
    rmProps.setProperty("useHeidelTime", useHeidelTime ? "True" : "False");
    tca = new TemporalChunkerAnnotator(new ResourceManager(rmProps));

    File folder = new File(fullFolderName);
    File[] listOfFiles = folder.listFiles();
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    for (File file : listOfFiles) {
        if (file.isFile()) {
            String testFile = fullFolderName + "/" + file.getName();
            byte[] encoded = Files.readAllBytes(Paths.get(testFile));
            String fileContent = new String(encoded, StandardCharsets.UTF_8);
            te3inputText.add(fileContent);
            Document document = builder.parse(new InputSource(new StringReader(fileContent)));
            Element rootElement = document.getDocumentElement();
            NodeList nodeList = rootElement.getChildNodes();
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node currentNode = nodeList.item(i);
                if (currentNode.getNodeName().equals("TEXT")) {
                    testText.add(currentNode.getTextContent());
                }
                if (currentNode.getNodeName().indexOf("DOCID") != -1) {
                    docIDs.add(currentNode.getTextContent());
                }
                if (currentNode.getNodeName().indexOf("DCT") != -1) {
                    Node dctNode = currentNode.getChildNodes().item(0);
                    NamedNodeMap dctAttrs = dctNode.getAttributes();
                    for (int j = 0; j < dctAttrs.getLength(); j++) {
                        if (dctAttrs.item(j).getNodeName().equals("value")) {
                            DCTs.add(dctAttrs.item(j).getNodeValue());
                        }
                    }
                }
            }
        }
    }
}