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.apache.camel.component.xmlsecurity.api.XAdESSignatureProperties.java

@Override
public Output get(Input input) throws Exception { //NOPMD

    XmlSignatureProperties.Output result = new Output();

    if (!isAddSignedSignatureProperties() && !isAddSignedDataObjectPropeties()) {
        LOG.debug(/* ww w.j  av a2s .c o m*/
                "XAdES signature properties are empty. Therefore no XAdES element will be added to the signature.");
        return result;
    }
    String signedPropertiesId = "_" + UUID.randomUUID().toString();
    Reference ref = input.getSignatureFactory().newReference("#" + signedPropertiesId,
            input.getSignatureFactory().newDigestMethod(input.getContentDigestAlgorithm(), null),
            Collections.emptyList(), "http://uri.etsi.org/01903#SignedProperties", null);

    Node parent = input.getParent();
    Document doc;
    if (Node.DOCUMENT_NODE == parent.getNodeType()) {
        doc = (Document) parent; // enveloping
    } else {
        doc = parent.getOwnerDocument(); // enveloped
    }

    Element qualifyingProperties = createElement("QualifyingProperties", doc, input);
    setIdAttributeFromHeader(XmlSignatureConstants.HEADER_XADES_QUALIFYING_PROPERTIES_ID, qualifyingProperties,
            input);
    String signatureId = input.getSignatureId();
    if (signatureId == null || signatureId.isEmpty()) {
        LOG.debug("No signature Id configured. Therefore a value is generated.");
        // generate one
        signatureId = "_" + UUID.randomUUID().toString();
        // and set to output
        result.setSignatureId(signatureId);
    }
    setAttribute(qualifyingProperties, "Target", "#" + signatureId);
    Element signedProperties = createElement("SignedProperties", doc, input);
    qualifyingProperties.appendChild(signedProperties);
    setAttribute(signedProperties, "Id", signedPropertiesId);
    signedProperties.setIdAttribute("Id", true);
    addSignedSignatureProperties(doc, signedProperties, input);
    String contentReferenceId = addSignedDataObjectProperties(doc, signedProperties, input);
    result.setContentReferenceId(contentReferenceId);
    DOMStructure structure = new DOMStructure(qualifyingProperties);

    XMLObject propertiesObject = input.getSignatureFactory().newXMLObject(Collections.singletonList(structure),
            null, null, null);

    result.setReferences(Collections.singletonList(ref));
    result.setObjects(Collections.singletonList(propertiesObject));

    return result;
}

From source file:org.apache.camel.spring.handler.CamelNamespaceHandler.java

public static void renameNamespaceRecursive(Node node) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Document doc = node.getOwnerDocument();
        if (node.getNamespaceURI().startsWith(SPRING_NS + "/v")) {
            doc.renameNode(node, SPRING_NS, node.getNodeName());
        }/*from w  ww.  j  a  va2 s.  c  o  m*/
    }
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); ++i) {
        renameNamespaceRecursive(list.item(i));
    }
}

From source file:org.apache.cocoon.forms.binding.InsertNodeJXPathBinding.java

/**
 * Registers a JXPath Factory on the JXPath Context.
 * <p>//  w  ww  . j  a v  a 2 s .  c  o  m
 * The factory will inserts a clone of the 'template' DocumentFragment
 * inside this object into the target objectmodel.
 */
public void doSave(Widget frmModel, JXPathContext jxpc) {

    Node parentNode = (Node) jxpc.getContextBean();
    Document targetDoc = parentNode.getOwnerDocument();
    Node toInsert = targetDoc.importNode(this.template, true);
    parentNode.appendChild(toInsert);

    if (getLogger().isDebugEnabled())
        getLogger().debug("InsertNode executed.");

    // jxpc.setFactory(new AbstractFactory() {
    //     public boolean createObject(JXPathContext context, Pointer pointer,
    //         Object parent, String name, int index) {
    //
    //         Node parentNode = (Node) parent;
    //         Document targetDoc = parentNode.getOwnerDocument();
    //         Node toInsert = targetDoc.importNode(InsertNodeJXPathBinding.this.template, true);
    //         parentNode.appendChild(toInsert);
    //
    //         if (getLogger().isDebugEnabled())
    //             getLogger().debug("InsertNode jxpath factory executed for index." + index);
    //         return true;
    //     }
    // });
    //
    // if (getLogger().isDebugEnabled())
    //     getLogger().debug("done registered factory for inserting node -- " + toString());
}

From source file:org.apache.cocoon.forms.binding.TempRepeaterJXPathBinding.java

public void doLoad(Widget frmModel, JXPathContext jctx) throws BindingException {
    // (There should be a general widget type checker for all the bindings to use,
    // coupled with a general informative exception class to throw if the widget is
    // of the wrong type or null.)
    Repeater repeater = (Repeater) selectWidget(frmModel, this.repeaterId);
    if (repeater == null) {
        String fullId = frmModel.getRequestParameterName();
        if (fullId == null || fullId.length() == 0) {
            fullId = "";
        } else {/*  w  w w.  j a va  2  s  .  c  om*/
            fullId = fullId + ".";
        }
        throw new RuntimeException("TempRepeaterJXPathBinding: Repeater \"" + fullId + this.repeaterId
                + "\" does not exist (" + frmModel.getLocation() + ")");
    }

    // Start by clearing the repeater, if necessary.
    if (this.clearOnLoad) {
        repeater.clear();
    }

    // Find the location of the repeater data.
    Pointer repeaterPointer = jctx.getPointer(this.repeaterPath);

    // Check if there is data present.
    //
    // (Otherwise, should we check the leniency config option
    // to decide whether to be silent or throw an exception?) 
    if (repeaterPointer != null) {

        // Narrow to repeater context.
        JXPathContext repeaterContext = jctx.getRelativeContext(repeaterPointer);

        // Build a jxpath iterator for the repeater row pointers.
        Iterator rowPointers = repeaterContext.iteratePointers(this.rowPath);

        // Iterate through the rows of data.
        int rowNum = 0;
        while (rowPointers.hasNext()) {

            // Get or create a row widget.
            Repeater.RepeaterRow thisRow;
            if (repeater.getSize() > rowNum) {
                thisRow = repeater.getRow(rowNum);
            } else {
                thisRow = repeater.addRow();
            }
            rowNum++;

            // Narrow to the row context.
            Pointer rowPointer = (Pointer) rowPointers.next();
            JXPathContext rowContext = repeaterContext.getRelativeContext(rowPointer);

            // If virtual rows are requested, place a deep clone of the row data
            // into a temporary node, and narrow the context to this virtual row.
            //
            // (A clone of the data is used to prevent modifying the source document.
            // Otherwise, the appendChild method would remove the data from the source
            // document.  Is this protection worth the penalty of a deep clone?)
            //
            // (This implementation of virtual rows currently only supports DOM
            // bindings, but could easily be extended to support other bindings.)

            if (virtualRows == true) {
                Node repeaterNode = (Node) repeaterPointer.getNode();
                Node virtualNode = repeaterNode.getOwnerDocument().createElementNS(null, "virtual");
                Node node = (Node) rowPointer.getNode();
                Node clone = node.cloneNode(true);
                Node fakeDocElement = node.getOwnerDocument().getDocumentElement().cloneNode(false);
                virtualNode.appendChild(clone);
                fakeDocElement.appendChild(virtualNode);
                rowContext = JXPathContext.newContext(repeaterContext, fakeDocElement);
                rowContext = rowContext.getRelativeContext(rowContext.getPointer("virtual"));
            }

            // Finally, perform the load row binding.
            this.rowBinding.loadFormFromModel(thisRow, rowContext);
        }
    }

    if (getLogger().isDebugEnabled())
        getLogger().debug("done loading rows " + toString());
}

From source file:org.apache.cocoon.forms.binding.TempRepeaterJXPathBinding.java

public void doSave(Widget frmModel, JXPathContext jctx) throws BindingException {
    // (See comment in doLoad about type checking and throwing a meaningful exception.)
    Repeater repeater = (Repeater) selectWidget(frmModel, this.repeaterId);

    // Perform shortcut binding if the repeater is empty
    // and the deleteIfEmpty config option is selected.
    if (repeater.getSize() == 0 && this.deleteIfEmpty) {
        // Delete all of the old data for this repeater.
        jctx.removeAll(this.repeaterPath);

        // Otherwise perform the normal save binding.
    } else {//from   ww w .  j a v a2  s. com

        // Narrow to the repeater context, creating the path if it did not exist.
        JXPathContext repeaterContext = jctx.getRelativeContext(jctx.createPath(this.repeaterPath));

        // Start by deleting all of the old row data.
        repeaterContext.removeAll(this.rowPath);

        // Verify that repeater is not empty and has an insert row binding.
        if (repeater.getSize() > 0) {
            if (this.insertRowBinding != null) {

                //register the factory!
                //this.insertRowBinding.saveFormToModel(repeater, repeaterContext);

                // Iterate through the repeater rows.
                for (int i = 0; i < repeater.getSize(); i++) {

                    // Narrow to the repeater row context.
                    Pointer rowPointer = repeaterContext.getPointer(this.rowPathInsert);
                    JXPathContext rowContext = repeaterContext.getRelativeContext(rowPointer);

                    // Variables used for virtual rows.
                    // They are initialized here just to keep the compiler happy. 
                    Node rowNode = null;
                    Node virtualNode = null;

                    // If virtual rows are requested, create a temporary node and
                    // narrow the context to this initially empty new virtual row.
                    if (virtualRows == true) {
                        rowNode = (Node) rowContext.getContextBean();
                        Document document = rowNode.getOwnerDocument();
                        virtualNode = document.createElementNS(null, "virtual");
                        Node fakeDocElement = document.getDocumentElement().cloneNode(false);
                        fakeDocElement.appendChild(virtualNode);
                        rowContext = JXPathContext.newContext(repeaterContext, fakeDocElement);
                        rowContext = rowContext.getRelativeContext(rowContext.getPointer("virtual"));
                    }

                    // Perform the insert row binding
                    this.insertRowBinding.saveFormToModel(repeater, rowContext);

                    // Perform the save row binding.
                    this.rowBinding.saveFormToModel(repeater.getRow(i), rowContext);

                    // If virtual rows are requested, finish by appending the
                    // children of the virtual row to the real context node.
                    if (virtualRows == true) {
                        NodeList list = virtualNode.getChildNodes();
                        int count = list.getLength();
                        for (int j = 0; j < count; j++) {
                            // The list shrinks when a child is appended to the context
                            // node, so we always reference the first child in the list.
                            rowNode.appendChild(list.item(0));
                        }
                    }
                    getLogger().debug("bound new row");
                }
            } else {
                getLogger().warn("TempRepeaterBinding has detected rows to insert, "
                        + "but misses the <on-insert-row> binding to do it.");
            }
        }
    }
}

From source file:org.apache.cocoon.util.jxpath.DOMFactory.java

private void addDOMElement(Node parent, int index, String tag) {
    int pos = tag.indexOf(':');
    String prefix = null;/*from www .ja va 2s  .c  o  m*/
    if (pos != -1) {
        prefix = tag.substring(0, pos);
    }
    String uri = null;

    Node child = parent.getFirstChild();
    int count = 0;
    while (child != null) {
        if (child.getNodeName().equals(tag)) {
            count++;
        }
        child = child.getNextSibling();
    }

    Document doc = parent.getOwnerDocument();

    if (doc != null) {
        uri = getNamespaceURI((Element) parent, prefix);
    } else {
        if (parent instanceof Document) {
            doc = (Document) parent;
            if (prefix != null) {
                throw new RuntimeException("Cannot map non-null prefix " + "when creating a document element");
            }
        } else { // Shouldn't happen (must be a DocumentType object)
            throw new RuntimeException("Node of class " + parent.getClass().getName()
                    + " has null owner document " + "but is not a Document");
        }

    }

    // Keep inserting new elements until we have index + 1 of them
    while (count <= index) {
        Node newElement = doc.createElementNS(uri, tag);
        parent.appendChild(newElement);
        count++;
    }
}

From source file:org.apache.cocoon.woody.binding.TempRepeaterJXPathBinding.java

public void doLoad(Widget frmModel, JXPathContext jctx) throws BindingException {
    // (There should be a general widget type checker for all the bindings to use,
    // coupled with a general informative exception class to throw if the widget is
    // of the wrong type or null.)
    Repeater repeater = (Repeater) frmModel.getWidget(this.repeaterId);
    if (repeater == null) {
        String fullId = frmModel.getFullyQualifiedId();
        if (fullId == null || fullId.length() == 0) {
            fullId = "";
        } else {//from w  w  w . j ava2s  .  com
            fullId = fullId + ".";
        }
        throw new RuntimeException("TempRepeaterJXPathBinding: Repeater \"" + fullId + this.repeaterId
                + "\" does not exist (" + frmModel.getLocation() + ")");
    }

    // Start by clearing the repeater, if necessary.
    if (this.clearOnLoad) {
        repeater.removeRows();
    }

    // Find the location of the repeater data.
    Pointer repeaterPointer = jctx.getPointer(this.repeaterPath);

    // Check if there is data present.
    //
    // (Otherwise, should we check the leniency config option
    // to decide whether to be silent or throw an exception?) 
    if (repeaterPointer != null) {

        // Narrow to repeater context.
        JXPathContext repeaterContext = jctx.getRelativeContext(repeaterPointer);

        // Build a jxpath iterator for the repeater row pointers.
        Iterator rowPointers = repeaterContext.iteratePointers(this.rowPath);

        // Iterate through the rows of data.
        int rowNum = 0;
        while (rowPointers.hasNext()) {

            // Get or create a row widget.
            Repeater.RepeaterRow thisRow;
            if (repeater.getSize() > rowNum) {
                thisRow = repeater.getRow(rowNum);
            } else {
                thisRow = repeater.addRow();
            }
            rowNum++;

            // Narrow to the row context.
            Pointer rowPointer = (Pointer) rowPointers.next();
            JXPathContext rowContext = repeaterContext.getRelativeContext(rowPointer);

            // If virtual rows are requested, place a deep clone of the row data
            // into a temporary node, and narrow the context to this virtual row.
            //
            // (A clone of the data is used to prevent modifying the source document.
            // Otherwise, the appendChild method would remove the data from the source
            // document.  Is this protection worth the penalty of a deep clone?)
            //
            // (This implementation of virtual rows currently only supports DOM
            // bindings, but could easily be extended to support other bindings.)

            if (virtualRows == true) {
                Node repeaterNode = (Node) repeaterPointer.getNode();
                Node virtualNode = repeaterNode.getOwnerDocument().createElementNS(null, "virtual");
                Node clone = ((Node) rowPointer.getNode()).cloneNode(true);
                virtualNode.appendChild(clone);
                rowContext = JXPathContext.newContext(repeaterContext, virtualNode);
            }

            // Finally, perform the load row binding.
            this.rowBinding.loadFormFromModel(thisRow, rowContext);
        }
    }

    if (getLogger().isDebugEnabled())
        getLogger().debug("done loading rows " + toString());
}

From source file:org.apache.cocoon.woody.binding.TempRepeaterJXPathBinding.java

public void doSave(Widget frmModel, JXPathContext jctx) throws BindingException {
    // (See comment in doLoad about type checking and throwing a meaningful exception.)
    Repeater repeater = (Repeater) frmModel.getWidget(this.repeaterId);

    // Perform shortcut binding if the repeater is empty
    // and the deleteIfEmpty config option is selected.
    if (repeater.getSize() == 0 && this.deleteIfEmpty) {
        // Delete all of the old data for this repeater.
        jctx.removeAll(this.repeaterPath);

        // Otherwise perform the normal save binding.
    } else {/*w  w  w .  j  av  a  2s.c  o m*/

        // Narrow to the repeater context, creating the path if it did not exist.
        JXPathContext repeaterContext = jctx.getRelativeContext(jctx.createPath(this.repeaterPath));

        // Start by deleting all of the old row data.
        repeaterContext.removeAll(this.rowPath);

        // Verify that repeater is not empty and has an insert row binding.
        if (repeater.getSize() > 0) {
            if (this.insertRowBinding != null) {

                //register the factory!
                //this.insertRowBinding.saveFormToModel(repeater, repeaterContext);

                // Iterate through the repeater rows.
                for (int i = 0; i < repeater.getSize(); i++) {

                    // Narrow to the repeater row context.
                    Pointer rowPointer = repeaterContext.getPointer(this.rowPathInsert);
                    JXPathContext rowContext = repeaterContext.getRelativeContext(rowPointer);

                    // Variables used for virtual rows.
                    // They are initialized here just to keep the compiler happy. 
                    Node rowNode = null;
                    Node virtualNode = null;

                    // If virtual rows are requested, create a temporary node and
                    // narrow the context to this initially empty new virtual row.
                    if (virtualRows == true) {
                        rowNode = (Node) rowContext.getContextBean();
                        virtualNode = rowNode.getOwnerDocument().createElementNS(null, "virtual");
                        rowContext = JXPathContext.newContext(repeaterContext, virtualNode);
                    }

                    // Perform the insert row binding
                    this.insertRowBinding.saveFormToModel(repeater, rowContext);

                    // Perform the save row binding.
                    this.rowBinding.saveFormToModel(repeater.getRow(i), rowContext);

                    // If virtual rows are requested, finish by appending the
                    // children of the virtual row to the real context node.
                    if (virtualRows == true) {
                        NodeList list = virtualNode.getChildNodes();
                        int count = list.getLength();
                        for (int j = 0; j < count; j++) {
                            // The list shrinks when a child is appended to the context
                            // node, so we always reference the first child in the list.
                            rowNode.appendChild(list.item(0));
                        }
                    }
                    getLogger().debug("bound new row");
                }
            } else {
                getLogger().warn("TempRepeaterBinding has detected rows to insert, "
                        + "but misses the <on-insert-row> binding to do it.");
            }
        }
    }
}

From source file:org.apache.cocoon.xml.dom.DOMUtil.java

/**
 * Get the owner of the DOM document belonging to the node.
 * This works even if the node is the document itself.
 *
 * @param node The node.//from w w  w  .  j a  va  2  s  .c om
 * @return     The corresponding document.
 */
public static Document getOwnerDocument(Node node) {
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        return (Document) node;
    } else {
        return node.getOwnerDocument();
    }
}

From source file:org.apache.cocoon.xml.dom.DOMUtil.java

/**
 * Set the value of the DOM node.//from   ww w .ja  va 2  s .c  o  m
 * All current children of the node are removed and a new text node
 * with the value is appended.
 */
public static void setValueOfNode(Node node, String value) {
    if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
        node.setNodeValue(value);
    } else {
        while (node.hasChildNodes() == true) {
            node.removeChild(node.getFirstChild());
        }
        node.appendChild(node.getOwnerDocument().createTextNode(value));
    }
}