Example usage for org.w3c.dom.ls LSOutput setCharacterStream

List of usage examples for org.w3c.dom.ls LSOutput setCharacterStream

Introduction

In this page you can find the example usage for org.w3c.dom.ls LSOutput setCharacterStream.

Prototype

public void setCharacterStream(java.io.Writer characterStream);

Source Link

Document

An attribute of a language and binding dependent type that represents a writable stream to which 16-bit units can be output.

Usage

From source file:org.apache.syncope.core.logic.init.CamelRouteLoader.java

private String nodeToString(final Node content, final DOMImplementationLS domImpl) {
    StringWriter writer = new StringWriter();
    try {// ww  w.j av a2  s.c o m
        LSSerializer serializer = domImpl.createLSSerializer();
        serializer.getDomConfig().setParameter("xml-declaration", false);
        LSOutput lso = domImpl.createLSOutput();
        lso.setCharacterStream(writer);
        serializer.write(content, lso);
    } catch (Exception e) {
        LOG.debug("While serializing route node", e);
    }
    return writer.toString();
}

From source file:org.intellij.xquery.runner.rt.vendor.exist.ExistRunnerApp.java

private String prettyPrint(Element node, DOMImplementation domImplementation)
        throws InstantiationException, IllegalAccessException, ClassNotFoundException {
    DOMImplementationRegistry domImplementationRegistry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementationRegistry
            .getDOMImplementation("LS");
    LSOutput lsOutput = domImplementationLS.createLSOutput();
    LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
    lsOutput.setEncoding("UTF-8");
    Writer stringWriter = new StringWriter();
    lsOutput.setCharacterStream(stringWriter);
    lsSerializer.write(node, lsOutput);/*www .  ja  v a 2 s  .  c  o  m*/
    String result = stringWriter.toString();
    return result;
}

From source file:org.ms123.common.importing.XmlImporter.java

private String prettyPrint(Element e) {
    DOMImplementation domImplementation = m_document.getImplementation();
    if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) {
        DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation.getFeature("LS",
                "3.0");
        LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
        DOMConfiguration domConfiguration = lsSerializer.getDomConfig();
        if (domConfiguration.canSetParameter("format-pretty-print", Boolean.TRUE)) {
            lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
            LSOutput lsOutput = domImplementationLS.createLSOutput();
            lsOutput.setEncoding("UTF-8");
            StringWriter stringWriter = new StringWriter();
            lsOutput.setCharacterStream(stringWriter);
            lsSerializer.write(e, lsOutput);
            return stringWriter.toString();
        } else {//from  w ww .j a  va2s  . com
            throw new RuntimeException("DOMConfiguration 'format-pretty-print' parameter isn't settable.");
        }
    } else {
        throw new RuntimeException("DOM 3.0 LS and/or DOM 2.0 Core not supported.");
    }
}

From source file:org.sakaiproject.util.StorageUtils.java

/**
 * Write a DOM Document to an output stream.
 * /*from  w ww .ja va 2s .c om*/
 * @param doc
 *        The DOM Document to write.
 * @param out
 *        The output stream.
 */
public static String writeDocumentToString(Document doc) {
    try {

        StringWriter sw = new StringWriter();

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        DOMImplementation impl = builder.getDOMImplementation();

        DOMImplementationLS feature = (DOMImplementationLS) impl.getFeature("LS", "3.0");
        LSSerializer serializer = feature.createLSSerializer();
        LSOutput output = feature.createLSOutput();
        output.setCharacterStream(sw);
        output.setEncoding("UTF-8");
        serializer.write(doc, output);

        sw.flush();
        return sw.toString();
    } catch (Exception any) {
        M_log.warn("writeDocumentToString: " + any.toString());
        return null;
    }
}

From source file:org.wso2.carbon.mediation.configadmin.util.XMLPrettyPrinter.java

/**
 * XML Pretty Print method with XML Comments support.
 *
 * @return XML formatted String/* w w  w .j  a v  a2 s.  c  o  m*/
 */
public String xmlFormatWithComments() {
    String xmlOutput = null;
    Document doc;
    LSSerializer lsSerializer;

    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        doc = documentBuilderFactory.newDocumentBuilder().parse(in);

        DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
        lsSerializer = domImplementation.createLSSerializer();
        lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);

        LSOutput lsOutput = domImplementation.createLSOutput();
        lsOutput.setEncoding(encoding);
        Writer stringWriter = new StringWriter();
        lsOutput.setCharacterStream(stringWriter);
        lsSerializer.write(doc, lsOutput);

        xmlOutput = stringWriter.toString();

    } catch (IOException e) {
        log.error("XML Pretty Printer Error reading data from given InputStream to XML Document ", e);
    } catch (SAXException e) {
        log.error("XML Pretty Printer Error parsing the given InputStream to XML Document", e);
    } catch (Exception e) {
        log.error("XML Pretty Printer failed. ", e);
    }
    return xmlOutput;
}