Search in sources :

Example 71 with Result

use of javax.xml.transform.Result in project bnd by bndtools.

the class Transform method transform.

public static void transform(TransformerFactory transformerFactory, URL xslt, InputStream in, OutputStream out) throws Exception {
    if (xslt == null)
        throw new IllegalArgumentException("No source template specified");
    Templates templates = cache.get(xslt.toURI());
    if (templates == null) {
        try (InputStream xsltIn = xslt.openStream()) {
            templates = transformerFactory.newTemplates(new StreamSource(xsltIn));
            cache.put(xslt.toURI(), templates);
        }
    }
    Result xmlResult = new StreamResult(out);
    Source xmlSource = new StreamSource(in);
    Transformer t = templates.newTransformer();
    t.transform(xmlSource, xmlResult);
    out.flush();
}
Also used : Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Templates(javax.xml.transform.Templates) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 72 with Result

use of javax.xml.transform.Result in project dhis2-core by dhis2.

the class DefaultHelpManager method getHelpItems.

@Override
public void getHelpItems(OutputStream out, Locale locale) {
    try {
        ClassPathResource classPathResource = resolveHelpFileResource(locale);
        Source source = new StreamSource(classPathResource.getInputStream(), ENCODING_UTF8);
        Result result = new StreamResult(out);
        getTransformer("helpitems_stylesheet.xsl").transform(source, result);
    } catch (Exception ex) {
        throw new RuntimeException("Failed to get help content", ex);
    }
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) ClassPathResource(org.springframework.core.io.ClassPathResource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 73 with Result

use of javax.xml.transform.Result in project dhis2-core by dhis2.

the class DefaultHelpManager method getHelpContent.

// -------------------------------------------------------------------------
// HelpManager implementation
// -------------------------------------------------------------------------
@Override
public void getHelpContent(OutputStream out, String id, Locale locale) {
    try {
        ClassPathResource classPathResource = resolveHelpFileResource(locale);
        Source source = new StreamSource(classPathResource.getInputStream(), ENCODING_UTF8);
        Result result = new StreamResult(out);
        Transformer transformer = getTransformer("help_stylesheet.xsl");
        transformer.setParameter("sectionId", id);
        transformer.transform(source, result);
    } catch (Exception ex) {
        throw new RuntimeException("Failed to get help content", ex);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) ClassPathResource(org.springframework.core.io.ClassPathResource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 74 with Result

use of javax.xml.transform.Result in project opentheso by miledrousset.

the class ApacheFOP method test_basic.

public void test_basic() throws SAXException, IOException, TransformerConfigurationException, TransformerException, ConfigurationException, URISyntaxException {
    // Step 1: Construct a FopFactory by specifying a reference to the configuration file
    // (reuse if you plan to render multiple documents!)
    DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
    Configuration cfg = cfgBuilder.buildFromFile(new File("fop-config.xml"));
    URI baseURI = new URI("https://www.testUri.com");
    FopFactoryBuilder builder;
    builder = new FopFactoryBuilder(baseURI).setConfiguration(cfg);
    FopFactory fopFactory = builder.build();
    // Step 2: Set up output stream.
    // Note: Using BufferedOutputStream for performance reasons (helpful with FileOutputStreams).
    OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("test-fop.pdf")));
    try {
        // Step 3: Construct fop with desired output format
        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
        // Step 4: Setup JAXP using identity transformer
        Source xslt = new StreamSource(new File("skos-alpha.xsl"));
        TransformerFactory factory = TransformerFactory.newInstance();
        // identity transformer
        Transformer transformer = factory.newTransformer(xslt);
        // Step 5: Setup input and output for XSLT transformation
        // Setup input stream
        Source src = new StreamSource(new File("test_unesco.rdf"));
        // Resulting SAX events (the generated FO) must be piped through to FOP
        Result res = new SAXResult(fop.getDefaultHandler());
        // Step 6: Start XSLT transformation and FOP processing
        transformer.transform(src, res);
    } finally {
        // Clean-up
        out.close();
    }
}
Also used : DefaultConfigurationBuilder(org.apache.avalon.framework.configuration.DefaultConfigurationBuilder) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) Configuration(org.apache.avalon.framework.configuration.Configuration) Fop(org.apache.fop.apps.Fop) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) StreamSource(javax.xml.transform.stream.StreamSource) FopFactory(org.apache.fop.apps.FopFactory) URI(java.net.URI) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) SAXResult(javax.xml.transform.sax.SAXResult) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 75 with Result

use of javax.xml.transform.Result in project winery by eclipse.

the class Util method getXMLAsString.

public static String getXMLAsString(Element el) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t;
    try {
        t = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new IllegalStateException("Could not instantiate Transformer", e);
    }
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    Source source = new DOMSource(el);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    Result target = new StreamResult(os);
    try {
        t.transform(source, target);
    } catch (TransformerException e) {
        Util.LOGGER.debug(e.getMessage(), e);
        throw new IllegalStateException("Could not transform dom node to string", e);
    }
    return os.toString();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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)

Aggregations

Result (javax.xml.transform.Result)80 StreamResult (javax.xml.transform.stream.StreamResult)72 Source (javax.xml.transform.Source)52 Transformer (javax.xml.transform.Transformer)52 DOMSource (javax.xml.transform.dom.DOMSource)42 TransformerFactory (javax.xml.transform.TransformerFactory)33 StringWriter (java.io.StringWriter)24 TransformerException (javax.xml.transform.TransformerException)21 IOException (java.io.IOException)20 StreamSource (javax.xml.transform.stream.StreamSource)20 SAXResult (javax.xml.transform.sax.SAXResult)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 File (java.io.File)15 Document (org.w3c.dom.Document)14 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)11 DOMResult (javax.xml.transform.dom.DOMResult)11 InputSource (org.xml.sax.InputSource)11 SAXException (org.xml.sax.SAXException)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 DocumentBuilder (javax.xml.parsers.DocumentBuilder)9