Example usage for org.w3c.dom Node DOCUMENT_NODE

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

Introduction

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

Prototype

short DOCUMENT_NODE

To view the source code for org.w3c.dom Node DOCUMENT_NODE.

Click Source Link

Document

The node is a Document.

Usage

From source file:DOMProcessor.java

/** Returns the DOM elements with the given name that are the children of the 
  * given node. This is a non-recursive method that only looks for immediate
  * children. Array will be 0 length if none found.
  * @param name Element name to search for.
  * @param node Node from which to examine children.
  * @return Child nodes or empty Node array if none found.
  *//*from  w ww  . j  a va 2 s  .  c om*/
public Node[] getNodeElements(String name, Node node) {
    // Only consider document or element nodes.
    if ((node.getNodeType() != Node.DOCUMENT_NODE) && (node.getNodeType() != Node.ELEMENT_NODE)) {
        return new Node[0];
    }

    Vector<Node> matchedChildren = new Vector<Node>();

    NodeList children = node.getChildNodes();

    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);

        // Only consider element child nodes.
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            if (child.getNodeName().equalsIgnoreCase(name)) {
                matchedChildren.add(child);
            }
        }
    }

    Node[] nodes = new Node[matchedChildren.size()];
    matchedChildren.toArray(nodes);
    return nodes;
}

From source file:de.betterform.xml.xforms.model.Instance.java

/**
 * Deletes the specified node.//from   w w w. j ava  2  s .c  o m
 *
 * @param path the path pointing to the node to be deleted.
 */
public boolean deleteNode(Node node, String path) throws XFormsException {
    String canonicalPath = DOMUtil.getCanonicalPath(node);
    if (node == null) {
        LOGGER.warn("Node is null - delete is terminated with no effect.");
        return false;
    }

    //don't delete readonly nodes
    if (isReadonly(node)) {
        LOGGER.warn("Node or one of it's parents is readonly - delete is terminated with no effect.");
        return false;
    }

    //don't delete content of a xmlns Attribute - not clear what Spec means by not allowing to delete a namespace node
    if (node.getNodeName().startsWith("xmlns")) {
        LOGGER.warn("Node is Namespace declaration - delete is terminated with no effect.");
        return false;
    }

    //don't delete root nodes
    if (node.getNodeType() != Node.ATTRIBUTE_NODE && node.getParentNode().getNodeType() == Node.DOCUMENT_NODE) {
        LOGGER.warn("Node is a root Node - delete is terminated with no effect.");
        return false;
    }

    //don't delete document nodes
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        LOGGER.warn("Node is a Document Node - delete is terminated with no effect.");
        return false;
    }

    Node canonNode = node;

    if (node.getNodeType() != Node.ATTRIBUTE_NODE) {
        node.getParentNode().removeChild(node);
    } else {
        Attr attr = (Attr) node;
        attr.getOwnerElement().removeAttributeNode(attr);
    }

    // dispatch internal betterform event (for instant repeat updating)
    String[] canonicalParts = XPathUtil.getNodesetAndPredicates(path);
    HashMap map = new HashMap();
    map.put("nodeset", canonicalParts[0]);
    map.put("position", canonicalParts[canonicalParts.length - 1]);
    map.put("canonPath", canonicalPath);
    this.container.dispatch(this.target, BetterFormEventNames.NODE_DELETED, map);

    if (getLogger().isDebugEnabled()) {
        getLogger().debug(
                this + " delete node: instance data after manipulation" + toString(this.instanceDocument));
    }
    return true;
}

From source file:de.mpg.escidoc.services.syndication.Utils.java

public static Document createDocument(Node sourceNode) throws Exception {
    Document doc = createDocumentBuilder().newDocument();
    Node source;//from  www  . j  a v a2  s  .  c o  m
    if (sourceNode.getNodeType() == Node.DOCUMENT_NODE) {
        source = ((Document) sourceNode).getDocumentElement();
    } else {
        source = sourceNode;
    }

    Node node = doc.importNode(source, true);
    doc.appendChild(node);

    return doc;
}

From source file:mondrian.test.DiffRepository.java

private static void writeNode(Node node, XMLOutput out) {
    final NodeList childNodes;
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        out.print("<?xml version=\"1.0\" ?>" + Util.nl);
        childNodes = node.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node child = childNodes.item(i);
            writeNode(child, out);// ww w  .  j  av  a  2 s. c  o  m
        }
        //            writeNode(((Document) node).getDocumentElement(), out);
        break;

    case Node.ELEMENT_NODE:
        Element element = (Element) node;
        final String tagName = element.getTagName();
        out.beginBeginTag(tagName);
        // Attributes.
        final NamedNodeMap attributeMap = element.getAttributes();
        for (int i = 0; i < attributeMap.getLength(); i++) {
            final Node att = attributeMap.item(i);
            out.attribute(att.getNodeName(), att.getNodeValue());
        }
        out.endBeginTag(tagName);
        // Write child nodes, ignoring attributes but including text.
        childNodes = node.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node child = childNodes.item(i);
            if (child.getNodeType() == Node.ATTRIBUTE_NODE) {
                continue;
            }
            writeNode(child, out);
        }
        out.endTag(tagName);
        break;

    case Node.ATTRIBUTE_NODE:
        out.attribute(node.getNodeName(), node.getNodeValue());
        break;

    case Node.CDATA_SECTION_NODE:
        CDATASection cdata = (CDATASection) node;
        out.cdata(cdata.getNodeValue(), true);
        break;

    case Node.TEXT_NODE:
        Text text = (Text) node;
        final String wholeText = text.getNodeValue();
        if (!isWhitespace(wholeText)) {
            out.cdata(wholeText, false);
        }
        break;

    case Node.COMMENT_NODE:
        Comment comment = (Comment) node;
        out.print("<!--" + comment.getNodeValue() + "-->" + Util.nl);
        break;

    default:
        throw new RuntimeException("unexpected node type: " + node.getNodeType() + " (" + node + ")");
    }
}

From source file:com.xpn.xwiki.plugin.feed.SyndEntryDocumentSource.java

/**
 * Computes the sum of lengths of all the text nodes within the given DOM sub-tree
 * //from   w  ww  .jav a2s . c o  m
 * @param node the root of the DOM sub-tree containing the text
 * @return the sum of lengths of text nodes within the given DOM sub-tree
 */
public static int innerTextLength(Node node) {
    switch (node.getNodeType()) {
    case Node.TEXT_NODE:
        return node.getNodeValue().length();
    case Node.ELEMENT_NODE:
        int length = 0;
        Node child = node.getFirstChild();
        while (child != null) {
            length += innerTextLength(child);
            child = child.getNextSibling();
        }
        return length;
    case Node.DOCUMENT_NODE:
        return innerTextLength(((org.w3c.dom.Document) node).getDocumentElement());
    default:
        return 0;
    }
}

From source file:DOMProcessor.java

/** Searches for a given element in the given node and updates list
  * of text within matched elements. Recursively searches for sub-nodes
  * of the given one./*from  w  ww  .ja va 2 s  . com*/
  * @param element Element to search for. If null, all elements searched.
  * @param node Node to start search from.
  */
private void searchText(String element, Node node) {
    // Only consider document or element nodes.
    if ((node.getNodeType() != Node.DOCUMENT_NODE) && (node.getNodeType() != Node.ELEMENT_NODE)) {
        return;
    }

    if ((element == null) || (node.getNodeName().equalsIgnoreCase(element))) {
        // Match found so look for text in children.
        NodeList children = node.getChildNodes();

        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);

            if ((child.getNodeType() == Node.CDATA_SECTION_NODE) || (child.getNodeType() == Node.TEXT_NODE)) {
                if (child.getNodeValue().trim().length() > 0) {
                    //matches.add(child.getNodeValue());
                    matches.add(child);
                }
            }
        }
    }

    if ((node.getNodeType() == Node.DOCUMENT_NODE) || (node.getNodeType() == Node.ELEMENT_NODE)) {
        // Search child nodes.
        NodeList children = node.getChildNodes();

        for (int i = 0; i < children.getLength(); i++) {
            searchText(element, children.item(i));
        }
    }
}

From source file:bridge.toolkit.commands.S1000DConverter.java

/**
 * Iterate through the DOM tree/*  www .  j av a 2 s . c o m*/
 * 
 * @param node
 * @param output
 * @throws IOException
 */
public static void writeNode(Node node, Writer output) throws IOException {

    int type = node.getNodeType();

    switch (type) {
    case Node.ATTRIBUTE_NODE:
        output.write(' ');
        output.write(node.getNodeName());
        output.write("=\"");
        writeXMLData(node.getNodeValue(), true, output);
        output.write('"');
        break;
    case Node.CDATA_SECTION_NODE:
    case Node.TEXT_NODE:
        writeXMLData(node.getNodeValue(), false, output);
        break;
    case Node.COMMENT_NODE:
        output.write("<!--");
        output.write(((Comment) node).getNodeValue());
        output.write("-->");
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        writeNodes(node.getChildNodes(), output);
        break;
    case Node.DOCUMENT_NODE:
        writeNodes(node.getChildNodes(), output);
        break;
    case Node.DOCUMENT_TYPE_NODE:
        break;
    case Node.ELEMENT_NODE: {
        NamedNodeMap atts = node.getAttributes();

        output.write('<');
        output.write(node.getNodeName());
        if (atts != null) {
            int length = atts.getLength();
            for (int i = 0; i < length; i++)
                writeNode(atts.item(i), output);
        }

        if (node.hasChildNodes()) {
            output.write('>');
            writeNodes(node.getChildNodes(), output);
            output.write("</");
            output.write(node.getNodeName());
            output.write('>');
        } else {
            output.write("/>");
        }
        break;
    }
    case Node.ENTITY_NODE:
        break;
    case Node.ENTITY_REFERENCE_NODE:
        writeNodes(node.getChildNodes(), output);
        break;
    case Node.NOTATION_NODE:
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        break;
    default:
        throw new Error("Unexpected DOM node type: " + type);
    }
}

From source file:DOMProcessor.java

/** Searches for a given attribute in the given node and updates list
  * of attribute values. Recursively searches for sub-nodes of the given one.
  * @param element Element to search for.
  * @param node Node to start search from.
  *///from w w w.ja va 2 s  . c  om
private void searchAttributes(String element, Node node) {
    // Only consider document or element nodes.
    if ((node.getNodeType() != Node.DOCUMENT_NODE) && (node.getNodeType() != Node.ELEMENT_NODE)) {
        return;
    }

    // Search attributes associated with current node.
    NamedNodeMap attributes = node.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node attribute = attributes.item(i);
        if (attribute.getNodeName().equalsIgnoreCase(element)) {
            //matches.add(attribute.getNodeValue());
            matches.add(attribute);
        }
    }

    // Search child nodes.
    NodeList children = node.getChildNodes();

    for (int i = 0; i < children.getLength(); i++) {
        searchAttributes(element, children.item(i));
    }
}

From source file:DOMProcessor.java

/** Searches for a given element in the given node and updates list
  * of elements with that name. Recursively searches for sub-nodes of the given one.
  * @param element Element to search for.
  * @param node Node to start search from.
  *//*from  www  .  ja v a  2s  .co m*/
private void searchNode(String element, Node node) {
    // Only consider document or element nodes.
    if ((node.getNodeType() != Node.DOCUMENT_NODE) && (node.getNodeType() != Node.ELEMENT_NODE)) {
        return;
    }

    // Match found, so add node to list.  
    if (node.getNodeName().equalsIgnoreCase(element)) {
        matches.add(node);
    }

    // Search children
    NodeList children = node.getChildNodes();

    for (int i = 0; i < children.getLength(); i++) {
        searchNode(element, children.item(i));
    }
}

From source file:DOMProcessor.java

/** Converts the given DOM node into XML. Recursively converts
  * any child nodes. This version allows the XML version, encoding and stand-alone
  * status to be set.// w w  w. j  a  v  a2  s. c om
  * @param node DOM Node to display.
  * @param version XML version, or null if default ('1.0') is to be used.
  * @param encoding XML encoding, or null if encoding is not to be specified.
  * @param standalone XML stand-alone status or null if not to be specified.
  */
private void outputNodeAsXML(Node node, String version, String encoding, Boolean standalone) {
    // Store node name, type and value.
    String name = node.getNodeName(), value = makeFriendly(node.getNodeValue());
    int type = node.getNodeType();

    // Ignore empty nodes (e.g. blank lines etc.)
    if ((value != null) && (value.trim().equals(""))) {
        return;
    }

    switch (type) {
    case Node.DOCUMENT_NODE: // Start of document.
    {
        if (version == null) {
            out.print("<?xml version=\"1.0\" ");
        } else {
            out.print("<?xml version=\"" + version + "\" ");
        }

        if (encoding != null) {
            out.print("encoding=\"" + encoding + "\" ");
        }

        if (standalone != null) {
            if (standalone.booleanValue()) {
                out.print("standalone=\"yes\" ");
            } else {
                out.print("standalone=\"no\" ");
            }
        }

        out.println("?>");

        // Output the document's child nodes.
        NodeList children = node.getChildNodes();

        for (int i = 0; i < children.getLength(); i++) {
            outputNodeAsXML(children.item(i));
        }
        break;
    }

    case Node.ELEMENT_NODE: // Document element with attributes.
    {
        // Output opening element tag.
        indent++;
        indent();
        out.print("<" + name);

        // Output any attributes the element might have.
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node attribute = attributes.item(i);
            out.print(" " + attribute.getNodeName() + "=\"" + attribute.getNodeValue() + "\"");
        }
        out.print(">");

        // Output any child nodes that exist.                    
        NodeList children = node.getChildNodes();

        for (int i = 0; i < children.getLength(); i++) {
            outputNodeAsXML(children.item(i));
        }

        break;
    }

    case Node.CDATA_SECTION_NODE: // Display text.
    case Node.TEXT_NODE: {
        out.print(value);
        break;
    }

    case Node.COMMENT_NODE: // Comment node.
    {
        indent++;
        indent();
        out.print("<!--" + value + "-->");
        indent--;
        break;
    }

    case Node.ENTITY_REFERENCE_NODE: // Entity reference nodes.
    {
        indent++;
        indent();
        out.print("&" + name + ";");
        indent--;
        break;
    }

    case Node.PROCESSING_INSTRUCTION_NODE: // Processing instruction.
    {
        indent++;
        indent();
        out.print("<?" + name);
        if ((value != null) && (value.length() > 0)) {
            out.print(" " + value);
        }
        out.println("?>");
        indent--;
        break;
    }
    }

    // Finally output closing tags for each element.
    if (type == Node.ELEMENT_NODE) {
        out.print("</" + node.getNodeName() + ">");
        indent--;
        if (node.getNextSibling() == null) {
            indent(); // Only throw new line if this is the last sibling.
        }
    }
}