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

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

Introduction

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

Prototype

public java.io.OutputStream getByteStream();

Source Link

Document

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

Usage

From source file:de.escidoc.core.test.EscidocTestBase.java

/**
 * Serialize the given Dom Object to a String.
 * /* w ww .  j  a  v a2 s  .c o m*/
 * @param xml
 *            The Xml Node to serialize.
 * @param omitXMLDeclaration
 *            Indicates if XML declaration will be omitted.
 * @return The String representation of the Xml Node.
 * @throws Exception
 *             If anything fails.
 */
public static String toString(final Node xml, final boolean omitXMLDeclaration) throws Exception {

    String result = new String();
    if (xml instanceof AttrImpl) {
        result = xml.getTextContent();
    } else if (xml instanceof Document) {
        StringWriter stringOut = new StringWriter();
        // format
        OutputFormat format = new OutputFormat((Document) xml);
        format.setIndenting(true);
        format.setPreserveSpace(true);
        format.setOmitXMLDeclaration(omitXMLDeclaration);
        format.setEncoding(DEFAULT_CHARSET);
        // serialize
        XMLSerializer serial = new XMLSerializer(stringOut, format);
        serial.asDOMSerializer();

        serial.serialize((Document) xml);
        result = stringOut.toString();
    } else {
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSOutput lsOutput = impl.createLSOutput();
        lsOutput.setEncoding(DEFAULT_CHARSET);

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        lsOutput.setByteStream(os);
        LSSerializer writer = impl.createLSSerializer();
        // result = writer.writeToString(xml);
        writer.write(xml, lsOutput);
        result = ((ByteArrayOutputStream) lsOutput.getByteStream()).toString(DEFAULT_CHARSET);
        if ((omitXMLDeclaration) && (result.indexOf("?>") != -1)) {
            result = result.substring(result.indexOf("?>") + 2);
        }
        // result = toString(getDocument(writer.writeToString(xml)),
        // true);
    }
    return result;
}

From source file:test.framework.TestBase.java

/**
 * Serialize the given Dom Object to a String.
 * /*from   w w  w. ja  v  a  2s.  c o  m*/
 * @param xml The Xml Node to serialize.
 * @param omitXMLDeclaration Indicates if XML declaration will be omitted.
 * @return The String representation of the Xml Node.
 * @throws Exception If anything fails.
 */
protected static String toString(final Node xml, final boolean omitXMLDeclaration) throws Exception {
    String result = new String();
    if (xml instanceof AttrImpl) {
        result = xml.getTextContent();
    } else if (xml instanceof Document) {
        StringWriter stringOut = new StringWriter();
        // format
        OutputFormat format = new OutputFormat((Document) xml);
        format.setIndenting(true);
        format.setPreserveSpace(false);
        format.setOmitXMLDeclaration(omitXMLDeclaration);
        format.setEncoding("UTF-8");
        // serialize
        XMLSerializer serial = new XMLSerializer(stringOut, format);
        serial.asDOMSerializer();
        serial.serialize((Document) xml);
        result = stringOut.toString();
    } else {
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSOutput lsOutput = impl.createLSOutput();
        lsOutput.setEncoding("UTF-8");
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        lsOutput.setByteStream(os);
        LSSerializer writer = impl.createLSSerializer();
        // result = writer.writeToString(xml);
        writer.write(xml, lsOutput);
        result = ((ByteArrayOutputStream) lsOutput.getByteStream()).toString();
        if ((omitXMLDeclaration) && (result.indexOf("?>") != -1)) {
            result = result.substring(result.indexOf("?>") + 2);
        }
        // result = toString(getDocument(writer.writeToString(xml)),
        // true);
    }
    return result;
}