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

/** Traverses the specified node, recursively. */
public void count(Node node) {

    // is there anything to do?
    if (node == null) {
        return;/* ww w  .j  a va2  s  .co m*/
    }

    int type = node.getNodeType();
    switch (type) {
    case Node.DOCUMENT_NODE: {
        fElements = 0;
        fAttributes = 0;
        fCharacters = 0;
        fIgnorableWhitespace = 0;
        Document document = (Document) node;
        count(document.getDocumentElement());
        break;
    }

    case Node.ELEMENT_NODE: {
        fElements++;
        NamedNodeMap attrs = node.getAttributes();
        if (attrs != null) {
            fAttributes += attrs.getLength();
        }
        // drop through to entity reference
    }

    case Node.ENTITY_REFERENCE_NODE: {
        Node child = node.getFirstChild();
        while (child != null) {
            count(child);
            child = child.getNextSibling();
        }
        break;
    }

    case Node.CDATA_SECTION_NODE: {
        fCharacters += ((Text) node).getLength();
        break;
    }

    case Node.TEXT_NODE: {
        if (fDocumentInfo != null) {
            Text text = (Text) node;
            int length = text.getLength();
            if (fDocumentInfo.isIgnorableWhitespace(text)) {
                fIgnorableWhitespace += length;
            } else {
                fCharacters += length;
            }
        }
        break;
    }
    }

}

From source file:com.francelabs.datafari.servlets.admin.FieldWeight.java

/**
 * Retrieve the default search handler '/select' from a list of request
 * handlers//from  www  . j a v a2 s .  c om
 *
 * @param requestHandlers
 * @return the search handler or null if not found
 */
private Node getSearchHandler(final NodeList requestHandlers) {
    for (int i = 0; i < requestHandlers.getLength(); i++) {

        final Node rh = requestHandlers.item(i);
        if (rh.hasAttributes()) {
            final NamedNodeMap rhAttributes = rh.getAttributes();
            for (int j = 0; j < rhAttributes.getLength(); j++) {
                if (rhAttributes.item(j).getNodeName().equals("name")
                        && rhAttributes.item(j).getNodeValue().equals("/select")) {
                    return rh;
                }
            }
        }

    }
    return null;
}

From source file:com.francelabs.datafari.servlets.admin.FieldWeight.java

private Node run(final NodeList child, final String type) {
    for (int i = 0; i < child.getLength(); i++) {
        String name = "";
        if (child.item(i).hasAttributes()) {
            final NamedNodeMap map = child.item(i).getAttributes();
            for (int j = 0; j < map.getLength(); j++) {
                if (map.item(j).getNodeName().equals("name")) {
                    name = map.item(j).getNodeValue();
                }//  w  w w.ja  v  a2  s  . c o m
            }
            if (name.equals(type)) {
                return child.item(i);
            }
        }
        if (child.item(i).hasChildNodes()) {
            if (run(child.item(i).getChildNodes(), type) != null) {
                return run(child.item(i).getChildNodes(), type);
            }
        }
    }
    return null;
}

From source file:de.betterform.xml.dom.DOMUtil.java

/**
 * copies all attributes from one Element to another
 *
 * @param from   - the Element which the source attributes
 * @param to     - the target Element for the Attributes
 * @param filter - a NodeFilter to apply during copy
 *//*  ww  w  . j a  va 2s . c o m*/
public static void copyAttributes(Element from, Element to, NodeFilter filter) {
    if ((from != null) && (to != null)) {
        NamedNodeMap map = from.getAttributes();

        /* if filter is null use our own default filter, which accepts
           everything (this saves us from always check if filter is
           null */
        if (filter == null) {
            filter = new NodeFilter() {
                public short acceptNode(Node n) {
                    return NodeFilter.FILTER_ACCEPT;
                }
            };
        }

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

            for (int i = 0; i < len; i++) {
                Node attr = map.item(i);

                if (attr.getNodeType() == Node.ATTRIBUTE_NODE) {
                    if (filter.acceptNode(attr) == NodeFilter.FILTER_ACCEPT) {
                        to.setAttributeNS(attr.getNamespaceURI(), attr.getNodeName(), attr.getNodeValue());
                    }
                }
            }
        }
    }
}

From source file:com.bstek.dorado.view.config.XmlDocumentPreprocessor.java

private void gothroughPlaceHolders(Document templateDocument, TemplateContext templateContext)
        throws Exception {
    Element templteViewElement = ViewConfigParserUtils.findViewElement(templateDocument.getDocumentElement(),
            templateContext.getResource());

    Document document = templateContext.getSourceDocument();
    Element viewElement = ViewConfigParserUtils.findViewElement(document.getDocumentElement(),
            templateContext.getSourceContext().getResource());

    NamedNodeMap attributes = viewElement.getAttributes();
    int len = attributes.getLength();
    for (int i = 0; i < len; i++) {
        Node item = attributes.item(i);
        String nodeName = item.getNodeName();
        if (!specialMergeProperties.contains(nodeName)) {
            templteViewElement.setAttribute(nodeName, item.getNodeValue());
        }//from  w  w  w  .  j  ava 2s.c o  m
    }

    List<Element> viewProperties = DomUtils.getChildrenByTagName(viewElement, XmlConstants.PROPERTY);
    for (Element propertyElement : viewProperties) {
        String propertyName = propertyElement.getAttribute(XmlConstants.ATTRIBUTE_NAME);
        if (!specialMergeProperties.contains(propertyName)) {
            Element clonedElement = (Element) templateDocument.importNode(propertyElement, true);
            templteViewElement.appendChild(clonedElement);
        }
    }

    mergeMetaData(templateDocument, templteViewElement, viewElement);
    mergeTemplateProperty(templteViewElement, viewElement, ViewXmlConstants.ATTRIBUTE_PACKAGES);
    mergeTemplateProperty(templteViewElement, viewElement, ViewXmlConstants.ATTRIBUTE_JAVASCRIPT_FILE);
    mergeTemplateProperty(templteViewElement, viewElement, ViewXmlConstants.ATTRIBUTE_STYLESHEET_FILE);

    for (Element element : DomUtils.getChildElements(viewElement)) {
        String nodeName = element.getNodeName();
        if (nodeName.equals(XmlConstants.PROPERTY) || nodeName.equals(XmlConstants.GROUP_START)
                || nodeName.equals(XmlConstants.GROUP_END) || nodeName.equals(XmlConstants.IMPORT)
                || nodeName.equals(XmlConstants.GROUP_END) || nodeName.equals(XmlConstants.PLACE_HOLDER_START)
                || nodeName.equals(XmlConstants.PLACE_HOLDER_END)) {
            continue;
        }
        if (componentTypeRegistry.getRegisterInfo(nodeName) == null) {
            Element clonedElement = (Element) templateDocument.importNode(element, true);
            templteViewElement.appendChild(clonedElement);
        }
    }

    processPlaceHolders(templteViewElement, templateContext);
}

From source file:com.google.enterprise.connector.salesforce.BaseTraversalManager.java

/**
 * Converts the <document></document> xml into a BaseSimpleDocument object
 * that we can send into a documentList object that ultimately gets returned
 * to the connector-manager/*from w  w w  . ja  v  a 2s  .  co  m*/
 * 
 * @param inxml
 *            the xml form of and individual <document></document> object
 */

private BaseSimpleDocument convertXMLtoBaseDocument(Document doc) {
    try {

        HashMap hm_spi = new HashMap();
        HashMap hm_meta_tags = new HashMap();
        Map props = new HashMap();
        String content_value = "";

        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        // TODO: figure out why the initial doc passed in to the method
        // doesn't have the stylesheet 'fully' applied
        // this is why we do the conversion back and forth below
        // because for some reason the doc->string->doc has the stylesheet
        // applied.
        String sdoc = Util.XMLDoctoString(doc);
        logger.log(Level.FINEST, "About to convert STORE XML to BaseDocument " + sdoc);
        doc = Util.XMLStringtoDoc(sdoc);

        NodeList nl_document = doc.getElementsByTagName("document");

        Node ndoc = nl_document.item(0);

        NodeList nl_doc_child = ndoc.getChildNodes();

        for (int j = 0; j < nl_doc_child.getLength(); j++) {
            Node cnode = nl_doc_child.item(j);
            String doc_child_node_name = cnode.getNodeName();

            if (doc_child_node_name.equalsIgnoreCase("spiheaders")) {
                NodeList nl_spi = cnode.getChildNodes();
                for (int k = 0; k < nl_spi.getLength(); k++) {
                    Node n_spi = nl_spi.item(k);
                    if (n_spi.getNodeType() == Node.ELEMENT_NODE) {
                        String spi_name = n_spi.getAttributes().getNamedItem("name").getNodeValue();
                        String spi_value = "";
                        if (n_spi.getFirstChild() != null) {
                            spi_value = n_spi.getFirstChild().getNodeValue();
                            logger.log(Level.FINEST, "Adding SPI " + spi_name + " " + spi_value);
                        }
                        hm_spi.put(spi_name, spi_value);
                    }
                }
            }

            if (doc_child_node_name.equalsIgnoreCase("metadata")) {
                NodeList nl_meta = cnode.getChildNodes();
                for (int k = 0; k < nl_meta.getLength(); k++) {
                    Node n_meta = nl_meta.item(k);
                    if (n_meta.getNodeType() == Node.ELEMENT_NODE) {
                        String meta_name = n_meta.getAttributes().getNamedItem("name").getNodeValue();
                        String meta_value = "";
                        if (n_meta.getFirstChild() != null) {
                            meta_value = n_meta.getFirstChild().getNodeValue();
                            logger.log(Level.FINEST, "Adding METATAG " + meta_name + " " + meta_value);
                        }
                        hm_meta_tags.put(meta_name, meta_value);
                    }
                }
            }

            if (doc_child_node_name.equalsIgnoreCase("content")) {
                content_value = cnode.getChildNodes().item(0).getNodeValue();
                String encoding_type = "";
                NamedNodeMap attribs = cnode.getAttributes();
                if (attribs.getLength() > 0) {
                    Node attrib = attribs.getNamedItem("encoding");
                    if (attrib != null)
                        encoding_type = attrib.getNodeValue();

                    if (encoding_type.equalsIgnoreCase("base64")
                            || encoding_type.equalsIgnoreCase("base64binary")) {
                        byte[] b = org.apache.commons.codec.binary.Base64
                                .decodeBase64(content_value.getBytes());
                        ByteArrayInputStream input1 = new ByteArrayInputStream(b);
                        logger.log(Level.FINEST, "Adding base64 encoded CONTENT " + content_value);
                        props.put(SpiConstants.PROPNAME_CONTENT, input1);
                    } else {
                        logger.log(Level.FINEST, "Adding Text/HTML CONTENT " + content_value);
                        props.put(SpiConstants.PROPNAME_CONTENT, content_value);
                    }
                } else {
                    logger.log(Level.FINEST, "Adding default Text/HTML CONTENT " + content_value);
                    props.put(SpiConstants.PROPNAME_CONTENT, content_value);
                }
            }
        }

        // the hashmap holding the spi headers
        Iterator itr_spi = hm_spi.keySet().iterator();

        while (itr_spi.hasNext()) {
            String key = (String) itr_spi.next();
            String value = (String) hm_spi.get(key);

            if (key.equals("DEFAULT_MIMETYPE"))
                props.put(SpiConstants.DEFAULT_MIMETYPE, value);

            if (key.equals("PROPNAME_ACTION"))
                props.put(SpiConstants.PROPNAME_ACTION, value);

            if (key.equals("PROPNAME_CONTENTURL"))
                props.put(SpiConstants.PROPNAME_CONTENTURL, value);

            if (key.equals("PROPNAME_DISPLAYURL"))
                props.put(SpiConstants.PROPNAME_DISPLAYURL, value);

            if (key.equals("PROPNAME_DOCID"))
                props.put(SpiConstants.PROPNAME_DOCID, value);

            if (key.equals("PROPNAME_ISPUBLIC"))
                props.put(SpiConstants.PROPNAME_ISPUBLIC, value);

            if (key.equals("PROPNAME_LASTMODIFIED"))
                props.put(SpiConstants.PROPNAME_LASTMODIFIED, value);

            if (key.equals("PROPNAME_MIMETYPE"))
                props.put(SpiConstants.PROPNAME_MIMETYPE, value);

            // if (key.equals("PROPNAME_SEARCHURL"))
            // props.put(SpiConstants.PROPNAME_SEARCHURL, value);

            // if (key.equals("PROPNAME_SECURITYTOKEN"))
            // props.put(SpiConstants.PROPNAME_SECURITYTOKEN, value);

        }

        // hashmap holding the custom metatags
        Iterator itr_meta = hm_meta_tags.keySet().iterator();

        while (itr_meta.hasNext()) {
            String key = (String) itr_meta.next();
            String value = (String) hm_meta_tags.get(key);
            props.put(key, value);
        }

        BaseSimpleDocument bsd = createSimpleDocument(new Date(), props);
        return bsd;
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Error " + ex);
    }
    return null;

}

From source file:com.portfolio.data.attachment.XSLService.java

public Connection getConnection()
        throws ParserConfigurationException, SAXException, IOException, SQLException, ClassNotFoundException {
    // Open META-INF/context.xml
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document doc = documentBuilder.parse(sc.getRealPath("/") + "/META-INF/context.xml");
    NodeList res = doc.getElementsByTagName("Resource");
    Node dbres = res.item(0);/*  ww  w  .jav  a  2  s .  c  om*/

    Properties info = new Properties();
    NamedNodeMap attr = dbres.getAttributes();
    String url = "";
    for (int i = 0; i < attr.getLength(); ++i) {
        Node att = attr.item(i);
        String name = att.getNodeName();
        String val = att.getNodeValue();
        if ("url".equals(name))
            url = val;
        else if ("username".equals(name)) // username (context.xml) -> user (properties)
            info.put("user", val);
        else if ("driverClassName".equals(name))
            Class.forName(val);
        else
            info.put(name, val);
    }

    return DriverManager.getConnection(url, info);
}

From source file:org.dasein.cloud.terremark.Terremark.java

private Organization toOrg(Document doc) throws CloudException {
    Logger logger = getLogger(Terremark.class);
    Organization org = new Organization();
    NodeList blocks = doc.getElementsByTagName("Organization");
    if (blocks == null) {
        throw new CloudException("Did not find any organizations");
    } else if (blocks.getLength() > 1) {
        logger.warn(/*ww  w  .ja  v  a2  s  .c  om*/
                "There is more than one organization in this account. This must be a reseller account. Only using the first organization.");
    }

    Node organization = blocks.item(0);
    NamedNodeMap attributes = organization.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node node = attributes.item(i);
        if (node.getNodeName().equals(Terremark.HREF)) {
            org.setId(hrefToId(node.getNodeValue()));
        } else if (node.getNodeName().equals(Terremark.NAME)) {
            org.setName(node.getNodeValue());
        }
    }

    return org;
}

From source file:org.adl.samplerte.server.LMSManifestHandler.java

License:asdf

/****************************************************************************
 ** /*  w  w  w.  ja v a 2 s .  c  o m*/
 ** Method: sortAttributes() Input: NamedNodeMap attrs - The list of
 * attributes Output: Attr[] - Sorted array of attributes Description: This
 * method returns an array of sorted attributes.
 ** 
 ** Side Effects: none
 ** 
 ***************************************************************************/
protected Attr[] sortAttributes(NamedNodeMap attrs) {
    int len = (attrs != null) ? attrs.getLength() : 0;
    Attr array[] = new Attr[len];
    for (int i = 0; i < len; i++) {
        array[i] = (Attr) attrs.item(i);
    }
    for (int i = 0; i < len - 1; i++) {
        String name = array[i].getNodeName();
        int index = i;
        for (int j = i + 1; j < len; j++) {
            String curName = array[j].getNodeName();
            if (curName.compareTo(name) < 0) {
                name = curName;
                index = j;
            }
        }
        if (index != i) {
            Attr temp = array[i];
            array[i] = array[index];
            array[index] = temp;
        }
    }

    return (array);

}

From source file:de.bayern.gdi.services.Atom.java

private ArrayList<String> getCRS(Node entry) {
    ArrayList<String> crs = new ArrayList<>();
    //Predefined in ATOM Service
    String getCategories = "category";
    NodeList cL = (NodeList) XML.xpath(entry, getCategories, XPathConstants.NODESET, this.nscontext);
    for (int i = 0; i < cL.getLength(); i++) {
        Node cat = cL.item(i);/*from  ww  w  . java2s . c  o m*/
        NamedNodeMap catAttributes = cat.getAttributes();
        String epsg = null;
        for (int j = 0; j < catAttributes.getLength(); j++) {
            Node catAttr = catAttributes.item(j);
            if (catAttr.getNodeName().equals("label")) {
                epsg = catAttr.getTextContent();
                crs.add(epsg);
            }
        }
    }
    return crs;
}