Java Code Examples for org.w3c.dom.Node#getOwnerDocument()

The following examples show how to use org.w3c.dom.Node#getOwnerDocument() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
public void handle(Callback[] callbacks)
    throws IOException, UnsupportedCallbackException {
    for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] instanceof DelegationCallback) {
            DelegationCallback callback = (DelegationCallback) callbacks[i];
            Message message = callback.getCurrentMessage();
            
            String username = 
                (String)message.getContextualProperty(SecurityConstants.USERNAME);
            if (username != null) {
                Node contentNode = message.getContent(Node.class);
                Document doc = null;
                if (contentNode != null) {
                    doc = contentNode.getOwnerDocument();
                } else {
                    doc = DOMUtils.createDocument();
                }
                UsernameToken usernameToken = createWSSEUsernameToken(username, doc);
                callback.setToken(usernameToken.getElement());
            }
        } else {
            throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
        }
    }
}
 
Example 2
Source Project: Bytecoder   File: SAX2DOM.java    License: Apache License 2.0 6 votes vote down vote up
public SAX2DOM(Node root, Node nextSibling, boolean overrideDefaultParser)
        throws ParserConfigurationException {
    _root = root;
    if (root instanceof Document) {
      _document = (Document)root;
    }
    else if (root != null) {
      _document = root.getOwnerDocument();
    }
    else {
      _document = createDocument(overrideDefaultParser);
      _root = _document;
    }

    _nextSibling = nextSibling;
}
 
Example 3
/**
 * Checks whether a Comment or ProcessingInstruction is before or after the
 * document element. This is needed for prepending or appending "\n"s.
 *
 * @param currentNode
 *            comment or pi to check
 * @return NODE_BEFORE_DOCUMENT_ELEMENT,
 *         NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT or
 *         NODE_AFTER_DOCUMENT_ELEMENT
 * @see #NODE_BEFORE_DOCUMENT_ELEMENT
 * @see #NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT
 * @see #NODE_AFTER_DOCUMENT_ELEMENT
 */
private int getPositionRelativeToDocumentElement(Node currentNode) {
    if (currentNode == null) {
        return NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
    }

    Document doc = currentNode.getOwnerDocument();

    if (currentNode.getParentNode() != doc) {
        return NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
    }

    Element documentElement = doc.getDocumentElement();

    if (documentElement == null) {
        return NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
    }

    if (documentElement == currentNode) {
        return NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
    }

    for (Node x = currentNode; x != null; x = x.getNextSibling()) {
        if (x == documentElement) {
            return NODE_BEFORE_DOCUMENT_ELEMENT;
        }
    }

    return NODE_AFTER_DOCUMENT_ELEMENT;
}
 
Example 4
Source Project: jdk1.8-source-analysis   File: SAX2DOM.java    License: Apache License 2.0 5 votes vote down vote up
public SAX2DOM(Node root, Node nextSibling, boolean useServicesMechanism) throws ParserConfigurationException {
    _root = root;
    if (root instanceof Document) {
      _document = (Document)root;
    }
    else if (root != null) {
      _document = root.getOwnerDocument();
    }
    else {
      _document = createDocument(useServicesMechanism);
      _root = _document;
    }

    _nextSibling = nextSibling;
}
 
Example 5
Source Project: Bytecoder   File: CoreDocumentImpl.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Since a Document may contain at most one top-level Element child,
 * and at most one DocumentType declaraction, we need to subclass our
 * add-children methods to implement this constraint.
 * Since appendChild() is implemented as insertBefore(,null),
 * altering the latter fixes both.
 * <p>
 * While I'm doing so, I've taken advantage of the opportunity to
 * cache documentElement and docType so we don't have to
 * search for them.
 *
 * REVISIT: According to the spec it is not allowed to alter neither the
 * document element nor the document type in any way
 */
public Node insertBefore(Node newChild, Node refChild)
        throws DOMException {

    // Only one such child permitted
    int type = newChild.getNodeType();
    if (errorChecking) {
        if((type == Node.ELEMENT_NODE && docElement != null) ||
        (type == Node.DOCUMENT_TYPE_NODE && docType != null)) {
            String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null);
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, msg);
        }
    }
    // Adopt orphan doctypes
    if (newChild.getOwnerDocument() == null &&
    newChild instanceof DocumentTypeImpl) {
        ((DocumentTypeImpl) newChild).ownerDocument = this;
    }
    super.insertBefore(newChild,refChild);

    // If insert succeeded, cache the kid appropriately
    if (type == Node.ELEMENT_NODE) {
        docElement = (ElementImpl)newChild;
    }
    else if (type == Node.DOCUMENT_TYPE_NODE) {
        docType = (DocumentTypeImpl)newChild;
    }

    return newChild;

}
 
Example 6
public void setStartBefore(Node refNode)
    throws RangeException
{
    if (fDocument.errorChecking) {
        if (fDetach) {
            throw new DOMException(
                    DOMException.INVALID_STATE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
        }
        if ( !hasLegalRootContainer(refNode) ||
                !isLegalContainedNode(refNode) )
        {
            throw new RangeExceptionImpl(
                    RangeException.INVALID_NODE_TYPE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_NODE_TYPE_ERR", null));
        }
        if ( fDocument != refNode.getOwnerDocument() && fDocument != refNode) {
            throw new DOMException(
                    DOMException.WRONG_DOCUMENT_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null));
        }
    }

    fStartContainer = refNode.getParentNode();
    int i = 0;
    for (Node n = refNode; n!=null; n = n.getPreviousSibling()) {
        i++;
    }
    fStartOffset = i-1;

    // If one boundary-point of a Range is set to have a root container
    // other
    // than the current one for the Range, the Range should be collapsed to
    // the new position.
    // The start position of a Range should never be after the end position.
    if (getCommonAncestorContainer() == null
            || (fStartContainer == fEndContainer && fEndOffset < fStartOffset)) {
        collapse(true);
    }
}
 
Example 7
private String _getInputEncoding(Node node) {
    Document doc = (node.getNodeType() == Node.DOCUMENT_NODE)
            ? (Document) node : node.getOwnerDocument();
    if (doc != null) {
        try {
            return doc.getInputEncoding();
        } // The VM ran out of memory or there was some other serious problem. Re-throw.
        catch (VirtualMachineError | ThreadDeath vme) {
            throw vme;
        } // Ignore all other exceptions and errors
        catch (Throwable t) {
        }
    }
    return null;
}
 
Example 8
/**
 * Since a Document may contain at most one top-level Element child,
 * and at most one DocumentType declaraction, we need to subclass our
 * add-children methods to implement this constraint.
 * Since appendChild() is implemented as insertBefore(,null),
 * altering the latter fixes both.
 * <p>
 * While I'm doing so, I've taken advantage of the opportunity to
 * cache documentElement and docType so we don't have to
 * search for them.
 *
 * REVISIT: According to the spec it is not allowed to alter neither the
 * document element nor the document type in any way
 */
public Node insertBefore(Node newChild, Node refChild)
        throws DOMException {

    // Only one such child permitted
    int type = newChild.getNodeType();
    if (errorChecking) {
        if((type == Node.ELEMENT_NODE && docElement != null) ||
        (type == Node.DOCUMENT_TYPE_NODE && docType != null)) {
            String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null);
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, msg);
        }
    }
    // Adopt orphan doctypes
    if (newChild.getOwnerDocument() == null &&
    newChild instanceof DocumentTypeImpl) {
        ((DocumentTypeImpl) newChild).ownerDocument = this;
    }
    super.insertBefore(newChild,refChild);

    // If insert succeeded, cache the kid appropriately
    if (type == Node.ELEMENT_NODE) {
        docElement = (ElementImpl)newChild;
    }
    else if (type == Node.DOCUMENT_TYPE_NODE) {
        docType = (DocumentTypeImpl)newChild;
    }

    return newChild;

}
 
Example 9
public void setStartAfter(Node refNode)
    throws RangeException
{
    if (fDocument.errorChecking) {
        if (fDetach) {
            throw new DOMException(
                    DOMException.INVALID_STATE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
        }
        if ( !hasLegalRootContainer(refNode) ||
                !isLegalContainedNode(refNode)) {
            throw new RangeExceptionImpl(
                    RangeException.INVALID_NODE_TYPE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_NODE_TYPE_ERR", null));
        }
        if ( fDocument != refNode.getOwnerDocument() && fDocument != refNode) {
            throw new DOMException(
                    DOMException.WRONG_DOCUMENT_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null));
        }
    }
    fStartContainer = refNode.getParentNode();
    int i = 0;
    for (Node n = refNode; n!=null; n = n.getPreviousSibling()) {
        i++;
    }
    fStartOffset = i;

    // If one boundary-point of a Range is set to have a root container
    // other
    // than the current one for the Range, the Range should be collapsed to
    // the new position.
    // The start position of a Range should never be after the end position.
    if (getCommonAncestorContainer() == null
            || (fStartContainer == fEndContainer && fEndOffset < fStartOffset)) {
        collapse(true);
    }
}
 
Example 10
Source Project: Bytecoder   File: DOMSerializerImpl.java    License: Apache License 2.0 5 votes vote down vote up
private String _getXmlEncoding(Node node) {
    Document doc = (node.getNodeType() == Node.DOCUMENT_NODE)
            ? (Document) node : node.getOwnerDocument();
    if (doc != null) {
        try {
            return doc.getXmlEncoding();
        } // The VM ran out of memory or there was some other serious problem. Re-throw.
        catch (VirtualMachineError | ThreadDeath vme) {
            throw vme;
        } // Ignore all other exceptions and errors
        catch (Throwable t) {
        }
    }
    return null;
}
 
Example 11
Source Project: hottub   File: RangeImpl.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void setStartBefore(Node refNode)
    throws RangeException
{
    if (fDocument.errorChecking) {
        if (fDetach) {
            throw new DOMException(
                    DOMException.INVALID_STATE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
        }
        if ( !hasLegalRootContainer(refNode) ||
                !isLegalContainedNode(refNode) )
        {
            throw new RangeExceptionImpl(
                    RangeException.INVALID_NODE_TYPE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_NODE_TYPE_ERR", null));
        }
        if ( fDocument != refNode.getOwnerDocument() && fDocument != refNode) {
            throw new DOMException(
                    DOMException.WRONG_DOCUMENT_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null));
        }
    }

    fStartContainer = refNode.getParentNode();
    int i = 0;
    for (Node n = refNode; n!=null; n = n.getPreviousSibling()) {
        i++;
    }
    fStartOffset = i-1;

    // If one boundary-point of a Range is set to have a root container
    // other
    // than the current one for the Range, the Range should be collapsed to
    // the new position.
    // The start position of a Range should never be after the end position.
    if (getCommonAncestorContainer() == null
            || (fStartContainer == fEndContainer && fEndOffset < fStartOffset)) {
        collapse(true);
    }
}
 
Example 12
/**
 * Checks if child element has same owner document before
 * appending to the parent, and imports it to the parent's document
 * if necessary.
 */
public static void appendChild(Node parent, Node child) {
    Document ownerDoc = getOwnerDocument(parent);
    if (child.getOwnerDocument() != ownerDoc) {
        parent.appendChild(ownerDoc.importNode(child, true));
    } else {
        parent.appendChild(child);
    }
}
 
Example 13
Source Project: Bytecoder   File: DOMSerializerImpl.java    License: Apache License 2.0 5 votes vote down vote up
private String _getInputEncoding(Node node) {
    Document doc = (node.getNodeType() == Node.DOCUMENT_NODE)
            ? (Document) node : node.getOwnerDocument();
    if (doc != null) {
        try {
            return doc.getInputEncoding();
        } // The VM ran out of memory or there was some other serious problem. Re-throw.
        catch (VirtualMachineError | ThreadDeath vme) {
            throw vme;
        } // Ignore all other exceptions and errors
        catch (Throwable t) {
        }
    }
    return null;
}
 
Example 14
public void selectNodeContents(Node refNode)
    throws RangeException
{
    if (fDocument.errorChecking) {
        if( fDetach) {
            throw new DOMException(
                    DOMException.INVALID_STATE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
        }
        if ( !isLegalContainer(refNode)) {
            throw new RangeExceptionImpl(
                    RangeException.INVALID_NODE_TYPE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_NODE_TYPE_ERR", null));
        }
        if ( fDocument != refNode.getOwnerDocument() && fDocument != refNode) {
            throw new DOMException(
                    DOMException.WRONG_DOCUMENT_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null));
        }
    }
    fStartContainer = refNode;
    fEndContainer = refNode;
    Node first = refNode.getFirstChild();
    fStartOffset = 0;
    if (first == null) {
        fEndOffset = 0;
    } else {
        int i = 0;
        for (Node n = first; n!=null; n = n.getNextSibling()) {
            i++;
        }
        fEndOffset = i;
    }

}
 
Example 15
/**
 * Since we cache the docElement (and, currently, docType),
 * replaceChild has to update the cache
 *
 * REVISIT: According to the spec it is not allowed to alter neither the
 * document element nor the document type in any way
 */
public Node replaceChild(Node newChild, Node oldChild)
throws DOMException {

    // Adopt orphan doctypes
    if (newChild.getOwnerDocument() == null &&
    newChild instanceof DocumentTypeImpl) {
        ((DocumentTypeImpl) newChild).ownerDocument = this;
    }

    if (errorChecking &&((docType != null &&
        oldChild.getNodeType() != Node.DOCUMENT_TYPE_NODE &&
        newChild.getNodeType() == Node.DOCUMENT_TYPE_NODE)
        || (docElement != null &&
        oldChild.getNodeType() != Node.ELEMENT_NODE &&
        newChild.getNodeType() == Node.ELEMENT_NODE))) {

        throw new DOMException(
            DOMException.HIERARCHY_REQUEST_ERR,
            DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null));
    }
    super.replaceChild(newChild, oldChild);

    int type = oldChild.getNodeType();
    if(type == Node.ELEMENT_NODE) {
        docElement = (ElementImpl)newChild;
    }
    else if (type == Node.DOCUMENT_TYPE_NODE) {
        docType = (DocumentTypeImpl)newChild;
    }
    return oldChild;
}
 
Example 16
private static void appendXmlFragment(Node parent, String fragment) {
	Document doc = parent.getOwnerDocument();
	Comment comment = doc.createComment(fragment);
	parent.appendChild(comment);
}
 
Example 17
Source Project: cxf   File: StaxUtils.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @param parent
 */
private static Document getDocument(Node parent) {
    return (parent instanceof Document) ? (Document)parent : parent.getOwnerDocument();
}
 
Example 18
/**
 * Gets the owner document of a node.
 *
 * @param node the node
 * @return the node's document or the node itself if it is a document node
 *
 * @throws NullPointerException if {@code node} is {@code null}
 */
public static Document getOwnerDocument(Node node)
{
    if (node.getNodeType() == Node.DOCUMENT_NODE)
        return (Document)node;
    return node.getOwnerDocument();
}
 
Example 19
Source Project: JDKSourceCode1.8   File: CoreDocumentImpl.java    License: MIT License 3 votes vote down vote up
/**
 * DOM Level 3 WD - Experimental.
 * Save the document or the given node and all its descendants to a string
 * (i.e. serialize the document or node).
 * <br>The parameters used in the <code>LSSerializer</code> interface are
 * assumed to have their default values when invoking this method.
 * <br> The result of a call to this method is the same the result of a
 * call to <code>LSSerializer.writeToString</code> with the document as
 * the node to write.
 * @param node Specifies what to serialize, if this parameter is
 *   <code>null</code> the whole document is serialized, if it's
 *   non-null the given node is serialized.
 * @return The serialized document or <code>null</code> in case an error
 *   occurred.
 * @exception DOMException
 *   WRONG_DOCUMENT_ERR: Raised if the node passed in as the node
 *   parameter is from an other document.
 */
public String saveXML(Node node)
        throws DOMException {
    if (errorChecking && node != null
            && this != node.getOwnerDocument()) {
        String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null);
        throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, msg);
    }
    DOMImplementationLS domImplLS = (DOMImplementationLS) DOMImplementationImpl.getDOMImplementation();
    LSSerializer xmlWriter = domImplLS.createLSSerializer();
    if (node == null) {
        node = this;
    }
    return xmlWriter.writeToString(node);
}
 
Example 20
/**
 * DOM Level 3 WD - Experimental.
 * Save the document or the given node and all its descendants to a string
 * (i.e. serialize the document or node).
 * <br>The parameters used in the <code>LSSerializer</code> interface are
 * assumed to have their default values when invoking this method.
 * <br> The result of a call to this method is the same the result of a
 * call to <code>LSSerializer.writeToString</code> with the document as
 * the node to write.
 * @param node Specifies what to serialize, if this parameter is
 *   <code>null</code> the whole document is serialized, if it's
 *   non-null the given node is serialized.
 * @return The serialized document or <code>null</code> in case an error
 *   occurred.
 * @exception DOMException
 *   WRONG_DOCUMENT_ERR: Raised if the node passed in as the node
 *   parameter is from an other document.
 */
public String saveXML(Node node)
throws DOMException {
    if ( errorChecking && node != null &&
        this != node.getOwnerDocument() ) {
        String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null);
        throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, msg);
    }
    DOMImplementationLS domImplLS = (DOMImplementationLS)DOMImplementationImpl.getDOMImplementation();
    LSSerializer xmlWriter = domImplLS.createLSSerializer();
    if (node == null) {
        node = this;
    }
    return xmlWriter.writeToString(node);
}