Example usage for org.w3c.dom Element getNextSibling

List of usage examples for org.w3c.dom Element getNextSibling

Introduction

In this page you can find the example usage for org.w3c.dom Element getNextSibling.

Prototype

public Node getNextSibling();

Source Link

Document

The node immediately following this node.

Usage

From source file:org.apache.tuscany.sca.implementation.bpel.ode.TuscanyProcessConfImpl.java

/**
 * Insert an initializer which supplies the value of an SCA property as specified by the
 * SCA Component using the BPEL process/*  w w w.jav  a  2s .co  m*/
 * @param bpelDOM - a DOM model representation of the BPEL process
 * @param property - an SCA ComponentProperty element for the property
 * This DOM model is updated, with an initializer being added for the BPEL variable
 * corresponding to the SCA property
 */
private void insertSCAPropertyInitializer(Document bpelDOM, ComponentProperty property) {
    // Only insert a Property initializer where there is a value for the Property
    if (property.getValue() == null)
        return;

    Element insertionElement = findInitializerInsertionPoint(bpelDOM);
    if (insertionElement == null)
        return;

    Element initializer = getInitializerSequence(bpelDOM, property);
    if (initializer == null)
        return;

    // Insert the initializer sequence as the next sibling element of the insertion point
    Element parent = (Element) insertionElement.getParentNode();
    // Get the next sibling element, if there is one
    Node sibling = insertionElement.getNextSibling();
    while (sibling != null && sibling.getNodeType() != Node.ELEMENT_NODE) {
        sibling = sibling.getNextSibling();
    } // end while
      // Either insert at the end or before the next element
    if (sibling == null) {
        parent.appendChild(initializer);
    } else {
        parent.insertBefore(initializer, sibling);
    } // end if

}

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

/**
 * Test for when the Signature element is modified
 *//* ww  w .  j a v a  2s. c o  m*/
@org.junit.Test
public void testModifiedSignature() throws Exception {
    WSSecSignature builder = new WSSecSignature();
    builder.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e", "security");
    Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
    WSSecHeader secHeader = new WSSecHeader();
    secHeader.insertSecurityHeader(doc);

    Document signedDoc = builder.build(doc, crypto, secHeader);

    // Modify the Signature element
    Element signatureElement = builder.getSignatureElement();
    Node firstChild = signatureElement.getFirstChild();
    while (!(firstChild instanceof Element) && firstChild != null) {
        firstChild = signatureElement.getNextSibling();
    }
    ((Element) firstChild).setAttributeNS(null, "Id", "xyz");

    if (LOG.isDebugEnabled()) {
        String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(signedDoc);
        LOG.debug(outputString);
    }

    try {
        verify(signedDoc);
        fail("Failure expected on a modified Signature element");
    } catch (WSSecurityException ex) {
        assertTrue(ex.getErrorCode() == 6);
        assertTrue(ex.getMessage().startsWith("The signature or decryption was invalid"));
    }
}

From source file:org.apache.xml.security.signature.Reference.java

/**
 * Build a {@link Reference} from an {@link Element}
 *
 * @param element <code>Reference</code> element
 * @param BaseURI the URI of the resource where the XML instance was stored
 * @param manifest is the {@link Manifest} of {@link SignedInfo} in which the Reference occurs.
 * We need this because the Manifest has the individual {@link ResourceResolver}s which have 
 * been set by the user/*w w  w.  j a  v a2s.c o  m*/
 * @throws XMLSecurityException
 */
protected Reference(Element element, String BaseURI, Manifest manifest) throws XMLSecurityException {
    super(element, BaseURI);
    this.baseURI = BaseURI;
    Element el = XMLUtils.getNextElement(element.getFirstChild());
    if (Constants._TAG_TRANSFORMS.equals(el.getLocalName())
            && Constants.SignatureSpecNS.equals(el.getNamespaceURI())) {
        transforms = new Transforms(el, this.baseURI);
        el = XMLUtils.getNextElement(el.getNextSibling());
    }
    digestMethodElem = el;
    digestValueElement = XMLUtils.getNextElement(digestMethodElem.getNextSibling());
    this.manifest = manifest;
}

From source file:org.apache.xml.security.signature.XMLSignature.java

/**
 * This will parse the element and construct the Java Objects.
 * That will allow a user to validate the signature.
 *
 * @param element ds:Signature element that contains the whole signature
 * @param BaseURI URI to be prepended to all relative URIs
 * @throws XMLSecurityException//  ww w  .  j  a v a 2  s . co m
 * @throws XMLSignatureException if the signature is badly formatted
 */
public XMLSignature(Element element, String BaseURI) throws XMLSignatureException, XMLSecurityException {
    super(element, BaseURI);

    // check out SignedInfo child
    Element signedInfoElem = XMLUtils.getNextElement(element.getFirstChild());

    // check to see if it is there
    if (signedInfoElem == null) {
        Object exArgs[] = { Constants._TAG_SIGNEDINFO, Constants._TAG_SIGNATURE };
        throw new XMLSignatureException("xml.WrongContent", exArgs);
    }

    // create a SignedInfo object from that element
    this.signedInfo = new SignedInfo(signedInfoElem, BaseURI);
    // get signedInfoElem again in case it has changed
    signedInfoElem = XMLUtils.getNextElement(element.getFirstChild());

    // check out SignatureValue child
    this.signatureValueElement = XMLUtils.getNextElement(signedInfoElem.getNextSibling());

    // check to see if it exists
    if (signatureValueElement == null) {
        Object exArgs[] = { Constants._TAG_SIGNATUREVALUE, Constants._TAG_SIGNATURE };
        throw new XMLSignatureException("xml.WrongContent", exArgs);
    }

    // <element ref="ds:KeyInfo" minOccurs="0"/>
    Element keyInfoElem = XMLUtils.getNextElement(signatureValueElement.getNextSibling());

    // If it exists use it, but it's not mandatory
    if (keyInfoElem != null && keyInfoElem.getNamespaceURI().equals(Constants.SignatureSpecNS)
            && keyInfoElem.getLocalName().equals(Constants._TAG_KEYINFO)) {
        this.keyInfo = new KeyInfo(keyInfoElem, BaseURI);
    }

    this.state = MODE_VERIFY;
}

From source file:org.apache.xml.security.test.encryption.EncryptContentTest.java

public void testContentRemoved() throws Exception {

    if (!haveISOPadding) {
        log.warn("Test testContentRemoved skipped as necessary algorithms not available");
        return;// w  ww. j a va2s  . c o  m
    }

    Document doc = db.parse(new ByteArrayInputStream(DATA.getBytes("UTF8")));
    NodeList dataToEncrypt = doc.getElementsByTagName("user");

    XMLCipher dataCipher = XMLCipher.getInstance(XMLCipher.TRIPLEDES);
    dataCipher.init(XMLCipher.ENCRYPT_MODE, secretKey);

    for (int i = 0; i < dataToEncrypt.getLength(); i++) {
        dataCipher.doFinal(doc, (Element) dataToEncrypt.item(i), true);
    }

    // Check that user content has been removed
    Element user = (Element) dataToEncrypt.item(0);
    Node child = user.getFirstChild();
    while (child != null && child.getNodeType() != Node.ELEMENT_NODE) {
        child = child.getNextSibling();
    }
    // child should be EncryptedData, if not throw exception
    Element childElem = (Element) child;
    if (!childElem.getLocalName().equals("EncryptedData")) {
        // t.transform(new DOMSource(doc), new StreamResult(System.out));
        throw new Exception("Element content not replaced");
    }
    // there shouldn't be any more children elements
    Node sibling = childElem.getNextSibling();
    while (sibling != null && sibling.getNodeType() != Node.ELEMENT_NODE) {
        sibling = sibling.getNextSibling();
    }
    if (sibling != null) {
        // t.transform(new DOMSource(doc), new StreamResult(System.out));
        throw new Exception("Sibling element content not replaced");
    }

    // t.transform(new DOMSource(doc), new StreamResult(System.out));
}

From source file:org.apereo.portal.layout.dlm.DeleteManager.java

/**
   This method does the actual work of adding a delete directive and then
   recursively calling itself for any incoporated children that need to be
   deleted as well./*from  w  w  w  . j av a 2 s  .  com*/
*/
private static void addDeleteDirective(Element compViewNode, String elementID, IPerson person, Document plf,
        Element delSet) throws PortalException {
    String ID = null;

    try {
        ID = getDLS().getNextStructDirectiveId(person);
    } catch (Exception e) {
        throw new PortalException("Exception encountered while " + "generating new delete node "
                + "Id for userId=" + person.getID(), e);
    }
    Element delete = plf.createElement(Constants.ELM_DELETE);
    delete.setAttribute(Constants.ATT_TYPE, Constants.ELM_DELETE);
    delete.setAttribute(Constants.ATT_ID, ID);
    delete.setAttributeNS(Constants.NS_URI, Constants.ATT_NAME, elementID);
    delSet.appendChild(delete);

    // now pass through children and add delete directives for those with
    // IDs indicating that they were incorporated
    Element child = (Element) compViewNode.getFirstChild();

    while (child != null) {
        String childID = child.getAttribute("ID");
        if (childID.startsWith(Constants.FRAGMENT_ID_USER_PREFIX))
            addDeleteDirective(child, childID, person, plf, delSet);
        child = (Element) child.getNextSibling();
    }
}

From source file:org.apereo.portal.layout.dlm.DistributedLayoutManager.java

protected boolean canMoveNode(IUserLayoutNodeDescription node, IUserLayoutNodeDescription parent,
        String nextSiblingId) throws PortalException {
    // todo if isFragmentOwner should probably verify both node and parent are part of the
    // same layout fragment as the fragment owner to insure a misbehaving front-end doesn't
    // do an improper operation.

    // are we moving to a new parent?
    if (!getParentId(node.getId()).equals(parent.getId()))
        return (isFragmentOwner || node.isMoveAllowed()) && canAddNode(node, parent, nextSiblingId);

    // same parent. which direction are we moving?
    Document uld = this.getUserLayoutDOM();
    Element parentE = uld.getElementById(parent.getId());
    Element child = (Element) parentE.getFirstChild();
    int idx = 0;/*from   w  w  w .j  a  v  a 2s.c o  m*/
    int nodeIdx = -1;
    int sibIdx = -1;

    while (child != null) {
        String id = child.getAttribute(Constants.ATT_ID);
        if (id.equals(node.getId()))
            nodeIdx = idx;
        if (id.equals(nextSiblingId))
            sibIdx = idx;
        idx++;
        child = (Element) child.getNextSibling();
    }
    if (nodeIdx == -1 || // couldn't find node
            (nextSiblingId != null && sibIdx == -1)) // couldn't find sibling
        return false;

    if (nodeIdx < sibIdx || // moving right
            sibIdx == -1) // appending to end
        return canMoveRight(node.getId(), nextSiblingId);
    return canMoveLeft(node.getId(), nextSiblingId);
}

From source file:org.apereo.portal.layout.dlm.DistributedLayoutManager.java

public String getNextSiblingId(String nodeId) throws PortalException {
    Document uld = this.getUserLayoutDOM();
    Element nelement = uld.getElementById(nodeId);
    if (nelement != null) {
        Node nsibling = nelement.getNextSibling();
        // scroll to the next element node
        while (nsibling != null && nsibling.getNodeType() != Node.ELEMENT_NODE) {
            nsibling = nsibling.getNextSibling();
        }// ww w .  j a va 2 s  .  c  o m
        if (nsibling != null) {
            Element e = (Element) nsibling;
            return e.getAttribute("ID");
        }
        return null;
    }
    throw new PortalException("Node with id=\"" + nodeId + "\" doesn't exist. Occurred " + "in layout for "
            + owner.getAttribute(IPerson.USERNAME) + ".");
}

From source file:org.apereo.portal.layout.dlm.EditManager.java

/**
   Create and append an edit directive to the edit set if not there.
*///from   w ww .j a  v  a  2  s .c om
private static void addDirective(Element plfNode, String attributeName, String type, IPerson person)
        throws PortalException {
    Document plf = (Document) person.getAttribute(Constants.PLF);
    Element editSet = getEditSet(plfNode, plf, person, true);

    // see if attributes has already been marked as being edited
    Element child = (Element) editSet.getFirstChild();
    Element edit = null;

    while (child != null && edit == null) {
        if (child.getNodeName().equals(type) && child.getAttribute(Constants.ATT_NAME).equals(attributeName))
            edit = child;
        child = (Element) child.getNextSibling();
    }
    if (edit == null) // if not found then newly mark as edited
    {
        String ID = null;

        try {
            ID = getDLS().getNextStructDirectiveId(person);
        } catch (Exception e) {
            throw new PortalException("Exception encountered while " + "generating new edit node "
                    + "Id for userId=" + person.getID(), e);
        }
        edit = plf.createElement(type);
        edit.setAttribute(Constants.ATT_TYPE, type);
        edit.setAttribute(Constants.ATT_ID, ID);
        edit.setAttribute(Constants.ATT_NAME, attributeName);
        editSet.appendChild(edit);
    }
}

From source file:org.apereo.portal.layout.dlm.EditManager.java

/**
   Evaluate whether attribute changes exist in the ilfChild and if so
   apply them. Returns true if some changes existed. If changes existed
   but matched those in the original node then they are not applicable,
   are removed from the editSet, and false is returned.
*///from ww  w  . j  a va  2s.  co m
public static boolean applyEditSet(Element plfChild, Element original) {
    // first get edit set if it exists
    Element editSet = null;
    try {
        editSet = getEditSet(plfChild, null, null, false);
    } catch (Exception e) {
        // should never occur unless problem during create in getEditSet
        // and we are telling it not to create.
        return false;
    }

    if (editSet == null || editSet.getChildNodes().getLength() == 0)
        return false;

    if (original.getAttribute(Constants.ATT_EDIT_ALLOWED).equals("false")) {
        // can't change anymore so discard changes
        plfChild.removeChild(editSet);
        return false;
    }

    Document ilf = original.getOwnerDocument();
    boolean attributeChanged = false;
    Element edit = (Element) editSet.getFirstChild();

    while (edit != null) {
        String attribName = edit.getAttribute(Constants.ATT_NAME);
        Attr attr = plfChild.getAttributeNode(attribName);

        // preferences are only updated at preference storage time so
        // if a preference change exists in the edit set assume it is
        // still valid so that the node being edited will persist in
        // the PLF.
        if (edit.getNodeName().equals(Constants.ELM_PREF))
            attributeChanged = true;
        else if (attr == null) {
            // attribute removed. See if needs removing in original.
            attr = original.getAttributeNode(attribName);
            if (attr == null) // edit irrelevant,
                editSet.removeChild(edit);
            else {
                // edit differs, apply to original
                original.removeAttribute(attribName);
                attributeChanged = true;
            }
        } else {
            // attribute there, see if original is also there
            Attr origAttr = original.getAttributeNode(attribName);
            if (origAttr == null) {
                // original attribute isn't defined so need to add
                origAttr = (Attr) ilf.importNode(attr, true);
                original.setAttributeNode(origAttr);
                attributeChanged = true;
            } else {
                // original attrib found, see if different
                if (attr.getValue().equals(origAttr.getValue())) {
                    // they are the same, edit irrelevant
                    editSet.removeChild(edit);
                } else {
                    // edit differs, apply to original
                    origAttr.setValue(attr.getValue());
                    attributeChanged = true;
                }
            }
        }
        edit = (Element) edit.getNextSibling();
    }
    return attributeChanged;
}