Example usage for org.w3c.dom Element getParentNode

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

Introduction

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

Prototype

public Node getParentNode();

Source Link

Document

The parent of this node.

Usage

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

/**
 * Remove a parameter edit directive from the parameter edits set for
 * applying user specified values to a named parameter of an incorporated
 * channel represented by the passed-in target id. If one doesn't exists
 * for that node and that name then this call returns without any effects.
 *//* w  ww. ja  v  a 2s .c  o m*/
public static void removeParmEditDirective(String targetId, String name, IPerson person)
        throws PortalException {
    Document plf = (Document) person.getAttribute(Constants.PLF);
    Element parmSet = getParmEditSet(plf, person, false);

    if (parmSet == null)
        return; // no set so no edit to remove

    NodeList edits = parmSet.getChildNodes();

    for (int i = 0; i < edits.getLength(); i++) {
        Element edit = (Element) edits.item(i);
        if (edit.getAttribute(Constants.ATT_TARGET).equals(targetId)
                && edit.getAttribute(Constants.ATT_NAME).equals(name)) {
            parmSet.removeChild(edit);
            break;
        }
    }
    if (parmSet.getChildNodes().getLength() == 0) // no more edits, remove
    {
        Node parent = parmSet.getParentNode();
        parent.removeChild(parmSet);
    }
}

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

/**
 * Returns the layout for a user. This method overrides the same
 * method in the superclass to return a composite layout for non
 * fragment owners and a regular layout for layout owners. A
 * composite layout is made up of layout pieces from potentially
 * multiple incorporated layouts. If no layouts are defined then
 * the composite layout will be the same as the user's personal
 * layout fragment or PLF, the one holding only those UI elements
 * that they own or incorporated elements that they have been
 * allowed to changed.// w w w . j  a v a2s .co m
 **/
private DistributedUserLayout _getUserLayout(IPerson person, IUserProfile profile)

{
    final String userName = (String) person.getAttribute("username");
    final FragmentDefinition ownedFragment = this.fragmentUtils.getFragmentDefinitionByOwner(person);
    final boolean isLayoutOwnerDefault = this.isLayoutOwnerDefault(person);
    final Set<String> fragmentNames = new LinkedHashSet<String>();

    final Document ILF;
    final Document PLF = this.getPLF(person, profile);

    // If this user is an owner then ownedFragment will be non null. For
    // fragment owners and owners of any default layout from which a
    // fragment owners layout is copied there should not be any imported
    // distributed layouts. Instead, load their PLF, mark as an owned
    // if a fragment owner, and return.
    if (ownedFragment != null || isLayoutOwnerDefault) {
        ILF = (Document) PLF.cloneNode(true);
        final Element layoutNode = ILF.getDocumentElement();

        final Element ownerDocument = layoutNode.getOwnerDocument().getDocumentElement();
        final NodeList channelNodes = ownerDocument.getElementsByTagName("channel");
        for (int i = 0; i < channelNodes.getLength(); i++) {
            Element channelNode = (Element) channelNodes.item(i);
            final Node chanIdNode = channelNode.getAttributeNode("chanID");
            if (chanIdNode == null || MissingPortletDefinition.CHANNEL_ID.equals(chanIdNode.getNodeValue())) {
                channelNode.getParentNode().removeChild(channelNode);
            }
        }

        if (ownedFragment != null) {
            fragmentNames.add(ownedFragment.getName());
            layoutNode.setAttributeNS(Constants.NS_URI, Constants.ATT_FRAGMENT_NAME, ownedFragment.getName());
            logger.debug("User '{}' is owner of '{}' fragment.", userName, ownedFragment.getName());
        } else if (isLayoutOwnerDefault) {
            layoutNode.setAttributeNS(Constants.NS_URI, Constants.ATT_IS_TEMPLATE_USER, "true");
            layoutNode.setAttributeNS(Constants.NS_URI, Constants.ATT_TEMPLATE_LOGIN_ID,
                    (String) person.getAttribute("username"));
        }
    } else {
        final Locale locale = profile.getLocaleManager().getLocales()[0];
        final List<FragmentDefinition> applicableFragmentDefinitions = this.fragmentUtils
                .getFragmentDefinitionsApplicableToPerson(person);
        final List<Document> applicableLayouts = this.fragmentUtils
                .getFragmentDefinitionUserViewLayouts(applicableFragmentDefinitions, locale);
        final IntegrationResult integrationResult = new IntegrationResult();
        ILF = this.createCompositeILF(person, PLF, applicableLayouts, integrationResult);
        // push optimizations made during merge back into db.
        if (integrationResult.isChangedPLF()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Saving PLF for {} due to changes during merge.",
                        person.getAttribute(IPerson.USERNAME));
            }
            super.setUserLayout(person, profile, PLF, false);
        }
        fragmentNames.addAll(this.fragmentUtils.getFragmentNames(applicableFragmentDefinitions));
    }
    return this.createDistributedUserLayout(person, profile, ILF, fragmentNames);
}

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

/**
   This method is called from the TabColumnPrefsState class after a node
   has already been moved from its old parent to its new in the ILF. We can
   get at the new parent via the compViewNode moved but need a separate
   handle of the parent from whence it came. The goal of this method is to
   make the appropriate change in the PLF to persist this action take by
   the user. For ILF nodes this generally means adding an entry to the
   position set for the new parent and removing any entry if it existed in
   the position set in the old parent. For nodes that are owned by the
   user (PLF owned nodes) the nodes are moved outright and now position
   set is needed unless the new parent contains ILF nodes as well
   requiring preservation of the user's ordering of the nodes for when the
   ILF and PLF are merged again later on.
 *///from   w w  w . j  av a 2s  .  co m
public static void moveElement(Element compViewNode, Element oldCompViewParent, IPerson person)
        throws PortalException {
    if (LOG.isInfoEnabled())
        LOG.info("moving " + compViewNode.getAttribute(Constants.ATT_ID));
    Element compViewParent = (Element) compViewNode.getParentNode();

    if (oldCompViewParent != compViewParent) {
        if (LOG.isInfoEnabled())
            LOG.info("reparenting from " + oldCompViewParent.getAttribute(Constants.ATT_ID) + " to "
                    + compViewParent.getAttribute(Constants.ATT_ID));
        // update previous parent if found in PLF
        Element plfParent = HandlerUtils.getPLFNode(oldCompViewParent, person, // only needed if creating
                false, // only look, don't create
                false); // also not needed
        if (plfParent != null) {
            PositionManager.updatePositionSet(oldCompViewParent, plfParent, person);
            if (LOG.isInfoEnabled())
                LOG.info("updating old parent's position set");
        }
    }
    // now take care of the destination
    Element plfParent = HandlerUtils.getPLFNode(compViewParent, person, true, // create parent if not found
            false); // don't create children
    if (compViewNode.getAttribute(Constants.ATT_ID).startsWith(Constants.FRAGMENT_ID_USER_PREFIX)) {
        // ilf node being inserted
        if (LOG.isInfoEnabled())
            LOG.info("ilf node being moved, only update new parent pos set");
        PositionManager.updatePositionSet(compViewParent, plfParent, person);
    } else {
        // plf node
        if (LOG.isInfoEnabled())
            LOG.info("plf node being moved, updating old parent's position set");
        Document plf = (Document) person.getAttribute(Constants.PLF);
        HandlerUtils.createOrMovePLFOwnedNode(compViewNode, compViewParent, false, // should always be found
                false, // irrelevant, not creating
                plf, plfParent, person);
    }
}

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

/**
   Handles user requests to delete UI elements. For ILF owned nodes it
   delegates to the DeleteManager to add a delete directive. For PLF
   owned nodes it deletes the node outright.
 *//*from  ww  w  . j a v  a 2 s  .  c o m*/
public static void deleteNode(Element compViewNode, Element compViewParent, IPerson person)
        throws PortalException {
    String ID = compViewNode.getAttribute(Constants.ATT_ID);

    if (ID.startsWith(Constants.FRAGMENT_ID_USER_PREFIX)) // ilf node
        DeleteManager.addDeleteDirective(compViewNode, ID, person);
    else {
        // plf node
        Document plf = (Document) person.getAttribute(Constants.PLF);
        Element node = plf.getElementById(ID);

        if (node == null)
            return;
        Element parent = (Element) node.getParentNode();
        if (parent == null)
            return;
        parent.removeChild(node);
    }
}

From source file:org.apereo.portal.layout.simple.SimpleLayout.java

@Override
public String getParentId(String nodeId) throws PortalException {
    String parentId = null;//www  .j a  va2s.  co m
    Element element = layout.getElementById(nodeId);
    if (element != null) {
        Node parent = element.getParentNode();
        if (parent != null && parent.getNodeType() == Node.ELEMENT_NODE) {
            Element parentE = (Element) parent;
            parentId = parentE.getAttribute("ID");
        }
    }
    return parentId;
}

From source file:org.bonitasoft.theme.builder.impl.AbstractXMLBuilder.java

/**
 * Find the first element with the given tag name among an element children
 * /*  www.  ja va 2  s . co  m*/
 * @param parent the parent element
 * @param childName the tag name
 * @return an {@link Element} or null if there are no elements with thegiven tag name among the element's children
 */
protected Element findChildElement(final Element parent, final String childName) {
    final NodeList nodeList = parent.getElementsByTagName(childName);
    for (int i = 0; i < nodeList.getLength(); i++) {
        final Element element = (Element) nodeList.item(i);
        if (element.getParentNode().isSameNode(parent)) {
            return element;
        }
    }
    return null;
}

From source file:org.bonitasoft.theme.builder.impl.AbstractXMLBuilder.java

/**
 * Add an element to the stack// w w w. j  ava  2  s .co  m
 * 
 * @param element
 */
protected void push(final Element element) {
    if (element.getParentNode() == null) {
        currentElement.appendChild(element);
    }
    currentElement = element;
}

From source file:org.broadleafcommerce.common.extensibility.context.merge.ImportProcessor.java

public ResourceInputStream[] extract(ResourceInputStream[] sources) throws MergeException {
    if (sources == null) {
        return null;
    }//from   ww w .  j  av a 2  s.  c o  m
    try {
        DynamicResourceIterator resourceList = new DynamicResourceIterator();
        resourceList.addAll(Arrays.asList(sources));
        while (resourceList.hasNext()) {
            ResourceInputStream myStream = resourceList.nextResource();
            Document doc = builder.parse(myStream);
            NodeList nodeList = (NodeList) xPath.evaluate(IMPORT_PATH, doc, XPathConstants.NODESET);
            int length = nodeList.getLength();
            for (int j = 0; j < length; j++) {
                Element element = (Element) nodeList.item(j);
                Resource resource = loader.getResource(element.getAttribute("resource"));
                ResourceInputStream ris = new ResourceInputStream(resource.getInputStream(),
                        resource.getURL().toString());
                resourceList.addEmbeddedResource(ris);
                element.getParentNode().removeChild(element);
            }
            if (length > 0) {
                TransformerFactory tFactory = TransformerFactory.newInstance();
                Transformer xmlTransformer = tFactory.newTransformer();
                xmlTransformer.setOutputProperty(OutputKeys.VERSION, "1.0");
                xmlTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
                xmlTransformer.setOutputProperty(OutputKeys.METHOD, "xml");
                xmlTransformer.setOutputProperty(OutputKeys.INDENT, "yes");

                DOMSource source = new DOMSource(doc);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(baos));
                StreamResult result = new StreamResult(writer);
                xmlTransformer.transform(source, result);

                byte[] itemArray = baos.toByteArray();

                resourceList.set(resourceList.getPosition() - 1, new ResourceInputStream(
                        new ByteArrayInputStream(itemArray), null, myStream.getNames()));
            } else {
                myStream.reset();
            }
        }

        return resourceList.toArray(new ResourceInputStream[resourceList.size()]);
    } catch (Exception e) {
        throw new MergeException(e);
    }
}

From source file:org.chiba.tools.schemabuilder.AbstractSchemaFormBuilder.java

/**
 * Add a repeat section if maxOccurs > 1.
 *//* w  w  w .j  a v  a2s.  com*/
private Element addRepeatIfNecessary(Document xForm, Element modelSection, Element formSection,
        XSTypeDefinition controlType, int minOccurs, int maxOccurs, String pathToRoot) {
    Element repeatSection = formSection;

    // add xforms:repeat section if this element re-occurs
    //
    if (maxOccurs != 1) {

        if (LOGGER.isDebugEnabled())
            LOGGER.debug("DEBUG: AddRepeatIfNecessary for multiple element for type " + controlType.getName()
                    + ", maxOccurs=" + maxOccurs);

        //repeatSection = (Element) formSection.appendChild(xForm.createElementNS(XFORMS_NS,getXFormsNSPrefix() + "repeat"));
        repeatSection = xForm.createElementNS(XFORMS_NS, getXFormsNSPrefix() + "repeat");

        //bind instead of repeat
        //repeatSection.setAttributeNS(XFORMS_NS,getXFormsNSPrefix() + "nodeset",pathToRoot);
        // bind -> last element in the modelSection
        Element bind = DOMUtil.getLastChildElement(modelSection);
        String bindId = null;

        if ((bind != null) && (bind.getLocalName() != null) && bind.getLocalName().equals("bind")) {
            bindId = bind.getAttributeNS(SchemaFormBuilder.XFORMS_NS, "id");
        } else {
            LOGGER.warn("addRepeatIfNecessary: bind not found: " + bind + " (model selection name="
                    + modelSection.getNodeName() + ")");

            //if no bind is found -> modelSection is already a bind, get its parent last child
            bind = DOMUtil.getLastChildElement(modelSection.getParentNode());

            if ((bind != null) && (bind.getLocalName() != null) && bind.getLocalName().equals("bind")) {
                bindId = bind.getAttributeNS(SchemaFormBuilder.XFORMS_NS, "id");
            } else {
                LOGGER.warn("addRepeatIfNecessary: bind really not found");
            }
        }

        repeatSection.setAttributeNS(XFORMS_NS, getXFormsNSPrefix() + "bind", bindId);
        this.setXFormsId(repeatSection);

        //appearance=full is more user friendly
        repeatSection.setAttributeNS(XFORMS_NS, getXFormsNSPrefix() + "appearance", "full");

        //triggers
        this.addTriggersForRepeat(xForm, formSection, repeatSection, minOccurs, maxOccurs, bindId);

        Element controlWrapper = _wrapper.createControlsWrapper(repeatSection);
        formSection.appendChild(controlWrapper);

        //add a group inside the repeat?
        Element group = xForm.createElementNS(XFORMS_NS, this.getXFormsNSPrefix() + "group");
        this.setXFormsId(group);
        repeatSection.appendChild(group);
        repeatSection = group;
    }

    return repeatSection;
}

From source file:org.chiba.tools.schemabuilder.AbstractSchemaFormBuilder.java

/**
 * add triggers to use the repeat elements (allow to add an element, ...)
 *//*w w w  .j  a  va2s  . c o m*/
private void addTriggersForRepeat(Document xForm, Element formSection, Element repeatSection, int minOccurs,
        int maxOccurs, String bindId) {
    ///////////// insert //////////////////
    //trigger insert
    Element trigger_insert = xForm.createElementNS(SchemaFormBuilder.XFORMS_NS,
            SchemaFormBuilder.xformsNSPrefix + "trigger");
    this.setXFormsId(trigger_insert);

    //label insert
    Element triggerLabel_insert = xForm.createElementNS(SchemaFormBuilder.XFORMS_NS,
            SchemaFormBuilder.xformsNSPrefix + "label");
    this.setXFormsId(triggerLabel_insert);
    trigger_insert.appendChild(triggerLabel_insert);
    triggerLabel_insert.setAttributeNS(SchemaFormBuilder.XLINK_NS, SchemaFormBuilder.xlinkNSPrefix + "href",
            "images/add_new.gif");

    Text label_insert = xForm.createTextNode("Insert after selected");
    triggerLabel_insert.appendChild(label_insert);

    //hint insert
    Element hint_insert = xForm.createElementNS(SchemaFormBuilder.XFORMS_NS,
            SchemaFormBuilder.xformsNSPrefix + "hint");
    this.setXFormsId(hint_insert);
    Text hint_insert_text = xForm.createTextNode("inserts a new entry in this collection");
    hint_insert.appendChild(hint_insert_text);
    trigger_insert.appendChild(hint_insert);

    //insert action
    Element action_insert = xForm.createElementNS(SchemaFormBuilder.XFORMS_NS,
            SchemaFormBuilder.xformsNSPrefix + "action");
    trigger_insert.appendChild(action_insert);
    this.setXFormsId(action_insert);

    Element insert = xForm.createElementNS(SchemaFormBuilder.XFORMS_NS,
            SchemaFormBuilder.xformsNSPrefix + "insert");
    action_insert.appendChild(insert);
    this.setXFormsId(insert);

    //insert: bind & other attributes
    if (bindId != null) {
        insert.setAttributeNS(SchemaFormBuilder.XFORMS_NS, SchemaFormBuilder.xformsNSPrefix + "bind", bindId);
    }

    insert.setAttributeNS(SchemaFormBuilder.XFORMS_NS, SchemaFormBuilder.xformsNSPrefix + "position", "after");

    //xforms:at = xforms:index from the "id" attribute on the repeat element
    String repeatId = repeatSection.getAttributeNS(SchemaFormBuilder.XFORMS_NS, "id");

    if (repeatId != null) {
        insert.setAttributeNS(SchemaFormBuilder.XFORMS_NS, SchemaFormBuilder.xformsNSPrefix + "at",
                SchemaFormBuilder.xformsNSPrefix + "index('" + repeatId + "')");
    }

    ///////////// delete //////////////////
    //trigger delete
    Element trigger_delete = xForm.createElementNS(SchemaFormBuilder.XFORMS_NS,
            SchemaFormBuilder.xformsNSPrefix + "trigger");
    this.setXFormsId(trigger_delete);

    //label delete
    Element triggerLabel_delete = xForm.createElementNS(SchemaFormBuilder.XFORMS_NS,
            SchemaFormBuilder.xformsNSPrefix + "label");
    this.setXFormsId(triggerLabel_delete);
    trigger_delete.appendChild(triggerLabel_delete);
    triggerLabel_delete.setAttributeNS(SchemaFormBuilder.XLINK_NS, SchemaFormBuilder.xlinkNSPrefix + "href",
            "images/delete.gif");

    Text label_delete = xForm.createTextNode("Delete selected");
    triggerLabel_delete.appendChild(label_delete);

    //hint delete
    Element hint_delete = xForm.createElementNS(SchemaFormBuilder.XFORMS_NS,
            SchemaFormBuilder.xformsNSPrefix + "hint");
    this.setXFormsId(hint_delete);
    Text hint_delete_text = xForm.createTextNode("deletes selected entry from this collection");
    hint_delete.appendChild(hint_delete_text);
    trigger_delete.appendChild(hint_delete);

    //delete action
    Element action_delete = xForm.createElementNS(SchemaFormBuilder.XFORMS_NS,
            SchemaFormBuilder.xformsNSPrefix + "action");
    trigger_delete.appendChild(action_delete);
    this.setXFormsId(action_delete);

    Element delete = xForm.createElementNS(SchemaFormBuilder.XFORMS_NS,
            SchemaFormBuilder.xformsNSPrefix + "delete");
    action_delete.appendChild(delete);
    this.setXFormsId(delete);

    //delete: bind & other attributes
    if (bindId != null) {
        delete.setAttributeNS(SchemaFormBuilder.XFORMS_NS, SchemaFormBuilder.xformsNSPrefix + "bind", bindId);
    }

    //xforms:at = xforms:index from the "id" attribute on the repeat element
    if (repeatId != null) {
        delete.setAttributeNS(SchemaFormBuilder.XFORMS_NS, SchemaFormBuilder.xformsNSPrefix + "at",
                SchemaFormBuilder.xformsNSPrefix + "index('" + repeatId + "')");
    }

    //add the triggers
    Element wrapper_triggers = _wrapper.createControlsWrapper(trigger_insert);

    if (wrapper_triggers == trigger_insert) { //no wrapper
        formSection.appendChild(trigger_insert);
        formSection.appendChild(trigger_delete);
    } else {
        formSection.appendChild(wrapper_triggers);

        Element insert_parent = (Element) trigger_insert.getParentNode();

        if (insert_parent != null) {
            insert_parent.appendChild(trigger_delete);
        }
    }
}