Search in sources :

Example 26 with Result

use of javax.xml.transform.Result in project jackrabbit by apache.

the class AbstractImportXmlTest method serialize.

public void serialize(Document document) throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
    try {
        // disable pretty printing/default line wrapping!
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        t.setOutputProperty(OutputKeys.INDENT, "no");
        Source s = new DOMSource(document);
        Result r = new StreamResult(bos);
        t.transform(s, r);
    } catch (TransformerException te) {
        throw (IOException) new IOException(te.getMessage()).initCause(te);
    } finally {
        bos.close();
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 27 with Result

use of javax.xml.transform.Result in project karaf by apache.

the class SpringTransformer method analyze.

public static Set<String> analyze(Source source) throws Exception {
    Set<String> refers = new TreeSet<>();
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Result r = new StreamResult(bout);
    XmlUtils.transform(new StreamSource(SpringTransformer.class.getResourceAsStream("extract.xsl")), source, r);
    ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
    bout.close();
    BufferedReader br = new BufferedReader(new InputStreamReader(bin));
    String line = br.readLine();
    while (line != null) {
        line = line.trim();
        if (line.length() > 0) {
            String[] parts = line.split("\\s*,\\s*");
            for (String part : parts) {
                int n = part.lastIndexOf('.');
                if (n > 0) {
                    String pkg = part.substring(0, n);
                    if (!pkg.startsWith("java.")) {
                        refers.add(part.substring(0, n));
                    }
                }
            }
        }
        line = br.readLine();
    }
    br.close();
    return refers;
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) TreeSet(java.util.TreeSet) StreamSource(javax.xml.transform.stream.StreamSource) BufferedReader(java.io.BufferedReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 28 with Result

use of javax.xml.transform.Result in project karaf by apache.

the class BlueprintTransformer method analyze.

public static Set<String> analyze(Source source) throws Exception {
    Set<String> refers = new TreeSet<String>();
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Result r = new StreamResult(bout);
    XmlUtils.transform(new StreamSource(BlueprintTransformer.class.getResourceAsStream("extract.xsl")), source, r);
    ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
    bout.close();
    BufferedReader br = new BufferedReader(new InputStreamReader(bin));
    String line = br.readLine();
    while (line != null) {
        line = line.trim();
        if (line.length() > 0) {
            String[] parts = line.split("\\s*,\\s*");
            for (String part : parts) {
                int n = part.lastIndexOf('.');
                if (n > 0) {
                    String pkg = part.substring(0, n);
                    if (!pkg.startsWith("java.")) {
                        refers.add(pkg);
                    }
                }
            }
        }
        line = br.readLine();
    }
    br.close();
    return refers;
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) TreeSet(java.util.TreeSet) StreamSource(javax.xml.transform.stream.StreamSource) BufferedReader(java.io.BufferedReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 29 with Result

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

the class RecordGenerator method transform.

/**
     * <p>Executes an XSL transformation. This process transforms an XML input
     * file into a text output file controlled by an XSLT specification.</p>
     * 
     * @param in the XML input file
     * @param out the text output file
     * @param xslt the XSLT specification, i.e. an XSL style sheet
     * @throws FileNotFoundException 
     * @throws TransformerException 
     */
private static void transform(final File in, final File out, final File xslt) throws FileNotFoundException, TransformerException {
    final StreamSource ss = new StreamSource(xslt);
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Transformer t;
    try {
        t = tf.newTransformer(ss);
    } catch (TransformerException ex) {
        System.err.println("Error compiling XSL style sheet " + xslt);
        throw ex;
    }
    final Properties p = new Properties();
    p.setProperty(OutputKeys.METHOD, "text");
    t.setOutputProperties(p);
    final Result result = new StreamResult(out);
    t.transform(new StreamSource(in), result);
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) Properties(java.util.Properties) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 30 with Result

use of javax.xml.transform.Result 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)

Aggregations

Result (javax.xml.transform.Result)68 StreamResult (javax.xml.transform.stream.StreamResult)60 Source (javax.xml.transform.Source)42 Transformer (javax.xml.transform.Transformer)42 DOMSource (javax.xml.transform.dom.DOMSource)33 TransformerFactory (javax.xml.transform.TransformerFactory)25 StringWriter (java.io.StringWriter)21 StreamSource (javax.xml.transform.stream.StreamSource)19 IOException (java.io.IOException)18 TransformerException (javax.xml.transform.TransformerException)17 SAXResult (javax.xml.transform.sax.SAXResult)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 File (java.io.File)11 Document (org.w3c.dom.Document)11 InputSource (org.xml.sax.InputSource)10 DocumentBuilder (javax.xml.parsers.DocumentBuilder)9 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)9 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)9 DOMResult (javax.xml.transform.dom.DOMResult)9 SAXException (org.xml.sax.SAXException)9