]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
This change abstracts the Renderer away from FOTreeHandler as follows:
authorGlen Mazza <gmazza@apache.org>
Tue, 6 Jul 2004 05:25:09 +0000 (05:25 +0000)
committerGlen Mazza <gmazza@apache.org>
Tue, 6 Jul 2004 05:25:09 +0000 (05:25 +0000)
Old:
driver-->fotreehandler-->areatree
                     \-->renderer

New:
driver-->fotreehandler-->areatree-->renderer

I am hoping this will streamline the code more.
Main drawback I can see is that the AreaTree constructor
gets a bit ugly--from one parameter, the Renderer,
to four (the items needed to create a Renderer.) Perhaps
we can have AreaTree construct the FontInfo object in the
future, dropping us to a three-parameter constructor.

Comments welcome.

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@197754 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/fop/apps/Driver.java
src/java/org/apache/fop/area/AreaTree.java
src/java/org/apache/fop/fo/FOTreeHandler.java
src/java/org/apache/fop/tools/AreaTreeBuilder.java

index e5ffada6e4094d55e34d10f7625b783961fc898b..0572066ba04731c96f1dd89481cded7a42a58c57 100644 (file)
@@ -313,7 +313,7 @@ public class Driver implements Constants {
             }
 
             foInputHandler = new FOTreeHandler(foUserAgent, rendererType, 
-                stream, true);
+                stream);
         }
 
         treeBuilder.setFOInputHandler(foInputHandler);
index ab7c1562cf03c07c79afc472dbb3829829c102ff..3334cda39fd4a8d96efb3dafa79f4ba1306ae725 100644 (file)
  */
 
 /* $Id$ */
-
 package org.apache.fop.area;
 
-import org.apache.fop.area.extensions.BookmarkData;
-import org.apache.fop.fo.extensions.Outline;
-import org.apache.fop.fo.extensions.Bookmarks;
-import org.apache.fop.render.Renderer;
-
+// Java
+import java.io.IOException;
+import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -31,7 +28,18 @@ import java.util.Set;
 import java.util.HashSet;
 import java.util.Iterator;
 
-// Commons-Logging
+// XML
+import org.xml.sax.SAXException;
+
+// Apache
+import org.apache.fop.apps.FOPException;
+import org.apache.fop.apps.FOUserAgent;
+import org.apache.fop.area.extensions.BookmarkData;
+import org.apache.fop.fo.Constants;
+import org.apache.fop.fo.extensions.Outline;
+import org.apache.fop.fo.extensions.Bookmarks;
+import org.apache.fop.fonts.FontInfo;
+import org.apache.fop.render.Renderer;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -65,15 +73,84 @@ public class AreaTree {
 
     private static Log log = LogFactory.getLog(AreaTree.class);
 
+    private FOUserAgent foUserAgent;
+
+    /**
+     * the renderer to use to output the area tree
+     */
+    private Renderer renderer;
+
     /**
      * Constructor.
      */
-    public AreaTree (Renderer renderer) {
+    public AreaTree (FOUserAgent userAgent, int renderType, 
+        FontInfo fontInfo, OutputStream stream) throws FOPException {
+
+        foUserAgent = userAgent;
+
+        if (foUserAgent.getRendererOverride() != null) {
+            renderer = foUserAgent.getRendererOverride();
+        } else {
+            renderer = createRenderer(renderType);
+            renderer.setUserAgent(foUserAgent);
+        }
+
+        try {
+            renderer.setupFontInfo(fontInfo);
+            // check that the "any,normal,400" font exists
+            if (!fontInfo.isSetupValid()) {
+                throw new FOPException(
+                    "No default font defined by OutputConverter");
+            }
+            renderer.startRenderer(stream);
+        } catch (IOException e) {
+            throw new FOPException(e);
+        }
+
         // this.atModel = new CachedRenderPagesModel(renderer);
         setTreeModel(new RenderPagesModel(renderer));
     }
 
 
+    /**
+     * Creates a Renderer object based on render-type desired
+     * @param renderType the type of renderer to use
+     * @return Renderer the new Renderer instance
+     * @throws IllegalArgumentException if an unsupported renderer type was requested
+     */
+    private Renderer createRenderer(int renderType) throws IllegalArgumentException {
+
+        switch (renderType) {
+        case Constants.RENDER_PDF:
+            return new org.apache.fop.render.pdf.PDFRenderer();
+        case Constants.RENDER_AWT:
+            return new org.apache.fop.render.awt.AWTRenderer();
+        case Constants.RENDER_PRINT:
+            return new org.apache.fop.render.awt.AWTPrintRenderer();
+        case Constants.RENDER_PCL:
+            return new org.apache.fop.render.pcl.PCLRenderer();
+        case Constants.RENDER_PS:
+            return new org.apache.fop.render.ps.PSRenderer();
+        case Constants.RENDER_TXT:
+            return new org.apache.fop.render.txt.TXTRenderer();
+        case Constants.RENDER_XML:
+            return new org.apache.fop.render.xml.XMLRenderer();
+        case Constants.RENDER_SVG:
+            return new org.apache.fop.render.svg.SVGRenderer();
+        default:
+            throw new IllegalArgumentException("Invalid renderer type " 
+                + renderType);
+        }
+    }
+
+    /**
+     * Temporary accessor for renderer for tools.AreaTreeBuilder
+     * @return renderer The renderer being used by this area tree
+     */
+    public Renderer getRenderer() {
+        return renderer;
+    }
+
     /**
      * Create a new store pages model.
      * @return StorePagesModel the new model
@@ -208,7 +285,7 @@ public class AreaTree {
      * This indicates that the document is complete and any unresolved
      * reference can be dealt with.
      */
-    public void endDocument() {
+    public void endDocument() throws SAXException {
         for (Iterator iter = resolve.keySet().iterator(); iter.hasNext();) {
             String id = (String)iter.next();
             Set list = (Set)resolve.get(id);
@@ -220,6 +297,11 @@ public class AreaTree {
             }
         }
         model.endDocument();
+        try {
+            renderer.stopRenderer();
+        } catch (IOException ex) {
+            throw new SAXException(ex);
+        }        
     }
 
     /**
index 8de48be23257fb0dcbd34109531a6fb6d9199a07..c8b0449be2d0547db0aea3c3d435278c1a4d54f5 100644 (file)
@@ -19,7 +19,6 @@
 package org.apache.fop.fo;
 
 // Java
-import java.io.IOException;
 import java.io.OutputStream;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -56,7 +55,6 @@ import org.apache.fop.layoutmgr.ContentLayoutManager;
 import org.apache.fop.layoutmgr.InlineStackingLayoutManager;
 import org.apache.fop.layoutmgr.LMiter;
 import org.apache.fop.layoutmgr.PageLayoutManager;
-import org.apache.fop.render.Renderer;
 
 
 /**
@@ -93,7 +91,7 @@ public class FOTreeHandler extends FOInputHandler {
     private long initialMemory;
 
     /**
-     * Keep track of time used by renderer.
+     * Keep track of time used in rendering
      */
     private long startTime;
 
@@ -101,84 +99,29 @@ public class FOTreeHandler extends FOInputHandler {
      extending FOP **/
     private AddLMVisitor addLMVisitor = null;
     
-    /**
-     * the renderer to use to output the area tree
-     */
-    private Renderer renderer;
-
     /**
      * Main constructor
      * @param userAgent the apps.userAgent implementation that governs
      *      this FO Tree
-     * @param OutputStream stream to use to output results of renderer
+     * @param renderType the fo.Constants value indicating desired
+     *          output type (RENDER_PDF, RENDER_PS, etc.)
+     * @param OutputStream stream to use to output results of rendering
      *
-     * @param store if true then use the store pages model and keep the
-     *        area tree in memory
      */
     public FOTreeHandler(FOUserAgent userAgent, int renderType, 
-        OutputStream stream, boolean store) throws FOPException {
+        OutputStream stream) throws FOPException {
         super(userAgent);
 
-        if (foUserAgent.getRendererOverride() != null) {
-            renderer = foUserAgent.getRendererOverride();
-        } else {
-            renderer = createRenderer(renderType);
-            renderer.setUserAgent(foUserAgent);
-        }
-        
-        areaTree = new AreaTree(renderer);
-
-        try {
-            renderer.setupFontInfo(fontInfo);
-            // check that the "any,normal,400" font exists
-            if (!fontInfo.isSetupValid()) {
-                throw new FOPException(
-                    "No default font defined by OutputConverter");
-            }
-            renderer.startRenderer(stream);
-        } catch (IOException e) {
-            throw new FOPException(e);
-        }
-        
+        areaTree = new AreaTree(userAgent, renderType, fontInfo, stream);
+
         if (collectStatistics) {
             runtime = Runtime.getRuntime();
         }
     }
 
     /**
-     * Creates a Renderer object based on render-type desired
-     * @param renderType the type of renderer to use
-     * @return Renderer the new Renderer instance
-     * @throws IllegalArgumentException if an unsupported renderer type was requested
-     */
-    private Renderer createRenderer(int renderType) throws IllegalArgumentException {
-
-        switch (renderType) {
-        case Constants.RENDER_PDF:
-            return new org.apache.fop.render.pdf.PDFRenderer();
-        case Constants.RENDER_AWT:
-            return new org.apache.fop.render.awt.AWTRenderer();
-        case Constants.RENDER_PRINT:
-            return new org.apache.fop.render.awt.AWTPrintRenderer();
-        case Constants.RENDER_PCL:
-            return new org.apache.fop.render.pcl.PCLRenderer();
-        case Constants.RENDER_PS:
-            return new org.apache.fop.render.ps.PSRenderer();
-        case Constants.RENDER_TXT:
-            return new org.apache.fop.render.txt.TXTRenderer();
-        case Constants.RENDER_XML:
-            return new org.apache.fop.render.xml.XMLRenderer();
-        case Constants.RENDER_SVG:
-            return new org.apache.fop.render.svg.SVGRenderer();
-        default:
-            throw new IllegalArgumentException("Invalid renderer type " 
-                + renderType);
-        }
-    }
-
-    /**
-     * Start the document.
-     * This starts the document in the renderer.
+     * Prepare FOTreeHandler for FO Tree building.
+     * This is called from FOTreeBuilder.startDocument()
      *
      * @throws SAXException if there is an error
      */
@@ -201,17 +144,14 @@ public class FOTreeHandler extends FOInputHandler {
      * @throws SAXException if there is some error
      */
     public void endDocument() throws SAXException {
-        try {
-            if (pageSequenceFound == false) {
-                throw new SAXException("Error: No fo:page-sequence child " +
-                    "found within fo:root element.");
-            }
-            areaTree.endDocument();
-            renderer.stopRenderer();
-        } catch (IOException ex) {
-            throw new SAXException(ex);
+
+        if (pageSequenceFound == false) {
+            throw new SAXException("Error: No fo:page-sequence child " +
+                "found within fo:root element.");
         }
 
+        areaTree.endDocument();
+
         if (collectStatistics) {
             if (MEM_PROFILE_WITH_GC) {
                 // This takes time but gives better results
index 37b923d7994e21969ef2821c190f46af740f142f..c844a0d8a0abb6c7c07ed432c429f95890e7ba95 100644 (file)
@@ -39,6 +39,7 @@ import org.w3c.dom.Document;
 import org.apache.batik.dom.svg.SVGDOMImplementation;
 
 // FOP
+import org.apache.fop.apps.FOPException;
 import org.apache.fop.area.Area;
 import org.apache.fop.area.AreaTree;
 import org.apache.fop.area.AreaTreeModel;
@@ -67,6 +68,7 @@ import org.apache.fop.area.inline.Leader;
 import org.apache.fop.area.inline.Space;
 import org.apache.fop.area.inline.Viewport;
 import org.apache.fop.area.inline.TextArea;
+import org.apache.fop.fo.Constants;
 import org.apache.fop.fonts.Font;
 import org.apache.fop.fonts.FontInfo;
 import org.apache.fop.render.Renderer;
@@ -150,31 +152,33 @@ public class AreaTreeBuilder {
      * @param out output filename
      */
     protected void runTest(String in, String type, String out) {
-        Renderer rend = null;
+        int renderType = Constants.NOT_SET;
         if ("xml".equals(type)) {
-            rend = new XMLRenderer();
+            renderType = Constants.RENDER_XML;
         } else if ("pdf".equals(type)) {
-            rend = new PDFRenderer();
+            renderType = Constants.RENDER_PDF;
         } else if ("svg".equals(type)) {
-            rend = new SVGRenderer();
+            renderType = Constants.RENDER_SVG;
         }
 
         FontInfo fontInfo = new FontInfo();
-        rend.setupFontInfo(fontInfo);
         FOUserAgent ua = new FOUserAgent();
-        rend.setUserAgent(ua);
 
-        StorePagesModel sm = AreaTree.createStorePagesModel();
-        TreeLoader tl = new TreeLoader(rend, fontInfo);
-        tl.setLogger(logger);
-        tl.setTreeModel(sm);
         try {
+            OutputStream os =
+              new java.io.BufferedOutputStream(new java.io.FileOutputStream(out));
+    
+            StorePagesModel sm = AreaTree.createStorePagesModel();
+            TreeLoader tl = new TreeLoader(ua, renderType, fontInfo, os);
+            tl.setLogger(logger);
+            tl.setTreeModel(sm);
             InputStream is =
               new java.io.BufferedInputStream(new java.io.FileInputStream(in));
             tl.buildAreaTree(is);
-            renderAreaTree(sm, rend, out);
-        } catch (IOException e) {
-            logger.error("error reading file" + e.getMessage(), e);
+            renderAreaTree(sm, tl.getAreaTree().getRenderer());
+            os.close();
+        } catch (Exception e) {
+            logger.error("Error processing file: " + e.getMessage(), e);
         }
     }
 
@@ -182,16 +186,10 @@ public class AreaTreeBuilder {
      * Renders an area tree to a target format using a renderer.
      * @param sm area tree pages
      * @param rend renderer to use for output
-     * @param out target filename
      */
     protected void renderAreaTree(StorePagesModel sm,
-                                  Renderer rend, String out) {
+                                  Renderer rend) {
         try {
-            OutputStream os =
-              new java.io.BufferedOutputStream(new java.io.FileOutputStream(out));
-
-            rend.startRenderer(os);
-
             int count = 0;
             int seqc = sm.getPageSequenceCount();
             while (count < seqc) {
@@ -223,7 +221,6 @@ public class AreaTreeBuilder {
             }
 
             rend.stopRenderer();
-            os.close();
         } catch (Exception e) {
             logger.error("error rendering output", e);
         }
@@ -241,10 +238,15 @@ class TreeLoader {
     private FontInfo fontInfo;
     private Font currentFontState;
     private Log logger = null;
-
-    TreeLoader(Renderer renderer, FontInfo fontInfo) {
-        this.renderer = renderer;
+    private FOUserAgent foUserAgent = null;
+    private int renderType = Constants.NOT_SET;
+    private OutputStream outputStream;
+    
+    TreeLoader(FOUserAgent userAgent, int rendType, FontInfo fontInfo, OutputStream os) {
+        this.foUserAgent = userAgent;
+        this.renderType = rendType;
         this.fontInfo = fontInfo;
+        this.outputStream = os;
     }
 
     /**
@@ -255,11 +257,15 @@ class TreeLoader {
         this.logger = logger;
     }
 
+    public AreaTree getAreaTree() {
+        return areaTree;
+    }
+
     public void setTreeModel(AreaTreeModel mo) {
         model = mo;
     }
 
-    public void buildAreaTree(InputStream is) {
+    public void buildAreaTree(InputStream is) throws FOPException {
         Document doc = null;
         try {
             DocumentBuilderFactory fact =
@@ -272,7 +278,7 @@ class TreeLoader {
         Element root = null;
         root = doc.getDocumentElement();
 
-        areaTree = new AreaTree(renderer);
+        areaTree = new AreaTree(foUserAgent, renderType, fontInfo, outputStream);
         areaTree.setTreeModel(model);
 
         readAreaTree(root);