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:org.talend.designer.maven.tools.creator.CreateMavenJobPom.java

@SuppressWarnings("nls")
private void addAssemblyFiles(Node filesElem, String source, String outputDirectory, String destName,
        String fileMode, String lineEnding, boolean filtered, String comment) {
    Assert.isNotNull(filesElem);//from w  w w  .  j  a  va2 s . c  o m
    Assert.isNotNull(source);
    Assert.isNotNull(outputDirectory);

    Document doc = filesElem.getOwnerDocument();

    Element fileEle = doc.createElement("file");
    filesElem.appendChild(fileEle);

    if (comment != null) {
        fileEle.appendChild(doc.createComment(comment));
    }

    Element sourceElement = doc.createElement("source");
    sourceElement.setTextContent(source);
    fileEle.appendChild(sourceElement);

    Element outputDirectoryElement = doc.createElement("outputDirectory");
    outputDirectoryElement.setTextContent(outputDirectory);
    fileEle.appendChild(outputDirectoryElement);

    if (destName != null) { // if not set, will be same as source
        Element destNameElement = doc.createElement("destName");
        destNameElement.setTextContent(destName);
        fileEle.appendChild(destNameElement);
    }
    if (fileMode != null) {
        Element fileModeElement = doc.createElement("fileMode");
        fileModeElement.setTextContent(fileMode);
        fileEle.appendChild(fileModeElement);
    }
    if (lineEnding != null) {
        Element lineEndingElement = doc.createElement("lineEnding");
        lineEndingElement.setTextContent(lineEnding);
        fileEle.appendChild(lineEndingElement);
    }
    if (filtered) { // by default is false
        Element filteredElement = doc.createElement("filtered");
        filteredElement.setTextContent(Boolean.TRUE.toString());
        fileEle.appendChild(filteredElement);
    }
}

From source file:org.talend.designer.maven.tools.creator.CreateMavenJobPom.java

@SuppressWarnings("nls")
private void addAssemblyFileSets(Node fileSetsNode, String directory, String outputDirectory,
        boolean useDefaultExcludes, List<String> includes, List<String> excludes, String fileMode,
        String directoryMode, String lineEnding, boolean filtered, String comment) {
    Assert.isNotNull(fileSetsNode);//from   w ww.  j a  v a2 s . c o  m
    Assert.isNotNull(outputDirectory);
    Assert.isNotNull(directory);

    Document doc = fileSetsNode.getOwnerDocument();

    Element fileSetEle = doc.createElement("fileSet");
    fileSetsNode.appendChild(fileSetEle);

    if (comment != null) {
        fileSetEle.appendChild(doc.createComment(comment));
    }

    Element outputDirectoryElement = doc.createElement("outputDirectory");
    outputDirectoryElement.setTextContent(outputDirectory);
    fileSetEle.appendChild(outputDirectoryElement);

    Element directoryElement = doc.createElement("directory");
    directoryElement.setTextContent(directory);
    fileSetEle.appendChild(directoryElement);

    if (useDefaultExcludes) { // false by default
        Element useDefaultExcludesElement = doc.createElement("useDefaultExcludes");
        useDefaultExcludesElement.setTextContent(Boolean.TRUE.toString());
        fileSetEle.appendChild(useDefaultExcludesElement);
    }

    if (includes != null && !includes.isEmpty()) {

        Element includesEle = doc.createElement("includes");
        fileSetEle.appendChild(includesEle);
        for (String in : includes) {
            Element includeElement = doc.createElement("include");
            includeElement.setTextContent(in);
            includesEle.appendChild(includeElement);
        }
    }

    if (excludes != null && !excludes.isEmpty()) {
        Element excludesEle = doc.createElement("excludes");
        fileSetEle.appendChild(excludesEle);
        for (String ex : excludes) {
            Element excludeElement = doc.createElement("exclude");
            excludeElement.setTextContent(ex);
            excludesEle.appendChild(excludeElement);

        }
    }
    if (fileMode != null) {
        Element fileModeElement = doc.createElement("fileMode");
        fileModeElement.setTextContent(fileMode);
        fileSetEle.appendChild(fileModeElement);
    }
    if (directoryMode != null) {
        Element directoryModeElement = doc.createElement("directoryMode");
        directoryModeElement.setTextContent(directoryMode);
        fileSetEle.appendChild(directoryModeElement);
    }

    if (lineEnding != null) {
        Element lineEndingElement = doc.createElement("lineEnding");
        lineEndingElement.setTextContent(lineEnding);
        fileSetEle.appendChild(lineEndingElement);
    }
    if (filtered) { // by default is false
        Element filteredElement = doc.createElement("filtered");
        filteredElement.setTextContent(Boolean.TRUE.toString());
        fileSetEle.appendChild(filteredElement);
    }
}

From source file:org.tizzit.util.XercesHelper.java

public static Node renameNode(Node nde, String strName) {
    if (!strName.equals(nde.getNodeName())) {
        Document xdoc = nde.getOwnerDocument();
        Element retnode = xdoc.createElement(strName);
        NodeList nl = nde.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            retnode.appendChild(nl.item(i).cloneNode(true));
        }/*  w  w  w  . ja v  a2 s.  c  o m*/
        NamedNodeMap al = nde.getAttributes();
        for (int i = 0; i < al.getLength(); i++) {
            Attr attr = (Attr) al.item(i);
            retnode.setAttribute(attr.getName(), attr.getValue());
        }
        return retnode;
    }
    return nde;
}

From source file:org.tizzit.util.XercesHelper.java

/**
 * Easily creates a new Node, containing Text and returns the new created Node.
 * @param doc The Node to append to//from w  w w  . j a v a  2  s  . c  om
 * @param elementName The Name of the new Node
 * @param elementText The Text to insert into the Node
 * @return The created Node
 */
public static Element createTextNode(Node doc, String elementName, String elementText) {
    Element elm = doc.getOwnerDocument().createElement(elementName);
    Text elmTxt = doc.getOwnerDocument().createTextNode(elementText);
    elm.appendChild(elmTxt);
    doc.appendChild(elm);
    return elm;
}

From source file:org.wso2.carbon.humantask.core.utils.DOMUtils.java

/**
 * Convert a DOM node to a stringified XML representation.
 *
 * @param node DOM Node//from   w ww.  j av  a  2s  .c  o m
 * @return String
 */
public static String domToString(Node node) {
    if (node == null) {
        throw new IllegalArgumentException("Cannot stringify null Node!");
    }

    String value;
    short nodeType = node.getNodeType();
    if (nodeType == Node.ELEMENT_NODE || nodeType == Node.DOCUMENT_NODE) {
        // serializer doesn't handle Node type well, only Element
        DOMSerializerImpl ser = new DOMSerializerImpl();
        ser.setParameter(Constants.DOM_NAMESPACES, Boolean.TRUE);
        ser.setParameter(Constants.DOM_WELLFORMED, Boolean.FALSE);
        ser.setParameter(Constants.DOM_VALIDATE, Boolean.FALSE);

        // create a proper XML encoding header based on the input document;
        // default to UTF-8 if the parent document's encoding is not accessible
        String usedEncoding = "UTF-8";
        Document parent = node.getOwnerDocument();
        if (parent != null) {
            String parentEncoding = parent.getXmlEncoding();
            if (parentEncoding != null) {
                usedEncoding = parentEncoding;
            }
        }

        // the receiver of the DOM
        DOMOutputImpl out = new DOMOutputImpl();
        out.setEncoding(usedEncoding);

        // we write into a String
        StringWriter writer = new StringWriter(4096);
        out.setCharacterStream(writer);

        // out, ye characters!
        ser.write(node, out);
        writer.flush();

        // finally get the String
        value = writer.toString();
    } else {
        value = node.getNodeValue();
    }
    return value;
}

From source file:org.xwiki.xml.Sax2Dom.java

/**
 * @param root the root node to fill with SAX events
 * @throws ParserConfigurationException failed to create a new {@link Document}
 *///from   w  w w . j  av a 2s. co  m
public Sax2Dom(Node root) throws ParserConfigurationException {
    if (root instanceof Document) {
        this.document = (Document) root;
        this.rootNode = root;
    } else if (root != null) {
        this.document = root.getOwnerDocument();
        this.rootNode = root;
    } else {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        this.document = factory.newDocumentBuilder().newDocument();
        this.rootNode = this.document;
    }
}

From source file:oscar.oscarMessenger.docxfer.util.MsgCommxml.java

public static Element addNode(Node parentNode, String name, String value) {
    Element node = null;//from w  ww  . ja v a  2 s.c  om

    if (parentNode.getNodeType() == Node.DOCUMENT_NODE) {
        node = ((Document) parentNode).createElement(name);
    } else {
        node = parentNode.getOwnerDocument().createElement(name);
    }

    if (value != null) {
        node.appendChild(node.getOwnerDocument().createTextNode(value));
    }

    return (Element) parentNode.appendChild(node);
}

From source file:sf.net.experimaestro.utils.JSUtils.java

/**
 * Transforms a DOM node to a E4X scriptable object
 *
 * @param node/*from w w w.ja v a  2  s.c om*/
 * @param cx
 * @param scope
 * @return
 */
public static Object domToE4X(Node node, Context cx, Scriptable scope) {
    if (node == null) {
        LOGGER.info("XML is null");
        return Context.getUndefinedValue();
    }
    if (node instanceof Document)
        node = ((Document) node).getDocumentElement();

    if (node instanceof DocumentFragment) {

        final Document document = node.getOwnerDocument();
        Element root = document.createElement("root");
        document.appendChild(root);

        Scriptable xml = cx.newObject(scope, "XML", new Node[] { root });

        final Scriptable list = (Scriptable) xml.get("*", xml);
        int count = 0;
        for (Node child : XMLUtils.children(node)) {
            list.put(count++, list, cx.newObject(scope, "XML", new Node[] { child }));
        }
        return list;
    }

    LOGGER.debug("XML is of type %s [%s]; %s", node.getClass(), XMLUtils.toStringObject(node),
            node.getUserData("org.mozilla.javascript.xmlimpl.XmlNode"));
    return cx.newObject(scope, "XML", new Node[] { node });
}

From source file:wsattacker.library.signatureFaking.SignatureFakingOracle.java

private void appendCertificate(Node keyInfo, String certificate) {
    keyInfo.setTextContent("");
    String prefix = keyInfo.getPrefix();
    if (prefix == null) {
        prefix = "";
    } else {//from  w  w  w . ja v a  2 s  .  c  o m
        prefix = prefix + ":";
    }
    Node data = keyInfo.getOwnerDocument().createElementNS(NamespaceConstants.URI_NS_DS, prefix + "X509Data");
    keyInfo.appendChild(data);
    Node cert = keyInfo.getOwnerDocument().createElementNS(NamespaceConstants.URI_NS_DS,
            prefix + "X509Certificate");
    data.appendChild(cert);
    cert.setTextContent(certificate);
    log.debug("Appending Certificate \r\n" + certificate + "\r\nto the" + prefix + "X509Certificate element");
}