Example usage for org.w3c.dom Document getElementById

List of usage examples for org.w3c.dom Document getElementById

Introduction

In this page you can find the example usage for org.w3c.dom Document getElementById.

Prototype

public Element getElementById(String elementId);

Source Link

Document

Returns the Element that has an ID attribute with the given value.

Usage

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

public IUserLayoutNodeDescription addNode(IUserLayoutNodeDescription node, String parentId,
        String nextSiblingId) throws PortalException {
    boolean isChannel = false;
    IUserLayoutNodeDescription parent = this.getNode(parentId);
    if (canAddNode(node, parent, nextSiblingId)) {
        // assign new Id
        try {//w  w w.  ja  v  a2 s. c  om
            if (node instanceof IUserLayoutChannelDescription) {
                isChannel = true;
                node.setId(this.distributedLayoutStore.generateNewChannelSubscribeId(owner));
            } else {
                node.setId(this.distributedLayoutStore.generateNewFolderId(owner));
            }
        } catch (Exception e) {
            throw new PortalException("Exception encountered while "
                    + "generating new user layout node Id for  for " + owner.getAttribute(IPerson.USERNAME), e);
        }

        Document uld = getUserLayoutDOM();
        Element childElement = node.getXML(uld);
        Element parentElement = uld.getElementById(parentId);
        if (nextSiblingId == null) {
            parentElement.appendChild(childElement);
        } else {
            Node nextSibling = uld.getElementById(nextSiblingId);
            parentElement.insertBefore(childElement, nextSibling);
        }
        // register element id
        childElement.setIdAttribute(Constants.ATT_ID, true);
        childElement.setAttribute(Constants.ATT_ID, node.getId());
        this.updateCacheKey();

        // push into the user's real layout that gets persisted.
        HandlerUtils.createPlfNodeAndPath(childElement, isChannel, owner);

        // fire event
        final int layoutId = this.getLayoutId();
        if (isChannel) {
            this.channelsAdded = true;
            final String fname = ((IUserLayoutChannelDescription) node).getFunctionalName();
            this.portalEventFactory.publishPortletAddedToLayoutPortalEvent(this, this.owner, layoutId,
                    parent.getId(), fname);
        } else {
            this.portalEventFactory.publishFolderAddedToLayoutPortalEvent(this, this.owner, layoutId,
                    node.getId());
        }

        return node;
    }
    return null;
}

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

public boolean moveNode(String nodeId, String parentId, String nextSiblingId) throws PortalException {
    IUserLayoutNodeDescription parent = this.getNode(parentId);
    IUserLayoutNodeDescription node = this.getNode(nodeId);
    String oldParentNodeId = getParentId(nodeId);
    if (canMoveNode(node, parent, nextSiblingId)) {
        // must be a folder
        Document uld = this.getUserLayoutDOM();
        Element childElement = uld.getElementById(nodeId);
        Element parentElement = uld.getElementById(parentId);
        if (nextSiblingId == null) {
            parentElement.appendChild(childElement);
        } else {/*from w ww . ja  v a  2 s.co  m*/
            Node nextSibling = uld.getElementById(nextSiblingId);
            parentElement.insertBefore(childElement, nextSibling);
        }
        this.updateCacheKey();

        // propagate the change into the PLF
        Element oldParent = uld.getElementById(oldParentNodeId);
        TabColumnPrefsHandler.moveElement(childElement, oldParent, owner);
        // fire event
        final int layoutId = this.getLayoutId();
        if (node instanceof IUserLayoutChannelDescription) {
            this.channelsAdded = true;
            final String fname = ((IUserLayoutChannelDescription) node).getFunctionalName();
            this.portalEventFactory.publishPortletMovedInLayoutPortalEvent(this, this.owner, layoutId,
                    oldParentNodeId, parent.getId(), fname);
        } else {
            this.portalEventFactory.publishFolderMovedInLayoutPortalEvent(this, this.owner, layoutId,
                    oldParentNodeId, parent.getId());
        }
        return true;
    }
    return false;
}

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

public boolean deleteNode(String nodeId) throws PortalException {
    if (canDeleteNode(nodeId)) {
        IUserLayoutNodeDescription nodeDescription = this.getNode(nodeId);
        String parentNodeId = this.getParentId(nodeId);

        Document uld = this.getUserLayoutDOM();
        Element ilfNode = uld.getElementById(nodeId);
        Node parent = ilfNode.getParentNode();
        if (parent != null) {
            parent.removeChild(ilfNode);
        } else {/*from  www  . j  a  va  2s.  co  m*/
            throw new PortalException("Node \"" + nodeId + "\" has a NULL parent for layout of "
                    + owner.getAttribute(IPerson.USERNAME) + ".");
        }
        this.updateCacheKey();

        // now push into the PLF
        TabColumnPrefsHandler.deleteNode(ilfNode, (Element) parent, owner);
        // inform the listeners
        final int layoutId = this.getLayoutId();
        if (nodeDescription instanceof IUserLayoutChannelDescription) {
            final IUserLayoutChannelDescription userLayoutChannelDescription = (IUserLayoutChannelDescription) nodeDescription;
            this.portalEventFactory.publishPortletDeletedFromLayoutPortalEvent(this, this.owner, layoutId,
                    parentNodeId, userLayoutChannelDescription.getFunctionalName());
        } else {
            this.portalEventFactory.publishFolderDeletedFromLayoutPortalEvent(this, this.owner, layoutId,
                    parentNodeId, nodeDescription.getId(), nodeDescription.getName());
        }

        return true;
    }
    return false;
}

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 www.j ava2s.  c om
    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 getParentId(String nodeId) throws PortalException {
    Document uld = this.getUserLayoutDOM();
    Element nelement = uld.getElementById(nodeId);
    if (nelement != null) {
        Node parent = nelement.getParentNode();
        if (parent != null) {
            if (parent.getNodeType() != Node.ELEMENT_NODE) {
                throw new PortalException(
                        "Node with id=\"" + nodeId + "\" is attached to something other then an element node.");
            }/*from   w w w.j av  a  2 s.co m*/
            Element e = (Element) parent;
            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 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  a va 2 s .  c om*/
        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  .  j a  v  a 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.DistributedLayoutManager.java

private Enumeration<String> getChildIds(String nodeId, boolean visibleOnly) throws PortalException {
    Vector<String> v = new Vector<String>();
    IUserLayoutNodeDescription node = getNode(nodeId);
    if (node instanceof IUserLayoutFolderDescription) {
        Document uld = this.getUserLayoutDOM();
        Element felement = uld.getElementById(nodeId);
        for (Node n = felement.getFirstChild(); n != null; n = n.getNextSibling()) {
            if (n.getNodeType() == Node.ELEMENT_NODE && (visibleOnly == false || (visibleOnly == true
                    && ((Element) n).getAttribute(Constants.ATT_HIDDEN).equals("false")))) {
                Element e = (Element) n;
                if (e.getAttribute("ID") != null) {
                    v.add(e.getAttribute("ID"));
                }//from   w  ww  .j a va 2s . c  o m
            }
        }
    }
    return v.elements();
}

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.//from w ww  . ja va2  s. c om
 */
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);
}

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

/**
 * Attempt to apply a single channel parameter edit command and return true
 * if it succeeds or false otherwise. If the edit is disallowed or the
 * target element no longer exists in the document the edit command fails
 * and returns false.//from w ww  .  j  av a  2s . c  o  m
 * @throws Exception
 */
private static boolean applyEdit(Element edit, Document ilf) {
    String nodeID = edit.getAttribute(Constants.ATT_TARGET);

    Element channel = ilf.getElementById(nodeID);

    if (channel == null)
        return false;

    // now get the name of the parameter to be edited and find that element
    String parmName = edit.getAttribute(Constants.ATT_NAME);
    String parmValue = edit.getAttribute(Constants.ATT_USER_VALUE);
    NodeList ilfParms = channel.getChildNodes();
    Element targetParm = null;

    for (int i = 0; i < ilfParms.getLength(); i++) {
        Element ilfParm = (Element) ilfParms.item(i);
        if (ilfParm.getAttribute(Constants.ATT_NAME).equals(parmName)) {
            targetParm = ilfParm;
            break;
        }
    }
    if (targetParm == null) // parameter not found so we are free to set
    {
        Element parameter = ilf.createElement("parameter");
        parameter.setAttribute("name", parmName);
        parameter.setAttribute("value", parmValue);
        parameter.setAttribute("override", "yes");
        channel.appendChild(parameter);
        return true;
    }
    /* TODO Add support for fragments to set dlm:editAllowed attribute for
     * channel parameters. (2005.11.04 mboyd)
     * 
     * In the commented code below, the check for editAllowed will never be 
     * seen on a parameter element in the 
     * current database schema approach used by DLM. This is because 
     * parameters are second class citizens of the layout structure. They
     * are not found in the up_layout_struct table but only in the 
     * up_layout_param table. DLM functionality like dlm:editAllowed,
     * dlm:moveAllowed, dlm:deleteAllowed, and dlm:addChildAllowed were 
     * implemented without schema changes by adding these as parameters to
     * structural elements and upon loading any parameter that begins with
     * 'dlm:' is placed as an attribute on the containing structural 
     * element. So any channel parameter entry with dlm:editAllowed has that
     * value placed as an attribute on the containing channel not on the 
     * parameter that was meant to have it.
     * 
     * The only solution would be to add special dlm:parm children below
     * channels that would get the editAllowed value and then when creating
     * the DOM don't create those as child elements but use them to set the
     * attribute on the corresponding parameter by having the name of the
     * dlm:parm element be the name of the parameter to which it is to be 
     * related.
     * 
     * The result of this lack of functionality is that fragments can't 
     * mark any channel parameters as dlm:editAllowed='false' thereby
     * further restricting which channel parameters can be edited beyond 
     * what the channel definition specifies during publishing.  
     */
    //Attr editAllowed = targetParm.getAttributeNode( Constants.ATT_EDIT_ALLOWED );
    //if ( editAllowed != null && editAllowed.getNodeValue().equals("false"))
    //    return false;

    // target parm found. See if channel definition will still allow changes.

    Attr override = targetParm.getAttributeNode(Constants.ATT_OVERRIDE);
    if (override != null && !override.getNodeValue().equals(Constants.CAN_OVERRIDE))
        return false;

    // now see if the change is still needed
    if (targetParm.getAttribute(Constants.ATT_VALUE).equals(parmValue))
        return false; // user's edit same as fragment or chan def

    targetParm.setAttribute("value", parmValue);
    return true;
}