Example usage for org.w3c.dom Element setIdAttribute

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

Introduction

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

Prototype

public void setIdAttribute(String name, boolean isId) throws DOMException;

Source Link

Document

If the parameter isId is true, this method declares the specified attribute to be a user-determined ID attribute .

Usage

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

/**
 * Recursive method that passes through a layout tree and changes all ids
 * from the regular format of sXX or nXX to the globally safe incorporated
 * id of form uXlXsXX or uXlXnXX indicating the user id and layout id from
 * which this node came.//from   w  ww. j  ava  2 s . co m
 */
private void setIdsAndAttribs(Element parent, String labelBase, String index, String precedence) {
    NodeList children = parent.getChildNodes();

    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
            Element child = (Element) children.item(i);
            String id = child.getAttribute(Constants.ATT_ID);
            if (!id.equals("")) {
                String newId = labelBase + id;
                child.setAttribute(Constants.ATT_ID, newId);
                child.setIdAttribute(Constants.ATT_ID, true);
                child.setAttributeNS(Constants.NS_URI, Constants.ATT_FRAGMENT, index);
                child.setAttributeNS(Constants.NS_URI, Constants.ATT_PRECEDENCE, precedence);
                setIdsAndAttribs(child, labelBase, index, precedence);
            }
        }
    }
}

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

public static Document constructILF(Document PLF, List<Document> sequence, IPerson person) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Constructing ILF for IPerson='" + person + "'");
    }/*from  w ww  .j av  a  2  s  .  c  o  m*/
    // first construct the destination document and root element. The root
    // element should be a complete copy of the PLF's root including its
    // node identifier in the new document. This requires the use of
    // the implementation class to set the identifier for that node
    // in the document.

    Document result = DocumentFactory.getThreadDocument();
    Element plfLayout = PLF.getDocumentElement();
    Element ilfLayout = (Element) result.importNode(plfLayout, false);
    result.appendChild(ilfLayout);
    Element plfRoot = (Element) plfLayout.getFirstChild();
    Element ilfRoot = (Element) result.importNode(plfRoot, false);
    ilfLayout.appendChild(ilfRoot);

    if (ilfRoot.getAttribute(Constants.ATT_ID) != null)
        ilfRoot.setIdAttribute(Constants.ATT_ID, true);

    // build the auth principal for determining if pushed channels can be 
    // used by this user
    EntityIdentifier ei = person.getEntityIdentifier();
    AuthorizationService authS = AuthorizationService.instance();
    IAuthorizationPrincipal ap = authS.newPrincipal(ei.getKey(), ei.getType());

    // now merge fragments one at a time into ILF document

    for (final Document document : sequence) {
        mergeFragment(document, result, ap);
    }
    return result;
}

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

/**
 * @param source parent of children//  www .  j a v a  2 s.  c o m
 * @param dest receiver of children
 * @param ap User's authorization principal for determining if they can view a channel
 * @param visitedNodes A Set of nodes from the source tree that have been visited to get to this node, used to ensure a loop doesn't exist in the source tree.
 * @throws AuthorizationException
 */
private static void mergeChildren(Element source, Element dest, IAuthorizationPrincipal ap, Set visitedNodes)
        throws AuthorizationException {
    //Record this node in the visited nodes set. If add returns false a loop has been detected
    if (!visitedNodes.add(source)) {
        final String msg = "mergeChildren has encountered a loop in the source DOM. currentNode='" + source
                + "', currentDepth='" + visitedNodes.size() + "', visitedNodes='" + visitedNodes + "'";
        final IllegalStateException ise = new IllegalStateException(msg);
        LOG.error(msg, ise);

        printNodeToDebug(source, "Source");
        printNodeToDebug(dest, "Dest");

        throw ise;
    }

    Document destDoc = dest.getOwnerDocument();

    Node item = source.getFirstChild();
    while (item != null) {
        if (item instanceof Element) {

            Element child = (Element) item;
            Element newChild = null;

            if (null != child && mergeAllowed(child, ap)) {
                newChild = (Element) destDoc.importNode(child, false);
                dest.appendChild(newChild);
                String id = newChild.getAttribute(Constants.ATT_ID);
                if (id != null && !id.equals(""))
                    newChild.setIdAttribute(Constants.ATT_ID, true);
                mergeChildren(child, newChild, ap, visitedNodes);
            }
        }

        item = item.getNextSibling();
    }

    //Remove this node from the visited nodes set
    visitedNodes.remove(source);
}

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

/**
 * Get the parameter edits set if any stored in the root of the document or
 * create it if passed-in create flag is true.
 *///w w  w.jav  a2  s  .  c o m
private static Element getParmEditSet(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_PARM_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 parameter edit set node "
                + "Id for userId=" + person.getID(), e);
    }
    Element parmSet = plf.createElement(Constants.ELM_PARM_SET);
    parmSet.setAttribute(Constants.ATT_TYPE, Constants.ELM_PARM_SET);
    parmSet.setAttribute(Constants.ATT_ID, ID);
    parmSet.setIdAttribute(Constants.ATT_ID, true);
    root.appendChild(parmSet);
    return parmSet;
}

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

/**
   This method does the actual work of adding a newly created parameter
   edit and adding it to the parameter edits set.
*//* w ww.j  ava  2 s . com*/
private static void addParmEditDirective(String targetID, String name, String value, IPerson person,
        Document plf, Element parmSet) throws PortalException {

    String ID = null;

    try {
        ID = getDLS().getNextStructDirectiveId(person);
    } catch (Exception e) {
        throw new PortalException("Exception encountered while " + "generating new parameter edit node "
                + "Id for userId=" + person.getID(), e);
    }
    Element parm = plf.createElement(Constants.ELM_PARM_EDIT);
    parm.setAttribute(Constants.ATT_TYPE, Constants.ELM_PARM_EDIT);
    parm.setAttribute(Constants.ATT_ID, ID);
    parm.setIdAttribute(Constants.ATT_ID, true);
    parm.setAttributeNS(Constants.NS_URI, Constants.ATT_TARGET, targetID);
    parm.setAttribute(Constants.ATT_NAME, name);
    parm.setAttribute(Constants.ATT_USER_VALUE, value);
    parmSet.appendChild(parm);
}

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

/**
   This method copies a plf node and any of its children into the passed
   in compViewParent./*from w  w  w.  j  ava  2s .  co m*/
 */
static Element appendChild(Element plfChild, Element parent, boolean copyChildren) {
    Document document = parent.getOwnerDocument();
    Element copy = (Element) document.importNode(plfChild, false);
    parent.appendChild(copy);

    // set the identifier for the doc if warrented
    String id = copy.getAttribute(Constants.ATT_ID);
    if (id != null && !id.equals(""))
        copy.setIdAttribute(Constants.ATT_ID, true);

    if (copyChildren) {
        NodeList children = plfChild.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            if (children.item(i) instanceof Element)
                appendChild((Element) children.item(i), copy, true);
        }
    }
    return copy;
}

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

@Override
protected Element getStructure(Document doc, LayoutStructure ls) {
    Element structure = null;

    String type = ls.getType();/*from w w w .  j  a v a2s . com*/

    if (ls.isChannel()) {
        final IPortletDefinition channelDef = this.portletDefinitionRegistry
                .getPortletDefinition(String.valueOf(ls.getChanId()));
        if (channelDef != null && channelApproved(channelDef.getApprovalDate())) {
            structure = this.getElementForChannel(doc, channelPrefix + ls.getStructId(), channelDef,
                    ls.getLocale());
        } else {
            structure = this.getElementForChannel(doc, channelPrefix + ls.getStructId(),
                    MissingPortletDefinition.INSTANCE, null);
        }
    } else {
        // create folder objects including dlm new types in cp namespace
        if (type != null && type.startsWith(Constants.NS)) {
            structure = doc.createElementNS(Constants.NS_URI, type);
        } else {
            structure = doc.createElement("folder");
        }
        structure.setAttribute("name", ls.getName());
        structure.setAttribute("type", (type != null ? type : "regular"));
    }

    structure.setAttribute("hidden", (ls.isHidden() ? "true" : "false"));
    structure.setAttribute("immutable", (ls.isImmutable() ? "true" : "false"));
    structure.setAttribute("unremovable", (ls.isUnremovable() ? "true" : "false"));
    if (localeAware) {
        structure.setAttribute("locale", ls.getLocale()); // for i18n by Shoji
    }

    /*
     * Parameters from up_layout_param are loaded slightly differently for
     * folders and channels. For folders all parameters are added as attributes
     * of the Element. For channels only those parameters with names starting
     * with the dlm namespace Constants.NS are added as attributes to the Element.
     * Others are added as child parameter Elements.
     */
    if (ls.getParameters() != null) {
        for (final Iterator itr = ls.getParameters().iterator(); itr.hasNext();) {
            final StructureParameter sp = (StructureParameter) itr.next();
            String pName = sp.getName();

            if (!ls.isChannel()) { // Folder
                if (pName.startsWith(Constants.NS)) {
                    structure.setAttributeNS(Constants.NS_URI, pName, sp.getValue());
                } else {
                    structure.setAttribute(pName, sp.getValue());
                }
            } else // Channel
            {
                // if dealing with a dlm namespace param add as attribute
                if (pName.startsWith(Constants.NS)) {
                    structure.setAttributeNS(Constants.NS_URI, pName, sp.getValue());
                    itr.remove();
                } else {
                    /*
                     * do traditional override processing. some explanation is in
                     * order. The structure element was created by the
                     * ChannelDefinition and only contains parameter children if the
                     * definition had defined parameters. These are checked for each
                     * layout loaded parameter as found in LayoutStructure.parameters.
                     * If a name match is found then we need to see if overriding is
                     * allowed and if so we set the value on the child parameter
                     * element. At that point we are done with that version loaded
                     * from the layout so we remove it from the in-memory set of
                     * parameters that are being merged-in. Then, after all such have
                     * been checked against those added by the channel definition we
                     * add in any remaining as adhoc, unregulated parameters.
                     */
                    final NodeList nodeListParameters = structure.getElementsByTagName("parameter");
                    for (int j = 0; j < nodeListParameters.getLength(); j++) {
                        final Element parmElement = (Element) nodeListParameters.item(j);
                        final NamedNodeMap nm = parmElement.getAttributes();

                        final String nodeName = nm.getNamedItem("name").getNodeValue();
                        if (nodeName.equals(pName)) {
                            final Node override = nm.getNamedItem("override");
                            if (override != null && override.getNodeValue().equals("yes")) {
                                final Node valueNode = nm.getNamedItem("value");
                                valueNode.setNodeValue(sp.getValue());
                            }
                            itr.remove();
                            break; // found the corresponding one so skip the rest
                        }
                    }
                }
            }
        }
        // For channels, add any remaining parameter elements loaded with the
        // layout as adhoc, unregulated, parameter children that can be overridden.
        if (ls.isChannel()) {
            for (final Iterator itr = ls.getParameters().iterator(); itr.hasNext();) {
                final StructureParameter sp = (StructureParameter) itr.next();
                final Element parameter = doc.createElement("parameter");
                parameter.setAttribute("name", sp.getName());
                parameter.setAttribute("value", sp.getValue());
                parameter.setAttribute("override", "yes");
                structure.appendChild(parameter);
            }
        }
    }
    // finish setting up elements based on loaded params
    final String origin = structure.getAttribute(Constants.ATT_ORIGIN);
    final String prefix = ls.isChannel() ? channelPrefix : folderPrefix;

    // if not null we are dealing with a node incorporated from another
    // layout and this node contains changes made by the user so handle
    // id swapping.
    if (!origin.equals("")) {
        structure.setAttributeNS(Constants.NS_URI, Constants.ATT_PLF_ID, prefix + ls.getStructId());
        structure.setAttribute("ID", origin);
    } else if (!ls.isChannel())
    // regular folder owned by this user, need to check if this is a
    // directive or ui element. If the latter then use traditional id
    // structure
    {
        if (type != null && type.startsWith(Constants.NS)) {
            structure.setAttribute("ID", Constants.DIRECTIVE_PREFIX + ls.getStructId());
        } else {
            structure.setAttribute("ID", folderPrefix + ls.getStructId());
        }
    } else {
        logger.debug("Adding identifier {}{}", folderPrefix, ls.getStructId());
        structure.setAttribute("ID", channelPrefix + ls.getStructId());
    }
    structure.setIdAttribute(Constants.ATT_ID, true);
    return structure;
}

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

private Element getElementForChannel(Document doc, String chanId, IPortletDefinition def, String locale) {
    final Element channel = doc.createElement("channel");

    // the ID attribute is the identifier for the Channel element
    channel.setAttribute("ID", chanId);
    channel.setIdAttribute("ID", true);

    channel.setAttribute("chanID", def.getPortletDefinitionId().getStringId());
    channel.setAttribute("timeout", String.valueOf(def.getTimeout()));
    if (locale != null) {
        channel.setAttribute("name", def.getName(locale));
        channel.setAttribute("title", def.getTitle(locale));
        channel.setAttribute("description", def.getDescription(locale));
        channel.setAttribute("locale", locale);
    } else {/*www.  j  a  v a  2  s .c om*/
        channel.setAttribute("name", def.getName());
        channel.setAttribute("title", def.getTitle());
        channel.setAttribute("description", def.getDescription());
    }
    channel.setAttribute("fname", def.getFName());

    // chanClassArg is so named to highlight that we are using the argument
    // to the method rather than the instance variable chanClass
    channel.setAttribute("typeID", String.valueOf(def.getType().getId()));

    for (final IPortletDefinitionParameter param : def.getParameters()) {
        final Element parameter = doc.createElement("parameter");
        parameter.setAttribute("name", param.getName());
        parameter.setAttribute("value", param.getValue());
        channel.appendChild(parameter);
    }

    return channel;

}

From source file:org.jasig.portal.layout.dlm.RDBMDistributedLayoutStore.java

@Override
protected Element getStructure(Document doc, LayoutStructure ls) {
    Element structure = null;

    // handle migration of legacy namespace
    String type = ls.getType();/*from ww  w .  j  a v  a  2s.c o m*/
    if (type != null && type.startsWith(Constants.LEGACY_NS)) {
        type = Constants.NS + type.substring(Constants.LEGACY_NS.length());
    }

    if (ls.isChannel()) {
        final IPortletDefinition channelDef = this.portletDefinitionRegistry
                .getPortletDefinition(String.valueOf(ls.getChanId()));
        if (channelDef != null && channelApproved(channelDef.getApprovalDate())) {
            structure = this.getElementForChannel(doc, channelPrefix + ls.getStructId(), channelDef,
                    ls.getLocale());
        } else {
            // Create an error channel if channel is missing or not approved
            String missingChannel = "Unknown";
            if (channelDef != null) {
                missingChannel = channelDef.getName();
            }
            structure = this.getElementForChannel(doc, channelPrefix + ls.getStructId(),
                    MissingPortletDefinition.INSTANCE, null);
            //        structure = MissingPortletDefinition.INSTANCE.getDocument(doc, channelPrefix + ls.getStructId());
            //        structure = MissingPortletDefinition.INSTANCE.getDocument(doc, channelPrefix + ls.getStructId(),
            //                "The '" + missingChannel + "' channel is no longer available. " +
            //                "Please remove it from your layout.",
            //                -1);
        }
    } else {
        // create folder objects including dlm new types in cp namespace
        if (type != null && type.startsWith(Constants.NS)) {
            structure = doc.createElementNS(Constants.NS_URI, type);
        } else {
            structure = doc.createElement("folder");
        }
        structure.setAttribute("name", ls.getName());
        structure.setAttribute("type", (type != null ? type : "regular"));
    }

    structure.setAttribute("hidden", (ls.isHidden() ? "true" : "false"));
    structure.setAttribute("immutable", (ls.isImmutable() ? "true" : "false"));
    structure.setAttribute("unremovable", (ls.isUnremovable() ? "true" : "false"));
    if (localeAware) {
        structure.setAttribute("locale", ls.getLocale()); // for i18n by Shoji
    }

    /*
     * Parameters from up_layout_param are loaded slightly differently for
     * folders and channels. For folders all parameters are added as attributes
     * of the Element. For channels only those parameters with names starting
     * with the dlm namespace Constants.NS are added as attributes to the Element.
     * Others are added as child parameter Elements.
     */
    if (ls.getParameters() != null) {
        for (final Iterator itr = ls.getParameters().iterator(); itr.hasNext();) {
            final StructureParameter sp = (StructureParameter) itr.next();
            String pName = sp.getName();

            // handle migration of legacy namespace
            if (pName.startsWith(Constants.LEGACY_NS)) {
                pName = Constants.NS + sp.getName().substring(Constants.LEGACY_NS.length());
            }

            if (!ls.isChannel()) { // Folder
                if (pName.startsWith(Constants.NS)) {
                    structure.setAttributeNS(Constants.NS_URI, pName, sp.getValue());
                } else {
                    structure.setAttribute(pName, sp.getValue());
                }
            } else // Channel
            {
                // if dealing with a dlm namespace param add as attribute
                if (pName.startsWith(Constants.NS)) {
                    structure.setAttributeNS(Constants.NS_URI, pName, sp.getValue());
                    itr.remove();
                } else {
                    /*
                     * do traditional override processing. some explanation is in
                     * order. The structure element was created by the
                     * ChannelDefinition and only contains parameter children if the
                     * definition had defined parameters. These are checked for each
                     * layout loaded parameter as found in LayoutStructure.parameters.
                     * If a name match is found then we need to see if overriding is
                     * allowed and if so we set the value on the child parameter
                     * element. At that point we are done with that version loaded
                     * from the layout so we remove it from the in-memory set of
                     * parameters that are being merged-in. Then, after all such have
                     * been checked against those added by the channel definition we
                     * add in any remaining as adhoc, unregulated parameters.
                     */
                    final NodeList nodeListParameters = structure.getElementsByTagName("parameter");
                    for (int j = 0; j < nodeListParameters.getLength(); j++) {
                        final Element parmElement = (Element) nodeListParameters.item(j);
                        final NamedNodeMap nm = parmElement.getAttributes();

                        final String nodeName = nm.getNamedItem("name").getNodeValue();
                        if (nodeName.equals(pName)) {
                            final Node override = nm.getNamedItem("override");
                            if (override != null && override.getNodeValue().equals("yes")) {
                                final Node valueNode = nm.getNamedItem("value");
                                valueNode.setNodeValue(sp.getValue());
                            }
                            itr.remove();
                            break; // found the corresponding one so skip the rest
                        }
                    }
                }
            }
        }
        // For channels, add any remaining parameter elements loaded with the
        // layout as adhoc, unregulated, parameter children that can be overridden.
        if (ls.isChannel()) {
            for (final Iterator itr = ls.getParameters().iterator(); itr.hasNext();) {
                final StructureParameter sp = (StructureParameter) itr.next();
                final Element parameter = doc.createElement("parameter");
                parameter.setAttribute("name", sp.getName());
                parameter.setAttribute("value", sp.getValue());
                parameter.setAttribute("override", "yes");
                structure.appendChild(parameter);
            }
        }
    }
    // finish setting up elements based on loaded params
    final String origin = structure.getAttribute(Constants.ATT_ORIGIN);
    final String prefix = ls.isChannel() ? channelPrefix : folderPrefix;

    // if not null we are dealing with a node incorporated from another
    // layout and this node contains changes made by the user so handle
    // id swapping.
    if (!origin.equals("")) {
        structure.setAttributeNS(Constants.NS_URI, Constants.ATT_PLF_ID, prefix + ls.getStructId());
        structure.setAttribute("ID", origin);
    } else if (!ls.isChannel())
    // regular folder owned by this user, need to check if this is a
    // directive or ui element. If the latter then use traditional id
    // structure
    {
        if (type != null && type.startsWith(Constants.NS)) {
            structure.setAttribute("ID", Constants.DIRECTIVE_PREFIX + ls.getStructId());
        } else {
            structure.setAttribute("ID", folderPrefix + ls.getStructId());
        }
    } else {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Adding identifier " + folderPrefix + ls.getStructId());
        }
        structure.setAttribute("ID", channelPrefix + ls.getStructId());
    }
    structure.setIdAttribute(Constants.ATT_ID, true);
    return structure;
}

From source file:org.jasig.portal.layout.simple.RDBMUserLayoutStore.java

/**
 * Create a layout/*from ww  w  .j  a va 2s. c om*/
 * @param layoutStructure
 * @param doc
 * @param root
 * @param structId
 * @exception java.sql.SQLException
 */
protected final void createLayout(HashMap layoutStructure, Document doc, Element root, int structId)
        throws java.sql.SQLException {
    while (structId != 0) {
        LayoutStructure ls = (LayoutStructure) layoutStructure.get(new Integer(structId));
        // replaced with call to method in containing class to allow overriding
        // by subclasses of RDBMUserLayoutStore.
        // Element structure = ls.getStructureDocument(doc);
        Element structure = getStructure(doc, ls);
        root.appendChild(structure);

        String id = structure.getAttribute("ID");
        if (id != null && !id.equals("")) {
            structure.setIdAttribute("ID", true);
        }

        createLayout(layoutStructure, doc, structure, ls.getChildId());
        structId = ls.getNextId();
    }
}