Search in sources :

Example 41 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 42 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS 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 43 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 lsSerializer = domImpl.createLSSerializer();
        lsSerializer.getDomConfig().setParameter(XML_DECLARATION, false);
        LSOutput lsout = domImpl.createLSOutput();
        lsout.setEncoding(encoding);
        lsout.setCharacterStream(stringOut);
        /*DDF-4382 Serializer doesn't serialize attribute nodes - attribute node's child node is a
      NODE.TEXT_NODE version with the same information. If Attribute node, get the child
      NODE.TEXT_NODE version and then use that node for serialization*/
        if (n.getNodeType() == Node.ATTRIBUTE_NODE && n.hasChildNodes()) {
            n = n.getFirstChild();
        }
        lsSerializer.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)

Example 44 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS in project Payara by payara.

the class CombinedXPath method writeXML.

private static synchronized void writeXML(final Node node, final Writer writer) {
    try {
        if (lsSerializer == null) {
            final DOMImplementation domImpl = DOMImplementationRegistry.newInstance().getDOMImplementation("");
            final DOMImplementationLS domLS = (DOMImplementationLS) domImpl.getFeature("LS", "3.0");
            lsOutput = domLS.createLSOutput();
            lsOutput.setEncoding("UTF-8");
            lsSerializer = domLS.createLSSerializer();
        }
        lsOutput.setCharacterStream(writer);
        lsSerializer.write(node, lsOutput);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) DOMImplementation(org.w3c.dom.DOMImplementation) XPathExpressionException(javax.xml.xpath.XPathExpressionException)

Example 45 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS in project iaf by ibissource.

the class Utils method dom2String2.

public static String dom2String2(Document document) {
    DOMImplementationRegistry registry;
    try {
        registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS domImplLS = (DOMImplementationLS) registry.getDOMImplementation("LS");
        // Create a serializer for the DOM
        LSSerializer ser = domImplLS.createLSSerializer();
        LSOutput out = domImplLS.createLSOutput();
        // Writer will be a String
        StringWriter stringOut = new StringWriter();
        out.setCharacterStream(stringOut);
        // Serialize the DOM
        ser.write(document, out);
        System.out.println("STRXML = " + // Spit out the DOM as a String
        stringOut.toString());
        return stringOut.toString();
    } catch (Exception e) {
        System.err.println("Cannot create registry: " + e.getMessage());
    }
    return null;
}
Also used : StringWriter(java.io.StringWriter) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) DOMImplementationRegistry(org.w3c.dom.bootstrap.DOMImplementationRegistry) LSSerializer(org.w3c.dom.ls.LSSerializer) LSOutput(org.w3c.dom.ls.LSOutput) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Aggregations

DOMImplementationLS (org.w3c.dom.ls.DOMImplementationLS)54 LSSerializer (org.w3c.dom.ls.LSSerializer)43 Document (org.w3c.dom.Document)22 DOMImplementationRegistry (org.w3c.dom.bootstrap.DOMImplementationRegistry)21 LSOutput (org.w3c.dom.ls.LSOutput)18 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)14 IOException (java.io.IOException)13 DocumentBuilder (javax.xml.parsers.DocumentBuilder)13 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)12 Node (org.w3c.dom.Node)12 DOMImplementation (org.w3c.dom.DOMImplementation)10 Element (org.w3c.dom.Element)10 StringWriter (java.io.StringWriter)9 LSParser (org.w3c.dom.ls.LSParser)8 InputSource (org.xml.sax.InputSource)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 StringReader (java.io.StringReader)5 TransformerException (javax.xml.transform.TransformerException)5 NodeList (org.w3c.dom.NodeList)5 LSInput (org.w3c.dom.ls.LSInput)5