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 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 void writeDocumentToDisk(Document document, File directoryToWriteTo, String fileName) {
    DOMImplementation domImplementation = document.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");
            try {
                lsOutput.setByteStream(new FileOutputStream(new File(directoryToWriteTo, fileName)));
            } catch (FileNotFoundException e) {
                throw new IllegalStateException(e);
            }/*from   www. j  a  va  2s  .  com*/
            lsSerializer.write(document, lsOutput);
        }
    }

}

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  .java 2s  . c o  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:edu.kit.dama.mdm.content.util.DublinCoreHelper.java

/**
 * Create the Dublin Core document.//from   w w w  .ja v a 2  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:XMLUtils.java

public static InputStream getInputStream(Document doc) throws Exception {
    DOMImplementationLS impl = null;
    DOMImplementation docImpl = doc.getImplementation();
    // Try to get the DOMImplementation from doc first before
    // defaulting to the sun implementation.
    if (docImpl != null && docImpl.hasFeature("LS", "3.0")) {
        impl = (DOMImplementationLS) docImpl.getFeature("LS", "3.0");
    } else {/*w  ww  .  ja v  a 2s . c o m*/
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        if (impl == null) {
            System.setProperty(DOMImplementationRegistry.PROPERTY,
                    "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");
            registry = DOMImplementationRegistry.newInstance();
            impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        }
    }
    LSOutput output = impl.createLSOutput();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    output.setByteStream(byteArrayOutputStream);
    LSSerializer writer = impl.createLSSerializer();
    writer.write(doc, output);
    byte[] buf = byteArrayOutputStream.toByteArray();
    return new ByteArrayInputStream(buf);
}

From source file:eu.europa.esig.dss.DSSXMLUtils.java

/**
 * Document Object Model (DOM) Level 3 Load and Save Specification See: http://www.w3.org/TR/2004/REC-DOM-Level-3-LS-20040407/
 *
 * @param xmlNode The node to be serialized.
 * @return/*from w  w  w.j  ava  2  s .  co  m*/
 */
public static byte[] serializeNode(final Node xmlNode) {

    try {

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

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

        final byte[] bytes = buffer.toByteArray();
        return bytes;
    } catch (Exception e) {
        throw new DSSException(e);
    }
}

From source file:de.mpg.mpdl.inge.xmltransforming.TestBase.java

/**
 * Serialize the given Dom Object to a String.
 * //from w  w  w  .j av a 2 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.
 */
protected static String toString(final Node xml, final boolean omitXMLDeclaration) throws Exception {
    if (xml == null) {
        throw new IllegalArgumentException(TestBase.class.getSimpleName() + ":toString:xml is null");
    }
    String result = null;

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    // serialize
    DOMImplementation implementation = DOMImplementationRegistry.newInstance().getDOMImplementation("XML 3.0");
    DOMImplementationLS feature = (DOMImplementationLS) implementation.getFeature("LS", "3.0");
    LSSerializer serial = feature.createLSSerializer();
    LSOutput output = feature.createLSOutput();
    output.setByteStream(outputStream);
    serial.write(xml, output);

    result = output.toString();

    return result;
}

From source file:com.redhat.plugin.eap6.AbstractEAP6Mojo.java

protected void writeXmlFile(final Document doc, final File workDirectory, final String fileName)
        throws MojoFailureException {
    final File destinationFile = new File(workDirectory, fileName);
    try {/*from   www.  jav a2s  .c om*/
        final FileOutputStream ostream = new FileOutputStream(destinationFile);
        final DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
        final LSSerializer lsSerializer = domImplementation.createLSSerializer();
        lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);

        final LSOutput lsOutput = domImplementation.createLSOutput();
        lsOutput.setByteStream(ostream);
        lsSerializer.write(doc, lsOutput);

        ostream.close();
        refreshEclipse(destinationFile);
    } catch (final Exception e) {
        throw new MojoFailureException("Cannot write output file", e);
    }
}

From source file:eu.europa.ec.markt.dss.signature.xades.XAdESProfileT.java

@Override
public Document extendSignatures(Document document, Document originalData, SignatureParameters parameters)
        throws IOException {
    InputStream input = document.openStream();

    if (this.tspSource == null) {
        throw new ConfigurationException(MSG.CONFIGURE_TSP_SERVER);
    }//from w ww. j a va  2  s  .  co m

    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        org.w3c.dom.Document doc = db.parse(input);

        NodeList signatureNodeList = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
        if (signatureNodeList.getLength() == 0) {
            throw new RuntimeException(
                    "Impossible to perform the extension of the signature, the document is not signed.");
        }
        for (int i = 0; i < signatureNodeList.getLength(); i++) {
            Element signatureEl = (Element) signatureNodeList.item(i);
            extendSignatureTag(signatureEl, originalData, parameters.getSignatureFormat());
        }

        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(doc, output);

        return new InMemoryDocument(buffer.toByteArray());

    } catch (ParserConfigurationException ex) {
        throw new RuntimeException(ex);
    } catch (SAXException e) {
        throw new IOException("Cannot parse document", e);
    } catch (ClassCastException e) {
        throw new IOException("Cannot save document", e);
    } catch (ClassNotFoundException e) {
        throw new IOException("Cannot save document", e);
    } catch (InstantiationException e) {
        throw new IOException("Cannot save document", e);
    } catch (IllegalAccessException e) {
        throw new IOException("Cannot save document", e);
    } finally {
        if (input != null) {
            input.close();
        }
    }

}

From source file:eu.europa.ec.markt.dss.signature.xades.XAdESProfileT.java

@Override
public Document extendSignature(Object signatureId, Document document, Document originalData,
        SignatureParameters parameters) throws IOException {
    InputStream input = document.openStream();

    if (this.tspSource == null) {
        throw new ConfigurationException(MSG.CONFIGURE_TSP_SERVER);
    }//from w w  w. ja va2  s  .  co  m

    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        org.w3c.dom.Document doc = db.parse(input);

        NodeList signatureNodeList = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
        if (signatureNodeList.getLength() == 0) {
            throw new RuntimeException(
                    "Impossible to perform the extension of the signature, the document is not signed.");
        }
        for (int i = 0; i < signatureNodeList.getLength(); i++) {
            Element signatureEl = (Element) signatureNodeList.item(i);
            String sid = signatureEl.getAttribute("Id");
            if (signatureId.equals(sid)) {
                extendSignatureTag(signatureEl, originalData, parameters.getSignatureFormat());
            }
        }

        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(doc, output);

        return new InMemoryDocument(buffer.toByteArray());

    } catch (ParserConfigurationException ex) {
        throw new RuntimeException(ex);
    } catch (SAXException e) {
        throw new IOException("Cannot parse document", e);
    } catch (ClassCastException e) {
        throw new IOException("Cannot save document", e);
    } catch (ClassNotFoundException e) {
        throw new IOException("Cannot save document", e);
    } catch (InstantiationException e) {
        throw new IOException("Cannot save document", e);
    } catch (IllegalAccessException e) {
        throw new IOException("Cannot save document", e);
    } finally {
        if (input != null) {
            input.close();
        }
    }

}