Search in sources :

Example 16 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS in project CloudStack-archive by CloudStack-extras.

the class VsmCommand method serialize.

private static String serialize(DOMImplementation domImpl, Document document) {
    DOMImplementationLS ls = (DOMImplementationLS) domImpl;
    LSSerializer lss = ls.createLSSerializer();
    return lss.writeToString(document);
}
Also used : DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) LSSerializer(org.w3c.dom.ls.LSSerializer)

Example 17 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS 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 18 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS in project wildfly by wildfly.

the class UsernameTokenCallbackHandler method toString.

private String toString(Node node) {
    String str = null;
    if (node != null) {
        DOMImplementationLS lsImpl = (DOMImplementationLS) node.getOwnerDocument().getImplementation().getFeature("LS", "3.0");
        LSSerializer serializer = lsImpl.createLSSerializer();
        //by default its true, so set it to false to get String without xml-declaration
        serializer.getDomConfig().setParameter("xml-declaration", false);
        str = serializer.writeToString(node);
    }
    return str;
}
Also used : DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) LSSerializer(org.w3c.dom.ls.LSSerializer)

Example 19 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS in project cloudstack by apache.

the class VsmCommand method serialize.

private static String serialize(DOMImplementation domImpl, Document document) {
    DOMImplementationLS ls = (DOMImplementationLS) domImpl;
    LSSerializer lss = ls.createLSSerializer();
    return lss.writeToString(document);
}
Also used : DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) LSSerializer(org.w3c.dom.ls.LSSerializer)

Example 20 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS 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)24 LSSerializer (org.w3c.dom.ls.LSSerializer)19 DocumentBuilder (javax.xml.parsers.DocumentBuilder)8 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)8 Document (org.w3c.dom.Document)8 DOMImplementation (org.w3c.dom.DOMImplementation)6 DOMImplementationRegistry (org.w3c.dom.bootstrap.DOMImplementationRegistry)6 LSOutput (org.w3c.dom.ls.LSOutput)6 IOException (java.io.IOException)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)5 Element (org.w3c.dom.Element)4 StringWriter (java.io.StringWriter)3 Node (org.w3c.dom.Node)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 TransformerException (javax.xml.transform.TransformerException)2 DOMException (org.w3c.dom.DOMException)2 NodeList (org.w3c.dom.NodeList)2 BinaryContent (ddf.catalog.data.BinaryContent)1 Metacard (ddf.catalog.data.Metacard)1