Search in sources :

Example 36 with SAXResult

use of javax.xml.transform.sax.SAXResult in project webservices-axiom by apache.

the class TestGetSAXResult method runTest.

@Override
protected void runTest() throws Throwable {
    TransformerFactory transformerFactory = xsltImplementation.newTransformerFactory();
    StreamSource source = new StreamSource(file.getUrl().toString());
    OMDocument document = metaFactory.getOMFactory().createOMDocument();
    SAXResult result = document.getSAXResult();
    transformerFactory.newTransformer().transform(source, result);
    assertAbout(xml()).that(xml(OMDocument.class, document)).ignoringWhitespaceInPrologAndEpilog().ignoringRedundantNamespaceDeclarations().expandingEntityReferences().hasSameContentAs(file.getUrl());
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) SAXResult(javax.xml.transform.sax.SAXResult) StreamSource(javax.xml.transform.stream.StreamSource) OMDocument(org.apache.axiom.om.OMDocument)

Example 37 with SAXResult

use of javax.xml.transform.sax.SAXResult in project webservices-axiom by apache.

the class XSLTImplementation method supportsLexicalHandlerWithStreamSource.

/**
     * Determine if an identity transformation from a {@link StreamSource} to a {@link SAXResult}
     * will generate events defined by {@link LexicalHandler}.
     * 
     * @return <code>true</code> if the XSLT implementation will invoke the methods on the
     *         {@link LexicalHandler} set on the {@link SAXResult}, <code>false</code> otherwise
     */
public final synchronized boolean supportsLexicalHandlerWithStreamSource() {
    if (supportsLexicalHandlerWithStreamSource == null) {
        StreamSource source = new StreamSource(new StringReader("<!DOCTYPE root><root><![CDATA[test]]></root>"));
        TestContentHandler handler = new TestContentHandler();
        SAXResult result = new SAXResult(handler);
        result.setLexicalHandler(handler);
        try {
            newTransformerFactory().newTransformer().transform(source, result);
        } catch (TransformerException ex) {
            throw new RuntimeException(ex);
        }
        supportsLexicalHandlerWithStreamSource = handler.getLexicalEventsReceived() == 4;
    }
    return supportsLexicalHandlerWithStreamSource;
}
Also used : SAXResult(javax.xml.transform.sax.SAXResult) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) TransformerException(javax.xml.transform.TransformerException)

Example 38 with SAXResult

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

Aggregations

SAXResult (javax.xml.transform.sax.SAXResult)38 Transformer (javax.xml.transform.Transformer)18 IOException (java.io.IOException)15 TransformerException (javax.xml.transform.TransformerException)15 StreamResult (javax.xml.transform.stream.StreamResult)13 TransformerFactory (javax.xml.transform.TransformerFactory)12 StreamSource (javax.xml.transform.stream.StreamSource)11 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)10 Source (javax.xml.transform.Source)10 DOMResult (javax.xml.transform.dom.DOMResult)10 Result (javax.xml.transform.Result)9 Document (org.w3c.dom.Document)9 DocumentBuilder (javax.xml.parsers.DocumentBuilder)8 Fop (org.apache.fop.apps.Fop)8 ContentHandler (org.xml.sax.ContentHandler)8 FileOutputStream (java.io.FileOutputStream)7 Node (org.w3c.dom.Node)7 SAXException (org.xml.sax.SAXException)7 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)6 JAXBAssertionError (com.sun.xml.bind.JAXBAssertionError)5