RE: Using Xalan Transformer with FOP Driver problem
Thanks Ryan, it works now !!! In fact when you said that this problem was not there in FOP 0.20.3 I tried that first :-) and it worked. Once I knew it did I went ahead to experiment with FOP 0.20.1 And the fix you suggested worked. So the code, just for reference (to others who may need it at a later time) is import java.io.*; import javax.xml.transform.*; import javax.xml.transform.stream.*; import javax.xml.transform.sax.*; import org.apache.fop.apps.Driver; import org.apache.fop.apps.StreamRenderer; import org.apache.fop.fo.FOTreeBuilder; public class TestXml { public static void main(String[] args) { try { // XML Will eventually be a Java String or Stream from another source. StreamSource xmlsource = new StreamSource(new FileReader("simple.xml")); Driver driver =new Driver(); FileOutputStream fos = new FileOutputStream("simple.pdf"); driver.setOutputStream(fos); driver.setRenderer(Driver.RENDER_PDF); // Fix : ADDED this code as suggested by Ryan to work with FOP 0.20.1 (yeah, not required by FOP 0.20.3) StreamRenderer x = new StreamRenderer(fos, driver.getRenderer()); ((FOTreeBuilder) driver.getContentHandler()).setStreamRenderer(x); // Fix ends Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(new FileReader("simple.xsl"))); // Previously threw error without above 2 line Fix transformer.transform(xmlsource, new SAXResult(driver.getContentHandler())); //For test purposes only //transformer.transform(xmlsource, new StreamResult(System.out)); } catch (Exception e) { e.printStackTrace(); } } } Ryan, Mehmood I really appreciate your help guys. It did help. Anil. -Original Message- From: [EMAIL PROTECTED] [SMTP:[EMAIL PROTECTED] Sent: Friday, June 21, 2002 2:56 PM To: [EMAIL PROTECTED] Subject: Re: Using Xalan Transformer with FOP Driver problem I use 0.20.1 also with SAX events. I had to add this to get rid of the null pointer: //Hack fix for FOP bug. Might be unnecessary in FOP 0.20.3. StreamRenderer x = new StreamRenderer(outputStream, pdfDriver.getRenderer()); ((FOTreeBuilder)pdfDriver.getContentHandler ()).setStreamRenderer(x); Hope this helps, -Ryan
RE: Using Xalan Transformer with FOP Driver problem
thanks for the prompt help Mehmood, but the same problem still persists. I did make slight changes to the code you sent me though (kept the ByteArrayOutputStream as is but changed the StringReader to a FileReader just for simplicity) before running it. Do you get the error when running the Java code with the XML and XSL I have pasted in the mail ? Just to make sure my classpath is not messed up with the wrong jars :-) Anil. PS: I just noticed after sending the prev mail that the driver.setRenderer(driver.RENDER_TEXT) should be driver.setRenderer(driver.RENDER_PDF) But the problem remains even with that change. -Original Message- From: Shaikh, Mehmood [SMTP:[EMAIL PROTECTED] Sent: Friday, June 21, 2002 2:37 PM To: '[EMAIL PROTECTED]' Subject: RE: Using Xalan Transformer with FOP Driver problem Try this. // --- // 1. Get a Source for XML document // --- // a) from string Source strSource = new StreamSource(new StringReader(xmlData)); // --- // 2. Get stylesheet transformer // --- // from file, see above examples for other types of XSL input //TransformerFactory TransformerFactory transformerFactory = TransformerFactory.newInstance (); Templates template = transformerFactory.newTemplates( new StreamSource( xslLayout )); // note - template object is multi-threaded and should be cached if you // plan to use the same XSL repeatedly Transformer transformer = template.newTransformer(); // --- // 3. Create FOP driver and set rendering mode and output stream // --- Driver driver = new Driver(); driver.setRenderer(driver.RENDER_PDF); ByteArrayOutputStream baos = new ByteArrayOutputStream (); driver.setOutputStream( baos); // initialize logger - see sample code on FOP website driver.setLogger(setLogger()); // --- // 4. Create SAXResult based on FOP Driver content handler // which will accept SAX events and build FOP tree // --- Result saxResult = new SAXResult( driver.getContentHandler() ); // 5. Use the Transformer to transform an XML Source and // send the output to a Result object. Implicitely it will // create the FOP tree by firing SAX events. transformer.transform( strSource, saxResult ); // 6. Your user is already viewing the PDF! response.getOutputStream().flush(); response.getOutputStream().close();
Re: Using Xalan Transformer with FOP Driver problem
I use 0.20.1 also with SAX events. I had to add this to get rid of the null pointer: //Hack fix for FOP bug. Might be unnecessary in FOP 0.20.3. StreamRenderer x = new StreamRenderer(outputStream, pdfDriver.getRenderer()); ((FOTreeBuilder)pdfDriver.getContentHandler ()).setStreamRenderer(x); Hope this helps, -Ryan
RE: Using Xalan Transformer with FOP Driver problem
Try this. // --- // 1. Get a Source for XML document // --- // a) from string Source strSource = new StreamSource(new StringReader(xmlData)); // --- // 2. Get stylesheet transformer // --- // from file, see above examples for other types of XSL input //TransformerFactory TransformerFactory transformerFactory = TransformerFactory.newInstance (); Templates template = transformerFactory.newTemplates( new StreamSource( xslLayout )); // note - template object is multi-threaded and should be cached if you // plan to use the same XSL repeatedly Transformer transformer = template.newTransformer(); // --- // 3. Create FOP driver and set rendering mode and output stream // --- Driver driver = new Driver(); driver.setRenderer(driver.RENDER_PDF); ByteArrayOutputStream baos = new ByteArrayOutputStream (); driver.setOutputStream( baos); // initialize logger - see sample code on FOP website driver.setLogger(setLogger()); // --- // 4. Create SAXResult based on FOP Driver content handler // which will accept SAX events and build FOP tree // --- Result saxResult = new SAXResult( driver.getContentHandler() ); // 5. Use the Transformer to transform an XML Source and // send the output to a Result object. Implicitely it will // create the FOP tree by firing SAX events. transformer.transform( strSource, saxResult ); // 6. Your user is already viewing the PDF! response.getOutputStream().flush(); response.getOutputStream().close(); -Original Message- From: Anil R. Pinto [mailto:[EMAIL PROTECTED] Sent: June 21, 2002 1:35 PM To: '[EMAIL PROTECTED]' Subject: Using Xalan Transformer with FOP Driver problem Hi, I have been using FOP to create PDF files for sometime now. Mostly as embedded code within my Struts Action classes, as mentioned in http://xml.apache.org/fop/embedding.html and it has been working just fine. But this method requires the xml source to exist only as a java.io.File (if I am not mistaken), with no provision as an in memory object - like a Java String or Stream for example. So when I looked at the Xalan site and this mailing list I saw the in memory implementation possible with the TransformerFactory and Transformer classes in Xalan. So, I tried this with a simple xml and xsl to test the concept first. But, I seem to be getting the error building formatting object tree building formatting object tree javax.xml.transform.TransformerException at org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerIm pl.java:1212) at org.apache.xalan.transformer.TransformerImpl.run(TransformerImpl.java:2894) at java.lang.Thread.run(Thread.java:484) - java.lang.NullPointerException at org.apache.fop.fo.FOTreeBuilder.startDocument(FOTreeBuilder.java:167) at org.apache.xalan.transformer.QueuedStartDocument.flush(QueuedStartDocume nt.java:108) at org.apache.xalan.transformer.ResultTreeHandler.flushPending(ResultTreeHa ndler.java:758) at org.apache.xalan.transformer.ResultTreeHandler.startElement(ResultTreeHa ndler.java:245) at org.apache.xalan.transformer.ResultTreeHandler.startElement(ResultTreeHa ndler.java:209) at org.apache.xalan.templates.ElemLiteralResult.execute(ElemLiteralResult.j ava:704) at org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(Trans formerImpl.java:2154) at org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(Trans formerImpl.java:2097) at org.apache.xalan.transformer.TransformerImpl.applyTemplateToNode(Transfo rmerImpl.java:2029) at org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerIm pl.java:1189) at org.apache.xalan.transformer.TransformerImpl.run(TransformerImpl.java:2894) at java.lang.Thread.run(Thread.java:484) javax.xml.transform.TransformerException at org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerIm pl.java:1212) at org.apache.xalan.transformer.TransformerImpl.run(TransformerImpl.java:2894) at java.lang.Thread.run(Thread.java:484) - java.lang.NullPointerException at org.apache.fop.fo.FOTreeBuilder.startDocument(FOTreeBuilder.java:167) at org.apache.xalan.transformer.QueuedStartDocument.flush(QueuedStartDocume nt.java:108) at org.apache.xalan.transformer.ResultTreeHandler.flushPending(ResultTreeHa ndler.java:758) at org.apache.xalan.transformer.ResultTreeHandler.startElement(ResultTreeHa ndler.java:245) at org.apache.xalan.transformer.ResultTreeHandler.startElement(R esultTreeHandler.java:209) at org.apache.xalan.templates.ElemLiteralResult.execute(ElemLiteralResult.j ava:704) at org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(Trans formerImpl.java:2154) at org.apache.xalan.transformer.TransformerImpl.executeChildTempl
Using Xalan Transformer with FOP Driver problem
Hi, I have been using FOP to create PDF files for sometime now. Mostly as embedded code within my Struts Action classes, as mentioned in http://xml.apache.org/fop/embedding.html and it has been working just fine. But this method requires the xml source to exist only as a java.io.File (if I am not mistaken), with no provision as an in memory object - like a Java String or Stream for example. So when I looked at the Xalan site and this mailing list I saw the in memory implementation possible with the TransformerFactory and Transformer classes in Xalan. So, I tried this with a simple xml and xsl to test the concept first. But, I seem to be getting the error building formatting object tree building formatting object tree javax.xml.transform.TransformerException at org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerIm pl.java:1212) at org.apache.xalan.transformer.TransformerImpl.run(TransformerImpl.java:2894) at java.lang.Thread.run(Thread.java:484) - java.lang.NullPointerException at org.apache.fop.fo.FOTreeBuilder.startDocument(FOTreeBuilder.java:167) at org.apache.xalan.transformer.QueuedStartDocument.flush(QueuedStartDocume nt.java:108) at org.apache.xalan.transformer.ResultTreeHandler.flushPending(ResultTreeHa ndler.java:758) at org.apache.xalan.transformer.ResultTreeHandler.startElement(ResultTreeHa ndler.java:245) at org.apache.xalan.transformer.ResultTreeHandler.startElement(ResultTreeHa ndler.java:209) at org.apache.xalan.templates.ElemLiteralResult.execute(ElemLiteralResult.j ava:704) at org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(Trans formerImpl.java:2154) at org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(Trans formerImpl.java:2097) at org.apache.xalan.transformer.TransformerImpl.applyTemplateToNode(Transfo rmerImpl.java:2029) at org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerIm pl.java:1189) at org.apache.xalan.transformer.TransformerImpl.run(TransformerImpl.java:2894) at java.lang.Thread.run(Thread.java:484) javax.xml.transform.TransformerException at org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerIm pl.java:1212) at org.apache.xalan.transformer.TransformerImpl.run(TransformerImpl.java:2894) at java.lang.Thread.run(Thread.java:484) - java.lang.NullPointerException at org.apache.fop.fo.FOTreeBuilder.startDocument(FOTreeBuilder.java:167) at org.apache.xalan.transformer.QueuedStartDocument.flush(QueuedStartDocume nt.java:108) at org.apache.xalan.transformer.ResultTreeHandler.flushPending(ResultTreeHa ndler.java:758) at org.apache.xalan.transformer.ResultTreeHandler.startElement(ResultTreeHa ndler.java:245) at org.apache.xalan.transformer.ResultTreeHandler.startElement(R esultTreeHandler.java:209) at org.apache.xalan.templates.ElemLiteralResult.execute(ElemLiteralResult.j ava:704) at org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(Trans formerImpl.java:2154) at org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(Trans formerImpl.java:2097) at org.apache.xalan.transformer.TransformerImpl.applyTemplateToNode(Transfo rmerImpl.java:2029) at org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerIm pl.java:1189) at org.apache.xalan.transformer.TransformerImpl.run(TransformerImpl.java:2894) at java.lang.Thread.run(Thread.java:484) I am using Fop-0.20.1 and the jars when compiling and running as below (on a win2000 platform) C:\Fop-0.20.1>javac -classpath C:\Fop-0.20.1\lib\xalan-2.0.0.jar;C:\Fop-0.20.1\build\fop.jar TestXml.java C:\Fop-0.20.1>java -classpath .;C:\Fop-0.20.1\lib\xalan-2.0.0.jar;C:\Fop -0.20.1\build\fop.jar;C:\Fop-0.20.1\lib\batik.jar;C:\Fop-0.20.1\lib\xerc es-1.2.3.jar TestXml I have the bare minimum jar reqd for this test. The xml and xsl are very simple and when used with FOP from the command line gives a PDF output with no problems. But the above error is thrown when used with the Java class TestXml I was wondering if someone could help. With the risk of making this mail even longer I will PASTE the Java code, XML and XSL as is in the mail so as to avoid attachments. So I apologize for the extremely long email :-) JAVA code import java.io.*; import javax.xml.transform.*; import javax.xml.transform.stream.*; import javax.xml.transform.sax.*; import org.apache.fop.apps.Driver; public class TestXml { public static void main(String[] args) { try { // XML Will eventually be a Java String or Stream from another source. StreamSource xmlsource = new StreamSource(new FileReader("simple.xml")); Driver driver =new Drive