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:com.twinsoft.convertigo.engine.localbuild.BuildLocally.java

private static Element cloneNode(Node node, String newNodeName) {

    Element newElement = node.getOwnerDocument().createElement(newNodeName);

    NamedNodeMap attrs = node.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr = (Attr) attrs.item(i);
        newElement.setAttribute(attr.getName(), attr.getValue());
    }/*  w w w  .j av a  2 s . c om*/

    return newElement;

}

From source file:Main.java

/**
 * /* w w  w.ja v a2 s.  c o m*/
 * 
 * @param node
 *            Node object
 * @param childName
 *            Name of child node looking for
 * @param createNode
 *            If true, appends a child node with given name when not found
 * @return Node Child Node
 * @throws IllegalArgumentException
 *             for Invalid input
 */
public static Node getChildNodeByName(final Node node, final String childName, final boolean createNode)
        throws IllegalArgumentException {
    // Validate node
    if (node == null) {
        throw new IllegalArgumentException("Node cannot " + "be null in XmlUtils.getChildNodebyName method");
    }

    // Validate child name
    if (childName == null) {
        throw new IllegalArgumentException(
                "Child name cannot" + " be null in XmlUtils.getChildNodebyName method");
    }

    final NodeList childList = node.getChildNodes();
    if (childList != null) {
        for (int childIndex = 0; childIndex < childList.getLength(); childIndex++) {
            if (childName.equals(childList.item(childIndex).getNodeName())) {
                return childList.item(childIndex);
            }
        }
    }

    if (createNode) {
        final Node newNode = node.getOwnerDocument().createElement(childName);
        node.appendChild(newNode);
        return newNode;
    } else {
        return null;
    }
}

From source file:it.cnr.icar.eric.server.common.Utility.java

/**
 * This is an implementation of the JAXP DOM Node.setTextContent
 * method, which does not exist in JDK 1.4. This code was adapted
 * from the Apache Xerces 2.8.0 implementation of this method.
 *//*w ww.j av  a  2s .co m*/
public static void setTextContent(Node node, String textContent) throws DOMException {
    Node child;

    // get rid of any existing children
    while ((child = node.getFirstChild()) != null) {
        node.removeChild(child);
    }

    // create a Text node to hold the given content
    if (textContent != null && textContent.length() != 0) {
        node.appendChild(node.getOwnerDocument().createTextNode(textContent));
    }
}

From source file:eu.semaine.util.XMLTool.java

/**
 * Create a child element with the given name and namespace, and append it below node.
 * @param node/* w  w  w .  j a  v a2s.c  om*/
 * @param childName
 * @param childNamespace the namespace of the child, or null if no namespace is desired.
 * @return the child element
 */
public static Element appendChildElement(Node node, String childName, String childNamespace) {
    if (node == null)
        throw new NullPointerException("Received null node");
    if (childName == null)
        throw new NullPointerException("Received null childName");
    return (Element) node.appendChild(createElement(node.getOwnerDocument(), childName, childNamespace));
}

From source file:eu.semaine.util.XMLTool.java

/**
 * Create a child element with the given name and append it below node.
 * The new element will have the same namespace as node.
 * If node has a namespace prefix, the new element will also use that namespace prefix.
 * @param node//from w  w w.jav  a 2  s.co  m
 * @param childName
 * @return the child element
 */
public static Element appendChildElement(Node node, String childName) {
    if (node == null)
        throw new NullPointerException("Received null node");
    if (childName == null)
        throw new NullPointerException("Received null childName");
    Element child = (Element) node
            .appendChild(createElement(node.getOwnerDocument(), childName, node.getNamespaceURI()));
    String parentPrefix = node.getPrefix();
    if (parentPrefix != null) {
        child.setPrefix(parentPrefix);
    }
    return child;
}

From source file:Main.java

/**
 * Builds an XPointer that refers to the given node. If a shorthand pointer
 * (using a schema-determined identifier) cannot be constructed, then a
 * scheme-based pointer is derived that indicates the absolute location path
 * of a node in a DOM document. The location is specified as a scheme-based
 * XPointer having two parts:/*from w ww  .j  a  va  2s . c om*/
 * <ul>
 * <li>an xmlns() part that declares a namespace binding context;</li>
 * <li>an xpointer() part that includes an XPath expression using the
 * abbreviated '//' syntax for selecting a descendant node.</li>
 * </ul>
 * 
 * @param node
 *            A node in a DOM document.
 * @return A String containing either a shorthand or a scheme-based pointer
 *         that refers to the node.
 * 
 * @see <a href="http://www.w3.org/TR/xptr-framework/"target="_blank">
 *      XPointer Framework</a>
 * @see <a href="http://www.w3.org/TR/xptr-xmlns/" target="_blank">XPointer
 *      xmlns() Scheme</a>
 * @see <a href="http://www.w3.org/TR/xptr-xpointer/"
 *      target="_blank">XPointer xpointer() Scheme</a>
 */
public static String buildXPointer(Node node) {
    if (null == node) {
        return "";
    }
    StringBuilder xpointer = new StringBuilder();
    if (null != node.getAttributes() && null != node.getAttributes().getNamedItem("id")) {
        String id = node.getAttributes().getNamedItem("id").getNodeValue();
        xpointer.append(node.getLocalName()).append("[@id='").append(id).append("']");
        return xpointer.toString();
    }
    String nsURI = node.getNamespaceURI();
    String nsPrefix = node.getPrefix();
    if (null == nsPrefix)
        nsPrefix = "tns";
    // WARNING: Escaping rules are currently ignored.
    xpointer.append("xmlns(").append(nsPrefix).append("=").append(nsURI).append(")");
    xpointer.append("xpointer((");
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        // Find the element in the list of all similarly named descendants
        // of the document root.
        NodeList elementsByName = node.getOwnerDocument().getElementsByTagNameNS(nsURI, node.getLocalName());
        for (int i = 0; i < elementsByName.getLength(); i++) {
            if (elementsByName.item(i).isSameNode(node)) {
                xpointer.append("//");
                xpointer.append(nsPrefix).append(':').append(node.getLocalName()).append(")[").append(i + 1)
                        .append("])");
                break;
            }
        }
        break;
    case Node.DOCUMENT_NODE:
        xpointer.append("/");
        break;
    case Node.ATTRIBUTE_NODE:
        Attr attrNode = (Attr) node;
        xpointer = new StringBuilder(buildXPointer(attrNode.getOwnerElement()));
        xpointer.insert(xpointer.lastIndexOf(")"), "/@" + attrNode.getName());
        break;
    default:
        xpointer.setLength(0);
        break;
    }
    return xpointer.toString();
}

From source file:com.adaptris.util.text.xml.InsertNode.java

@Override
public Document merge(Document original, Document newDoc) throws Exception {
    if (StringUtils.isEmpty(getXpathToParentNode())) {
        throw new Exception("No parent node configured");
    }/*w ww. j  a va2  s. c  o m*/
    Document resultDoc = original;
    XmlUtils xml = create(resultDoc);
    Node parent = resolve(xml, getXpathToParentNode());
    if (parent.getOwnerDocument() == null) {
        throw new Exception("Invalid xpath-to-parent-node [" + getXpathToParentNode() + "]");
    }
    xml.appendNode(newDoc.getDocumentElement(), parent);
    return resultDoc;
}

From source file:ca.brood.tunneller.Tunneller.java

@Override
public boolean configure(Node rootNode) {
    NodeList nodes = rootNode.getOwnerDocument().getElementsByTagName("tunnel");

    if (nodes.getLength() == 0) {
        log.fatal("No tunneller configured");
        return false;
    }//from w ww  .  j  a v a 2  s.  c  om

    for (int i = 0; i < nodes.getLength(); i++) {
        TunnelKeepaliveThread newThread = new TunnelKeepaliveThread();
        if (!newThread.configure(nodes.item(i))) {
            log.fatal("Error configuring tunneller");
            return false;
        }
        tunnelThreads.add(newThread);
    }

    return true;
}

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

/**
 * Iterate through the DOM tree/*from w w w  .  ja  va 2 s. c o  m*/
 * 
 * @param node
 * @param output
 * @param isDocument
 * @param encoding
 * @param internalSubset
 * @throws IOException
 */
static void writeNode(Node node, Writer output, boolean isDocument, String encoding, String internalSubset)
        throws IOException {

    if (isDocument) {
        if (encoding == null)
            output.write("<?xml version=\"1.0\"?>\n\n");
        else
            output.write("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n\n");

        DocumentType doctype = node.getOwnerDocument().getDoctype();

        if (doctype != null) {
            String pubid = doctype.getPublicId();
            String sysid = doctype.getSystemId();
            output.write("<!DOCTYPE ");
            output.write(node.getNodeName());
            if (pubid != null) {
                output.write(" PUBLIC \"");
                output.write(pubid);
                if (sysid != null) {
                    output.write("\" \"");
                    output.write(sysid);
                }
                output.write('"');
            } else if (sysid != null) {
                output.write(" SYSTEM \"");
                output.write(sysid);
                output.write('"');
            }
            String subset = internalSubset;
            if (subset == null)
                subset = doctype.getInternalSubset();
            if (subset != null) {
                output.write(" [");
                output.write(subset);
                output.write(']');
            }
            output.write(">\n\n");
        }
    }
    writeNode(node, output);

    if (isDocument)
        output.write("\n");
}

From source file:Main.java

/**
 * This method sets value to given node/* www  .ja va 2  s .c  o  m*/
 * 
 * @param inputNode
 *            Node to which value needs to be set
 * @param nodeValue
 *            Value to set
 * @throws IllegalArgumentException
 *             if input is invalid
 */
public static void setNodeTextValue(final Node inputNode, final String nodeValue)
        throws IllegalArgumentException {
    // Child list
    NodeList childList = null;

    // Validate input stream
    if (inputNode == null) {
        throw new IllegalArgumentException("Input Node cannot be null in XmlUtils.setNodeValue");
    }

    // Get child list
    childList = inputNode.getChildNodes();

    // If child nodes found
    if ((childList != null) && (childList.getLength() > 0)) {
        // Get child count
        final int childCount = childList.getLength();

        // For each child
        for (int childIndex = 0; childIndex < childCount; childIndex++) {
            final Node childNode = childList.item(childIndex);
            // Check if text node
            if ((childNode != null) && (childNode.getNodeType() == Node.TEXT_NODE)) {
                // Set value to text node
                childNode.setNodeValue(nodeValue);
                break;
            }
        }
    } else {
        // Create text node and set node value
        inputNode.appendChild(inputNode.getOwnerDocument().createTextNode(nodeValue));
    }
}