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.evolveum.midpoint.prism.xml.XmlTypeConverter.java

public static void toXsdElement(Object val, Element element, boolean recordType) throws SchemaException {
    if (val instanceof Element) {
        return;//from ww w.  jav a2 s .  com
    } else if (val instanceof QName) {
        QName qname = (QName) val;
        DOMUtil.setQNameValue(element, qname);
    } else if (val instanceof PolyString) {
        polyStringToElement(element, (PolyString) val);
    } else {
        element.setTextContent(toXmlTextContent(val, DOMUtil.getQName(element)));
    }
    if (recordType) {
        QName xsdType = XsdTypeMapper.toXsdType(val.getClass());
        DOMUtil.setXsiType(element, xsdType);
    }
}

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

/**
 * This method creates a policy set element of the XACML policy
 * @param policyElementDTO  policy element data object
 * @param doc XML document/*www.  j  a v  a  2 s.c  om*/
 * @return policyElement
 * @throws PolicyBuilderException if
 */

public static Element createPolicySetElement(PolicySetElementDTO policyElementDTO, Document doc)
        throws PolicyBuilderException {

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

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

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

    if (policyElementDTO.getPolicyCombiningAlgId() != null
            && policyElementDTO.getPolicyCombiningAlgId().trim().length() > 0) {
        policyElement.setAttribute(PolicyConstants.POLICY_ALGORITHM,
                policyElementDTO.getPolicyCombiningAlgId());
    } else {
        policyElement.setAttribute(PolicyConstants.POLICY_ALGORITHM,
                PolicyConstants.PolicyCombiningAlog.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.getDescription() != null && policyElementDTO.getDescription().trim().length() > 0) {

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

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

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

    List<String> policySets = policyElementDTO.getPolicySets();
    if (policySets != null && policySets.size() > 0) {
        // TODO 
    }

    List<String> policies = policyElementDTO.getPolicies();
    if (policies != null && policies.size() > 0) {
        // TODO    
    }

    List<String> policySetIds = policyElementDTO.getPolicySetIdReferences();
    if (policySetIds != null && policySetIds.size() > 0) {
        for (String policySetId : policySetIds) {
            Element element = doc.createElement(PolicyConstants.POLICY_SET_ID_REFERENCE_ELEMENT);
            element.setTextContent(policySetId);
            policyElement.appendChild(element);
        }
    }

    List<String> policyIds = policyElementDTO.getPolicyIdReferences();
    if (policyIds != null && policyIds.size() > 0) {
        for (String policyId : policyIds) {
            Element element = doc.createElement(PolicyConstants.POLICY_ID_REFERENCE_ELEMENT);
            element.setTextContent(policyId);
            policyElement.appendChild(element);
        }
    }

    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.rapidminer.gui.OperatorDocLoader.java

/**
 * /*from w  ww .  j  ava 2 s.com*/
 * @param operatorWikiName
 * @param opDesc
 * @return The parsed <tt>Document</tt> (not finally parsed) of the selected operator.
 * @throws MalformedURLException
 * @throws ParserConfigurationException
 */
private static Document parseDocumentForOperator(String operatorWikiName, OperatorDescription opDesc)
        throws MalformedURLException, ParserConfigurationException {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setIgnoringComments(true);
    builderFactory.setIgnoringElementContentWhitespace(true);
    DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
    documentBuilder.setEntityResolver(new XHTMLEntityResolver());

    Document document = null;
    URL url = new URL(WIKI_PREFIX_FOR_OPERATORS + operatorWikiName);
    if (url != null) {
        try {
            document = documentBuilder.parse(WebServiceTools.openStreamFromURL(url));
        } catch (IOException e) {
            logger.warning("Could not open " + url.toExternalForm() + ": " + e.getMessage());
        } catch (SAXException e) {
            logger.warning("Could not parse operator documentation: " + e.getMessage());
        }

        int i = 0;

        if (document != null) {
            Element contentElement = document.getElementById("content");

            // removing content element from document
            if (contentElement != null) {
                contentElement.getParentNode().removeChild(contentElement);
            }

            // removing everything from body
            NodeList bodies = document.getElementsByTagName("body");
            for (int k = 0; k < bodies.getLength(); k++) {
                Node body = bodies.item(k);
                while (body.hasChildNodes()) {
                    body.removeChild(body.getFirstChild());
                }

                // read content element to body
                if (contentElement != null && k == 0) {
                    body.appendChild(contentElement);
                }
            }

            // removing everything from head
            NodeList heads = document.getElementsByTagName("head");
            for (int k = 0; k < heads.getLength(); k++) {
                Node head = heads.item(k);
                while (head.hasChildNodes()) {
                    head.removeChild(head.getFirstChild());
                }
            }
            // removing...<head/> from document
            if (heads != null) {
                while (i < heads.getLength()) {
                    Node head = heads.item(i);
                    head.getParentNode().removeChild(head);
                }
            }

            // removing jump-to-nav element from document
            Element jumpToNavElement = document.getElementById("jump-to-nav");
            if (jumpToNavElement != null) {
                jumpToNavElement.getParentNode().removeChild(jumpToNavElement);
            }

            // removing mw-normal-catlinks element from document
            Element mwNormalCatlinksElement = document.getElementById("mw-normal-catlinks");
            if (mwNormalCatlinksElement != null) {
                mwNormalCatlinksElement.getParentNode().removeChild(mwNormalCatlinksElement);
            }

            // removing complete link navigation
            Element tocElement = document.getElementById("toc");
            if (tocElement != null) {
                tocElement.getParentNode().removeChild(tocElement);
            }

            // removing everything from class printfooter
            NodeList nodeListDiv = document.getElementsByTagName("div");
            for (int k = 0; k < nodeListDiv.getLength(); k++) {
                Element div = (Element) nodeListDiv.item(k);
                if (div.getAttribute("class").equals("printfooter")) {
                    div.getParentNode().removeChild(div);
                }
            }

            // removing everything from class editsection
            NodeList spanList = document.getElementsByTagName("span");
            for (int k = 0; k < spanList.getLength(); k++) {
                Element span = (Element) spanList.item(k);
                if (span.getAttribute("class").equals("editsection")) {
                    span.getParentNode().removeChild(span);
                }
            }

            // Synopsis Header
            boolean doIt = true;
            NodeList pList = document.getElementsByTagName("p");
            for (int k = 0; k < pList.getLength(); k++) {

                if (doIt) {
                    Node p = pList.item(k);
                    NodeList pChildList = p.getChildNodes();

                    for (int j = 0; j < pChildList.getLength(); j++) {

                        Node pChild = pChildList.item(j);
                        if (pChild.getNodeType() == Node.TEXT_NODE && pChild.getNodeValue() != null
                                && StringUtils.isNotBlank(pChild.getNodeValue())
                                && StringUtils.isNotEmpty(pChild.getNodeValue())) {

                            String pChildString = pChild.getNodeValue();
                            Element newPWithoutSpaces = document.createElement("p");
                            newPWithoutSpaces.setTextContent(pChildString);

                            Node synopsis = document.createTextNode("Synopsis");

                            Element span = document.createElement("span");
                            span.setAttribute("class", "mw-headline");
                            span.setAttribute("id", "Synopsis");
                            span.appendChild(synopsis);

                            Element h2 = document.createElement("h2");
                            h2.appendChild(span);

                            Element div = document.createElement("div");
                            div.setAttribute("id", "synopsis");
                            div.appendChild(h2);
                            div.appendChild(newPWithoutSpaces);

                            Node pChildParentParent = pChild.getParentNode().getParentNode();
                            Node pChildParent = pChild.getParentNode();

                            pChildParentParent.replaceChild(div, pChildParent);
                            doIt = false;
                            break;
                        }
                    }
                } else {
                    break;
                }
            }

            // removing all <br...>-Tags
            NodeList brList = document.getElementsByTagName("br");

            while (i < brList.getLength()) {
                Node br = brList.item(i);
                Node parentBrNode = br.getParentNode();
                parentBrNode.removeChild(br);
            }

            // removing everything from script
            NodeList scriptList = document.getElementsByTagName("script");
            while (i < scriptList.getLength()) {
                Node scriptNode = scriptList.item(i);
                Node parentNode = scriptNode.getParentNode();
                parentNode.removeChild(scriptNode);
            }

            // removing all empty <p...>-Tags
            NodeList pList2 = document.getElementsByTagName("p");
            int ccc = 0;
            while (ccc < pList2.getLength()) {
                Node p = pList2.item(ccc);
                NodeList pChilds = p.getChildNodes();

                int kk = 0;

                while (kk < pChilds.getLength()) {
                    Node pChild = pChilds.item(kk);
                    if (pChild.getNodeType() == Node.TEXT_NODE) {
                        String pNodeValue = pChild.getNodeValue();
                        if (pNodeValue == null || StringUtils.isBlank(pNodeValue)
                                || StringUtils.isEmpty(pNodeValue)) {
                            kk++;
                        } else {
                            ccc++;
                            break;
                        }
                    } else {
                        ccc++;
                        break;
                    }
                    if (kk == pChilds.getLength()) {
                        Node parentBrNode = p.getParentNode();
                        parentBrNode.removeChild(p);
                    }
                }
            }

            // removing firstHeading element from document
            Element firstHeadingElement = document.getElementById("firstHeading");
            if (firstHeadingElement != null) {
                CURRENT_OPERATOR_NAME_READ_FROM_RAPIDWIKI = firstHeadingElement.getFirstChild().getNodeValue()
                        .replaceFirst(".*:", "");
                firstHeadingElement.getParentNode().removeChild(firstHeadingElement);
            }

            // setting operator plugin name
            if (opDesc != null && opDesc.getProvider() != null) {
                CURRENT_OPERATOR_PLUGIN_NAME = opDesc.getProvider().getName();
            }

            // removing sitesub element from document
            Element siteSubElement = document.getElementById("siteSub");
            if (siteSubElement != null) {
                siteSubElement.getParentNode().removeChild(siteSubElement);
            }

            // removing contentSub element from document
            Element contentSubElement = document.getElementById("contentSub");
            if (contentSubElement != null) {
                contentSubElement.getParentNode().removeChild(contentSubElement);
            }

            // removing catlinks element from document
            Element catlinksElement = document.getElementById("catlinks");
            if (catlinksElement != null) {
                catlinksElement.getParentNode().removeChild(catlinksElement);
            }

            // removing <a...> element from document, if they are empty
            NodeList aList = document.getElementsByTagName("a");
            if (aList != null) {
                int k = 0;
                while (k < aList.getLength()) {
                    Node a = aList.item(k);
                    Element aElement = (Element) a;
                    if (aElement.getAttribute("class").equals("internal")) {
                        a.getParentNode().removeChild(a);
                    } else {
                        Node aChild = a.getFirstChild();
                        if (aChild != null
                                && (aChild.getNodeValue() != null && aChild.getNodeType() == Node.TEXT_NODE
                                        && StringUtils.isNotBlank(aChild.getNodeValue())
                                        && StringUtils.isNotEmpty(aChild.getNodeValue())
                                        || aChild.getNodeName() != null)) {
                            Element aChildElement = null;
                            if (aChild.getNodeName().startsWith("img")) {
                                aChildElement = (Element) aChild;

                                Element imgElement = document.createElement("img");
                                imgElement.setAttribute("alt", aChildElement.getAttribute("alt"));
                                imgElement.setAttribute("class", aChildElement.getAttribute("class"));
                                imgElement.setAttribute("height", aChildElement.getAttribute("height"));
                                imgElement.setAttribute("src",
                                        WIKI_PREFIX_FOR_IMAGES + aChildElement.getAttribute("src"));
                                imgElement.setAttribute("width", aChildElement.getAttribute("width"));
                                imgElement.setAttribute("border", "1");

                                Node aParent = a.getParentNode();
                                aParent.replaceChild(imgElement, a);
                            } else {
                                k++;
                            }
                        } else {
                            a.getParentNode().removeChild(a);
                        }
                    }
                }
            }

        }
    }
    return document;
}

From source file:BarAdapter.java

@Override
public Object marshal(Bar v) throws Exception {
    Document document = documentBuilder.newDocument();
    Element root = document.createElement("bar");
    root.setTextContent(v.value);
    return root;/* ww  w.j  av  a 2s .c o  m*/
}

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

/**
 * @param foxmlDocument//  ww  w. j a v  a 2  s.com
 * @param streamToModify
 * @param newContent
 */
private static void modifyStream(Document foxmlDocument, String streamToModify, String newContent) {

    if (newContent != null) {
        try {
            Element versionElement = foxmlDocument.createElement("foxml:datastreamVersion");

            if (streamToModify.equals(RELS_EXT.getValue())) {
                versionElement.setAttribute("LABEL", "RDF Statements about this object");
                versionElement.setAttribute("FORMAT_URI", RELS_EXT_NAMESPACE_URI);
                versionElement.setAttribute("MIMETYPE", "application/rdf+xml");

            } else {
                versionElement.setAttribute("MIMETYPE", "text/xml");

                if (streamToModify.equals(DC.getValue())) {
                    versionElement.setAttribute("LABEL", "Dublin Core Record for this object");
                    versionElement.setAttribute("FORMAT_URI", OAI_DC_NAMESPACE_URI);

                } else if (streamToModify.equals(BIBLIO_MODS.getValue())) {
                    versionElement.setAttribute("LABEL", "BIBLIO_MODS description of current object");
                    versionElement.setAttribute("FORMAT_URI", BIBILO_MODS_URI);

                } else if (streamToModify.equals(TEXT_OCR.getValue())) {
                    versionElement.setAttribute("LABEL", "");
                    Element contLocElement = foxmlDocument.createElement("foxml:contentLocation");
                    contLocElement.setAttribute("TYPE", "INTERNAL_ID");
                    contLocElement.setAttribute("REF", "LOCAL");
                    Element localContElement = foxmlDocument.createElement("foxml:content");
                    localContElement.setTextContent(newContent);
                    contLocElement.appendChild(localContElement);
                    versionElement.appendChild(contLocElement);
                }
            }

            String lastStreamXPath = "//foxml:datastream[@ID=\'" + streamToModify
                    + "\']/foxml:datastreamVersion[last()]";
            Element element = FoxmlUtils.getElement(foxmlDocument, lastStreamXPath);

            int versionNumber = 0;
            if (element != null) {
                versionNumber = getVersionNumber(element.getAttribute("ID"));
            }

            versionElement.setAttribute("ID", streamToModify + "." + (versionNumber + 1));
            versionElement.setAttribute("CREATED", "NOT YET");
            versionElement.setAttribute("SIZE", "0");

            Element contentElement = foxmlDocument.createElement("foxml:xmlContent");

            try {
                InputStream is = new ByteArrayInputStream(newContent.getBytes("UTF-8"));
                Document newStreamDocument = FoxmlUtils.getFoxmlDocument(is);
                NodeList streamNodeList = newStreamDocument.getChildNodes();
                for (int i = 0; i < streamNodeList.getLength(); i++) {
                    Node myNewNode = foxmlDocument.importNode(streamNodeList.item(i), true);
                    contentElement.appendChild(myNewNode);
                }
            } catch (IOException e) {
                System.err.println("IO fauilure" + e);
            }

            versionElement.appendChild(contentElement);

            String streamXPath = "//foxml:datastream[@ID=\'" + streamToModify + "\']";
            Element parentOfStream = FoxmlUtils.getElement(foxmlDocument, streamXPath);

            if (parentOfStream == null) {
                String digObjXPath = "//foxml:digitalObject";
                Element digObjElement = FoxmlUtils.getElement(foxmlDocument, digObjXPath);

                parentOfStream = foxmlDocument.createElement("datastream");
                parentOfStream.setAttribute("ID", streamToModify);
                parentOfStream.setAttribute("STATE", "A");
                //TODO for other streams if necessary
                if (streamToModify.equals(TEXT_OCR.getValue()))
                    parentOfStream.setAttribute("CONTROL_GROUP", "M");
                parentOfStream.setAttribute("VERSIONABLE", "false");
                digObjElement.appendChild(parentOfStream);
            }

            parentOfStream.appendChild(versionElement);

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

From source file:de.mpg.escidoc.services.common.util.Util.java

public static Node getSize(String url) {
    DocumentBuilder documentBuilder;

    HttpClient httpClient = new HttpClient();
    HeadMethod headMethod = new HeadMethod(url);

    try {/*from   w ww . j  ava  2s  . c o m*/
        logger.info("Getting size of " + url);
        ProxyHelper.executeMethod(httpClient, headMethod);

        if (headMethod.getStatusCode() != 200) {
            logger.warn("Wrong status code " + headMethod.getStatusCode() + " at " + url);
        }

        documentBuilder = DocumentBuilderFactoryImpl.newInstance().newDocumentBuilder();
        Document document = documentBuilder.newDocument();
        Element element = document.createElement("size");
        document.appendChild(element);
        Header header = headMethod.getResponseHeader("Content-Length");
        if (header != null) {
            element.setTextContent(header.getValue());
            return document;
        } else {
            element.setTextContent("0");
            return document;
        }

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

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

/**
 * This method creates the attribute value element
 * @param attributeValueElementDTO attribute value element data object
 * @param doc XML document/*from   www  .  j  a  v a2  s. co m*/
 * @return attribute value element
 */
public static Element createAttributeValueElement(AttributeValueElementDTO attributeValueElementDTO,
        Document doc) {

    Element attributeValueElement = doc.createElement(PolicyConstants.ATTRIBUTE_VALUE);

    if (attributeValueElementDTO.getAttributeValue() != null
            && attributeValueElementDTO.getAttributeValue().trim().length() > 0) {

        attributeValueElement.setTextContent(attributeValueElementDTO.getAttributeValue().trim());

        if (attributeValueElementDTO.getAttributeDataType() != null
                && attributeValueElementDTO.getAttributeDataType().trim().length() > 0) {
            attributeValueElement.setAttribute(PolicyConstants.DATA_TYPE,
                    attributeValueElementDTO.getAttributeDataType());
        } else {
            attributeValueElement.setAttribute(PolicyConstants.DATA_TYPE, PolicyConstants.STRING_DATA_TYPE);
        }

    }

    return attributeValueElement;

}

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

/**
 * This creates XML representation of Attributes Element using AttributesElementDTO object
 *
 * @param elementDTO  AttributesElementDTO
 * @param doc Document//w w  w  .  jav  a 2s. c  o m
 * @return DOM element
 */
public static Element createAttributesElement(AttributesElementDTO elementDTO, Document doc) {

    Element attributesElement = doc.createElement(PolicyConstants.ATTRIBUTES);

    attributesElement.setAttribute(PolicyConstants.CATEGORY, elementDTO.getCategory());

    List<AttributeElementDTO> attributeElementDTOs = elementDTO.getAttributeElementDTOs();
    if (attributeElementDTOs != null && attributeElementDTOs.size() > 0) {
        for (AttributeElementDTO attributeElementDTO : attributeElementDTOs) {
            Element attributeElement = doc.createElement(PolicyConstants.ATTRIBUTE);
            attributeElement.setAttribute(PolicyConstants.ATTRIBUTE_ID, attributeElementDTO.getAttributeId());
            attributeElement.setAttribute(PolicyConstants.INCLUDE_RESULT,
                    Boolean.toString(attributeElementDTO.isIncludeInResult()));

            if (attributeElementDTO.getIssuer() != null
                    && attributeElementDTO.getIssuer().trim().length() > 0) {
                attributeElement.setAttribute(PolicyConstants.ISSUER, attributeElementDTO.getIssuer());
            }

            List<String> values = attributeElementDTO.getAttributeValues();
            for (String value : values) {
                Element attributeValueElement = doc.createElement(PolicyConstants.ATTRIBUTE_VALUE);
                attributeValueElement.setAttribute(PolicyConstants.DATA_TYPE,
                        attributeElementDTO.getDataType());
                attributeValueElement.setTextContent(value.trim());
                attributeElement.appendChild(attributeValueElement);
            }
            attributesElement.appendChild(attributeElement);
        }
    }
    return attributesElement;
}

From source file:com.nebhale.gpxconverter.StandardRouteBuilder.java

private Element ele(Document document, Point point) {
    Element ele = document.createElement("ele");
    ele.setTextContent(String.valueOf(point.getElevation()));

    return ele;/*from   w  ww  . java  2  s  . c  o  m*/
}

From source file:com.nebhale.gpxconverter.StandardRouteBuilder.java

private Element name(Document document, String name) {
    Element element = document.createElement("name");
    element.setTextContent(name);

    return element;
}