Example usage for org.w3c.dom Node isSameNode

List of usage examples for org.w3c.dom Node isSameNode

Introduction

In this page you can find the example usage for org.w3c.dom Node isSameNode.

Prototype

public boolean isSameNode(Node other);

Source Link

Document

Returns whether this node is the same node as the given one.

Usage

From source file:edu.virginia.speclab.juxta.author.model.JuxtaXMLParser.java

private static String nodeToSimpleXPath(Node node, Node root) {
    // recursion ahoy
    if (node == null)
        return "";

    // need to get my index by looping through previous siblings for nodes with the same name as me
    Node sibling = node.getPreviousSibling();
    int index = 1;
    while (sibling != null) {
        if (sibling.getNodeType() == node.getNodeType() && sibling.getNodeName().equals(node.getNodeName()))
            index++;/*from  w ww .j ava2  s  .  c  o  m*/
        sibling = sibling.getPreviousSibling();
    }

    String path = "/" + node.getNodeName();
    if (!node.isSameNode(root))
        path = path + "[" + index + "]";
    else
        return path;

    // If we've picked anything other than an element node, ignore it, and walk up
    // the tree for other things. We get this when someone asks for /blah/blah/text() or something,
    // which we don't support.
    if (node.getNodeType() != Node.ELEMENT_NODE)
        path = "";

    return (nodeToSimpleXPath(node.getParentNode(), root) + path);
}

From source file:org.structr.web.entity.dom.DOMNode.java

protected void checkHierarchy(Node otherNode) throws DOMException {

    // we can only check DOMNodes
    if (otherNode instanceof DOMNode) {

        // verify that the other node is not this node
        if (isSameNode(otherNode)) {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, HIERARCHY_REQUEST_ERR_MESSAGE_SAME_NODE);
        }//from  w w  w . j a  v  a 2  s. c o  m

        // verify that otherNode is not one of the
        // the ancestors of this node
        // (prevent circular relationships)
        Node _parent = getParentNode();
        while (_parent != null) {

            if (_parent.isSameNode(otherNode)) {
                throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
                        HIERARCHY_REQUEST_ERR_MESSAGE_ANCESTOR);
            }

            _parent = _parent.getParentNode();
        }

        // TODO: check hierarchy constraints imposed by the schema
        // validation sucessful
        return;
    }

    throw new DOMException(DOMException.NOT_SUPPORTED_ERR, NOT_SUPPORTED_ERR_MESSAGE);
}

From source file:com.vmware.identity.sts.ws.SignatureValidator.java

/**
 * Validate references present in the XmlSignature.
 * @param xmlSignature the xml signature whose references are to be validated. not null.
 * @param valContext validation context used to validate the signature itself. not null.
 * @param document document the signature belongs to. not null.
 * @param timestampNode the timestamp node of the soap security header within the document.
 * @throws XMLSignatureException when the validation fails.
 *///from   w ww . j  a  v  a 2  s . c  om
private void validateSignatureReferences(XMLSignature xmlSignature, DOMValidateContext valContext,
        Document document, Node timestampNode) throws XMLSignatureException {

    assert xmlSignature != null;
    assert valContext != null;
    assert document != null;
    assert timestampNode != null;

    //    If a signature is applied to a request then it must include:
    //    Either the <S11:Body>, or the WS-Trust element as a direct child of the <S11:Body>
    //    The <wsu:Timestamp>, if present in the <S11:Header>. 
    //        (in fact this must be present as per same spec, and SOAPHeaderExtractor validates it)

    Node soapBody = getSoapBody(document);
    Node wsTrustNode = getWsTrustNode(soapBody);
    boolean foundTimestampElement = false;
    boolean foundBodyOrWSTrustElement = false;

    List<Reference> references = xmlSignature.getSignedInfo().getReferences();
    if ((references == null) || (references.size() == 0)) {
        throw new XMLSignatureException("Signature's SignInfo does not contain any references.");
    }

    for (Reference reference : references) {

        if (reference != null) {
            validateReferenceTransforms(reference);
            validateReferenceUri(reference);
            // note: order is important, we should not try to validate digests
            // before we checked expected transforms, and uri etc.
            if (!reference.validate(valContext)) {
                throw new XMLSignatureException(
                        String.format("Signature reference '%s' is invalid.", reference.getURI()));
            }

            if (!foundTimestampElement || !foundBodyOrWSTrustElement) {
                String id = org.jcp.xml.dsig.internal.dom.Utils.parseIdFromSameDocumentURI(reference.getURI());
                Node referencedNode = document.getElementById(id);
                foundTimestampElement = (foundTimestampElement) || (timestampNode.isSameNode(referencedNode));
                foundBodyOrWSTrustElement = (foundBodyOrWSTrustElement) || (soapBody.isSameNode(referencedNode))
                        || (wsTrustNode.isSameNode(referencedNode));
            }
        }
    } // for each reference

    if (!foundTimestampElement || !foundBodyOrWSTrustElement) {
        throw new XMLSignatureException(
                "Signature must include <wsu:Timestamp> and either SoapBody, or the WSTrust element within it.");
    }
}

From source file:org.apache.ws.security.message.TestMessageTransformer.java

public static Element duplicateEncryptedDataInWsseWrapperHeader(Element saaj, boolean moveReferenceList) {
    if (moveReferenceList) {
        moveReferenceList(saaj);/* w  w w .  j  a  v  a  2  s. c  om*/
    }
    Element body = getFirstChildElement(saaj, new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body"),
            true);
    Element encData = getFirstChildElement(body,
            new QName("http://www.w3.org/2001/04/xmlenc#", "EncryptedData"), true);
    Element newEncData = createNewEncryptedData(encData);
    Element sh = getFirstChildElement(saaj, new QName("http://schemas.xmlsoap.org/soap/envelope/", "Header"),
            true);
    Element signature = getFirstChildElement(sh, new QName("http://www.w3.org/2000/09/xmldsig#", "Signature"),
            true);

    Node wsseHeader = signature.getParentNode();
    Node newWsseHeader = wsseHeader.cloneNode(false);
    Node cur = wsseHeader.getFirstChild();
    String newId = newEncData.getAttributeNS(null, "Id");
    while (!cur.isSameNode(signature)) {
        cur = copyHeadersAndUpdateRefList(cur, newWsseHeader, newId);
    }
    Element wrapper = encData.getOwnerDocument().createElementNS(null, "a");
    wrapper.appendChild(newEncData);
    newWsseHeader.appendChild(wrapper);
    while (cur != null) {
        cur = copyHeadersAndUpdateRefList(cur, newWsseHeader, newId);
    }

    if (!moveReferenceList) {
        updateEncryptedKeyRefList(newWsseHeader, newId);
    }

    Node parent = wsseHeader.getParentNode();
    parent.removeChild(wsseHeader);
    parent.appendChild(newWsseHeader);
    print(saaj.getOwnerDocument());
    return newEncData;
}