Example usage for org.w3c.dom Node insertBefore

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

Introduction

In this page you can find the example usage for org.w3c.dom Node insertBefore.

Prototype

public Node insertBefore(Node newChild, Node refChild) throws DOMException;

Source Link

Document

Inserts the node newChild before the existing child node refChild.

Usage

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

public String deleteSection(String sectionsSeqXML, String section_id) throws MeleteException {
    try {//  ww  w  .j av  a 2  s.c o  m
        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 {/*from   w  ww.  j ava2s.  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 {//  w ww  .j  a v  a 2  s . com
        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.ja  v a 2s  .c  o  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 {//w w w. j  a  v  a2s .  c o  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 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.
 *///  w ww  . ja v  a2 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.
 *///from  w  ww  .j av a2 s.  com

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 moves the component up a step if possible within the same slot. 
 *///from  ww w  .  j ava2s . co 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.common.contenttypeeditor.actions.ViewContentTypeDefinitionAction.java

/**
 * This method moves an content type assetKey up one step.
 */// w  ww  .  ja v a  2s  . c  o  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 UPDATED;
}

From source file:org.infoglue.common.contenttypeeditor.actions.ViewContentTypeDefinitionAction.java

/**
 * This method moves an content type asset key down one step.
 *//*from  w  ww. jav  a  2 s  .  co  m*/

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 UPDATED;
}