Example usage for org.w3c.dom.ls DOMImplementationLS createLSSerializer

List of usage examples for org.w3c.dom.ls DOMImplementationLS createLSSerializer

Introduction

In this page you can find the example usage for org.w3c.dom.ls DOMImplementationLS createLSSerializer.

Prototype

public LSSerializer createLSSerializer();

Source Link

Document

Create a new LSSerializer object.

Usage

From source file:Main.java

/**
 * Converts a Document to a String// w w  w .  ja v a  2  s . c  o  m
 * 
 * @param doc
 *            The Document to be converted
 * @return The String representation of the Document
 */
public static String convertDocumentToString(final Document doc) {
    final DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
    final LSSerializer lsSerializer = domImplementation.createLSSerializer();
    // lsSerializer.getDomConfig().setParameter("format-pretty-print",
    // Boolean.TRUE);
    final String xml = lsSerializer.writeToString(doc);

    return xml;
}

From source file:Main.java

public static boolean serialize(Document doc, boolean setXmlDecl, File f) throws FileNotFoundException {
    DOMImplementationLS lsImpl = (DOMImplementationLS) doc.getImplementation().getFeature("LS", "3.0");
    LSSerializer serializer = lsImpl.createLSSerializer();
    serializer.getDomConfig().setParameter("xml-declaration", setXmlDecl); // set it to false to get

    LSOutput output = lsImpl.createLSOutput();
    output.setByteStream(new FileOutputStream(f));
    return serializer.write(doc, output);
}

From source file:eu.dasish.annotation.backend.Helpers.java

public static String elementToString(Element element) {
    Document document = element.getOwnerDocument();
    DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
    LSSerializer serializer = domImplLS.createLSSerializer();
    String result = serializer.writeToString(element);
    return result;
}

From source file:com.qualogy.qafe.bind.orm.jibx.ORMBinder.java

public static String docToString(Document doc) {
    DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
    LSSerializer lsSerializer = domImplementation.createLSSerializer();
    return lsSerializer.writeToString(doc);
}

From source file:Main.java

public static String prettyPrint(Document document) {
    // Pretty-prints a DOM document to XML using DOM Load and Save's
    // LSSerializer.
    // Note that the "format-pretty-print" DOM configuration parameter can
    // only be set in JDK 1.6+.

    DOMImplementationRegistry domImplementationRegistry;
    try {//from ww w . j  av  a2s.co  m
        domImplementationRegistry = DOMImplementationRegistry.newInstance();
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | ClassCastException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    DOMImplementation domImplementation = document.getImplementation();
    if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) {
        /*DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation
          .getFeature("LS", "3.0");*/
        DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementationRegistry
                .getDOMImplementation("LS");

        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(document, lsOutput);
            return stringWriter.toString();
        } else {
            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:Main.java

private static void serializeXML(Element doc, Writer writer, boolean addXmlDeclaration) throws IOException {
    try {//from  w  w  w. j a va2 s  .c o  m
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer serializer = impl.createLSSerializer();
        DOMConfiguration config = serializer.getDomConfig();
        config.setParameter("xml-declaration", addXmlDeclaration);
        // config.setParameter("format-pretty-print", true);
        // config.setParameter("normalize-characters", true);
        LSOutput out = impl.createLSOutput();
        out.setCharacterStream(writer);
        serializer.write(doc, out);
    } catch (Throwable e) {
        throw new IOException(e.getMessage());
    }
}

From source file:Main.java

/**
 * Obtain a the DOM, level 3, Load/Save serializer {@link LSSerializer} instance from the
 * given {@link DOMImplementationLS} instance.
 * //from  w  w w  . j ava 2  s  .  co m
 * <p>
 * The serializer instance will be configured with the parameters passed as the <code>serializerParams</code>
 * argument. It will also be configured with an {@link LSSerializerFilter} that shows all nodes to the filter, 
 * and accepts all nodes shown.
 * </p>
 * 
 * @param domImplLS the DOM Level 3 Load/Save implementation to use
 * @param serializerParams parameters to pass to the {@link DOMConfiguration} of the serializer
 *         instance, obtained via {@link LSSerializer#getDomConfig()}. May be null.
 *         
 * @return a new LSSerializer instance
 */
public static LSSerializer getLSSerializer(DOMImplementationLS domImplLS,
        Map<String, Object> serializerParams) {
    LSSerializer serializer = domImplLS.createLSSerializer();

    serializer.setFilter(new LSSerializerFilter() {

        public short acceptNode(Node arg0) {
            return FILTER_ACCEPT;
        }

        public int getWhatToShow() {
            return SHOW_ALL;
        }
    });

    if (serializerParams != null) {
        DOMConfiguration serializerDOMConfig = serializer.getDomConfig();
        for (String key : serializerParams.keySet()) {
            serializerDOMConfig.setParameter(key, serializerParams.get(key));
        }
    }

    return serializer;
}

From source file:Main.java

/**
 * Converts a dom element to a String//from   w  w  w.  j  a  v  a2 s.c  o  m
 * 
 * @param node
 * @return the dom as a String
 */
public static String writeDomToString(Element node) {
    DOMImplementation domImplementation = node.getOwnerDocument().getImplementation();
    if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) {
        DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation.getFeature("LS",
                "3.0");
        LSSerializer lsSerializer = domImplementationLS.createLSSerializer();

        LSOutput lsOutput = domImplementationLS.createLSOutput();
        lsOutput.setEncoding("UTF-8");

        StringWriter stringWriter = new StringWriter();
        lsOutput.setCharacterStream(stringWriter);
        lsSerializer.write(node, lsOutput);
        return stringWriter.toString();
    } else {
        throw new RuntimeException("DOM 3.0 LS and/or DOM 2.0 Core not supported.");
    }
}

From source file:Main.java

public static <T extends OutputStream> T lsSerializeDom(Node doc, T byteStream, String encoding)
        throws Exception {
    if (doc == null)
        return byteStream;
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");

    LSOutput lsOutput = impl.createLSOutput();
    lsOutput.setByteStream(byteStream);// ww  w  .ja  v  a2 s.  c om
    encoding = encoding == null ? "UTF-8" : encoding;
    lsOutput.setEncoding(encoding);

    impl.createLSSerializer().write(doc, lsOutput);

    return byteStream;
}

From source file:Main.java

public static void writeNode(Node node, OutputStream os, boolean formatted)
        throws ClassCastException, ClassNotFoundException, InstantiationException, IllegalAccessException {
    DOMImplementationRegistry domImplementationRegistry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementationRegistry
            .getDOMImplementation("LS");

    LSOutput lsOutput = domImplementationLS.createLSOutput();
    lsOutput.setEncoding("UTF-8");
    lsOutput.setByteStream(os);/*from w w w .j  a  v  a  2s  .  c o m*/

    LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
    if (formatted)
        setLsSeriliserToFormatted(lsSerializer);
    lsSerializer.write(node, lsOutput);
}