From 11c49bb91290add676d8629b3a548b041d48c72c Mon Sep 17 00:00:00 2001 From: fotis Date: Sun, 26 Mar 2000 20:47:58 +0000 Subject: [PATCH] calls Fop from xt if output method is pdf. Submitted by Jon Smirl git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@193315 13f79535-47bb-0310-9956-ffa450edef68 --- src/org/apache/fop/apps/PDFOutputHandler.java | 183 ++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 src/org/apache/fop/apps/PDFOutputHandler.java diff --git a/src/org/apache/fop/apps/PDFOutputHandler.java b/src/org/apache/fop/apps/PDFOutputHandler.java new file mode 100644 index 000000000..db1d549b5 --- /dev/null +++ b/src/org/apache/fop/apps/PDFOutputHandler.java @@ -0,0 +1,183 @@ +package org.apache.fop.apps; + +import org.xml.sax.*; +import com.jclark.xsl.sax.*; +import java.io.*; + +// FOP +import org.apache.fop.fo.FOTreeBuilder; +import org.apache.fop.fo.ElementMapping; +import org.apache.fop.layout.AreaTree; +import org.apache.fop.layout.FontInfo; +import org.apache.fop.render.Renderer; + +////////////////////////////////////////////////////////////////////////////////////// +/** + * A DocumentHandler that writes a PDF representation to an OutputStream. + * + * Use with James Clark's XT. Just put FOP on your class path and add + * + * to your stylesheet. Now XT will automatically call FOP. + * + */ +public class PDFOutputHandler extends FOTreeBuilder implements OutputDocumentHandler { + + /** the area tree that is the result of formatting the FO tree */ + protected AreaTree areaTree; + + /** the renderer to use to output the area tree */ + protected Renderer renderer; + + /** the PrintWriter to use to output the results of the renderer */ + protected PrintWriter writer; + + private boolean keepOpen; + + ////////////////////////////////////////////////////////////////////////////////////// + /** + */ + public PDFOutputHandler() { + } + + ////////////////////////////////////////////////////////////////////////////////////// + /** + */ + public PDFOutputHandler(OutputStream out) { + this(); + this.writer = new PrintWriter(out); + } + + ////////////////////////////////////////////////////////////////////////////////////// + /** + */ + public DocumentHandler init(Destination dest, AttributeList atts) throws IOException { + this.writer = new PrintWriter(dest.getOutputStream("application/pdf", null)); + this.keepOpen = dest.keepOpen(); + + String version = org.apache.fop.apps.Version.getVersion(); + setRenderer("org.apache.fop.render.pdf.PDFRenderer", version); + addElementMapping("org.apache.fop.fo.StandardElementMapping"); + addElementMapping("org.apache.fop.svg.SVGElementMapping"); + return this; + } + + ////////////////////////////////////////////////////////////////////////////////////// + /** + * set the class name of the Renderer to use as well as the + * producer string for those renderers that can make use of it + */ + public void setRenderer(String rendererClassName, String producer) { + this.renderer = createRenderer(rendererClassName); + this.renderer.setProducer(producer); + } + + ////////////////////////////////////////////////////////////////////////////////////// + /** + * SAX passthrough, finish rendering the document + */ + public void endDocument() throws SAXException { + super.endDocument(); + + try { + doFormat(); + doRender(); + } catch (IOException io) { + throw new SAXException(io); + } catch (FOPException fop) { + throw new SAXException(fop); + } + writer.flush(); + } + + ////////////////////////////////////////////////////////////////////////////////////// + /** + * format the formatting object tree into an area tree + */ + public void doFormat() + throws FOPException { + FontInfo fontInfo = new FontInfo(); + this.renderer.setupFontInfo(fontInfo); + + this.areaTree = new AreaTree(); + this.areaTree.setFontInfo(fontInfo); + + format(areaTree); + } + + ////////////////////////////////////////////////////////////////////////////////////// + /** + * render the area tree to the output form + */ + public void doRender() + throws IOException { + this.renderer.render(areaTree, this.writer); + } + + ////////////////////////////////////////////////////////////////////////////////////// + /** + * add the given element mapping. + * + * an element mapping maps element names to Java classes + */ + public void addElementMapping(ElementMapping mapping) { + mapping.addToBuilder(this); + } + + ////////////////////////////////////////////////////////////////////////////////////// + /** + * add the element mapping with the given class name + */ + public void addElementMapping(String mappingClassName) { + createElementMapping(mappingClassName).addToBuilder(this); + } + + ////////////////////////////////////////////////////////////////////////////////////// + /** + * protected method used by addElementMapping(String) to + * instantiate element mapping class + */ + protected ElementMapping createElementMapping(String mappingClassName) { + System.err.println("using element mapping " + mappingClassName); + + try { + return (ElementMapping) + Class.forName(mappingClassName).newInstance(); + } catch (ClassNotFoundException e) { + System.err.println("Could not find " + mappingClassName); + } catch (InstantiationException e) { + System.err.println("Could not instantiate " + + mappingClassName); + } catch (IllegalAccessException e) { + System.err.println("Could not access " + mappingClassName); + } catch (ClassCastException e) { + System.err.println(mappingClassName + " is not an element mapping"); + } + return null; + } + + ////////////////////////////////////////////////////////////////////////////////////// + /** + * protected method used by setRenderer(String, String) to + * instantiate the Renderer class + */ + protected Renderer createRenderer(String rendererClassName) { + System.err.println("using renderer " + rendererClassName); + + try { + return (Renderer) + Class.forName(rendererClassName).newInstance(); + } catch (ClassNotFoundException e) { + System.err.println("Could not find " + rendererClassName); + } catch (InstantiationException e) { + System.err.println("Could not instantiate " + + rendererClassName); + } catch (IllegalAccessException e) { + System.err.println("Could not access " + rendererClassName); + } catch (ClassCastException e) { + System.err.println(rendererClassName + " is not a renderer"); + } + return null; + } +} + -- 2.39.5