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:jp.go.nict.langrid.client.soap.io.SoapResponseParser.java

private static Node resolveHref(XPathWorkspace w, Node node) throws DOMException {
    Node href = node.getAttributes().getNamedItem("href");
    if (href != null) {
        return findFirstDescendantHasAttr(node.getOwnerDocument().getDocumentElement(), "id",
                href.getTextContent().substring(1));
    } else {//from  w  w w  .ja v a 2  s  .  c o m
        return node;
    }
}

From source file:Main.java

/**
 * @param parent//from   ww w  .ja  v a  2s  .c o  m
 *          node to add fragment to
 * @param fragment
 *          a well formed XML fragment
 * @throws ParserConfigurationException 
 */
public static void appendXmlFragment(Node parent, String fragment)
        throws IOException, SAXException, ParserConfigurationException {

    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder docBuilder = domFactory.newDocumentBuilder();
    Document doc = parent.getOwnerDocument();

    Node fragmentNode = docBuilder.parse(new InputSource(new StringReader(fragment))).getDocumentElement();

    fragmentNode = doc.importNode(fragmentNode, true);

    parent.appendChild(fragmentNode);
}

From source file:Main.java

public static void writeOutAttributesForNode(Map<?, ?> attributes, Node node) {
    if (attributes != null) {
        // Add attributes
        for (Iterator<?> i = attributes.keySet().iterator(); i.hasNext();) {
            Object key = i.next();
            Object value = attributes.get(key);
            if ((key != null) && (value != null)) {
                Attr attNode = node.getOwnerDocument().createAttribute(key.toString());
                attNode.setNodeValue(value.toString());
                node.getAttributes().setNamedItem(attNode);
            }/*ww w  . j a  va 2s  .  c o m*/
        }
    }
}

From source file:Main.java

/**
 * Creates a new <code>Node</code>-object with the specified <code>name</code>
 * and appends it as a child element to the provided <code>Node</code>-
 * parameter.//w w  w . j  av a 2 s  . com
 * 
 * @param elem the <code>Node</code> to which the newly created 
 * <code>Node</code> will be appended to. Not allowed to be <code>null</code>.
 * @param elemName the name of the to-be-created <code>Element</code>, not allowed 
 * to be empty or <code>null</code>.
 * @return the created element
 * @exception IllegalArgumentException if <code>elem</code> and/ord
 * <code>elemName</code> are null (or empty in the case of <code>elemName</code>)
 */
public static Element createChild(Node node, String elemName) {
    if (node == null || elemName == null || "".equals(elemName))
        throw new IllegalArgumentException("Arguments are not allowed to be null or empty");

    Document document = null;

    if (node instanceof Document) {
        document = (Document) node;
    } else if (node.getOwnerDocument() != null) {
        document = node.getOwnerDocument();
    }

    Element newChild = null;
    if (document != null) {
        newChild = document.createElement(elemName);
        node.appendChild(newChild);
    }

    return newChild;
}

From source file:Main.java

static public void SetNodeText(Node node, String value) {
    NodeList node_;// w  w w.j a  v  a2s.c o  m
    Node x;
    int n, nnode;
    if (value == null)
        value = "";
    if (node.getNodeType() == Node.TEXT_NODE)
        node.setNodeValue(value);
    else {
        node_ = node.getChildNodes();
        nnode = node_.getLength();
        if (nnode == 0) {
            x = (Node) node.getOwnerDocument().createTextNode(value);
            node.appendChild(x);
        } else {
            for (n = 0; n < nnode; n++) {
                x = (Node) node_.item(n);
                if (x == null)
                    continue;
                if (x.getNodeType() == Node.TEXT_NODE) {
                    x.setNodeValue(value);
                    break;
                }
            }
        }
    }
}

From source file:Main.java

/**
 * Same as {@link createChild(Node, String) createChild()}, but adds the
 * specified <code>text</code> to the newly created <code>Node</code>.
 * //w w  w . jav a 2s. c  o  m
 * @see #createChild(Node, String)
 * @param elem the <code>Node</code> to which the newly created 
 * <code>Node</code> will be appended to. Not allowed to be <code>null</code>.
 * @param elemName the name of the to-be-created <code>Node</code>, not allowed 
 * to be empty or <code>null</code>.
 * @param text the text-contents of the created/inserted node  
 * @return the created element
 * @exception IllegalArgumentException if <code>elem</code> and/ord
 * <code>elemName</code> are null (or empty in the case of <code>elemName</code>)
 */
public static Element createTextChild(Node node, String elemName, String text) {
    if (node == null || elemName == null || "".equals(elemName))
        throw new IllegalArgumentException("Arguments are not allowed to be null or empty");

    Document document = null;

    if (node instanceof Document) {
        document = (Document) node;
    } else if (node.getOwnerDocument() != null) {
        document = node.getOwnerDocument();
    }

    Element newChild = null;
    if (document != null) {
        newChild = document.createElement(elemName);
        node.appendChild(newChild);
        newChild.appendChild(document.createTextNode(text));
    }

    return newChild;
}

From source file:Main.java

/**
 * This method returns the first non-null owner document of the Nodes in this Set.
 * This method is necessary because it <I>always</I> returns a
 * {@link Document}. {@link Node#getOwnerDocument} returns <CODE>null</CODE>
 * if the {@link Node} is a {@link Document}.
 *
 * @param xpathNodeSet/*from   w w  w . ja  v a  2 s  . c  o  m*/
 * @return the owner document
 */
public static Document getOwnerDocument(Set<Node> xpathNodeSet) {
    NullPointerException npe = null;
    for (Node node : xpathNodeSet) {
        int nodeType = node.getNodeType();
        if (nodeType == Node.DOCUMENT_NODE) {
            return (Document) node;
        }
        try {
            if (nodeType == Node.ATTRIBUTE_NODE) {
                return ((Attr) node).getOwnerElement().getOwnerDocument();
            }
            return node.getOwnerDocument();
        } catch (NullPointerException e) {
            npe = e;
        }
    }

    throw new NullPointerException(npe.getMessage());
}

From source file:Main.java

/**
 * Creates a Text child, or replaces all existing children of type Text.
 * Non-Text descendants are preserved without change.
 * @return The Text child which receives the specified text. 
 * @see Node.setTextContent()/* ww w. java 2 s  .c  om*/
 */
public static Text setText(Node node, String text) {
    NodeList children = node.getChildNodes();
    if (children != null) {
        for (int i = 0, n = children.getLength(); i < n; ++i) {
            Node child = children.item(i);
            if (child instanceof Text) {
                return ((Text) child).replaceWholeText(text);
            }
        }
    }
    Text ans = node.getOwnerDocument().createTextNode(text);
    node.appendChild(ans);
    return ans;
}

From source file:Main.java

/**
 * The method inserts end-of-line+indentation Text nodes where indentation is necessary.
 *
 * @param node - node to be pretty formatted
 * @param identLevel - initial indentation level of the node
 * @param ident - additional indentation inside the node
 *//*ww  w.j  ava  2  s  . com*/
private static void prettyFormat(Node node, String identLevel, String ident) {
    NodeList nodelist = node.getChildNodes();
    int iStart = 0;
    Node item = nodelist.item(0);
    if (item != null) {
        short type = item.getNodeType();
        if (type == Node.ELEMENT_NODE || type == Node.COMMENT_NODE) {
            Node newChild = node.getOwnerDocument().createTextNode(EOL_XML + identLevel + ident);
            node.insertBefore(newChild, item);
            iStart = 1;
        }
    }
    for (int i = iStart; i < nodelist.getLength(); i++) {
        item = nodelist.item(i);
        if (item != null) {
            short type = item.getNodeType();
            if (type == Node.TEXT_NODE && item.getNodeValue().trim().length() == 0) {
                if (i + 1 < nodelist.getLength()) {
                    item.setNodeValue(EOL_XML + identLevel + ident);
                } else {
                    item.setNodeValue(EOL_XML + identLevel);
                }
            } else if (type == Node.ELEMENT_NODE) {
                prettyFormat(item, identLevel + ident, ident);
                if (i + 1 < nodelist.getLength()) {
                    Node nextItem = nodelist.item(i + 1);
                    if (nextItem != null) {
                        short nextType = nextItem.getNodeType();
                        if (nextType == Node.ELEMENT_NODE || nextType == Node.COMMENT_NODE) {
                            Node newChild = node.getOwnerDocument()
                                    .createTextNode(EOL_XML + identLevel + ident);
                            node.insertBefore(newChild, nextItem);
                            i++;
                            continue;
                        }
                    }
                } else {
                    Node newChild = node.getOwnerDocument().createTextNode(EOL_XML + identLevel);
                    node.appendChild(newChild);
                    i++;
                    continue;
                }
            }
        }
    }
}

From source file:com.gargoylesoftware.htmlunit.xml.XmlUtil.java

/**
 * Recursively appends a {@link Node} child to {@link DomNode} parent.
 *
 * @param page the owner page of {@link DomElement}s to be created
 * @param parent the parent DomNode//w  ww . ja  v  a 2s. co m
 * @param child the child Node
 * @param handleXHTMLAsHTML if true elements from the XHTML namespace are handled as HTML elements instead of
 *     DOM elements
 */
public static void appendChild(final SgmlPage page, final DomNode parent, final Node child,
        final boolean handleXHTMLAsHTML) {
    final DocumentType documentType = child.getOwnerDocument().getDoctype();
    if (documentType != null && page instanceof XmlPage) {
        final DomDocumentType domDoctype = new DomDocumentType(page, documentType.getName(),
                documentType.getPublicId(), documentType.getSystemId());
        ((XmlPage) page).setDocumentType(domDoctype);
    }
    final DomNode childXml = createFrom(page, child, handleXHTMLAsHTML);
    parent.appendChild(childXml);
    copy(page, child, childXml, handleXHTMLAsHTML);
}