Example usage for org.w3c.dom Node getChildNodes

List of usage examples for org.w3c.dom Node getChildNodes

Introduction

In this page you can find the example usage for org.w3c.dom Node getChildNodes.

Prototype

public NodeList getChildNodes();

Source Link

Document

A NodeList that contains all children of this node.

Usage

From source file:cz.incad.kramerius.virtualcollections.VirtualCollectionsManager.java

public static List<VirtualCollection> getVirtualCollectionsFromFedora(FedoraAccess fedoraAccess,
        ArrayList<String> languages) throws Exception {
    try {/*from   w w  w.  j a va  2  s  . c  o  m*/
        IResourceIndex g = ResourceIndexService.getResourceIndexImpl();
        Document doc = g.getVirtualCollections();

        NodeList nodes = doc.getDocumentElement().getElementsByTagNameNS(SPARQL_NS, "result");
        NodeList children;
        Node child;
        String name;
        String pid;
        boolean canLeave;

        ArrayList<String> langs = new ArrayList<String>();

        if (languages == null || languages.isEmpty()) {
            String[] ls = KConfiguration.getInstance().getPropertyList("interface.languages");
            for (int i = 0; i < ls.length; i++) {
                String lang = ls[++i];
                langs.add(lang);
            }
        } else {
            langs = new ArrayList<String>(languages);
        }

        List<VirtualCollection> vcs = new ArrayList<VirtualCollection>();
        for (int i = 0; i < nodes.getLength(); i++) {
            canLeave = false;
            name = null;
            pid = null;
            Node node = nodes.item(i);
            children = node.getChildNodes();
            for (int j = 0; j < children.getLength(); j++) {
                child = children.item(j);
                if ("title".equals(child.getLocalName())) {
                    name = child.getFirstChild().getNodeValue();
                } else if ("object".equals(child.getLocalName())) {
                    pid = ((Element) child).getAttribute("uri").replaceAll("info:fedora/", "");
                } else if ("canLeave".equals(child.getLocalName())) {
                    canLeave = Boolean.parseBoolean(child.getFirstChild().getNodeValue().replaceAll("\"", "")
                            .substring(("canLeave:").length()));
                }
            }

            if (name != null && pid != null) {
                try {
                    VirtualCollection vc = new VirtualCollection(name, pid, canLeave);

                    for (String lang : langs) {
                        String dsName = TEXT_DS_PREFIX + lang;
                        String value = IOUtils.readAsString(fedoraAccess.getDataStream(pid, dsName),
                                Charset.forName("UTF8"), true);
                        vc.addDescription(lang, value);
                    }
                    vcs.add(vc);
                } catch (Exception vcex) {
                    logger.log(Level.WARNING,
                            "Could not get virtual collection for  " + pid + ": " + vcex.toString());

                }
            }
        }
        return vcs;
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Error getting virtual collections", ex);
        throw new Exception(ex);
    }
}

From source file:Main.java

/**
 * _more_//from  ww  w  . j  a va 2s  . c om
 *
 * @param html _more_
 * @param node _more_
 */
public static void toHtml(StringBuffer html, Node node) {
    switch (node.getNodeType()) {

    case Node.ELEMENT_NODE: {
        NodeList children = node.getChildNodes();
        int numChildren = children.getLength();
        html.append("<b>" + node.getNodeName().replace("_", " ") + "</b>");
        html.append(": ");

        for (int i = 0; i < numChildren; i++) {
            Node child = children.item(i);
            if (((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE))) {
                String v = child.getNodeValue();
                if (v == null) {
                    continue;
                }
                if (v.trim().length() == 0) {
                    continue;
                }
                html.append(v);
                html.append(" ");
            }
        }
        boolean didone = false;
        NamedNodeMap nnm = node.getAttributes();
        if (nnm != null) {
            for (int i = 0; i < nnm.getLength(); i++) {
                Attr attr = (Attr) nnm.item(i);
                String attrName = attr.getNodeName();
                if (attrName.startsWith("xmlns") || attrName.startsWith("xsi:")) {
                    continue;
                }
                if (!didone) {
                    html.append("<ul>");
                    didone = true;
                }
                html.append(attrName.replace("_", " ") + "=" + attr.getNodeValue());
                html.append("<br>\n");
            }
        }
        int cnt = 0;
        for (int i = 0; i < numChildren; i++) {
            Node child = children.item(i);
            if (((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE))) {
                continue;
            }
            if (!didone) {
                html.append("<ul>");
                didone = true;
            }
            if (cnt > 0) {
                html.append("<br>");
            }
            toHtml(html, child);
            cnt++;
        }
        if (didone) {
            html.append("</ul>");
        }
        break;
    }
    }
}

From source file:Main.java

/**
 * Finds element in DOM tree/*from  w w w .  jav  a2s.  c  o  m*/
 * @param topElm Top element
 * @param nodeName Node name
 * @return returns found node
 */
public static List<Node> findNodesByType(Element topElm, int type) {
    List<Node> retvals = new ArrayList<Node>();
    if (topElm == null)
        throw new IllegalArgumentException("topElm cannot be null");
    synchronized (topElm.getOwnerDocument()) {
        Stack<Node> stack = new Stack<Node>();
        stack.push(topElm);
        while (!stack.isEmpty()) {
            Node curElm = stack.pop();
            if (curElm.getNodeType() == type) {
                retvals.add(curElm);
            }
            List<Node> nodesToProcess = new ArrayList<Node>();

            NodeList childNodes = curElm.getChildNodes();
            for (int i = 0, ll = childNodes.getLength(); i < ll; i++) {
                Node item = childNodes.item(i);
                //stack.push((Element) item);
                nodesToProcess.add(item);
            }
            Collections.reverse(nodesToProcess);
            for (Node node : nodesToProcess) {
                stack.push(node);
            }
        }
        return retvals;
    }
}

From source file:Main.java

public static String getTextContent(Node node) {

    if (node == null)
        throw new NullPointerException();

    String textContent;/*from ww w  . j a va2 s .  co m*/

    textContent = node.getTextContent();
    if (textContent != null)
        return xmlDecode(textContent);

    NodeList childNodes = node.getChildNodes();
    if (childNodes.getLength() > 0 && childNodes.item(0) instanceof Text)
        textContent = node.getNodeValue();
    if (textContent != null)
        return xmlDecode(textContent);

    return "";
}

From source file:Main.java

/**
 * Serialise the supplied W3C DOM subtree.
 *
 * @param node The DOM node to be serialized.
 * @param format Format the output./*w w  w. java  2s.com*/
 * @param writer The target writer for serialization.
 * @throws DOMException Unable to serialise the DOM.
 */
public static void serialize(final Node node, boolean format, Writer writer) throws DOMException {
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        serialize(node.getChildNodes(), format, writer);
    } else {
        serialize(new NodeList() {
            public Node item(int index) {
                return node;
            }

            public int getLength() {
                return 1;
            }
        }, format, writer);
    }
}

From source file:de.matzefratze123.heavyspleef.util.I18NNew.java

private static void readEntry(Node node, String parentEntry) {
    NodeList list = node.getChildNodes();

    for (int i = 0; i < list.getLength(); i++) {
        Node childNode = list.item(i);
        String nodeName = childNode.getNodeName();

        if (nodeName.equalsIgnoreCase(MESSAGE_ENTRY)) {
            NamedNodeMap attributes = childNode.getAttributes();
            Node idNode = attributes.getNamedItem(ID_ATTRIBUTE);
            if (idNode == null) {
                Logger.warning("Warning: No id for message in " + MESSAGES_FILE + ". Ignoring message...");
                continue;
            }//from  ww w.j a v  a  2  s  .c o m

            String id = idNode.getNodeValue();
            String value = childNode.getTextContent();

            messages.put(parentEntry + id, value);
        } else if (nodeName.equalsIgnoreCase(ENTRY_ENTRY)) {
            NamedNodeMap attributes = childNode.getAttributes();
            Node nameNode = attributes.getNamedItem(ENTRY_NAME);
            String entryName = nameNode.getNodeValue();

            readEntry(childNode, parentEntry + entryName + ".");
        }
    }
}

From source file:Main.java

public static String domNode2String(Node node, boolean escapeStrings) {
    String ret = "";
    switch (node.getNodeType()) {

    case Node.DOCUMENT_NODE:
        // recurse on each child
        NodeList nodes = node.getChildNodes();
        if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {
                ret += domNode2String(nodes.item(i), escapeStrings);
            }//from   w  w  w  .  java 2  s  .c o  m
        }
        break;

    case Node.ELEMENT_NODE:
        String name = node.getNodeName();
        ret += "<" + name;
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node current = attributes.item(i);
            ret += " " + current.getNodeName() + "=\""
                    + ((escapeStrings) ? escapeStringForXML(current.getNodeValue()) : current.getNodeValue())
                    + "\"";
        }
        ret += ">";

        // recurse on each child
        NodeList children = node.getChildNodes();
        if (children != null) {
            for (int i = 0; i < children.getLength(); i++) {
                ret += domNode2String(children.item(i), escapeStrings);
            }
        }

        ret += "</" + name + ">";
        break;

    case Node.TEXT_NODE:
        ret += (escapeStrings) ? escapeStringForXML(node.getNodeValue()) : node.getNodeValue();
        break;
    case Node.COMMENT_NODE:
        ret += "<!--" + node.getNodeValue() + "-->";
        break;
    }
    return ret;
}

From source file:be.fedict.eid.dss.spi.utils.XAdESSigAndRefsTimeStampValidation.java

public static List<TimeStampToken> verify(XAdESTimeStampType sigAndRefsTimeStamp, Element signatureElement)
        throws XAdESValidationException {

    LOG.debug("validate SigAndRefsTimeStamp...");

    List<TimeStampToken> timeStampTokens = XAdESUtils.getTimeStampTokens(sigAndRefsTimeStamp);
    if (timeStampTokens.isEmpty()) {
        LOG.error("No timestamp tokens present in SigAndRefsTimeStamp");
        throw new XAdESValidationException("No timestamp tokens present in SigAndRefsTimeStamp");
    }//from  w ww  .j a  v a2 s  .com

    TimeStampDigestInput digestInput = new TimeStampDigestInput(
            sigAndRefsTimeStamp.getCanonicalizationMethod().getAlgorithm());

    /*
     * 2. check ds:SignatureValue present 3. take ds:SignatureValue,
     * cannonicalize and concatenate bytes.
     */
    NodeList signatureValueNodeList = signatureElement.getElementsByTagNameNS(XMLSignature.XMLNS,
            "SignatureValue");
    if (0 == signatureValueNodeList.getLength()) {
        LOG.error("no XML signature valuefound");
        throw new XAdESValidationException("no XML signature valuefound");
    }
    digestInput.addNode(signatureValueNodeList.item(0));

    /*
     * 4. check SignatureTimeStamp(s), CompleteCertificateRefs,
     * CompleteRevocationRefs, AttributeCertificateRefs,
     * AttributeRevocationRefs 5. canonicalize these and concatenate to
     * bytestream from step 3 These nodes should be added in their order of
     * appearance.
     */

    NodeList unsignedSignaturePropertiesNodeList = signatureElement
            .getElementsByTagNameNS(XAdESUtils.XADES_132_NS_URI, "UnsignedSignatureProperties");
    if (unsignedSignaturePropertiesNodeList.getLength() == 0) {
        throw new XAdESValidationException("UnsignedSignatureProperties node not present");
    }
    Node unsignedSignaturePropertiesNode = unsignedSignaturePropertiesNodeList.item(0);
    NodeList childNodes = unsignedSignaturePropertiesNode.getChildNodes();
    int childNodesCount = childNodes.getLength();
    for (int idx = 0; idx < childNodesCount; idx++) {
        Node childNode = childNodes.item(idx);
        if (Node.ELEMENT_NODE != childNode.getNodeType()) {
            continue;
        }
        if (!XAdESUtils.XADES_132_NS_URI.equals(childNode.getNamespaceURI())) {
            continue;
        }
        String localName = childNode.getLocalName();
        if ("SignatureTimeStamp".equals(localName)) {
            digestInput.addNode(childNode);
            continue;
        }
        if ("CompleteCertificateRefs".equals(localName)) {
            digestInput.addNode(childNode);
            continue;
        }
        if ("CompleteRevocationRefs".equals(localName)) {
            digestInput.addNode(childNode);
            continue;
        }
        if ("AttributeCertificateRefs".equals(localName)) {
            digestInput.addNode(childNode);
            continue;
        }
        if ("AttributeRevocationRefs".equals(localName)) {
            digestInput.addNode(childNode);
            continue;
        }
    }

    for (TimeStampToken timeStampToken : timeStampTokens) {

        // 1. verify signature in timestamp token
        XAdESUtils.verifyTimeStampTokenSignature(timeStampToken);

        // 6. compute digest and compare with token
        XAdESUtils.verifyTimeStampTokenDigest(timeStampToken, digestInput);
    }

    return timeStampTokens;
}

From source file:com.nridge.core.base.std.XMLUtl.java

public static String getElementStrValue(Element anElement) {
    int count;//from   w  w  w  .  j  a  v a 2 s . com
    Node textNode;
    NodeList nodeList;

    Node curNode = (Node) anElement;
    nodeList = curNode.getChildNodes();
    count = nodeList.getLength();

    for (int i = 0; i < count; i++) {
        textNode = nodeList.item(i);

        if (textNode.getNodeType() == Node.TEXT_NODE)
            return textNode.getNodeValue();
    }

    return StringUtils.EMPTY;
}