Example usage for org.w3c.dom Node removeChild

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

Introduction

In this page you can find the example usage for org.w3c.dom Node 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.etudes.component.app.melete.SubSectionUtilImpl.java

public String deleteSection(String sectionsSeqXML, String section_id) throws MeleteException {
    try {//from w w w . j  a  v a  2s  . c om
        org.w3c.dom.Document subSectionW3CDOM = Xml.readDocumentFromString(sectionsSeqXML);
        org.w3c.dom.Element root = subSectionW3CDOM.getDocumentElement();
        org.w3c.dom.Element deleteThisElement = subSectionW3CDOM.getElementById(section_id);
        if (deleteThisElement != null) {
            org.w3c.dom.Node deleteElementParent = deleteThisElement.getParentNode();

            // child nodes becomes children of parent node
            if (deleteThisElement.hasChildNodes()) {
                NodeList children = deleteThisElement.getChildNodes();
                for (int i = 0; i < children.getLength(); i++) {
                    org.w3c.dom.Node deleteThisElementChild = children.item(i);
                    if (deleteThisElementChild.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE)
                        deleteElementParent.insertBefore(deleteThisElementChild.cloneNode(true),
                                deleteThisElement);
                }
            }

            //remove the element
            deleteElementParent.removeChild(deleteThisElement);
        }

        return writeDocumentToString(subSectionW3CDOM);
    } catch (Exception ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("some other error on delete subsections xml string" + ex.toString());
            ex.printStackTrace();
        }
        throw new MeleteException("delete_module_fail");
    }
}

From source file:org.etudes.component.app.melete.SubSectionUtilImpl.java

public String moveAllUpSection(String sectionsSeqXML, String section_id) throws MeleteException {
    try {//w w w .  j a va 2 s  . c om
        org.w3c.dom.Document subSectionW3CDOM = Xml.readDocumentFromString(sectionsSeqXML);
        org.w3c.dom.Element root = subSectionW3CDOM.getDocumentElement();
        org.w3c.dom.Element moveUpThisElement = subSectionW3CDOM.getElementById(section_id);
        org.w3c.dom.Node moveUpThisElementParent = moveUpThisElement.getParentNode();
        org.w3c.dom.Node cloneMoveElement = moveUpThisElement.cloneNode(true);

        org.w3c.dom.Node FirstChildOfmoveUpThisElementParent = moveUpThisElementParent.getFirstChild();
        if (!FirstChildOfmoveUpThisElementParent.equals(moveUpThisElement)) {
            moveUpThisElementParent.insertBefore(cloneMoveElement, FirstChildOfmoveUpThisElementParent);
            moveUpThisElementParent.removeChild(moveUpThisElement);
        }

        return writeDocumentToString(subSectionW3CDOM);
    } catch (Exception ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("some other error on moving up subsections xml string" + ex.toString());
            ex.printStackTrace();
        }
        throw new MeleteException("move_up_fail");
    }
}

From source file:org.etudes.component.app.melete.SubSectionUtilImpl.java

public String moveUpSection(String sectionsSeqXML, String section_id) throws MeleteException {
    try {//from   w  w  w .j  a v  a2  s . c o  m
        org.w3c.dom.Document subSectionW3CDOM = Xml.readDocumentFromString(sectionsSeqXML);
        org.w3c.dom.Element root = subSectionW3CDOM.getDocumentElement();
        org.w3c.dom.Element moveUpThisElement = subSectionW3CDOM.getElementById(section_id);
        org.w3c.dom.Node moveUpThisElementParent = moveUpThisElement.getParentNode();
        org.w3c.dom.Node cloneMoveElement = moveUpThisElement.cloneNode(true);

        if (!moveUpThisElementParent.getFirstChild().equals(moveUpThisElement)) {
            org.w3c.dom.Node beforeElement = moveUpThisElement.getPreviousSibling();
            moveUpThisElementParent.insertBefore(cloneMoveElement, beforeElement);
            moveUpThisElementParent.removeChild(moveUpThisElement);
        }

        return writeDocumentToString(subSectionW3CDOM);
    } catch (Exception ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("some other error on moving up subsections xml string" + ex.toString());
            ex.printStackTrace();
        }
        throw new MeleteException("move_up_fail");
    }
}

From source file:org.etudes.component.app.melete.SubSectionUtilImpl.java

public String moveDownSection(String sectionsSeqXML, String section_id) throws MeleteException {
    try {/*from w w  w .j a  v  a2s  . co m*/
        org.w3c.dom.Document subSectionW3CDOM = Xml.readDocumentFromString(sectionsSeqXML);
        org.w3c.dom.Element root = subSectionW3CDOM.getDocumentElement();
        org.w3c.dom.Element moveDownThisElement = subSectionW3CDOM.getElementById(section_id);
        org.w3c.dom.Node afterElementParent = moveDownThisElement.getParentNode();
        if (!afterElementParent.getLastChild().equals(moveDownThisElement)) {
            org.w3c.dom.Node afterElement = moveDownThisElement.getNextSibling();
            org.w3c.dom.Node cloneafterElement = afterElement.cloneNode(true);
            afterElementParent.insertBefore(cloneafterElement, moveDownThisElement);
            afterElementParent.removeChild(afterElement);
        }
        return writeDocumentToString(subSectionW3CDOM);
    } catch (Exception ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("some other error on moving down subsections xml string" + ex.toString());
            ex.printStackTrace();
        }
        throw new MeleteException("move_down_fail");
    }
}

From source file:org.etudes.component.app.melete.SubSectionUtilImpl.java

public String moveAllDownSection(String sectionsSeqXML, String section_id) throws MeleteException {
    try {/*from  w ww.  ja v a2  s .c  om*/
        org.w3c.dom.Document subSectionW3CDOM = Xml.readDocumentFromString(sectionsSeqXML);
        org.w3c.dom.Element root = subSectionW3CDOM.getDocumentElement();
        org.w3c.dom.Element moveDownThisElement = subSectionW3CDOM.getElementById(section_id);
        org.w3c.dom.Node cloneMoveElement = moveDownThisElement.cloneNode(true);
        org.w3c.dom.Node afterElementParent = moveDownThisElement.getParentNode();
        org.w3c.dom.Node LastChildOfafterElementParent = afterElementParent.getLastChild();
        org.w3c.dom.Node cloneLastChildElement = LastChildOfafterElementParent.cloneNode(true);

        if (!LastChildOfafterElementParent.equals(moveDownThisElement)) {
            afterElementParent.replaceChild(cloneMoveElement, LastChildOfafterElementParent);
            org.w3c.dom.Node newLastChild = afterElementParent.getLastChild();
            afterElementParent.insertBefore(cloneLastChildElement, newLastChild);
            afterElementParent.removeChild(moveDownThisElement);
        }
        return writeDocumentToString(subSectionW3CDOM);
    } catch (Exception ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("some other error on moving down subsections xml string" + ex.toString());
            ex.printStackTrace();
        }
        throw new MeleteException("move_down_fail");
    }
}

From source file:org.infoglue.cms.applications.managementtool.actions.ViewContentTypeDefinitionAction.java

/**
 * This method moves an content type assetKey up one step.
 *//*from   w w  w  .j  av a 2 s  .co m*/

public String doMoveAssetKeyUp() throws Exception {
    this.initialize(getContentTypeDefinitionId());

    try {
        Document document = createDocumentFromDefinition();

        String attributesXPath = "/xs:schema/xs:simpleType[@name = '"
                + ContentTypeDefinitionController.ASSET_KEYS + "']/xs:restriction/xs:enumeration[@value='"
                + this.assetKey + "']";
        NodeList anl = XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath);
        if (anl != null && anl.getLength() > 0) {
            Element element = (Element) anl.item(0);
            Node parentElement = element.getParentNode();
            Node previuosSibling = element.getPreviousSibling();
            if (previuosSibling != null) {
                parentElement.removeChild(element);
                parentElement.insertBefore(element, previuosSibling);
            }
        }

        saveUpdatedDefinition(document);
    } catch (Exception e) {
        e.printStackTrace();
    }

    this.initialize(getContentTypeDefinitionId());

    return USE_EDITOR;
}

From source file:org.infoglue.cms.applications.managementtool.actions.ViewContentTypeDefinitionAction.java

/**
 * This method moves an content type asset key down one step.
 *//* w w  w.  j ava  2  s .c  om*/

public String doMoveAssetKeyDown() throws Exception {
    this.initialize(getContentTypeDefinitionId());

    try {
        Document document = createDocumentFromDefinition();

        String attributesXPath = "/xs:schema/xs:simpleType[@name = '"
                + ContentTypeDefinitionController.ASSET_KEYS + "']/xs:restriction/xs:enumeration[@value='"
                + this.assetKey + "']";
        NodeList anl = XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath);
        if (anl != null && anl.getLength() > 0) {
            Element element = (Element) anl.item(0);
            Node parentElement = element.getParentNode();
            Node nextSibling = element.getNextSibling();
            if (nextSibling != null) {
                parentElement.removeChild(nextSibling);
                parentElement.insertBefore(nextSibling, element);
            }
        }

        saveUpdatedDefinition(document);
    } catch (Exception e) {
        e.printStackTrace();
    }

    this.initialize(getContentTypeDefinitionId());
    return USE_EDITOR;
}

From source file:org.infoglue.cms.applications.structuretool.actions.ViewSiteNodePageComponentsAction.java

/**
 * This method adds a component to the page. 
 *//*from ww  w.j a v a 2  s  .  c om*/

public String doAddOrReplaceComponent() throws Exception {
    logger.info("************************************************************");
    logger.info("* ADDING OR REPLACING COMPONENT                            *");
    logger.info("************************************************************");
    logger.info("siteNodeId:" + this.siteNodeId);
    logger.info("languageId:" + this.languageId);
    logger.info("contentId:" + this.contentId);
    logger.info("queryString:" + this.getRequest().getQueryString());
    logger.info("parentComponentId:" + this.parentComponentId);
    //logger.info("componentId:" + this.componentId);
    logger.info("slotId:" + this.slotId);
    logger.info("specifyBaseTemplate:" + this.specifyBaseTemplate);
    logger.info("pagePartContentId:" + this.pagePartContentId);

    try {
        initialize();

        logger.info("masterLanguageId:" + this.masterLanguageVO.getId());

        Integer newComponentId = new Integer(0);

        String componentXML = getPageComponentsString(siteNodeId, this.masterLanguageVO.getId());

        Document document = XMLHelper.readDocumentFromByteArray(componentXML.getBytes("UTF-8"));
        String componentXPath = "//component[@id=" + this.parentComponentId + "]";

        Node componentNode = org.apache.xpath.XPathAPI.selectSingleNode(document.getDocumentElement(),
                componentXPath);
        if (componentNode != null) {
            //Element componentElement = (Element)componentNode;

            String componentsXPath = "//component";
            NodeList nodes = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(),
                    componentsXPath);
            for (int i = 0; i < nodes.getLength(); i++) {
                Element element = (Element) nodes.item(i);
                if (new Integer(element.getAttribute("id")).intValue() > newComponentId.intValue())
                    newComponentId = new Integer(element.getAttribute("id"));
            }
            newComponentId = new Integer(newComponentId.intValue() + 1);

            NodeList childNodes = componentNode.getChildNodes();
            logger.info("childNodes:" + childNodes.getLength());

            Node child = componentNode.getFirstChild();
            while (child != null) {
                logger.info("Removing:" + child);
                componentNode.removeChild(child);
                child = componentNode.getFirstChild();
            }

            logger.info("childNodes:" + childNodes.getLength());
            //StringBuffer sb = new StringBuffer();
            //XMLHelper.serializeDom(componentNode, sb);
            //logger.info("SB:" + sb);

            if (this.pagePartContentId != null) {
                ContentVersionVO pagePartContentVersionVO = ContentVersionController
                        .getContentVersionController()
                        .getLatestActiveContentVersionVO(this.pagePartContentId, this.masterLanguageVO.getId());
                String componentStructure = ContentVersionController.getContentVersionController()
                        .getAttributeValue(pagePartContentVersionVO.getId(), "ComponentStructure", false);

                componentStructure = componentStructure.replaceAll(" isInherited=\"true\"", "");
                componentStructure = componentStructure.replaceAll(" pagePartTemplateContentId=\"-1\"", "");
                componentStructure = componentStructure
                        .replaceAll("<property name=\"pagePartContentId\" path=\".*?\"></property>", "");
                componentStructure = componentStructure
                        .replaceAll("<property name=\"pagePartContentId\" path=\".*?\"/>", "");
                componentStructure = componentStructure.replaceAll("<properties>",
                        "<properties><property name=\"pagePartContentId\" path=\"" + pagePartContentId
                                + "\"/>");
                logger.info("componentStructure:" + componentStructure);

                Document componentStructureDocument = XMLHelper
                        .readDocumentFromByteArray(componentStructure.getBytes("UTF-8"));
                Node rootNode = componentStructureDocument.getDocumentElement();

                componentNode.appendChild(document.importNode(rootNode, true));

                String modifiedXML = XMLHelper.serializeDom(document, new StringBuffer()).toString();

                ContentVO contentVO = NodeDeliveryController
                        .getNodeDeliveryController(siteNodeId, this.masterLanguageVO.getId(), contentId)
                        .getBoundContent(this.getInfoGluePrincipal(), siteNodeId, this.masterLanguageVO.getId(),
                                true, "Meta information", DeliveryContext.getDeliveryContext());
                ContentVersionVO contentVersionVO = ContentVersionController.getContentVersionController()
                        .getLatestActiveContentVersionVO(contentVO.getId(), this.masterLanguageVO.getId());

                ContentVersionController.getContentVersionController().updateAttributeValue(
                        contentVersionVO.getContentVersionId(), "ComponentStructure", modifiedXML,
                        this.getInfoGluePrincipal());
            }
        }

        logger.info("newComponentId:" + newComponentId);

        this.url = getComponentRendererUrl() + getComponentRendererAction() + "?siteNodeId=" + this.siteNodeId
                + "&languageId=" + this.languageId + "&contentId=" + this.contentId + "&focusElementId="
                + newComponentId + "&activatedComponentId=" + newComponentId + "&componentContentId="
                + this.componentId + "&showSimple=" + this.showSimple;
        //this.getResponse().sendRedirect(url);      

        this.url = this.getResponse().encodeURL(url);
        this.getResponse().sendRedirect(url);
        return NONE;
    } catch (Exception e) {
        logger.error("Error adding/changing component:" + e.getMessage());
        logger.warn("Error adding/changing component:" + e.getMessage(), e);
        return ERROR;
    }
}

From source file:org.infoglue.cms.applications.structuretool.actions.ViewSiteNodePageComponentsAction.java

/**
 * This method moves the component up a step if possible within the same slot. 
 *//*w  w  w. java 2s . c  o m*/

public String doMoveComponent() throws Exception {
    initialize();

    String componentXML = getPageComponentsString(siteNodeId, this.masterLanguageVO.getId());
    //logger.info("componentXML:" + componentXML);

    Document document = XMLHelper.readDocumentFromByteArray(componentXML.getBytes("UTF-8"));
    String componentXPath = "//component[@id=" + this.componentId + "]";

    NodeList anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), componentXPath);
    if (anl.getLength() > 0) {
        Element component = (Element) anl.item(0);
        String name = component.getAttribute("name");
        //logger.info(XMLHelper.serializeDom(component, new StringBuffer()));
        Node parentNode = component.getParentNode();

        boolean hasChanged = false;

        if (this.direction.intValue() == 0) //Up
        {
            Node previousNode = component.getPreviousSibling();

            while (previousNode != null && previousNode.getNodeType() != Node.ELEMENT_NODE) {
                previousNode = previousNode.getPreviousSibling();
                //break;
            }

            Element element = ((Element) previousNode);
            while (element != null && !element.getAttribute("name").equalsIgnoreCase(name)) {
                previousNode = previousNode.getPreviousSibling();
                while (previousNode != null && previousNode.getNodeType() != Node.ELEMENT_NODE) {
                    previousNode = previousNode.getPreviousSibling();
                    //break;
                }
                element = ((Element) previousNode);
            }

            if (previousNode != null) {
                parentNode.removeChild(component);
                parentNode.insertBefore(component, previousNode);
                hasChanged = true;
            }
        } else if (this.direction.intValue() == 1) //Down
        {
            Node nextNode = component.getNextSibling();

            while (nextNode != null && nextNode.getNodeType() != Node.ELEMENT_NODE) {
                nextNode = nextNode.getNextSibling();
                break;
            }

            Element element = ((Element) nextNode);
            while (element != null && !element.getAttribute("name").equalsIgnoreCase(name)) {
                nextNode = nextNode.getNextSibling();
                element = ((Element) nextNode);
            }

            if (nextNode != null)
                nextNode = nextNode.getNextSibling();

            if (nextNode != null) {
                parentNode.removeChild(component);
                parentNode.insertBefore(component, nextNode);
                hasChanged = true;
            } else {
                parentNode.removeChild(component);
                parentNode.appendChild(component);
                hasChanged = true;
            }
        }

        if (hasChanged) {
            String modifiedXML = XMLHelper.serializeDom(document, new StringBuffer()).toString();
            //logger.info("modifiedXML:" + modifiedXML);

            ContentVO contentVO = NodeDeliveryController
                    .getNodeDeliveryController(siteNodeId, languageId, contentId)
                    .getBoundContent(this.getInfoGluePrincipal(), siteNodeId, this.masterLanguageVO.getId(),
                            true, "Meta information", DeliveryContext.getDeliveryContext());
            ContentVersionVO contentVersionVO = ContentVersionController.getContentVersionController()
                    .getLatestActiveContentVersionVO(contentVO.getId(), this.masterLanguageVO.getId());
            ContentVersionController.getContentVersionController().updateAttributeValue(
                    contentVersionVO.getContentVersionId(), "ComponentStructure", modifiedXML,
                    this.getInfoGluePrincipal());
        }
    }

    this.url = getComponentRendererUrl() + getComponentRendererAction() + "?siteNodeId=" + this.siteNodeId
            + "&languageId=" + this.languageId + "&contentId=" + this.contentId + "&focusElementId="
            + this.componentId + "&showSimple=" + this.showSimple;
    //this.getResponse().sendRedirect(url);      

    this.url = this.getResponse().encodeURL(url);
    this.getResponse().sendRedirect(url);
    return NONE;
}

From source file:org.infoglue.cms.applications.structuretool.actions.ViewSiteNodePageComponentsAction.java

private void changeComponent(Integer siteNodeId, Boolean recursive, Boolean regardAsCompatible)
        throws Exception {
    String componentXML = getPageComponentsString(siteNodeId, this.masterLanguageVO.getId());
    logger.info("componentXML:" + componentXML);

    Document document = XMLHelper.readDocumentFromByteArray(componentXML.getBytes("UTF-8"));
    String componentXPath = "//component[@id=" + this.componentId + "]";

    NodeList anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), componentXPath);
    if (anl.getLength() > 0 && this.newComponentContentId != null) {
        Element component = (Element) anl.item(0);

        ContentVO contentVO = ContentController.getContentController()
                .getContentVOWithId(this.newComponentContentId);
        ContentTypeDefinitionVO contentTypeDefinitionVO = ContentTypeDefinitionController.getController()
                .getContentTypeDefinitionVOWithId(contentVO.getContentTypeDefinitionId());
        boolean isPagePartReference = false;
        if (contentTypeDefinitionVO.getName().equals("PagePartTemplate"))
            isPagePartReference = true;/* www.ja  va  2  s. c  o m*/

        ContentVersionVO newComponentContentVersionVO = ContentVersionController.getContentVersionController()
                .getLatestActiveContentVersionVO(this.newComponentContentId, this.masterLanguageVO.getId());
        if (newComponentContentVersionVO == null) {
            LanguageVO contentMasterLanguageVO = LanguageController.getController()
                    .getMasterLanguage(contentVO.getRepositoryId());
            newComponentContentVersionVO = ContentVersionController.getContentVersionController()
                    .getLatestActiveContentVersionVO(this.newComponentContentId,
                            contentMasterLanguageVO.getId());
        }

        if (newComponentContentVersionVO != null && !regardAsCompatible) {
            String template = ContentVersionController.getContentVersionController()
                    .getAttributeValue(newComponentContentVersionVO, "Template", false);
            logger.info("template:" + template);

            String subComponentsXPath = "//component[@id=" + this.componentId + "]//component";
            NodeList subComponents = org.apache.xpath.XPathAPI.selectNodeList(component, subComponentsXPath);
            logger.info("subComponents:" + subComponents.getLength());
            for (int i = 0; i < subComponents.getLength(); i++) {
                Element subComponent = (Element) subComponents.item(i);
                if (isPagePartReference) {
                    //Removing children if it was a pagePartReference
                    NodeList propertiesNodeList = subComponent.getElementsByTagName("properties");
                    if (propertiesNodeList.getLength() > 0) {
                        Element propertiesElement = (Element) propertiesNodeList.item(0);
                        NodeList propertyNodeList = propertiesElement.getElementsByTagName("property");
                        for (int j = 0; j < propertyNodeList.getLength(); j++) {
                            Element property = (Element) propertyNodeList.item(j);
                            Node parentNode = property.getParentNode();
                            parentNode.removeChild(property);
                        }
                    }

                    Node parentNode = subComponent.getParentNode();
                    parentNode.removeChild(subComponent);
                } else {
                    String slotId = subComponent.getAttribute("name");
                    logger.info("subComponent slotId:" + slotId);
                    if (template.indexOf("id=\"" + slotId + "\"") == -1) {
                        logger.info("deleting subComponent as it was not part of the new template");
                        Node parentNode = subComponent.getParentNode();
                        parentNode.removeChild(subComponent);
                    }
                }
            }
        }

        component.setAttribute("contentId", "" + this.newComponentContentId);
        if (isPagePartReference)
            component.setAttribute("isPagePartReference", "true");

        String modifiedXML = XMLHelper.serializeDom(document, new StringBuffer()).toString();
        logger.info("modifiedXML:" + modifiedXML);

        ContentVO boundContentVO = NodeDeliveryController
                .getNodeDeliveryController(siteNodeId, this.masterLanguageVO.getId(), contentId)
                .getBoundContent(this.getInfoGluePrincipal(), siteNodeId, this.masterLanguageVO.getId(), true,
                        "Meta information", DeliveryContext.getDeliveryContext());
        ContentVersionVO contentVersionVO = ContentVersionController.getContentVersionController()
                .getLatestActiveContentVersionVO(boundContentVO.getId(), this.masterLanguageVO.getId());

        ContentVersionController.getContentVersionController().updateAttributeValue(
                contentVersionVO.getContentVersionId(), "ComponentStructure", modifiedXML,
                this.getInfoGluePrincipal());
    }

    if (recursive) {
        List<SiteNodeVO> childSiteNodeVOList = SiteNodeController.getController()
                .getSiteNodeChildrenVOList(siteNodeId);
        for (SiteNodeVO childSiteNodeVO : childSiteNodeVOList)
            changeComponent(childSiteNodeVO.getId(), recursive, regardAsCompatible);
    }
}