Example usage for org.w3c.dom Element setTextContent

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

Introduction

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

Prototype

public void setTextContent(String textContent) throws DOMException;

Source Link

Document

This attribute returns the text content of this node and its descendants.

Usage

From source file:com.photon.phresco.service.util.ServerUtil.java

/**
 * To create pom.xml file for artifact upload
 * /*from w  w w .  j  av a  2  s  . c  om*/
 * @param info
 * @return
 * @throws PhrescoException
 */
public static File createPomFile(ArtifactGroup info) throws PhrescoException {
    FileWriter writer = null;
    File pomFile = getPomFile();
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder domBuilder = domFactory.newDocumentBuilder();

        org.w3c.dom.Document newDoc = domBuilder.newDocument();
        Element rootElement = newDoc.createElement(ServerConstants.POM_PROJECT);
        newDoc.appendChild(rootElement);

        Element modelVersion = newDoc.createElement(ServerConstants.POM_MODELVERSION);
        modelVersion.setTextContent(ServerConstants.POM_MODELVERSION_VAL);
        rootElement.appendChild(modelVersion);

        Element groupId = newDoc.createElement(ServerConstants.POM_GROUPID);
        groupId.setTextContent(info.getGroupId());
        rootElement.appendChild(groupId);

        Element artifactId = newDoc.createElement(ServerConstants.POM_ARTIFACTID);
        artifactId.setTextContent(info.getArtifactId());
        rootElement.appendChild(artifactId);

        Element version = newDoc.createElement(ServerConstants.POM_VERSION);
        version.setTextContent(info.getVersions().get(0).getVersion());
        rootElement.appendChild(version);

        Element packaging = newDoc.createElement(ServerConstants.POM_PACKAGING);
        packaging.setTextContent(info.getPackaging());
        rootElement.appendChild(packaging);

        Element description = newDoc.createElement(ServerConstants.POM_DESC);
        description.setTextContent(ServerConstants.POM_DESC_VAL);
        rootElement.appendChild(description);

        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, ServerConstants.POM_OMMIT);
        trans.setOutputProperty(OutputKeys.INDENT, ServerConstants.POM_DESC);

        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(newDoc);
        trans.transform(source, result);
        String xmlString = sw.toString();
        writer = new FileWriter(pomFile);
        writer.write(xmlString);
        writer.close();
    } catch (Exception e) {
        throw new PhrescoException(e);
    } finally {
        Utility.closeStream(writer);
    }

    return pomFile;
}

From source file:com.evolveum.midpoint.model.client.ModelClientUtil.java

public static Element createTextElement(QName qname, String value, Document doc) {
    Element element = doc.createElementNS(qname.getNamespaceURI(), qname.getLocalPart());
    element.setTextContent(value);
    return element;
}

From source file:com.evolveum.midpoint.prism.xml.XmlTypeConverter.java

/**
 * Serialize PolyString to DOM element./*from w  w w . ja  v  a2  s  . c  o m*/
 */
private static void polyStringToElement(Element polyStringElement, PolyString polyString) {
    if (polyString.getOrig() != null) {
        Element origElement = DOMUtil.createSubElement(polyStringElement,
                PrismConstants.POLYSTRING_ELEMENT_ORIG_QNAME);
        origElement.setTextContent(polyString.getOrig());
    }
    if (polyString.getNorm() != null) {
        Element origElement = DOMUtil.createSubElement(polyStringElement,
                PrismConstants.POLYSTRING_ELEMENT_NORM_QNAME);
        origElement.setTextContent(polyString.getNorm());
    }
}

From source file:com.evolveum.midpoint.testing.wstest.AbstractWebserviceTest.java

protected static Element createTextElement(QName qname, String value, Document doc) {
    Element element = doc.createElementNS(qname.getNamespaceURI(), qname.getLocalPart());
    element.setTextContent(value);
    return element;
}

From source file:com.evolveum.liferay.usercreatehook.ws.ModelPortWrapper.java

private static Element createTextElement(QName qname, String value, Document doc) {
    Element element = doc.createElementNS(qname.getNamespaceURI(), qname.getLocalPart());
    element.setTextContent(value);
    return element;
}

From source file:cz.incad.kramerius.rest.api.k5.client.search.SearchResource.java

public static void replacePidsInDOM(Element docE) {
    String[] apiReplace = KConfiguration.getInstance().getAPIPIDReplace();
    for (String k : apiReplace) {
        if (k.equals("PID"))
            continue; // already replaced
        Element foundElm = findSolrElement(docE, k);
        if (foundElm != null) {

            if (foundElm.getNodeName().equals("str")) {
                String value = SOLRUtils.value(foundElm.getTextContent(), String.class);
                if (value != null && (value.indexOf("/@") > 0)) {
                    value = value.replace("/@", "@");
                    foundElm.setTextContent(value);
                }/* w ww. j  av  a 2s. co m*/
            } else if (foundElm.getNodeName().equals("arr")) {
                List<String> array = SOLRUtils.array(docE, k, String.class);
                List<String> newArray = new ArrayList<String>();
                for (String value : array) {
                    value = value.replace("/@", "@");
                    newArray.add(value);
                }

                docE.removeChild(foundElm);
                Element newArrElm = SOLRUtils.arr(foundElm.getOwnerDocument(), k, newArray);
                docE.appendChild(newArrElm);
            } else {
                LOGGER.warning("skipping object type '" + foundElm.getNodeName() + "'");
            }

        }
    }

}

From source file:com.connexta.arbitro.utils.PolicyUtils.java

/**
 * This creates XML representation of rule element using RuleElementDTO object
 *
 * @param ruleElementDTO RuleElementDTO/* w  w  w . j ava 2 s . c  o  m*/
 * @param doc Document
 * @return DOM element
 * @throws PolicyBuilderException throws
 */
public static Element createRuleElement(RuleElementDTO ruleElementDTO, Document doc)
        throws PolicyBuilderException {

    TargetElementDTO targetElementDTO = ruleElementDTO.getTargetElementDTO();
    ConditionElementDT0 conditionElementDT0 = ruleElementDTO.getConditionElementDT0();
    List<ObligationElementDTO> obligationElementDTOs = ruleElementDTO.getObligationElementDTOs();

    Element ruleElement = doc.createElement(PolicyConstants.RULE_ELEMENT);

    if (ruleElementDTO.getRuleId() != null && ruleElementDTO.getRuleId().trim().length() > 0) {
        ruleElement.setAttribute(PolicyConstants.RULE_ID, ruleElementDTO.getRuleId());
    }

    if (ruleElementDTO.getRuleEffect() != null && ruleElementDTO.getRuleEffect().trim().length() > 0) {
        ruleElement.setAttribute(PolicyConstants.RULE_EFFECT, ruleElementDTO.getRuleEffect());
    }

    if (ruleElementDTO.getRuleDescription() != null
            && ruleElementDTO.getRuleDescription().trim().length() > 0) {
        Element descriptionElement = doc.createElement(PolicyConstants.DESCRIPTION_ELEMENT);
        descriptionElement.setTextContent(ruleElementDTO.getRuleDescription());
        ruleElement.appendChild(descriptionElement);
    }

    if (targetElementDTO != null) {
        Element targetElement = createTargetElement(targetElementDTO, doc);
        ruleElement.appendChild(targetElement);
    }

    if (conditionElementDT0 != null) {
        ruleElement.appendChild(createConditionElement(conditionElementDT0, doc));
    }

    if (obligationElementDTOs != null && obligationElementDTOs.size() > 0) {
        List<ObligationElementDTO> obligations = new ArrayList<ObligationElementDTO>();
        List<ObligationElementDTO> advices = new ArrayList<ObligationElementDTO>();
        for (ObligationElementDTO obligationElementDTO : obligationElementDTOs) {
            if (obligationElementDTO.getType() == ObligationElementDTO.ADVICE) {
                advices.add(obligationElementDTO);
            } else {
                obligations.add(obligationElementDTO);
            }
        }
        Element obligation = createObligationsElement(obligations, doc);
        Element advice = createAdvicesElement(advices, doc);
        if (obligation != null) {
            ruleElement.appendChild(obligation);
        }
        if (advice != null) {
            ruleElement.appendChild(advice);
        }
    }

    return ruleElement;
}

From source file:cz.mzk.editor.server.fedora.utils.FedoraUtils.java

/**
 * @param foxmlDocument//  w w  w . j a  v a  2  s  .c o  m
 * @param oldContent
 */
private static void addOldOcr(Document foxmlDocument, String oldContent) {
    try {
        String lastContLocXPath = "//foxml:datastream[@ID=\'" + TEXT_OCR.getValue()
                + "\']/foxml:datastreamVersion[last()]/foxml:contentLocation[last()]";

        XPathExpression all = makeNSAwareXpath().compile(lastContLocXPath);
        NodeList listOfstream = (NodeList) all.evaluate(foxmlDocument, XPathConstants.NODESET);
        Element lastContLocElement = null;
        if (listOfstream.getLength() != 0) {
            lastContLocElement = (Element) listOfstream.item(0);
        }
        Element localContElement = foxmlDocument.createElement("foxml:content");

        localContElement.setTextContent(oldContent);
        lastContLocElement.appendChild(localContElement);

    } catch (XPathExpressionException e) {
        LOGGER.warn("XPath failure", e);
    }
}

From source file:com.connexta.arbitro.utils.PolicyUtils.java

/**
 * This method creates a policy element of the XACML policy
 * @param policyElementDTO  policy element data object
 * @param doc XML document//from   w w w  .  jav  a2 s  . co  m
 * @return policyElement
 * @throws PolicyBuilderException if
 */

public static Element createPolicyElement(PolicyElementDTO policyElementDTO, Document doc)
        throws PolicyBuilderException {

    Element policyElement = doc.createElement(PolicyConstants.POLICY_ELEMENT);

    policyElement.setAttribute("xmlns", PolicyConstants.XACMLData.XACML3_POLICY_NAMESPACE);

    if (policyElementDTO.getPolicyName() != null && policyElementDTO.getPolicyName().trim().length() > 0) {
        policyElement.setAttribute(PolicyConstants.POLICY_ID, policyElementDTO.getPolicyName());
    } else {
        throw new PolicyBuilderException("Policy name can not be null");
    }

    if (policyElementDTO.getRuleCombiningAlgorithms() != null
            && policyElementDTO.getRuleCombiningAlgorithms().trim().length() > 0) {
        policyElement.setAttribute(PolicyConstants.RULE_ALGORITHM,
                policyElementDTO.getRuleCombiningAlgorithms());
    } else {
        policyElement.setAttribute(PolicyConstants.RULE_ALGORITHM,
                PolicyConstants.RuleCombiningAlog.DENY_OVERRIDE_ID); // TODO
        log.warn("Rule combining algorithm is not defined. Use default algorithm; Deny Override");
    }

    if (policyElementDTO.getVersion() != null && policyElementDTO.getVersion().trim().length() > 0) {
        policyElement.setAttribute(PolicyConstants.POLICY_VERSION, policyElementDTO.getVersion());
    } else {
        // policy version is can be handled by policy registry.  therefore we can ignore it, although it
        // is a required attribute
        policyElement.setAttribute(PolicyConstants.POLICY_VERSION, "1.0");
    }

    if (policyElementDTO.getPolicyDescription() != null
            && policyElementDTO.getPolicyDescription().trim().length() > 0) {

        Element descriptionElement = doc.createElement(PolicyConstants.DESCRIPTION_ELEMENT);
        descriptionElement.setTextContent(policyElementDTO.getPolicyDescription());
        policyElement.appendChild(descriptionElement);
    }

    TargetElementDTO targetElementDTO = policyElementDTO.getTargetElementDTO();
    List<RuleElementDTO> ruleElementDTOs = policyElementDTO.getRuleElementDTOs();
    List<ObligationElementDTO> obligationElementDTOs = policyElementDTO.getObligationElementDTOs();

    if (targetElementDTO != null) {
        policyElement.appendChild(createTargetElement(targetElementDTO, doc));
    } else {
        policyElement.appendChild(doc.createElement(PolicyConstants.TARGET_ELEMENT));
    }

    if (ruleElementDTOs != null && ruleElementDTOs.size() > 0) {
        for (RuleElementDTO ruleElementDTO : ruleElementDTOs) {
            policyElement.appendChild(createRuleElement(ruleElementDTO, doc));
        }
    } else {
        RuleElementDTO ruleElementDTO = new RuleElementDTO();
        ruleElementDTO.setRuleId(UUID.randomUUID().toString());
        ruleElementDTO.setRuleEffect(PolicyConstants.RuleEffect.DENY);
        policyElement.appendChild(createRuleElement(ruleElementDTO, doc));
    }

    if (obligationElementDTOs != null && obligationElementDTOs.size() > 0) {
        List<ObligationElementDTO> obligations = new ArrayList<ObligationElementDTO>();
        List<ObligationElementDTO> advices = new ArrayList<ObligationElementDTO>();
        for (ObligationElementDTO obligationElementDTO : obligationElementDTOs) {
            if (obligationElementDTO.getType() == ObligationElementDTO.ADVICE) {
                advices.add(obligationElementDTO);
            } else {
                obligations.add(obligationElementDTO);
            }
        }
        Element obligation = createObligationsElement(obligations, doc);
        Element advice = createAdvicesElement(advices, doc);
        if (obligation != null) {
            policyElement.appendChild(obligation);
        }
        if (advice != null) {
            policyElement.appendChild(advice);
        }
    }

    return policyElement;
}

From source file:com.msopentech.odatajclient.engine.data.ODataBinder.java

private static Element toPrimitivePropertyElement(final String name, final ODataPrimitiveValue value,
        final Document doc, final boolean setType) {

    final Element element = doc.createElement(ODataConstants.PREFIX_DATASERVICES + name);
    if (setType) {
        element.setAttribute(ODataConstants.ATTR_M_TYPE, value.getTypeName());
    }/*www. j  a v  a2 s  . c  o  m*/

    if (value instanceof ODataGeospatialValue) {
        element.appendChild(doc.importNode(((ODataGeospatialValue) value).toTree(), true));
    } else {
        element.setTextContent(value.toString());
    }

    return element;
}