xmlgraphics-fop-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Eric Douglas" <edoug...@blockhouse.com>
Subject RE: Combining FOP with multiple transformations
Date Tue, 06 Jul 2010 15:15:00 GMT
This sounds similar to what I'm doing.  Though I only use the one
transformer, I would guess it could be applied to another transformer
since the output is in the ByteArrayOutputStream.  Why would you need to
do a setResult?  I just create and transform.  The ByteArrayOutputStream
is populated after executing the transform method.

File xmlFile = new File("xmlfilenamewithpath");
FileInputStream fis = new FileInputStream(xmlFile);
byte[] fisBytes = new byte[fis.available()];
fis.read(fisBytes);
ByteArrayInputStream bais = new ByteArrayInputStream(fisBytes);
InputSource inputSource = new InputSource(bais);
SAXSource xmlSS = new SAXSource(inputSource);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

Fop fop = fopFactory.newFop(MimeConstants.MIME_FOP_PRINT, userAgent,
baos);
SAXResult transformResult = new SAXResult(fop.getDefaultHandler());
transformer.transform(xmlSS,transformResult);

The SAXResult just links the transformer to the Fop.  The transform
method populates the baos.  I can then use that output as input for the
next step.

ByteArrayInputStream bais = new
ByteArrayInputStream(baos.toByteArray());
PDDocument pg = PDDocument.load(bais);
printerJob.setPageable(pg);


-----Original Message-----
From: Cedric Staub [mailto:cedric.staub@wyona.com] 
Sent: Tuesday, July 06, 2010 10:00 AM
To: fop-users@xmlgraphics.apache.org
Cc: Michael Wechner
Subject: Combining FOP with multiple transformations

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

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


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


Mime
View raw message