Search in sources :

Example 1 with LSOutput

use of org.w3c.dom.ls.LSOutput in project camel by apache.

the class XmlSignatureHelper method transformNonTextNodeToOutputStream.

/**
     * Serializes a node using a certain character encoding.
     * 
     * @param node
     *            DOM node to serialize
     * @param os
     *            output stream, to which the node is serialized
     * @param omitXmlDeclaration
     *            indicator whether to omit the XML declaration or not
     * @param encoding
     *            character encoding, can be <code>null</code>, if
     *            <code>null</code> then "UTF-8" is used
     * @throws Exception
     */
public static void transformNonTextNodeToOutputStream(Node node, OutputStream os, boolean omitXmlDeclaration, String encoding) throws Exception {
    // therefore we switched to DOMImplementationLS
    if (encoding == null) {
        encoding = "UTF-8";
    }
    DOMImplementationRegistry domImplementationRegistry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementationRegistry.getDOMImplementation("LS");
    LSOutput lsOutput = domImplementationLS.createLSOutput();
    lsOutput.setEncoding(encoding);
    lsOutput.setByteStream(os);
    LSSerializer lss = domImplementationLS.createLSSerializer();
    lss.getDomConfig().setParameter("xml-declaration", !omitXmlDeclaration);
    lss.write(node, lsOutput);
}
Also used : DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) DOMImplementationRegistry(org.w3c.dom.bootstrap.DOMImplementationRegistry) LSSerializer(org.w3c.dom.ls.LSSerializer) LSOutput(org.w3c.dom.ls.LSOutput)

Example 2 with LSOutput

use of org.w3c.dom.ls.LSOutput in project android by JetBrains.

the class ThemePreviewBuilder method printDebug.

private static void printDebug(@NotNull PrintStream out, @NotNull Document document) {
    try {
        DOMImplementationRegistry reg = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) reg.getDOMImplementation("LS");
        LSSerializer serializer = impl.createLSSerializer();
        LSOutput lsOutput = impl.createLSOutput();
        lsOutput.setEncoding("UTF-8");
        lsOutput.setByteStream(out);
        serializer.write(document, lsOutput);
    } catch (ClassNotFoundException e) {
        e.printStackTrace(out);
    } catch (InstantiationException e) {
        e.printStackTrace(out);
    } catch (IllegalAccessException e) {
        e.printStackTrace(out);
    }
}
Also used : DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) DOMImplementationRegistry(org.w3c.dom.bootstrap.DOMImplementationRegistry) LSSerializer(org.w3c.dom.ls.LSSerializer) LSOutput(org.w3c.dom.ls.LSOutput)

Example 3 with LSOutput

use of org.w3c.dom.ls.LSOutput in project openhab1-addons by openhab.

the class Helper method documentToString.

/***
     * Helper method which converts XML Document into pretty formatted string
     *
     * @param doc to convert
     * @return converted XML as String
     */
public static String documentToString(Document doc) {
    String strMsg = "";
    try {
        DOMImplementation domImpl = doc.getImplementation();
        DOMImplementationLS domImplLS = (DOMImplementationLS) domImpl.getFeature("LS", "3.0");
        LSSerializer lsSerializer = domImplLS.createLSSerializer();
        lsSerializer.getDomConfig().setParameter("format-pretty-print", true);
        Writer stringWriter = new StringWriter();
        LSOutput lsOutput = domImplLS.createLSOutput();
        lsOutput.setEncoding("UTF-8");
        lsOutput.setCharacterStream(stringWriter);
        lsSerializer.write(doc, lsOutput);
        strMsg = stringWriter.toString();
    } catch (Exception e) {
        logger.warn("Error occured when converting document to string", e);
    }
    return strMsg;
}
Also used : StringWriter(java.io.StringWriter) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) DOMImplementation(org.w3c.dom.DOMImplementation) LSSerializer(org.w3c.dom.ls.LSSerializer) LSOutput(org.w3c.dom.ls.LSOutput) StringWriter(java.io.StringWriter) Writer(java.io.Writer) TransformerException(javax.xml.transform.TransformerException)

Example 4 with LSOutput

use of org.w3c.dom.ls.LSOutput in project tomee by apache.

the class XmlFormatter method format.

public static String format(final String in) {
    try {
        final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        final DocumentBuilder db = dbf.newDocumentBuilder();
        final InputSource is = new InputSource(new StringReader(in));
        final Document document = db.parse(is);
        final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        final DOMImplementationLS impl = DOMImplementationLS.class.cast(registry.getDOMImplementation("XML 3.0 LS 3.0"));
        if (impl == null) {
            return in;
        }
        final LSSerializer serializer = impl.createLSSerializer();
        if (serializer.getDomConfig().canSetParameter("format-pretty-print", Boolean.TRUE)) {
            serializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
            final LSOutput lsOutput = impl.createLSOutput();
            lsOutput.setEncoding("UTF-8");
            final StringWriter stringWriter = new StringWriter();
            lsOutput.setCharacterStream(stringWriter);
            serializer.write(document, lsOutput);
            return stringWriter.toString().replace("\"UTF-8\"?><", "\"UTF-8\"?>\n<");
        }
        return in;
    } catch (final Throwable t) {
        // just to be more sexy so ignore it and use ugly xml
        return in;
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) StringWriter(java.io.StringWriter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) StringReader(java.io.StringReader) DOMImplementationRegistry(org.w3c.dom.bootstrap.DOMImplementationRegistry) LSSerializer(org.w3c.dom.ls.LSSerializer) Document(org.w3c.dom.Document) LSOutput(org.w3c.dom.ls.LSOutput)

Example 5 with LSOutput

use of org.w3c.dom.ls.LSOutput in project ddf by codice.

the class XPathHelper method print.

/**
     * Prints a given node as a String
     *
     * @param n
     *            - the node to print as a String
     * @param encoding
     *            - the character encoding to use for the returned String
     * @return the Node as a String, null if an exception is thrown or null is passed in.
     */
public static String print(Node n, String encoding) {
    if (n == null) {
        return null;
    }
    try {
        Document document = null;
        if (n instanceof Document) {
            document = (Document) n;
        } else {
            document = n.getOwnerDocument();
        }
        StringWriter stringOut = new StringWriter();
        DOMImplementationLS domImpl = (DOMImplementationLS) document.getImplementation();
        LSSerializer serializer = domImpl.createLSSerializer();
        LSOutput lsOut = domImpl.createLSOutput();
        lsOut.setEncoding(encoding);
        lsOut.setCharacterStream(stringOut);
        serializer.write(n, lsOut);
        return stringOut.toString();
    } catch (DOMException | LSException e) {
        LOGGER.debug(e.getMessage(), e);
    }
    return null;
}
Also used : DOMException(org.w3c.dom.DOMException) StringWriter(java.io.StringWriter) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) LSSerializer(org.w3c.dom.ls.LSSerializer) Document(org.w3c.dom.Document) LSOutput(org.w3c.dom.ls.LSOutput) LSException(org.w3c.dom.ls.LSException)

Aggregations

DOMImplementationLS (org.w3c.dom.ls.DOMImplementationLS)5 LSOutput (org.w3c.dom.ls.LSOutput)5 LSSerializer (org.w3c.dom.ls.LSSerializer)5 StringWriter (java.io.StringWriter)3 DOMImplementationRegistry (org.w3c.dom.bootstrap.DOMImplementationRegistry)3 Document (org.w3c.dom.Document)2 StringReader (java.io.StringReader)1 Writer (java.io.Writer)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 TransformerException (javax.xml.transform.TransformerException)1 DOMException (org.w3c.dom.DOMException)1 DOMImplementation (org.w3c.dom.DOMImplementation)1 LSException (org.w3c.dom.ls.LSException)1 InputSource (org.xml.sax.InputSource)1