Example usage for org.w3c.dom Element removeChild

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

Introduction

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

Prototype

public Node removeChild(Node oldChild) throws DOMException;

Source Link

Document

Removes the child node indicated by oldChild from the list of children, and returns it.

Usage

From source file:org.infoscoop.service.SiteAggregationMenuService.java

public synchronized void updateMenuItem(String menuId, String title, String href, String display, String type,
        String serviceURL, String serviceAuthType, Map props, String alert, String menuType, Collection auths,
        Collection<String> menuTreeAdmins, Boolean linkDisabled, String directoryTitle, String sitetopId,
        boolean multi) throws Exception {

    href = StringUtil.getTruncatedString(href, 1024, "UTF-8");

    if (log.isInfoEnabled()) {
        log.info("UpdateMenuItem: menuId=" + menuId + ", title=" + title + ", " + title + ", href=" + href
                + ", display=" + display + ", alert" + alert + ", properties=" + props);
    }//w w w  .jav a2 s . c o  m
    // Obtain data and transfer the result to Document.
    Siteaggregationmenu_temp entity = this.siteAggregationMenuTempDAO.selectBySitetopId(menuType, sitetopId);
    Node node = getTargetElement(entity, menuId);

    // Error
    if (node == null)
        throw new Exception("element not found [//site],[//site-top]");

    Document document = node.getOwnerDocument();

    // Create element to be updated
    Element element;
    element = (Element) node;
    element.setAttribute("title", title);
    element.setAttribute("href", href);
    element.setAttribute("display", display);
    element.setAttribute("link_disabled", linkDisabled.toString());
    if (serviceURL != null)
        element.setAttribute("serviceURL", serviceURL);
    if (serviceAuthType != null)
        element.setAttribute("serviceAuthType", serviceAuthType);
    if (alert != null && !"".equals(alert))
        element.setAttribute("alert", alert);

    element.setAttribute("multi", String.valueOf(multi));

    element.setAttribute("type", type);

    if (directoryTitle != null && !"".equals(directoryTitle)) {
        element.setAttribute("directory_title", directoryTitle);
    } else if (element.hasAttribute("directory_title")) {
        element.removeAttribute("directory_title");
    }

    element.insertBefore(recreatePropertiesNode(document, element, props), element.getFirstChild());

    Element oldAuths = getFirstChildElementByName(element, "auths");
    if (oldAuths != null) {
        element.removeChild(oldAuths);
    }
    if (auths != null) {
        element.insertBefore(MenuAuthorization.createAuthsElement(document, auths),
                getFirstChildElementByName(element, "site"));
    }

    NodeList oldAdmins = element.getElementsByTagName("menuTreeAdmins");
    if (oldAdmins != null) {
        while (oldAdmins.getLength() != 0) {
            oldAdmins.item(0).getParentNode().removeChild(oldAdmins.item(0));
        }
    }
    if (menuTreeAdmins != null) {
        element.insertBefore(createAdminsElement(document, menuTreeAdmins),
                getFirstChildElementByName(element, "site"));
    }

    // Update
    entity.setElement(document.getDocumentElement());
    this.siteAggregationMenuTempDAO.update(entity);
}

From source file:org.infoscoop.service.WidgetConfService.java

/**
 * @param doc//from  w ww .ja v  a  2s.c  o  m
 * @param widgetConfNode
 * @param updatePrefList
 * @throws JSONException
 */
public static void updateWidgetPrefNode(Document doc, Element widgetConfNode, JSONObject updatePrefList)
        throws JSONException {

    NodeList prefList = widgetConfNode.getElementsByTagName("WidgetPref");

    Iterator keys = updatePrefList.keys();
    while (keys.hasNext()) {
        String id = (String) keys.next();
        JSONObject prefJson = updatePrefList.getJSONObject(id);
        if (!prefJson.has("name"))
            continue;

        String name = prefJson.getString("name");

        String datatype = "";
        if (prefJson.has("datatype"))
            datatype = prefJson.getString("datatype");

        String value = prefJson.getString("value");

        int prefLength = prefList.getLength();
        boolean update = false;
        for (int i = 0; i < prefLength; i++) {
            Element pref = (Element) prefList.item(i);
            if (!name.equals(pref.getAttribute("name")))
                continue;

            if ("xml".equals(datatype) || "json".equals(datatype)) {
                while (pref.getFirstChild() != null)
                    pref.removeChild(pref.getFirstChild());

                pref.appendChild(doc.createTextNode(value));
            } else {
                pref.setAttribute("value", value);
            }

            update = true;
        }

        // is this code require ?
        if (!update) {
            Element newPref = doc.createElement("WidgetPref");
            newPref.setAttribute("name", name);

            if ("xml".equals(datatype) || "json".equals(datatype)) {
                newPref.appendChild(doc.createTextNode(value));
            } else {
                newPref.setAttribute("value", value);
            }
            int lastPrefIndex = prefList.getLength() - 1;
            Element lastPref = (Element) prefList.item(lastPrefIndex);
            Element nextPrefNode = (Element) lastPref.getNextSibling();
            if (nextPrefNode != null) {
                widgetConfNode.insertBefore(newPref, nextPrefNode);
            } else {
                widgetConfNode.appendChild(newPref);
            }
        }
    }
}

From source file:org.infoscoop.service.WidgetConfService.java

/**
 * @param doc/*  w w  w  . j a  v a2s . com*/
 * @param widgetConfNode
 * @param updatePrefList
 * @throws JSONException
 */
private void updateUserPrefNode(Document doc, Element widgetConfNode, JSONObject updatePrefList)
        throws JSONException {

    NodeList prefList = widgetConfNode.getElementsByTagName("UserPref");

    Iterator keys = updatePrefList.keys();
    while (keys.hasNext()) {
        String id = (String) keys.next();
        JSONObject prefJson = updatePrefList.getJSONObject(id);

        if (!prefJson.has("name"))
            continue;

        String name = prefJson.getString("name");

        if (!prefJson.has("default_value"))
            continue;
        String value = prefJson.getString("default_value");

        String datatype = null;
        if (prefJson.has("datatype"))
            datatype = prefJson.getString("datatype");

        int prefLength = prefList.getLength();
        boolean update = false;
        for (int i = 0; i < prefLength; i++) {
            Element pref = (Element) prefList.item(i);
            if (!name.equals(pref.getAttribute("name")))
                continue;

            if ("xml".equals(datatype) || "json".equals(datatype)) {
                while (pref.getFirstChild() != null)
                    pref.removeChild(pref.getFirstChild());

                pref.appendChild(doc.createTextNode(value));
            } else {
                pref.setAttribute("default_value", value);
            }

            update = true;
        }

        // is this code require ?
        // and this code is obsolete.  what is "inputType" attribute ?
        if (!update) {
            Element newPref = doc.createElement("UserPref");
            newPref.setAttribute("name", name);

            if (("xml".equals(datatype) || "json".equals(datatype))) {
                newPref.appendChild(doc.createTextNode(value));
            } else {
                newPref.setAttribute("default_value", value);
            }

            String inputType = prefJson.getString("inputType");
            if (inputType != null) {
                newPref.setAttribute("inputType", inputType);
            }
            String displayName = prefJson.getString("display_name");
            if (prefJson.has("display_name")) {
                newPref.setAttribute("display_name", displayName);
            }
            if (prefJson.has("EnumValue")) {
                JSONArray options = prefJson.getJSONArray("EnumValue");
                for (int j = 0; j < options.length(); j++) {
                    JSONObject option = options.getJSONObject(j);
                    Element optionNode = doc.createElement("EnumValue");
                    optionNode.setAttribute("display_value", option.getString("display_value"));
                    optionNode.setAttribute("value", option.getString("value"));
                    newPref.appendChild(optionNode);
                }
            }

            int lastPrefIndex = prefList.getLength() - 1;
            Element lastPref = (Element) prefList.item(lastPrefIndex);
            Element nextPrefNode = (Element) lastPref.getNextSibling();
            if (nextPrefNode != null) {
                widgetConfNode.insertBefore(newPref, nextPrefNode);
            } else {
                widgetConfNode.appendChild(newPref);
            }
        }
    }
}

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

/**
   Get the delete set if any from the plf and process each delete command
   removing any that fail from the delete set so that the delete set is
   self cleaning./*from w  w  w  . j a va 2  s . c om*/
*/
static void applyAndUpdateDeleteSet(Document plf, Document ilf, IntegrationResult result) {

    Element dSet = null;
    try {
        dSet = getDeleteSet(plf, null, false);
    } catch (Exception e) {
        LOG.error("Exception occurred while getting user's DLM delete-set.", e);
    }

    if (dSet == null)
        return;

    NodeList deletes = dSet.getChildNodes();

    for (int i = deletes.getLength() - 1; i >= 0; i--) {
        if (applyDelete((Element) deletes.item(i), ilf) == false) {
            dSet.removeChild(deletes.item(i));
            result.changedPLF = true;
        } else {
            result.changedILF = true;
        }
    }

    if (dSet.getChildNodes().getLength() == 0) {
        plf.getDocumentElement().removeChild(dSet);
        result.changedPLF = true;
    }
}

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

/**
 * Removes unwanted and hidden folders, then changes all node ids to their 
 * globally safe incorporated version.//from   ww w .  jav a  2 s  . c  o m
 */
private void fragmentizeLayout(UserView view, FragmentDefinition fragment) {
    // if fragment not bound to user or layout empty due to error, return
    if (view.getUserId() == -1 || view.layout == null)
        return;

    // Choose what types of content to apply from the fragment
    Pattern contentPattern = STANDARD_PATTERN; // default
    boolean allowExpandedContent = Boolean
            .parseBoolean(PropertiesManager.getProperty(PROPERTY_ALLOW_EXPANDED_CONTENT));
    if (allowExpandedContent) {
        contentPattern = EXPANDED_PATTERN;
    }

    // remove all non-regular or hidden top level folders
    // skip root folder that is only child of top level layout element
    Element layout = view.layout.getDocumentElement();
    Element root = (Element) layout.getFirstChild();
    NodeList children = root.getChildNodes();

    // process the children backwards since as we delete some the indices
    // shift around
    for (int i = children.getLength() - 1; i >= 0; i--) {
        Node node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals("folder")) {
            Element folder = (Element) node;

            // strip out folder types 'header', 'footer' and regular, 
            // hidden folder "User Preferences" since users have their own
            boolean isApplicable = contentPattern.matcher(folder.getAttribute("type")).matches();
            if (!isApplicable || folder.getAttribute("hidden").equals("true")) {
                try {
                    root.removeChild(folder);
                } catch (Exception e) {
                    throw new RuntimeException("Anomaly occurred while stripping out "
                            + " portions of layout for fragment '" + fragment.getName()
                            + "'. The fragment will not be available for " + "inclusion into user layouts.", e);
                }
            }
        }
    }
    // now re-lable all remaining nodes below root to have a safe system
    // wide id.

    setIdsAndAttribs(layout, layout.getAttribute(Constants.ATT_ID), "" + fragment.getIndex(),
            "" + fragment.getPrecedence());
}

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

/**
   Get the parm edit set if any from the plf and process each edit command
   removing any that fail from the set so that the set is self cleaning.
 * @throws Exception//from  ww  w.j a  va 2 s. c  o m
*/
static void applyAndUpdateParmEditSet(Document plf, Document ilf, IntegrationResult result) {

    Element pSet = null;
    try {
        pSet = getParmEditSet(plf, null, false);
    } catch (Exception e) {
        LOG.error("Exception occurred while getting user's DLM " + "paramter-edit-set.", e);
    }

    if (pSet == null)
        return;

    NodeList edits = pSet.getChildNodes();

    for (int i = edits.getLength() - 1; i >= 0; i--) {
        if (applyEdit((Element) edits.item(i), ilf) == false) {
            pSet.removeChild(edits.item(i));
            result.changedPLF = true;
        } else {
            result.changedILF = true;
        }
    }

    if (pSet.getChildNodes().getLength() == 0) {
        plf.getDocumentElement().removeChild(pSet);
        result.changedPLF = true;
    }
}

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

private static void applyChildChanges(Element plfParent, Element ilfParent, IntegrationResult result)
        throws PortalException {
    Element positions = null;/*ww w .  jav  a 2 s  . c om*/
    Element node = (Element) plfParent.getFirstChild();

    while (node != null) {
        Element nextNode = (Element) node.getNextSibling();

        if (node.getNodeName().equals("folder"))
            mergeFolder(node, plfParent, ilfParent, result);
        else if (node.getNodeName().equals(Constants.ELM_POSITION_SET))
            positions = node;
        else if (node.getNodeName().equals("channel"))
            mergeChannel(node, plfParent, ilfParent, result);
        node = nextNode;
    }

    if (positions != null) {
        IntegrationResult posResult = new IntegrationResult();
        if (LOG.isInfoEnabled())
            LOG.info("applying positions");
        PositionManager.applyPositions(ilfParent, positions, posResult);
        if (posResult.changedILF == false) {
            if (LOG.isInfoEnabled())
                LOG.info("removing positionSet");
            plfParent.removeChild(positions);
            result.changedPLF = true;
        } else {
            result.changedILF = true;
            if (posResult.changedPLF)
                result.changedPLF = true;
        }
    }
}

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

private static void mergeChannel(Element plfChild, Element plfParent, Element ilfParent,
        IntegrationResult result) {/* w ww  .  j  a  v a 2 s.  com*/
    String id = plfChild.getAttribute(Constants.ATT_ID);

    if (id.startsWith(Constants.FRAGMENT_ID_USER_PREFIX)) {
        // incorporated channel - if a copy of an inc'd channel is in the
        // plf it is because either it has attribute edits. It does not
        // imply movement.
        // That is accomplished by the position set. So see if it still
        // exists in the ilf for applying changes

        Document ilf = ilfParent.getOwnerDocument();
        Element original = ilf.getElementById(id);

        if (original == null) {
            // not there anymore, discard from plf
            plfParent.removeChild(plfChild);
            result.changedPLF = true;
            return;
        }

        // found it, apply changes and see if they had any affect
        boolean attributeChanged = false;
        IntegrationResult childChanges = new IntegrationResult();

        attributeChanged = EditManager.applyEditSet(plfChild, original);
        applyChildChanges(plfChild, original, childChanges);

        if (attributeChanged == false && childChanges.changedILF == false) {
            // no changes were used so remove this guy from plf.
            plfParent.removeChild(plfChild);
            result.changedPLF = true;
        } else
            result.changedILF = true;
        // need to pass on up whether PLF changed in called methods
        if (childChanges.changedPLF)
            result.changedPLF = true;
    } else // plf channel
    {
        if (LOG.isInfoEnabled())
            LOG.info("merging into ilf channel " + id);

        if (ilfParent.getAttribute(Constants.ATT_ADD_CHILD_ALLOWED).equals("false")) {
            if (LOG.isInfoEnabled())
                LOG.info("removing from plf disallowed add of channel " + id);
            plfParent.removeChild(plfChild);
            result.changedPLF = true;
        } else {
            appendChild(plfChild, ilfParent, true);
            result.changedILF = true;
        }
    }
}

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

private static void mergeFolder(Element plfChild, Element plfParent, Element ilfParent,
        IntegrationResult result) throws PortalException {
    String id = plfChild.getAttribute(Constants.ATT_ID);

    if (id.startsWith(Constants.FRAGMENT_ID_USER_PREFIX)) {
        // incorporated folder - if a copy of an inc'd folder is in the
        // plf it is because either it has attribute edits or there are
        // nested child changes to be applied. It does not imply movement.
        // That is accomplished by the position set. So see if it still
        // exists in the ilf for applying changes

        Document ilf = ilfParent.getOwnerDocument();
        Element original = ilf.getElementById(id);

        if (original == null) {
            // not there anymore, discard from plf
            plfParent.removeChild(plfChild);
            result.changedPLF = true;/*from  w  w w  .  ja  v a  2s. com*/
            return;
        }

        // found it, apply changes and see if they had any affect
        boolean attributeChanged = false;
        IntegrationResult childChanges = new IntegrationResult();

        attributeChanged = EditManager.applyEditSet(plfChild, original);
        applyChildChanges(plfChild, original, childChanges);

        if (attributeChanged == false && childChanges.changedILF == false) {
            // no changes were used so remove this guy from plf.
            plfParent.removeChild(plfChild);
            result.changedPLF = true;
        } else
            result.changedILF = true;
        // need to pass on up whether PLF changed in called methods
        if (childChanges.changedPLF)
            result.changedPLF = true;
    } else {
        // plf folder - the real node. What is being portrayed in this
        // case is a plf node that is supposed to be added into the ilf
        // parent

        if (ilfParent.getAttribute(Constants.ATT_ADD_CHILD_ALLOWED).equals("false")) {
            // nope, delete directive from plf
            if (LOG.isInfoEnabled())
                LOG.info("removing folder from plf " + id);
            plfParent.removeChild(plfChild);
            result.changedPLF = true;
            return;
        }
        Element ilfChild = appendChild(plfChild, ilfParent, false);
        result.changedILF = true;

        IntegrationResult childChanges = new IntegrationResult();
        applyChildChanges(plfChild, ilfChild, childChanges);

        if (childChanges.changedPLF)
            result.changedPLF = true;
    }
}

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

/**
   This method trims down the position set to the position directives on
   the node info elements still having a position directive. Any directives
   that violated restrictions were removed from the node info objects so
   the position set should be made to match the order of those still
   having one.//  w  ww. j a  va2  s  . c om
 */
static void adjustPositionSet(List<NodeInfo> order, Element positionSet, IntegrationResult result) {
    Node nodeToMatch = positionSet.getFirstChild();
    Element nodeToInsertBefore = positionSet.getOwnerDocument().createElement("INSERT_POINT");
    positionSet.insertBefore(nodeToInsertBefore, nodeToMatch);

    for (Iterator iter = order.iterator(); iter.hasNext();) {
        NodeInfo ni = (NodeInfo) iter.next();

        if (ni.positionDirective != null) {
            // found one check it against the current one in the position
            // set to see if it is different. If so then indicate that
            // something (the position set) has changed in the plf
            if (ni.positionDirective != nodeToMatch)
                result.changedPLF = true;

            // now bump the insertion point forward prior to
            // moving on to the next position node to be evaluated
            if (nodeToMatch != null)
                nodeToMatch = nodeToMatch.getNextSibling();

            // now insert it prior to insertion point
            positionSet.insertBefore(ni.positionDirective, nodeToInsertBefore);
        }
    }

    // now for any left over after the insert point remove them.

    while (nodeToInsertBefore.getNextSibling() != null)
        positionSet.removeChild(nodeToInsertBefore.getNextSibling());

    // now remove the insertion point
    positionSet.removeChild(nodeToInsertBefore);
}