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.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 Driver(); FileOutputStream fos = new FileOutputStream("simple.pdf"); driver.setOutputStream(fos); driver.setRenderer(Driver.RENDER_TXT); Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(new FileReader("simple.xsl"))); // Does not work transformer.transform(xmlsource, new SAXResult(driver.getContentHandler())); //Works //transformer.transform(xmlsource, new StreamResult(System.out)); } catch (Exception e) { e.printStackTrace(); } } } XML -------- <?xml version="1.0"?> <source> <books> <book year="2002"> <name>XML Bible</name> <author>Elliot Rusty</author> <desc>A great book for XML beginners.</desc> </book> <book year="2002"> <name>Thinking in Java</name> <author>Bruce Eckel</author> <desc>A great book for Java beginners.</desc> </book> </books> </source> XSL ---- <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> <xsl:template match="/"> <fo:root> <fo:layout-master-set> <fo:simple-page-master master-name="book-page" page-height="11in" page-width="8in" margin-top=".3in" margin-bottom=".5in" margin-left=".4in" margin-right=".2in"> <fo:region-body margin-top="5.0in" margin-bottom=".5in"/> <fo:region-before extent="6in"/> <fo:region-after extent=".5in"/> </fo:simple-page-master> </fo:layout-master-set> <xsl:apply-templates/> </fo:root> </xsl:template> <xsl:template match="source"> <fo:page-sequence master-name="book-page"> <fo:static-content flow-name="xsl-region-before"> <fo:block> Various sources for XML/Java </fo:block> </fo:static-content> <fo:flow flow-name="xsl-region-body"> <fo:block font-size="7pt" text-indent=".5in"> <xsl:apply-templates/> </fo:block> </fo:flow> </fo:page-sequence> </xsl:template> <xsl:template match="books"> <fo:block color="blue"> Books - <xsl:apply-templates/> </fo:block> </xsl:template> <xsl:template match="book"> <fo:block color="green"> <xsl:apply-templates/> </fo:block> </xsl:template> <xsl:template match="name"> <fo:block color="red"> <xsl:apply-templates/> </fo:block> </xsl:template> <xsl:template match="author"> <fo:block color="orange"> <xsl:apply-templates/> </fo:block> </xsl:template> <xsl:template match="desc"> <fo:block> <xsl:apply-templates/> </fo:block> </xsl:template> </xsl:stylesheet> Thanks for the patience in reading this mail. Any help whatsoever will be highly appreciated. Thanks, Anil.