Example usage for org.w3c.dom Node appendChild

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

Introduction

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

Prototype

public Node appendChild(Node newChild) throws DOMException;

Source Link

Document

Adds the node newChild to the end of the list of children of this node.

Usage

From source file:org.gvnix.web.screen.roo.addon.SeleniumServicesImpl.java

/**
 * Add verification value command for register pattern field.
 * /*from   w  ww  . ja v a2s. c  om*/
 * @param document Document to write command
 * @param entity Field parent entity
 * @param field Field to verify value
 * @return Verification node
 */
protected Node verifyCommandRegister(Document document, JavaType entity, FieldMetadata field) {

    Node tr = document.createElement("tr");

    Node td1 = tr.appendChild(document.createElement("td"));
    td1.setTextContent("verifyText");

    Node td2 = tr.appendChild(document.createElement("td"));
    td2.setTextContent(XmlUtils.convertId("_s_" + entity.getFullyQualifiedTypeName() + "_"
            + field.getFieldName().getSymbolName() + "_" + field.getFieldName().getSymbolName() + "_id"));

    Node td3 = tr.appendChild(document.createElement("td"));
    td3.setTextContent(convertToInitializer(field));

    return tr;
}

From source file:org.gvnix.web.screen.roo.addon.SeleniumServicesImpl.java

/**
 * Add verification value command for tabular pattern field.
 * /*  w w  w. j  a v  a2s.c  o  m*/
 * @param document Document to write command
 * @param entity Field parent entity
 * @param field Field to verify value
 * @return Verification node
 */
protected Node verifyCommandTabular(Document document, JavaType entity, FieldMetadata field) {

    Node tr = document.createElement("tr");

    Node td1 = tr.appendChild(document.createElement("td"));
    td1.setTextContent("verifyValue");

    Node td2 = tr.appendChild(document.createElement("td"));
    String id = "_" + XmlUtils.convertId("fu:" + entity.getFullyQualifiedTypeName()) + "[0]_"
            + field.getFieldName() + "_id_update";
    td2.setTextContent(XmlUtils.convertId(id));

    Node td3 = tr.appendChild(document.createElement("td"));
    td3.setTextContent(convertToInitializer(field));

    return tr;
}

From source file:org.gvnix.web.screen.roo.addon.SeleniumServicesImpl.java

/**
 * Add store confirmation command, required before a javascript
 * confirmation.//w  w  w.  jav  a 2  s . c  om
 * 
 * @param document Document to write command
 * @param varName Name of variable to store confirmation value
 * @return Confirmation node
 */
protected Node storeConfirmationCommand(Document document, String varName) {

    Node tr = document.createElement("tr");

    Node td1 = tr.appendChild(document.createElement("td"));
    td1.setTextContent("storeConfirmation");

    Node td2 = tr.appendChild(document.createElement("td"));
    td2.setTextContent(XmlUtils.convertId(varName));

    Node td3 = tr.appendChild(document.createElement("td"));
    td3.setTextContent(" ");

    return tr;
}

From source file:org.gvnix.web.screen.roo.addon.SeleniumServicesImpl.java

/**
 * Add check command, to check a checkbox or radio.
 * /*from  w  w  w. j av a 2 s.  com*/
 * @param document Document to write command
 * @param entity Entity owner of check property
 * @return Check node
 */
protected Node checkCommand(Document document, JavaType entity) {

    Node tr = document.createElement("tr");

    Node td1 = tr.appendChild(document.createElement("td"));
    td1.setTextContent("check");

    Node td2 = tr.appendChild(document.createElement("td"));
    String id = GVNIX_CHECKBOX + XmlUtils.convertId(entity.getFullyQualifiedTypeName()) + "_0";
    td2.setTextContent(id);

    Node td3 = tr.appendChild(document.createElement("td"));
    td3.setTextContent(" ");

    return tr;
}

From source file:org.infoglue.cms.applications.contenttool.actions.UpdateContentVersionAttributeAction.java

/**
 * This method sets a value to the xml that is the contentVersions Value. 
 *//* w w  w.  j a v  a2  s  .c  o m*/

private void setAttributeValue(ContentVersionVO contentVersionVO, String attributeName, String attributeValue) {
    String value = "";
    if (this.contentVersionVO != null) {
        try {
            logger.info("VersionValue:" + this.contentVersionVO.getVersionValue());
            InputSource inputSource = new InputSource(
                    new StringReader(this.contentVersionVO.getVersionValue()));

            DOMParser parser = new DOMParser();
            parser.parse(inputSource);
            Document document = parser.getDocument();

            NodeList nl = document.getDocumentElement().getChildNodes();
            Node n = nl.item(0);

            nl = n.getChildNodes();
            for (int i = 0; i < nl.getLength(); i++) {
                n = nl.item(i);
                if (n.getNodeName().equalsIgnoreCase(attributeName)) {
                    logger.info("Setting attributeValue: " + attributeValue);
                    if (n.getFirstChild() != null && n.getFirstChild().getNodeValue() != null) {
                        n.getFirstChild().setNodeValue(attributeValue);
                        break;
                    } else {
                        CDATASection cdata = document.createCDATASection(attributeValue);
                        n.appendChild(cdata);
                        break;
                    }
                    /*
                    Node valueNode = n.getFirstChild();
                    n.getFirstChild().setNodeValue(attributeValue);
                    break;
                    */
                }
            }
            contentVersionVO.setVersionValue(XMLHelper.serializeDom(document, new StringBuffer()).toString());
        } catch (Exception e) {
            logger.error("Problem updating version value:" + attributeName + "=" + attributeValue + " reason:"
                    + e.getMessage(), e);
        }
    }
}

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

/**
 * This method adds a component to the page. 
 *///from ww  w .  j av a2 s  . c  o  m

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. 
 *///from  w w w . j  a v a 2s  .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.cms.controllers.kernel.impl.simple.ContentTypeDefinitionController.java

public String copyAttribute(String remoteSchemaValue, String localSchemaValue,
        String contentTypeAttributeName) {
    String newSchemaValue = localSchemaValue;

    try {//from  w w w.j  a v  a2s .c  o  m
        Document remoteDocument = createDocumentFromDefinition(remoteSchemaValue);
        Document localDocument = createDocumentFromDefinition(localSchemaValue);

        String attributeXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element[@name='"
                + contentTypeAttributeName + "']";
        Node attributeNode = org.apache.xpath.XPathAPI.selectSingleNode(remoteDocument.getDocumentElement(),
                attributeXPath);

        String attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all";
        Node attributesNode = org.apache.xpath.XPathAPI.selectSingleNode(localDocument.getDocumentElement(),
                attributesXPath);
        logger.info("attributesNode:" + attributesNode);
        if (attributesNode != null && localDocument != null && attributeNode != null) {
            Node node = localDocument.importNode(attributeNode, true);

            attributesNode.appendChild(node);

            StringBuffer sb = new StringBuffer();
            org.infoglue.cms.util.XMLHelper.serializeDom(localDocument.getDocumentElement(), sb);
            newSchemaValue = sb.toString();
        } else {
            logger.error("Problem:" + attributesNode + " - " + localDocument + " - " + attributeNode);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return newSchemaValue;
}

From source file:org.infoglue.cms.controllers.kernel.impl.simple.ContentTypeDefinitionController.java

public String copyCategory(String remoteSchemaValue, String localSchemaValue, String categoryName) {
    String newSchemaValue = localSchemaValue;

    try {/*from w  ww . ja v a  2s . c  o  m*/
        Document remoteDocument = createDocumentFromDefinition(remoteSchemaValue);
        Document localDocument = createDocumentFromDefinition(localSchemaValue);

        String attributeXPath = "/xs:schema/xs:simpleType[@name='categoryKeys']/xs:restriction/xs:enumeration[@value='"
                + categoryName + "']";
        Node attributeNode = org.apache.xpath.XPathAPI.selectSingleNode(remoteDocument.getDocumentElement(),
                attributeXPath);

        String attributesXPath = "/xs:schema/xs:simpleType[@name='categoryKeys']/xs:restriction";
        Node attributesNode = org.apache.xpath.XPathAPI.selectSingleNode(localDocument.getDocumentElement(),
                attributesXPath);
        if (attributesNode == null) {
            attributesNode = ContentTypeDefinitionController.getController()
                    .createNewEnumerationKey(localDocument, ContentTypeDefinitionController.CATEGORY_KEYS);
        }

        if (attributesNode != null && localDocument != null && attributeNode != null) {
            Node node = localDocument.importNode(attributeNode, true);
            attributesNode.appendChild(node);

            StringBuffer sb = new StringBuffer();
            org.infoglue.cms.util.XMLHelper.serializeDom(localDocument.getDocumentElement(), sb);
            newSchemaValue = sb.toString();
        } else {
            logger.error("Problem:" + attributesNode + " - " + localDocument + " - " + attributeNode);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return newSchemaValue;
}

From source file:org.infoglue.cms.controllers.kernel.impl.simple.ContentTypeDefinitionController.java

public String copyAssetKey(String remoteSchemaValue, String localSchemaValue, String assetKey) {
    String newSchemaValue = localSchemaValue;

    try {/*from   w w w.j a  v a 2 s. c  o  m*/
        Document remoteDocument = createDocumentFromDefinition(remoteSchemaValue);
        Document localDocument = createDocumentFromDefinition(localSchemaValue);

        String attributeXPath = "/xs:schema/xs:simpleType[@name='assetKeys']/xs:restriction/xs:enumeration[@value='"
                + assetKey + "']";
        Node attributeNode = org.apache.xpath.XPathAPI.selectSingleNode(remoteDocument.getDocumentElement(),
                attributeXPath);

        String attributesXPath = "/xs:schema/xs:simpleType[@name='assetKeys']/xs:restriction";
        Node attributesNode = org.apache.xpath.XPathAPI.selectSingleNode(localDocument.getDocumentElement(),
                attributesXPath);
        if (attributesNode == null) {
            attributesNode = ContentTypeDefinitionController.getController()
                    .createNewEnumerationKey(localDocument, ContentTypeDefinitionController.ASSET_KEYS);
        }

        if (attributesNode != null && localDocument != null && attributeNode != null) {
            Node node = localDocument.importNode(attributeNode, true);
            attributesNode.appendChild(node);

            StringBuffer sb = new StringBuffer();
            org.infoglue.cms.util.XMLHelper.serializeDom(localDocument.getDocumentElement(), sb);
            newSchemaValue = sb.toString();
        } else {
            logger.error("Problem:" + attributesNode + " - " + localDocument + " - " + attributeNode);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return newSchemaValue;
}