Example usage for org.w3c.dom Node getOwnerDocument

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

Introduction

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

Prototype

public Document getOwnerDocument();

Source Link

Document

The Document object associated with this node.

Usage

From source file:Main.java

public static void replaceText(Node node, String text) {
    for (;;) {//from w  w w .j a  va2s .c o m
        Node n = node.getFirstChild();
        if (n == null) {
            break;
        }
        node.removeChild(n);
    }
    Text t = node.getOwnerDocument().createTextNode(text);
    node.appendChild(t);
}

From source file:Main.java

/**
 * based on public Java5 javadoc of org.w3c.dom.Node.setTextContent method
 *///from   w w w . j  a  v a2s .co  m
public static void setTextContent(Node node, final String text) {
    while (node.hasChildNodes()) {
        node.removeChild(node.getFirstChild());
    }

    if (text != null && text.length() > 0) {
        Node textNode = node.getOwnerDocument().createTextNode(text);
        node.appendChild(textNode);
    }
}

From source file:Main.java

/**
 * Clone given Node into target Document. If targe is null, same Document will be used.
 * If deep is specified, all children below will also be cloned.
 *///  w w  w  . j a  va2 s  .  co  m
public static Node cloneNode(Node node, Document target, boolean deep) throws DOMException {
    if (target == null || node.getOwnerDocument() == target)
        // same Document
        return node.cloneNode(deep);
    else {
        //DOM level 2 provides this in Document, so once xalan switches to that,
        //we can take out all the below and just call target.importNode(node, deep);
        //For now, we implement based on the javadocs for importNode
        Node newNode;
        int nodeType = node.getNodeType();

        switch (nodeType) {
        case Node.ATTRIBUTE_NODE:
            newNode = target.createAttribute(node.getNodeName());

            break;

        case Node.DOCUMENT_FRAGMENT_NODE:
            newNode = target.createDocumentFragment();

            break;

        case Node.ELEMENT_NODE:

            Element newElement = target.createElement(node.getNodeName());
            NamedNodeMap nodeAttr = node.getAttributes();

            if (nodeAttr != null)
                for (int i = 0; i < nodeAttr.getLength(); i++) {
                    Attr attr = (Attr) nodeAttr.item(i);

                    if (attr.getSpecified()) {
                        Attr newAttr = (Attr) cloneNode(attr, target, true);
                        newElement.setAttributeNode(newAttr);
                    }
                }

            newNode = newElement;

            break;

        case Node.ENTITY_REFERENCE_NODE:
            newNode = target.createEntityReference(node.getNodeName());

            break;

        case Node.PROCESSING_INSTRUCTION_NODE:
            newNode = target.createProcessingInstruction(node.getNodeName(), node.getNodeValue());

            break;

        case Node.TEXT_NODE:
            newNode = target.createTextNode(node.getNodeValue());

            break;

        case Node.CDATA_SECTION_NODE:
            newNode = target.createCDATASection(node.getNodeValue());

            break;

        case Node.COMMENT_NODE:
            newNode = target.createComment(node.getNodeValue());

            break;

        case Node.NOTATION_NODE:
        case Node.ENTITY_NODE:
        case Node.DOCUMENT_TYPE_NODE:
        case Node.DOCUMENT_NODE:
        default:
            throw new IllegalArgumentException("Importing of " + node + " not supported yet");
        }

        if (deep)
            for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling())
                newNode.appendChild(cloneNode(child, target, true));

        return newNode;
    }
}

From source file:Main.java

/**
 * Clone given Node into target Document. If targe is null, same Document will be used.
 * If deep is specified, all children below will also be cloned.
 *//*from  ww w .  ja va 2  s  . c  o  m*/
public final static Node cloneNode(Node node, Document target, boolean deep) throws DOMException {
    if ((target == null) || (node.getOwnerDocument() == target)) {
        // same Document
        return node.cloneNode(deep);
    } else {
        //DOM level 2 provides this in Document, so once xalan switches to that,
        //we can take out all the below and just call target.importNode(node, deep);
        //For now, we implement based on the javadocs for importNode
        Node newNode;
        int nodeType = node.getNodeType();

        switch (nodeType) {
        case Node.ATTRIBUTE_NODE:
            newNode = target.createAttribute(node.getNodeName());

            break;

        case Node.DOCUMENT_FRAGMENT_NODE:
            newNode = target.createDocumentFragment();

            break;

        case Node.ELEMENT_NODE:

            Element newElement = target.createElement(node.getNodeName());
            NamedNodeMap nodeAttr = node.getAttributes();

            if (nodeAttr != null) {
                for (int i = 0; i < nodeAttr.getLength(); i++) {
                    Attr attr = (Attr) nodeAttr.item(i);

                    if (attr.getSpecified()) {
                        Attr newAttr = (Attr) cloneNode(attr, target, true);
                        newElement.setAttributeNode(newAttr);
                    }
                }
            }

            newNode = newElement;

            break;

        case Node.ENTITY_REFERENCE_NODE:
            newNode = target.createEntityReference(node.getNodeName());

            break;

        case Node.PROCESSING_INSTRUCTION_NODE:
            newNode = target.createProcessingInstruction(node.getNodeName(), node.getNodeValue());

            break;

        case Node.TEXT_NODE:
            newNode = target.createTextNode(node.getNodeValue());

            break;

        case Node.CDATA_SECTION_NODE:
            newNode = target.createCDATASection(node.getNodeValue());

            break;

        case Node.COMMENT_NODE:
            newNode = target.createComment(node.getNodeValue());

            break;

        case Node.NOTATION_NODE:
        case Node.ENTITY_NODE:
        case Node.DOCUMENT_TYPE_NODE:
        case Node.DOCUMENT_NODE:
        default:
            throw new IllegalArgumentException("Importing of " + node + " not supported yet");
        }

        if (deep) {
            for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
                newNode.appendChild(cloneNode(child, target, true));
            }
        }

        return newNode;
    }
}

From source file:Main.java

/**
 * Adds a new {@link Element} with multiple attributes to the given parent
 * {@link Element}. The attribute data should be passed in pairs of key and
 * attribute value as the value./*from  w  w  w  .j  av  a 2  s. c o m*/
 * 
 * @param doc
 * @param parent
 * @param name
 * @param attributeData
 * @return
 */
public static Element addElement(Document doc, Node parent, String name, Object... attributeData) {
    if (doc == null) {
        doc = parent.getOwnerDocument();
    }
    final Element elem = addElement(doc, parent, name);

    // check we have an even number of attribute pairs
    if ((attributeData.length & 1) == 1) {
        throw new RuntimeException("Need an even number attribute args to make attribute name/value pairs.");
    }
    addAttributes(elem, attributeData);
    return elem;
}

From source file:Main.java

/**
   Return the first children of a node with a given name. If no
   child has that name a new one of type element is added to the
   node and returned. //from   ww w .j  ava 2  s  . c o m
 */
static public Node findChildAssertive(Node node, String child_name) {
    if (node == null)
        return null;

    Node child = findChild(node, child_name);
    if (child != null)
        return child;
    return node.appendChild(node.getOwnerDocument().createElement(child_name));
}

From source file:Main.java

public static Element addTextElement(Node parent, String name, String value, Attr[] attrs) {
    Element element;//w w  w.  j av  a  2s .co  m
    if (parent instanceof Document) {
        element = ((Document) parent).createElement(name);
    } else {
        element = parent.getOwnerDocument().createElement(name);
    }

    if (attrs != null && attrs.length > 0) {
        for (Attr attr : attrs) {
            element.setAttributeNode(attr);
        }
    }

    if (value != null) {
        element.setTextContent(value);
    }
    parent.appendChild(element);
    return element;
}

From source file:Main.java

public static Element createElement(Node node, String name, String value, Map<String, Object> attributes) {
    Document doc = node.getNodeType() == Node.DOCUMENT_NODE ? (Document) node : node.getOwnerDocument();
    Element element = doc.createElement(name);
    element.setTextContent(value);//from  w w w .java2 s  .  c  o m
    addAttributes(element, attributes);
    return element;
}

From source file:Main.java

/**
 * Adds a new {@link Element} with the given text to the given parent
 * {@link Element}./*from ww w .  j av  a2  s .c o m*/
 * 
 * @param doc the document to add to.
 * @param parent the parent element.
 * @param name the name of the new element.
 * @param value the text value.
 * @return the created element.
 */
public static Element addTextElement(Document doc, Node parent, String name, String value) {
    if (doc == null) {
        doc = parent.getOwnerDocument();
    }
    final Element elem = addElement(doc, parent, name);
    if (value != null) {
        elem.appendChild(doc.createTextNode(value));
    }
    return elem;
}

From source file:Main.java

public static Node getNode(Node parentNode, String nodeName, Map<String, String> attributesMap) {
    Node returnNode = null;/*w w  w.ja  va  2 s .  c  o  m*/
    try {
        DocumentTraversal dt = (DocumentTraversal) parentNode.getOwnerDocument();
        NodeIterator i = dt.createNodeIterator(parentNode, NodeFilter.SHOW_ELEMENT, null, false);
        Node node = i.nextNode();
        while (node != null) {
            if (node.getNodeName().equals(nodeName)) {
                if (attributesExist(node, attributesMap)) {
                    returnNode = node;
                    break;
                }
            }
            node = i.nextNode();
        }
    } catch (Exception ex) {
        logger.error(ex);
    }
    return returnNode;
}