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

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

Introduction

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

Prototype

public void setByteStream(java.io.OutputStream byteStream);

Source Link

Document

An attribute of a language and binding dependent type that represents a writable stream of bytes.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("XML 3.0 LS 3.0");
    if (impl == null) {
        System.out.println("No DOMImplementation found !");
        System.exit(0);/* w ww.j  a v  a 2 s . c o m*/
    }

    System.out.printf("DOMImplementationLS: %s\n", impl.getClass().getName());

    LSParser parser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, "http://www.w3.org/TR/REC-xml");
    // http://www.w3.org/2001/XMLSchema
    System.out.printf("LSParser: %s\n", parser.getClass().getName());

    Document doc = parser.parseURI("");

    LSSerializer serializer = impl.createLSSerializer();
    LSOutput output = impl.createLSOutput();
    output.setEncoding("UTF-8");
    output.setByteStream(System.out);
    serializer.write(doc, output);
    System.out.println();
}

From source file:DOM3.java

public static void main(String[] argv) {

    if (argv.length == 0) {
        printUsage();//from w  w w . j  av a  2  s. com
        System.exit(1);
    }

    try {

        // get DOM Implementation using DOM Registry
        System.setProperty(DOMImplementationRegistry.PROPERTY,
                "org.apache.xerces.dom.DOMXSImplementationSourceImpl");
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();

        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");

        // create DOMBuilder
        builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);

        DOMConfiguration config = builder.getDomConfig();

        // create Error Handler
        DOMErrorHandler errorHandler = new DOM3();

        // create filter
        LSParserFilter filter = new DOM3();

        builder.setFilter(filter);

        // set error handler
        config.setParameter("error-handler", errorHandler);

        // set validation feature
        // config.setParameter("validate", Boolean.FALSE);
        config.setParameter("validate", Boolean.TRUE);

        // set schema language
        config.setParameter("schema-type", "http://www.w3.org/2001/XMLSchema");
        // config.setParameter("psvi",Boolean.TRUE);
        // config.setParameter("schema-type","http://www.w3.org/TR/REC-xml");

        // set schema location
        config.setParameter("schema-location", "personal.xsd");

        // parse document
        System.out.println("Parsing " + argv[0] + "...");
        Document doc = builder.parseURI(argv[0]);

        // set error handler on the Document
        config = doc.getDomConfig();

        config.setParameter("error-handler", errorHandler);

        // set validation feature
        config.setParameter("validate", Boolean.TRUE);
        config.setParameter("schema-type", "http://www.w3.org/2001/XMLSchema");
        // config.setParameter("schema-type","http://www.w3.org/TR/REC-xml");
        config.setParameter("schema-location", "data/personal.xsd");

        // remove comments from the document
        config.setParameter("comments", Boolean.FALSE);

        System.out.println("Normalizing document... ");
        doc.normalizeDocument();

        // create DOMWriter
        LSSerializer domWriter = impl.createLSSerializer();

        System.out.println("Serializing document... ");
        config = domWriter.getDomConfig();
        config.setParameter("xml-declaration", Boolean.FALSE);
        // config.setParameter("validate",errorHandler);

        // serialize document to standard output
        // domWriter.writeNode(System.out, doc);
        LSOutput dOut = impl.createLSOutput();
        dOut.setByteStream(System.out);
        domWriter.write(doc, dOut);

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

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);
    encoding = encoding == null ? "UTF-8" : encoding;
    lsOutput.setEncoding(encoding);/*from   ww w .j av  a  2  s .  c  o m*/

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

    return byteStream;
}

From source file:Main.java

public static boolean writeDocumentToStream(Document doc, OutputStream os) {
    // Check if DOM Load and Save is supported

    if (!((doc.getFeature("Core", "3.0") != null) && (doc.getFeature("LS", "3.0") != null)))
        throw new RuntimeException("DOM Load and Save unsupported");

    // Grab the available implementation

    DOMImplementationLS DOMiLS = (DOMImplementationLS) (doc.getImplementation()).getFeature("LS", "3.0");

    // Create LS output destination

    LSOutput lso = DOMiLS.createLSOutput();
    lso.setByteStream(os);

    // Create a LS serializer
    // and tell it to make the output 'pretty'

    LSSerializer serializer = DOMiLS.createLSSerializer();
    serializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);

    // Serialize the xml to your output stream

    return serializer.write(doc, lso);
}

From source file:Main.java

/**
 * Writes the given document to a stream (pretty-printed)
 * /*from   www .j  a  v a 2s . c  o m*/
 * @param doc Document to serialize
 * @param out the Stream to write to
 * @param encoding The encoding to use
 */
public static void writeDocument(Document doc, OutputStream out, String encoding) {
    if (doc == null)
        return;
    LSSerializer writer = impl.createLSSerializer();
    writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
    LSOutput lsOutput = impl.createLSOutput();
    lsOutput.setByteStream(out);
    lsOutput.setEncoding(encoding);
    writer.write(doc, lsOutput);
}

From source file:Main.java

/**
 * Save an XML file./*www . j  ava 2s  .com*/
 */
public static void saveXML(Document document, File file) {
    try {
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();
        FileOutputStream fos = new FileOutputStream(file);
        LSOutput lso = impl.createLSOutput();
        lso.setByteStream(fos);
        writer.write(document, lso);
        /*
        OutputFormat of = new OutputFormat(document, "ISO-8859-1", true);
        serializer.setOutputFormat(of);
        serializer.setOutputCharStream(new FileWriter(file));
        serializer.serialize(document);
         */
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * //from  w  ww . j av a  2s  . c o  m
 * @param xmlNode
 * @return
 */
public static String serializeNode(Node xmlNode) {

    try {
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        LSOutput output = impl.createLSOutput();
        output.setByteStream(buffer);
        writer.write(xmlNode, output);

        return new String(buffer.toByteArray());
    } catch (Exception e) {
        /* Serialize node is for debuging only */
        return null;
    }
}

From source file:com.axway.ebxml.XmlUtil.java

/**
 * Write the specified <code>Document</code> to the specified <code>OutputStream</code>
 * @param document the <code>Document</code> to write out
 * @param out     the <code>OutputStream</code> to write to
 * @throws java.io.IOException//  w w w. j a  v a 2  s . com
 */
public static void writeTo(Document document, OutputStream out) throws KeyInfoWriterException {
    // Write the signed message to the provided OutputStream. If the provided
    // OutputStream is null then write the message to System.out
    if (document == null)
        throw new IllegalArgumentException("document cannot be null");

    if (out == null) {
        logger.debug("Writing document to System.out");
        out = System.out;
    }

    try {
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();
        LSOutput output = impl.createLSOutput();
        output.setByteStream(out);
        writer.write(document, output);
    } catch (ClassNotFoundException e) {
        throw new KeyInfoWriterException("Unexpected error serializing document to XML", e);
    } catch (InstantiationException e) {
        throw new KeyInfoWriterException("Unexpected error serializing document to XML", e);
    } catch (IllegalAccessException e) {
        throw new KeyInfoWriterException("Unexpected error serializing document to XML", e);
    }
}

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);

    LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
    if (formatted)
        setLsSeriliserToFormatted(lsSerializer);
    lsSerializer.write(node, lsOutput);/* ww w.  ja v a 2  s  .  c o  m*/
}

From source file:Main.java

public static void writeDocument(Document doc, Writer out) throws IOException {
    // happy to replace this code w/ the non-deprecated code, but I couldn't get the transformer 
    // approach to work. 
    //    OutputFormat format = new OutputFormat(doc);
    //    format.setIndenting(true);
    //    format.setIndent(2);
    //    XMLSerializer serializer = new XMLSerializer(out, format);
    //    serializer.serialize(doc);

    DOMImplementationLS impl = (DOMImplementationLS) doc.getImplementation();
    LSSerializer writer = impl.createLSSerializer();
    DOMConfiguration config = writer.getDomConfig();

    if (config.canSetParameter("format-pretty-print", Boolean.TRUE)) {
        config.setParameter("format-pretty-print", Boolean.TRUE);
    }/*from w  w  w  . j  a  v a2  s .  c o m*/

    // what a crappy way to force the stream to be UTF-8.  yuck!
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    LSOutput output = impl.createLSOutput();
    output.setEncoding("UTF-8");
    output.setByteStream(baos);

    writer.write(doc, output);

    out.write(baos.toString());
    out.flush();
}