]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
First step towards reactivating CachedRenderPagesModel (helps preparing for the inter...
authorJeremias Maerki <jeremias@apache.org>
Tue, 20 Dec 2005 12:53:50 +0000 (12:53 +0000)
committerJeremias Maerki <jeremias@apache.org>
Tue, 20 Dec 2005 12:53:50 +0000 (12:53 +0000)
Fix some serialization problems in the area tree.

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

src/java/org/apache/fop/area/Area.java
src/java/org/apache/fop/area/AreaTreeHandler.java
src/java/org/apache/fop/area/CachedRenderPagesModel.java
src/java/org/apache/fop/area/LineArea.java
src/java/org/apache/fop/area/RenderPagesModel.java
src/java/org/apache/fop/area/Trait.java
src/java/org/apache/fop/traits/BorderProps.java

index df0292bec3c7732298b539eb6518440be7cff9ca..b0916abdac15802dff5992ea5facd689a609fb5d 100644 (file)
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-/* $Id: Area.java,v 1.2 2004/02/27 17:41:26 jeremias Exp $ */
+/* $Id$ */
 
 package org.apache.fop.area;
 
@@ -25,6 +25,7 @@ import java.util.HashMap;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.fop.datatypes.ColorType;
 import org.apache.fop.traits.BorderProps;
 
 // If the area appears more than once in the output
@@ -390,7 +391,11 @@ public class Area implements Serializable {
         if (props == null) {
             props = new java.util.HashMap(20);
         }
-        props.put(traitCode, prop);
+        if (prop instanceof ColorType) {
+            props.put(traitCode, Trait.Color.makeSerializable((ColorType)prop));
+        } else {
+            props.put(traitCode, prop);
+        }
     }
 
     /**
index df7b74fb36f5bd4479f7305345a2f2cf6796f692..09d8cb80b00ceddcaae738cdbb59b8740f101a3b 100644 (file)
@@ -80,8 +80,8 @@ public class AreaTreeHandler extends FOEventHandler {
     // the LayoutManager maker
     private LayoutManagerMaker lmMaker;
 
-    // AreaTreeModel in use
-    private AreaTreeModel model;
+    /** AreaTreeModel in use */
+    protected AreaTreeModel model;
 
     // The fo:root node of the document
     private Root rootFObj;
@@ -111,8 +111,7 @@ public class AreaTreeHandler extends FOEventHandler {
                 OutputStream stream) throws FOPException {
         super(userAgent);
 
-        model = new RenderPagesModel(userAgent, outputFormat, fontInfo,
-            stream);
+        setupModel(userAgent, outputFormat, stream);
             
         lmMaker = userAgent.getLayoutManagerMakerOverride();
         if (lmMaker == null) {
@@ -126,6 +125,19 @@ public class AreaTreeHandler extends FOEventHandler {
         }
     }
 
+    /**
+     * Sets up the AreaTreeModel instance for use by the AreaTreeHandler.
+     * @param userAgent FOUserAgent object for process
+     * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
+     * @param stream OutputStream
+     * @throws FOPException if the RenderPagesModel cannot be created
+     */
+    protected void setupModel(FOUserAgent userAgent, String outputFormat, 
+            OutputStream stream) throws FOPException {
+        model = new RenderPagesModel(userAgent, outputFormat, fontInfo,
+                stream);
+    }
+    
     /**
      * Get the area tree model for this area tree.
      *
index 171b071c60c728ec047a7f985ec6307c5569138d..6692b5f5b4ea6351166d367e4dfe26ddaf7f5374 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 1999-2004 The Apache Software Foundation.
+ * Copyright 1999-2005 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  
 package org.apache.fop.area;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.fop.apps.FOPException;
 import org.apache.fop.apps.FOUserAgent;
 import org.apache.fop.fonts.FontInfo;
+import org.xml.sax.SAXException;
 
 import java.util.Map;
 import java.util.HashMap;
@@ -44,13 +46,21 @@ import java.io.BufferedInputStream;
 public class CachedRenderPagesModel extends RenderPagesModel {
     private Map pageMap = new HashMap();
 
+    /** Base directory to save temporary file in, typically points to the user's temp dir. */
+    protected File baseDir;
+    
     /**
-     * Constructor
-     * @see org.apache.fop.area.RenderPagesModel#RenderPagesModel(FOUserAgent, String, FontInfo, OutputStream)
+     * Main Constructor
+     * @param userAgent FOUserAgent object for process
+     * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
+     * @param fontInfo FontInfo object
+     * @param stream OutputStream
+     * @throws FOPException if the renderer cannot be properly initialized
      */
     public CachedRenderPagesModel (FOUserAgent userAgent, String outputFormat, 
             FontInfo fontInfo, OutputStream stream) throws FOPException {
         super(userAgent, outputFormat, fontInfo, stream);
+        this.baseDir = new File(System.getProperty("java.io.tmpdir"));
     }
 
     /**
@@ -64,14 +74,19 @@ public class CachedRenderPagesModel extends RenderPagesModel {
                     try {
                         // load page from cache
                         String name = (String)pageMap.get(p);
-                        File temp = new File(name);
-                        log.debug("page serialized to: " + temp.length());
+                        File tempFile = new File(baseDir, name);
+                        log.debug("Loading page from: " + tempFile);
                         ObjectInputStream in = new ObjectInputStream(
                                              new BufferedInputStream(
-                                               new FileInputStream(temp)));
-                        p.loadPage(in);
-                        in.close();
-                        temp.delete();
+                                               new FileInputStream(tempFile)));
+                        try {
+                            p.loadPage(in);
+                        } finally {
+                            IOUtils.closeQuietly(in);
+                        }
+                        if (!tempFile.delete()) {
+                            log.warn("Temporary file could not be deleted: " + tempFile);
+                        }
                         pageMap.remove(p);
                     } catch (Exception e) {
                         log.error(e);
@@ -102,6 +117,7 @@ public class CachedRenderPagesModel extends RenderPagesModel {
         }
         if (newpage != null && newpage.getPage() != null) {
             savePage(newpage);
+            newpage.clear();
         }
         return renderer.supportsOutOfOrder() || prepared.isEmpty();
     }
@@ -116,15 +132,28 @@ public class CachedRenderPagesModel extends RenderPagesModel {
         try {
             // save page to cache
             ObjectOutputStream tempstream;
-            String fname = "page" + page.toString() + ".ser";
+            String fname = "fop-page-" + page.toString() + ".ser";
+            File tempFile = new File(baseDir, fname);
+            tempFile.deleteOnExit();
             tempstream = new ObjectOutputStream(new BufferedOutputStream(
-                                                new FileOutputStream(fname)));
-            page.savePage(tempstream);
-            tempstream.close();
+                                                new FileOutputStream(tempFile)));
+            try {
+                page.savePage(tempstream);
+            } finally {
+                IOUtils.closeQuietly(tempstream);
+            }
             pageMap.put(page, fname);
+            if (log.isDebugEnabled()) {
+                log.debug("Page saved to temporary file: " + tempFile);
+            }
         } catch (Exception e) {
             log.error(e);
         }
     }
+
+    /** @see org.apache.fop.area.RenderPagesModel#endDocument() */
+    public void endDocument() throws SAXException {
+        super.endDocument();
+    }
 }
 
index 7b0037506ae20192d94c790aed17c6fec7816717..4f13ad65fe3982dfdb24baff562234152b60061d 100644 (file)
@@ -21,6 +21,7 @@ package org.apache.fop.area;
 import org.apache.fop.area.inline.InlineArea;
 import org.apache.fop.fo.Constants;
 
+import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -35,7 +36,7 @@ public class LineArea extends Area {
      * that can be used in order to re-compute adjustement and / or indents when a
      * page-number or a page-number-citation is resolved
      */
-    private class LineAdjustingInfo {
+    private class LineAdjustingInfo implements Serializable {
         private int lineAlignment;
         private int difference;
         private int availableStretch;
index 8757a1c518e3cfa4c463438b3cab91871a5b4a27..3f44ee736dae4a01525b29ae06eb500b6db71048 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 1999-2004 The Apache Software Foundation.
+ * Copyright 1999-2005 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -32,7 +32,6 @@ import org.apache.fop.apps.FOPException;
 import org.apache.fop.apps.FOUserAgent;
 import org.apache.fop.fonts.FontInfo;
 import org.apache.fop.render.Renderer;
-import org.apache.fop.render.RendererFactory;
 
 /**
  * This uses the AreaTreeModel to store the pages
@@ -109,8 +108,8 @@ public class RenderPagesModel extends AreaTreeModel {
         // it is more appropriate to do this after queued pages but
         // it will mean that the renderer has not prepared a page that
         // could be referenced
-        boolean done = renderer.supportsOutOfOrder() && page.isResolved();
-        if (done) {
+        boolean ready = renderer.supportsOutOfOrder() && page.isResolved();
+        if (ready) {
             try {
                 renderer.renderPage(page);
             } catch (Exception e) {
index 249b170cfc2adffeb0dcee4bc120775ac3fb584b..a576ede6e8ce6f4bbdabe07bbe865f197fde4098 100644 (file)
@@ -422,6 +422,79 @@ public class Trait implements Serializable {
         return null;
     }
 
+    /**
+     * Serializable ColorType implementation for the area tree.
+     * @TODO Think about switching to java.awt.Color entirely!
+     */
+    public static class Color implements ColorType, Serializable {
+
+        private float red;
+        private float green;
+        private float blue;
+        private float alpha;
+        
+        /**
+         * Creates a new Color instance
+         * @param r the red component
+         * @param g the green component
+         * @param b the blue component
+         * @param a the alpha component
+         */
+        public Color(float r, float g, float b, float a) {
+            this.red = r;
+            this.green = g;
+            this.blue = b;
+            this.alpha = a;
+        }
+        
+        /**
+         * Copy constructor
+         * @param col the ColorType instance which shall be duplicated
+         */
+        public Color(ColorType col) {
+            this(col.getRed(), col.getGreen(), col.getBlue(), col.getAlpha());
+        }
+        
+        /** @see org.apache.fop.datatypes.ColorType#getRed() */
+        public float getRed() {
+            return this.red;
+        }
+
+        /** @see org.apache.fop.datatypes.ColorType#getGreen() */
+        public float getGreen() {
+            return this.green;
+        }
+
+        /** @see org.apache.fop.datatypes.ColorType#getBlue() */
+        public float getBlue() {
+            return this.blue;
+        }
+
+        /** @see org.apache.fop.datatypes.ColorType#getAlpha() */
+        public float getAlpha() {
+            return this.alpha;
+        }
+
+        /** @see org.apache.fop.datatypes.ColorType#getAWTColor() */
+        public java.awt.Color getAWTColor() {
+            return new java.awt.Color(red, green, blue, alpha);
+        }
+        
+        /**
+         * Converts a given color to a serializable instance if necessary.
+         * @param col the color
+         * @return the serializable color value.
+         */
+        public static ColorType makeSerializable(ColorType col) {
+            if (col instanceof Serializable) {
+                return col;
+            } else {
+                return new Color(col);
+            }
+        }
+        
+    }
+    
     /**
      * Background trait structure.
      * Used for storing back trait information which are related.
@@ -499,7 +572,7 @@ public class Trait implements Serializable {
          * @param color The color to set
          */
         public void setColor(ColorType color) {
-            this.color = color;
+            this.color = Color.makeSerializable(color);
         }
 
         /**
index 34a8d3d3bc52e34e6759d6c1d51bccad8d7381a8..8de33920fec704593babf937756a954826b1bddf 100644 (file)
@@ -18,6 +18,7 @@
  
 package org.apache.fop.traits;
 
+import org.apache.fop.area.Trait;
 import org.apache.fop.datatypes.ColorType;
 import org.apache.fop.fo.Constants;
 
@@ -55,7 +56,7 @@ public class BorderProps implements Serializable {
     public BorderProps(int style, int width, ColorType color, int mode) {
         this.style = style;
         this.width = width;
-        this.color = color;
+        this.color = Trait.Color.makeSerializable(color);
         this.mode = mode;
     }