Example usage for org.w3c.dom Document createCDATASection

List of usage examples for org.w3c.dom Document createCDATASection

Introduction

In this page you can find the example usage for org.w3c.dom Document createCDATASection.

Prototype

public CDATASection createCDATASection(String data) throws DOMException;

Source Link

Document

Creates a CDATASection node whose value is the specified string.

Usage

From source file:org.fireflow.pdl.fpdl.io.FPDLSerializer.java

protected void writeDescription(Element parent, String desc) {
    if (desc == null || desc.trim().equals(""))
        return;//from w  w  w.  ja v a 2s .  co m
    Document doc = parent.getOwnerDocument();
    Element descElem = Util4Serializer.addElement(parent, DESCRIPTION);

    CDATASection cdata = doc.createCDATASection(useJDKTransformerFactory ? (" " + desc) : desc);//?jdkGBK bug 
    descElem.appendChild(cdata);
}

From source file:org.htmlcleaner.XWikiDOMSerializer.java

/**
 * Perform CDATA transformations if the user has specified to use CDATA inside scripts and style elements.
 *
 * @param document the W3C Document to use for creating new DOM elements
 * @param element the W3C element to which we'll add the text content to
 * @param bufferedContent the buffered text content on which we need to perform the CDATA transformations
 * @param item the current HTML Cleaner node being processed
 *///from   www .  j av a 2s. com
private void flushContent(Document document, Element element, StringBuffer bufferedContent, Object item) {
    if (bufferedContent.length() > 0 && !(item instanceof ContentNode)) {
        // Flush the buffered content
        boolean specialCase = this.props.isUseCdataForScriptAndStyle() && isScriptOrStyle(element);
        String content = bufferedContent.toString();

        if (this.escapeXml && !specialCase) {
            content = Utils.escapeXml(content, this.props, true);
        } else if (specialCase) {
            content = processCDATABlocks(content);
        }

        // Generate a javascript comment in front on the CDATA block so that it works in IE and when
        // serving XHTML under a mimetype of HTML.
        if (specialCase) {
            if (SCRIPT_TAG_NAME.equalsIgnoreCase(element.getNodeName())) {
                // JS
                element.appendChild(document.createTextNode(JS_COMMENT));
                element.appendChild(document.createCDATASection(NEW_LINE + content + NEW_LINE + JS_COMMENT));
            } else {
                // CSS
                element.appendChild(document.createTextNode(CSS_COMMENT_START));
                element.appendChild(document.createCDATASection(
                        CSS_COMMENT_END + StringUtils.chomp(content) + NEW_LINE + CSS_COMMENT_START));
                element.appendChild(document.createTextNode(CSS_COMMENT_END));
            }
        } else {
            element.appendChild(document.createTextNode(content));
        }

        bufferedContent.setLength(0);
    }
}

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

/**
 * This method sets a value to the xml that is the contentVersions Value. 
 *//*from www.  ja  va  2  s.co  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.controllers.kernel.impl.simple.ContentVersionController.java

public void updateAttributeValue(Integer contentVersionId, String attributeName, String attributeValue,
        InfoGluePrincipal infogluePrincipal, boolean skipValidate, Database db,
        boolean skipSiteNodeVersionUpdate) throws SystemException, Bug {
    ContentVersionVO contentVersionVO = null;
    try {/*from ww w  . j a v  a2s .c o m*/
        contentVersionVO = getContentVersionVOWithId(contentVersionId);
    } catch (Exception e) {
        logger.warn("Problem finding version: " + e.getMessage() + " - skipping - was probably deleted");
    }

    if (contentVersionVO != null) {
        try {
            InputSource inputSource = new InputSource(new StringReader(contentVersionVO.getVersionValue()));

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

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

            boolean existed = false;
            nl = attributesNode.getChildNodes();
            for (int i = 0; i < nl.getLength(); i++) {
                Node n = nl.item(i);
                if (n.getNodeName().equalsIgnoreCase(attributeName)) {
                    if (n.getFirstChild() != null && n.getFirstChild().getNodeValue() != null) {
                        n.getFirstChild().setNodeValue(attributeValue);
                        existed = true;
                        break;
                    } else {
                        CDATASection cdata = document.createCDATASection(attributeValue);
                        n.appendChild(cdata);
                        existed = true;
                        break;
                    }
                }
            }

            if (existed == false) {
                org.w3c.dom.Element attributeElement = document.createElement(attributeName);
                attributesNode.appendChild(attributeElement);
                CDATASection cdata = document.createCDATASection(attributeValue);
                attributeElement.appendChild(cdata);
            }

            StringBuffer sb = new StringBuffer();
            org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb);
            logger.info("sb:" + sb);
            contentVersionVO.setVersionValue(sb.toString());
            contentVersionVO.setVersionModifier(infogluePrincipal.getName());
            update(contentVersionVO.getContentId(), contentVersionVO.getLanguageId(), contentVersionVO,
                    infogluePrincipal, skipValidate, db, skipSiteNodeVersionUpdate);
        } catch (Exception e) {
            logger.error("Error updating version value: " + e.getMessage(), e);
        }
    }
}

From source file:org.kapott.hbci.tools.TransactionsToXML.java

public Document createXMLDocument(List<UmsLine> transactions, String rawMT940) {
    // Empfangene Transaktionen als XML-Datei aufbereiten
    DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
    fac.setIgnoringComments(true);/*from  w w w .  ja v a  2  s . co m*/
    fac.setValidating(false);

    // create document
    DocumentBuilder builder;
    try {
        builder = fac.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
    Document doc = builder.newDocument();

    Element root = doc.createElement("account_transactions");
    doc.appendChild(root);

    // <transactions>
    if (transactions != null) {
        Element transElement = doc.createElement("transactions");
        root.appendChild(transElement);
        createTransactionElements(doc, transElement, transactions);
    }

    // <raw>
    if (rawMT940 != null) {
        Element rawElem = doc.createElement("raw");
        root.appendChild(rawElem);

        try {
            String mt940_encoded = Base64.encodeBase64String(rawMT940.getBytes("ISO-8859-1"));
            rawElem.appendChild(doc.createCDATASection(mt940_encoded));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    return doc;
}

From source file:org.kuali.coeus.common.budget.framework.nonpersonnel.BudgetJustificationWrapper.java

public String toString() {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();

    try (Writer out = new StringWriter()) {
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document document = docBuilder.newDocument();

        Element rootElement = document.createElement("budgetJustification");
        rootElement.setAttribute("lastUpdateBy", lastUpdateUser);
        rootElement.setAttribute("lastUpdateOn", lastUpdateTime);
        document.appendChild(rootElement);

        CDATASection cdata = document.createCDATASection(justificationText == null ? "" : justificationText);
        rootElement.appendChild(cdata);/*from ww w  .  java2  s  . c  o  m*/

        Transformer tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        tf.setOutputProperty(OutputKeys.INDENT, "yes");
        tf.transform(new DOMSource(document), new StreamResult(out));
        return out.toString();
    } catch (ParserConfigurationException | TransformerException | IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.kuali.kfs.module.ar.batch.service.impl.CustomerInvoiceWriteoffBatchServiceImpl.java

protected Document transformVOtoXml(CustomerInvoiceWriteoffBatchVO writeoffBatchVO) {

    Document xmldoc = new DocumentImpl();
    Element e = null;//from   w  w w  . ja  va2 s  . c  om
    Element invoicesElement = null;
    Node n = null;

    Element root = xmldoc.createElementNS("http://www.kuali.org/kfs/ar/customer", XML_ROOT_ELEMENT_NAME);
    root.setAttribute("xmlns", getBatchXMLNamespace());
    root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");

    //  create submittedBy element
    e = xmldoc.createElement(ArPropertyConstants.SUBMITTED_BY_PRINCIPAL_ID);
    n = xmldoc.createCDATASection(writeoffBatchVO.getSubmittedByPrincipalName());
    e.appendChild(n);
    root.appendChild(e);

    //  create submittedOn element
    e = xmldoc.createElement(ArPropertyConstants.SUBMITTED_ON);
    n = xmldoc.createCDATASection(writeoffBatchVO.getSubmittedOn());
    e.appendChild(n);
    root.appendChild(e);

    //  create note element
    e = xmldoc.createElement(ArPropertyConstants.NOTE);
    n = xmldoc.createCDATASection(writeoffBatchVO.getNote());
    e.appendChild(n);
    root.appendChild(e);

    //  create invoices element and list of invoice child elements
    invoicesElement = xmldoc.createElement(ArConstants.LOOKUP_INVOICE_NUMBERS);
    for (String invoiceNumber : writeoffBatchVO.getInvoiceNumbers()) {
        e = xmldoc.createElement(ArConstants.LOOKUP_INVOICE_NUMBER);
        n = xmldoc.createCDATASection(invoiceNumber);
        e.appendChild(n);
        invoicesElement.appendChild(e);
    }
    root.appendChild(invoicesElement);

    xmldoc.appendChild(root);

    return xmldoc;
}

From source file:org.kuali.rice.kew.mail.service.impl.StyleableEmailContentServiceImpl.java

protected static void addCDataElement(Document doc, Element baseElement, String elementName,
        Object elementData) {/*from   w ww .  ja  v a2  s  . c om*/
    Element element = doc.createElement(elementName);
    String dataValue = "";
    if (elementData != null) {
        dataValue = elementData.toString();
    }
    element.appendChild(doc.createCDATASection(dataValue));
    baseElement.appendChild(element);
}

From source file:org.mrgeo.resources.wms.WmsGenerator.java

private Response writeError(Response.Status httpStatus, final Exception e) {
    try {//from www.ja v  a  2s. c om
        Document doc;
        final DocumentBuilderFactory dBF = DocumentBuilderFactory.newInstance();
        final DocumentBuilder builder;
        builder = dBF.newDocumentBuilder();
        doc = builder.newDocument();

        final Element ser = doc.createElement("ServiceExceptionReport");
        doc.appendChild(ser);
        ser.setAttribute("version", WMS_VERSION);
        final Element se = XmlUtils.createElement(ser, "ServiceException");
        String msg = e.getLocalizedMessage();
        if (msg == null || msg.isEmpty()) {
            msg = e.getClass().getName();
        }
        final ByteArrayOutputStream strm = new ByteArrayOutputStream();
        e.printStackTrace(new PrintStream(strm));
        CDATASection msgNode = doc.createCDATASection(strm.toString());
        se.appendChild(msgNode);
        final ByteArrayOutputStream xmlStream = new ByteArrayOutputStream();
        final PrintWriter out = new PrintWriter(xmlStream);
        DocumentUtils.writeDocument(doc, version, WMS_SERVICE, out);
        out.close();
        return Response.status(httpStatus).header("Content-Type", MediaType.TEXT_XML)
                .entity(xmlStream.toString()).build();
    } catch (ParserConfigurationException e1) {
    } catch (TransformerException e1) {
    }
    // Fallback in case there is an XML exception above
    return Response.status(httpStatus).entity(e.getLocalizedMessage()).build();
}

From source file:org.mrgeo.resources.wms.WmsGenerator.java

private Response writeError(Response.Status httpStatus, final String msg) {
    try {//from   w w  w .ja  va 2s.co m
        Document doc;
        final DocumentBuilderFactory dBF = DocumentBuilderFactory.newInstance();
        final DocumentBuilder builder = dBF.newDocumentBuilder();
        doc = builder.newDocument();

        final Element ser = doc.createElement("ServiceExceptionReport");
        doc.appendChild(ser);
        ser.setAttribute("version", WMS_VERSION);
        final Element se = XmlUtils.createElement(ser, "ServiceException");
        CDATASection msgNode = doc.createCDATASection(msg);
        se.appendChild(msgNode);
        final ByteArrayOutputStream xmlStream = new ByteArrayOutputStream();
        final PrintWriter out = new PrintWriter(xmlStream);
        DocumentUtils.writeDocument(doc, version, WMS_SERVICE, out);
        out.close();
        return Response.status(httpStatus).header("Content-Type", MediaType.TEXT_XML)
                .entity(xmlStream.toString()).build();
    } catch (ParserConfigurationException e1) {
    } catch (TransformerException e1) {
    }
    // Fallback in case there is an XML exception above
    return Response.status(httpStatus).entity(msg).build();
}