xmlgraphics-fop-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeremias Maerki <...@jeremias-maerki.ch>
Subject Re: Combining FOP with multiple transformations
Date Wed, 07 Jul 2010 12:45:38 GMT
you're right, there's something odd there, but I get a RuntimeException
when I try to do something similar to what you have. I don't have time
right now to investigate what exactly goes wrong.

Here's a variant that works for me:

    public void testFilterChainingWithFOP1() throws Exception {
        File inputFile = new File("C:/Dev/FOP/temp/helloworld.fo");
        File outputFile = new File("D:/out1.pdf");

        InputStream in = new java.io.FileInputStream(inputFile);
        try {
            InputSource inputSource = new InputSource(in);

            SAXTransformerFactory tFactory = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
            SAXParserFactory pFactory = SAXParserFactory.newInstance();
            System.out.println("SAXParserFactory: " + pFactory.getClass().getName());
            System.out.println("SAXTransformerFactory: " + tFactory.getClass().getName());

            SAXParser parser = pFactory.newSAXParser();

            XMLReader parent = parser.getXMLReader();
            for (int i = 0; i < STYLESHEETS.length; i++) {
                String xsltFile = STYLESHEETS[i];

                XMLFilter filter = tFactory.newXMLFilter(new StreamSource(xsltFile));
                parent = filter;

            Source src = new SAXSource(parent, inputSource);

            FopFactory fopFactory = FopFactory.newInstance();
            FOUserAgent ua = fopFactory.newFOUserAgent();
            OutputStream out = new java.io.FileOutputStream(outputFile);
            Fop fop = fopFactory.newFop("application/pdf", ua, out);

            Result res = new SAXResult(fop.getDefaultHandler());

            Transformer transformer = tFactory.newTransformer(); //You can add a stylesheet
here, too
            transformer.transform(src, res);
        } finally {

On 06.07.2010 15:59:38 Cedric Staub wrote:
> Hello everyone
> I ran into some problems with FOP today, and the documentation wasn't
> able to help me solve them. I am trying to apply multiple transformers
> to an XML file before handing it over to FOP in order to generate a
> PDF, but I keep running into exceptions. I am using version 0.95.
> My code looks like this:
> -------
>  xmlReader.setContentHandler(i18nTrans);
>  i18nTrans.setResult(new SAXResult(xsltTrans));
>  xsltTrans.setResult(new SAXResult(fop.getDefaultHandler()));
>  xmlReader.parse(new InputSource(docSource));
> -------
> Where i18nTrans, xsltTrans are of the type TransformerHandler (SAX),
> xmlReader is of the type XMLReader (SAX) and fop is of the type Fop.
> Here's how I created the fop object:
> -------
>  ByteArrayOutputStream baos = new ByteArrayOutputStream();
>  FopFactory fopFactory = FopFactory.newInstance();
>  fopFactory.setURIResolver(srcResolver);
>  Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, baos);
> -------
> Now, my problem is the following line from above:
> -------
>  xsltTrans.setResult(new SAXResult(fop.getDefaultHandler()));
> -------
> Everytime I call setResult with Fop's default handler as an argument on
> any of my TransformerHandlers, I get either a NullPointerException or
> a ClassCastException, depending on which transformer I call setResult
> on.
> I seems this is happening because Fop's default handler returns null
> when the function getLexicalHandler is called. This seems to be the
> reason for the NullPointerException. 
> Now, the SAX API documentation states that: "If the lexical handler is
> not set, an attempt should be made by the transformer to cast the
> ContentHandler to a LexicalHandler". But casting the return value of
> getHandler() to a LexicalHandler fails (FOTreeBuilder cannot be cast to
> LexicalHandler).
> Is this behaviour correct? If so, how can I apply both transformers
> (i18nTrans and xsltTrans) to the XML file before handing it over to Fop?
> The documentation says to use the transform(src, res) function from the
> Transformer class, but since I have more then one transformer this
> approach doesn't seem sufficient (unless I misunderstood it).
> I would appreciate it if someone could help me out here, as I'm kind of
> lost on how to do this ;-). Thank you in advance.
> Greetings,
> Cedric

Jeremias Maerki

To unsubscribe, e-mail: fop-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-users-help@xmlgraphics.apache.org

View raw message