Search in sources :

Example 91 with Source

use of javax.xml.transform.Source in project poi by apache.

the class OOXMLPrettyPrint method pretty.

private static void pretty(Document document, OutputStream outputStream, int indent) throws TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    if (indent > 0) {
        // set properties to indent the resulting XML nicely
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(indent));
    }
    Result result = new StreamResult(outputStream);
    Source source = new DOMSource(document);
    transformer.transform(source, result);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 92 with Source

use of javax.xml.transform.Source in project poi by apache.

the class StreamHelper method saveXmlInStream.

/**
	 * Save the document object in the specified output stream.
	 *
	 * @param xmlContent
	 *            The XML document.
	 * @param outStream
	 *            The OutputStream in which the XML document will be written.
	 * @return <b>true</b> if the xml is successfully written in the stream,
	 *         else <b>false</b>.
	 */
public static boolean saveXmlInStream(Document xmlContent, OutputStream outStream) {
    try {
        Transformer trans = getIdentityTransformer();
        Source xmlSource = new DOMSource(xmlContent);
        // prevent close of stream by transformer:
        Result outputTarget = new StreamResult(new FilterOutputStream(outStream) {

            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                out.write(b, off, len);
            }

            @Override
            public void close() throws IOException {
                // only flush, don't close!
                out.flush();
            }
        });
        trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty(OutputKeys.STANDALONE, "yes");
        trans.transform(xmlSource, outputTarget);
    } catch (TransformerException e) {
        return false;
    }
    return true;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) IOException(java.io.IOException) FilterOutputStream(java.io.FilterOutputStream) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 93 with Source

use of javax.xml.transform.Source in project poi by apache.

the class PkiTestUtils method toString.

static String toString(Node dom) throws TransformerException {
    Source source = new DOMSource(dom);
    StringWriter stringWriter = new StringWriter();
    Result result = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    /*
         * We have to omit the ?xml declaration if we want to embed the
         * document.
         */
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(source, result);
    return stringWriter.getBuffer().toString();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 94 with Source

use of javax.xml.transform.Source in project sling by apache.

the class XmlUtil method prettyPrint.

public static String prettyPrint(InputSource is) {
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        // initialize StreamResult with File object to save to file
        StreamResult result = new StreamResult(new StringWriter());
        Source source = new SAXSource(is);
        transformer.transform(source, result);
        return result.getWriter().toString();
    } catch (Exception e) {
        // Catch generic error as panel should still work if xml apis are
        // not
        // resolved
        log.warn("Error occurred while transforming xml", e);
    } finally {
        Util.close(is);
    }
    return "Source not found";
}
Also used : Transformer(javax.xml.transform.Transformer) SAXSource(javax.xml.transform.sax.SAXSource) StreamResult(javax.xml.transform.stream.StreamResult) StringWriter(java.io.StringWriter) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) Source(javax.xml.transform.Source)

Example 95 with Source

use of javax.xml.transform.Source in project wildfly by wildfly.

the class StandardConfigsXMLValidationUnitTestCase method setUp.

@BeforeClass
public static void setUp() {
    final List<Source> sources = new LinkedList<Source>();
    for (File file : jbossSchemaFiles(true)) {
        sources.add(new StreamSource(file));
    }
    SCHEMAS = sources.toArray(new StreamSource[0]);
}
Also used : StreamSource(javax.xml.transform.stream.StreamSource) File(java.io.File) LinkedList(java.util.LinkedList) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) BeforeClass(org.junit.BeforeClass)

Aggregations

Source (javax.xml.transform.Source)238 StreamSource (javax.xml.transform.stream.StreamSource)161 DOMSource (javax.xml.transform.dom.DOMSource)108 Transformer (javax.xml.transform.Transformer)76 StreamResult (javax.xml.transform.stream.StreamResult)74 InputSource (org.xml.sax.InputSource)67 SAXSource (javax.xml.transform.sax.SAXSource)56 StringReader (java.io.StringReader)52 IOException (java.io.IOException)46 TransformerException (javax.xml.transform.TransformerException)45 Result (javax.xml.transform.Result)42 TransformerFactory (javax.xml.transform.TransformerFactory)41 Test (org.junit.Test)39 StringWriter (java.io.StringWriter)35 InputStream (java.io.InputStream)32 SAXException (org.xml.sax.SAXException)32 Schema (javax.xml.validation.Schema)29 Validator (javax.xml.validation.Validator)29 SchemaFactory (javax.xml.validation.SchemaFactory)28 ByteArrayInputStream (java.io.ByteArrayInputStream)26