Example usage for org.w3c.dom Node getNextSibling

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

Introduction

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

Prototype

public Node getNextSibling();

Source Link

Document

The node immediately following this node.

Usage

From source file:org.apache.xml.security.utils.XMLUtils.java

/**
 * This is the work horse for {@link #circumventBug2650}.
 *
 * @param node/*w w  w  . j a v  a  2 s . c  om*/
 * @see <A HREF="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=2650">
 * Namespace axis resolution is not XPath compliant </A>
 */
private static void circumventBug2650internal(Node node) {
    Node parent = null;
    Node sibling = null;
    final String namespaceNs = Constants.NamespaceSpecNS;
    do {
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            Element element = (Element) node;
            if (!element.hasChildNodes()) {
                break;
            }
            if (element.hasAttributes()) {
                NamedNodeMap attributes = element.getAttributes();
                int attributesLength = attributes.getLength();

                for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {

                    if (child.getNodeType() != Node.ELEMENT_NODE) {
                        continue;
                    }
                    Element childElement = (Element) child;

                    for (int i = 0; i < attributesLength; i++) {
                        Attr currentAttr = (Attr) attributes.item(i);
                        if (!namespaceNs.equals(currentAttr.getNamespaceURI()))
                            continue;
                        if (childElement.hasAttributeNS(namespaceNs, currentAttr.getLocalName())) {
                            continue;
                        }
                        childElement.setAttributeNS(namespaceNs, currentAttr.getName(),
                                currentAttr.getNodeValue());
                    }
                }
            }
        case Node.ENTITY_REFERENCE_NODE:
            parent = node;
            sibling = node.getFirstChild();
            break;
        case Node.DOCUMENT_NODE:
            parent = node;
            sibling = node.getFirstChild();
            break;
        }
        while ((sibling == null) && (parent != null)) {
            sibling = parent.getNextSibling();
            parent = parent.getParentNode();
        }
        if (sibling == null) {
            return;
        }

        node = sibling;
        sibling = node.getNextSibling();
    } while (true);
}

From source file:org.apache.xml.security.utils.XMLUtils.java

/**
 * @param sibling// w w w .  j a  v  a 2 s  .  c om
 * @param nodeName
 * @param number
 * @return nodes with the constraint
 */
public static Element selectDsNode(Node sibling, String nodeName, int number) {
    while (sibling != null) {
        if (Constants.SignatureSpecNS.equals(sibling.getNamespaceURI())
                && sibling.getLocalName().equals(nodeName)) {
            if (number == 0) {
                return (Element) sibling;
            }
            number--;
        }
        sibling = sibling.getNextSibling();
    }
    return null;
}

From source file:org.apache.xml.security.utils.XMLUtils.java

/**
 * @param sibling/*ww w . ja  v  a2  s  .co  m*/
 * @param nodeName
 * @param number
 * @return nodes with the constrain
 */
public static Element selectXencNode(Node sibling, String nodeName, int number) {
    while (sibling != null) {
        if (EncryptionConstants.EncryptionSpecNS.equals(sibling.getNamespaceURI())
                && sibling.getLocalName().equals(nodeName)) {
            if (number == 0) {
                return (Element) sibling;
            }
            number--;
        }
        sibling = sibling.getNextSibling();
    }
    return null;
}

From source file:org.apache.xml.security.utils.XMLUtils.java

/**
 * @param sibling//  w  w w  .  j a va 2s. c om
 * @param nodeName
 * @param number
 * @return nodes with the constrain
 */
public static Text selectDsNodeText(Node sibling, String nodeName, int number) {
    Node n = selectDsNode(sibling, nodeName, number);
    if (n == null) {
        return null;
    }
    n = n.getFirstChild();
    while (n != null && n.getNodeType() != Node.TEXT_NODE) {
        n = n.getNextSibling();
    }
    return (Text) n;
}

From source file:org.apereo.portal.groups.ldap.LDAPGroupStore.java

protected void init(Document config) {
    this.groups = new HashMap();
    this.contexts = new SmartCache(120);
    config.normalize();/*from   ww w .j a v  a 2  s  .  co  m*/
    int refreshminutes = 120;
    Element root = config.getDocumentElement();
    NodeList nl = root.getElementsByTagName("config");
    if (nl.getLength() == 1) {
        Element conf = (Element) nl.item(0);
        Node cc = conf.getFirstChild();
        //NodeList cl= conf.getF.getChildNodes();
        //for(int i=0; i<cl.getLength(); i++){
        while (cc != null) {
            if (cc.getNodeType() == ELEMENT_NODE) {
                Element c = (Element) cc;
                c.normalize();
                Node t = c.getFirstChild();
                if (t != null && t.getNodeType() == Node.TEXT_NODE) {
                    String name = c.getNodeName();
                    String text = ((Text) t).getData();
                    //System.out.println(name+" = "+text);
                    if (name.equals("url")) {
                        url = text;
                    } else if (name.equals("logonid")) {
                        logonid = text;
                    } else if (name.equals("logonpassword")) {
                        logonpassword = text;
                    } else if (name.equals("keyfield")) {
                        keyfield = text;
                    } else if (name.equals("namefield")) {
                        namefield = text;
                    } else if (name.equals("usercontext")) {
                        usercontext = text;
                    } else if (name.equals("refresh-minutes")) {
                        try {
                            refreshminutes = Integer.parseInt(text);
                        } catch (Exception e) {
                        }
                    }
                }
            }
            cc = cc.getNextSibling();
        }
    } else {
        throw new RuntimeException("LDAPGroupStore: config file must contain one config element");
    }

    this.personkeys = new SmartCache(refreshminutes * 60);

    NodeList gl = root.getChildNodes();
    for (int j = 0; j < gl.getLength(); j++) {
        if (gl.item(j).getNodeType() == ELEMENT_NODE) {
            Element g = (Element) gl.item(j);
            if (g.getNodeName().equals("group")) {
                GroupShadow shadow = processXmlGroupRecursive(g);
                groups.put(shadow.key, shadow);
            }
        }
    }

}

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

/**
   Get the delete set if any stored in the root of the document or create
   it is passed in create flag is true.//from  ww w  .j a va2 s  . co  m
*/
private static Element getDeleteSet(Document plf, IPerson person, boolean create) throws PortalException {
    Node root = plf.getDocumentElement();
    Node child = root.getFirstChild();

    while (child != null) {
        if (child.getNodeName().equals(Constants.ELM_DELETE_SET))
            return (Element) child;
        child = child.getNextSibling();
    }

    if (create == false)
        return null;

    String ID = null;

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

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();
        }//from   w ww  .  j  ava 2 s. com
        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.DistributedLayoutManager.java

public String getPreviousSiblingId(String nodeId) throws PortalException {
    Document uld = this.getUserLayoutDOM();
    Element nelement = uld.getElementById(nodeId);
    if (nelement != null) {
        Node nsibling = nelement.getPreviousSibling();
        // scroll to the next element node
        while (nsibling != null && nsibling.getNodeType() != Node.ELEMENT_NODE) {
            nsibling = nsibling.getNextSibling();
        }/*from   ww  w .ja va  2s.  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

/**
   Get the edit set if any stored in the passed in node. If not found and
   if the create flag is true then create a new edit set and add it as a
   child to the passed in node. Then return it.
*///from   ww  w .j  av a2 s .co  m
private static Element getEditSet(Element node, Document plf, IPerson person, boolean create)
        throws PortalException {
    Node child = node.getFirstChild();

    while (child != null) {
        if (child.getNodeName().equals(Constants.ELM_EDIT_SET))
            return (Element) child;
        child = child.getNextSibling();
    }

    if (create == false)
        return null;

    String ID = null;

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

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

/**
 * Searches for a command of the passed-in type and if found removes it from
 * the user's PLF./*w  ww .  jav  a2 s  .  c o m*/
 */
private static void removeDirective(String elementId, String attributeName, String type, IPerson person) {
    Document plf = (Document) person.getAttribute(Constants.PLF);
    Element node = plf.getElementById(elementId);
    if (node == null)
        return;

    Element editSet = null;

    try {
        editSet = getEditSet(node, plf, person, false);
    } catch (Exception e) {
        /*
         * we should never get here since we are calling getEditSet passing
         * create=false meaning that the only portion of that method that
         * tosses an exception will not be reached with this call. But if a
         * runtime exception somehow occurs we will log it so that we don't
         * lose the information.
         */
        LOG.error(e, e);
        return;
    }

    // if no edit set then the edit can't be there either
    if (editSet == null)
        return;

    Node child = editSet.getFirstChild();

    while (child != null) {
        if (child.getNodeName().equals(type)) {
            Attr attr = ((Element) child).getAttributeNode(Constants.ATT_NAME);
            if (attr != null && attr.getValue().equals(attributeName)) {
                // we found it, remove it
                editSet.removeChild(child);
                break;
            }
        }
        child = child.getNextSibling();
    }
    // if that was the last on in the edit set then delete it
    if (editSet.getFirstChild() == null)
        node.removeChild(editSet);
}