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

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

Introduction

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

Prototype

public LSOutput createLSOutput();

Source Link

Document

Create a new empty output destination object where LSOutput.characterStream, LSOutput.byteStream, LSOutput.systemId, LSOutput.encoding are null.

Usage

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/*from  w w  w. j  a  va  2 s  .  c o  m*/
 */
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

/**
 * Save an XML file.//from   w w  w.j ava 2  s.c  om
 */
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

/**
 * //  w  w  w.  j av  a 2s.  c om
 * @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:edu.kit.dama.mdm.content.util.DublinCoreHelper.java

/**
 * Create the Dublin Core document./*from w  w  w.  j a  v a2  s.co m*/
 *
 * @param theObject The object to create the DC information for.
 * @param pCreator A custom creator stored as author/publisher in Dublin
 * Core. If not provided, the object's uploader is used if available.
 * @param out The output stream to which the DC document is written.
 *
 * @throws ParserConfigurationException If creating the Dublin Core document
 * failed.
 */
public static void writeDublinCoreDocument(DigitalObject theObject, UserData pCreator, OutputStream out)
        throws ParserConfigurationException {
    Document doc = createDublinCoreDocument(theObject, pCreator);
    DOMImplementation impl = doc.getImplementation();
    DOMImplementationLS implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0");

    LSOutput lso = implLS.createLSOutput();
    lso.setByteStream(out);
    LSSerializer writer = implLS.createLSSerializer();
    writer.write(doc, lso);
}

From source file:Main.java

/**
 * Writes a Node out to a Writer using the DOM, level 3, Load/Save serializer. The written content is encoded using
 * the encoding specified in the writer configuration.
 * // w w  w.  jav a2 s  .  c o  m
 * @param node the node to write out
 * @param output the writer to write the XML to
 * @param serializerParams parameters to pass to the {@link DOMConfiguration} of the serializer
 *         instance, obtained via {@link LSSerializer#getDomConfig()}. May be null.
 */
public static void writeNode(Node node, Writer output, Map<String, Object> serializerParams) {
    DOMImplementationLS domImplLS = getLSDOMImpl(node);

    LSSerializer serializer = getLSSerializer(domImplLS, serializerParams);

    LSOutput serializerOut = domImplLS.createLSOutput();
    serializerOut.setCharacterStream(output);

    serializer.write(node, serializerOut);
}

From source file:Main.java

/**
 * Writes a Node out to an OutputStream using the DOM, level 3, Load/Save serializer. The written content 
 * is encoded using the encoding specified in the output stream configuration.
 * //from w w w . ja v  a 2  s.  co  m
 * @param node the node to write out
 * @param output the output stream to write the XML to
 * @param serializerParams parameters to pass to the {@link DOMConfiguration} of the serializer
 *         instance, obtained via {@link LSSerializer#getDomConfig()}. May be null.
 */
public static void writeNode(Node node, OutputStream output, Map<String, Object> serializerParams) {
    DOMImplementationLS domImplLS = getLSDOMImpl(node);

    LSSerializer serializer = getLSSerializer(domImplLS, serializerParams);

    LSOutput serializerOut = domImplLS.createLSOutput();
    serializerOut.setByteStream(output);

    serializer.write(node, serializerOut);
}

From source file:Main.java

/**
 * Converts a dom element to a String/* ww w  .  j  av a2 s  . co 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 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:Main.java

public static String serialize(Document document, boolean prettyPrint) {
    DOMImplementationLS impl = getDOMImpl();
    LSSerializer serializer = impl.createLSSerializer();
    // document.normalizeDocument();
    DOMConfiguration config = serializer.getDomConfig();
    if (prettyPrint && config.canSetParameter("format-pretty-print", Boolean.TRUE)) {
        config.setParameter("format-pretty-print", true);
    }/*from   w  w w .  j  a  va2 s.  c om*/
    config.setParameter("xml-declaration", true);
    LSOutput output = impl.createLSOutput();
    output.setEncoding("UTF-8");
    Writer writer = new StringWriter();
    output.setCharacterStream(writer);
    serializer.write(document, output);
    return writer.toString();
}

From source file:Main.java

public static String lsSerializePretty(Document doc) {
    DOMImplementation domImplementation = doc.getImplementation();
    if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) {
        DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation.getFeature("LS",
                "3.0");
        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(doc, lsOutput);
            return stringWriter.toString();
        } else {/*from   w  w  w  .  j ava  2 s  . com*/
            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.");
    }
}