diff options
author | Jeremias Maerki <jeremias@apache.org> | 2005-12-20 12:53:50 +0000 |
---|---|---|
committer | Jeremias Maerki <jeremias@apache.org> | 2005-12-20 12:53:50 +0000 |
commit | 16890a0b27db4e7f9f712b9fb18fbf7b27f7931c (patch) | |
tree | b07fc45607fb4de91b778866487909336422ca46 /src/java/org/apache | |
parent | 6e70a0073ae05ec5e71e81e4290c86a39d2e4afe (diff) | |
download | xmlgraphics-fop-16890a0b27db4e7f9f712b9fb18fbf7b27f7931c.tar.gz xmlgraphics-fop-16890a0b27db4e7f9f712b9fb18fbf7b27f7931c.zip |
First step towards reactivating CachedRenderPagesModel (helps preparing for the intermediate format).
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
Diffstat (limited to 'src/java/org/apache')
-rw-r--r-- | src/java/org/apache/fop/area/Area.java | 9 | ||||
-rw-r--r-- | src/java/org/apache/fop/area/AreaTreeHandler.java | 20 | ||||
-rw-r--r-- | src/java/org/apache/fop/area/CachedRenderPagesModel.java | 55 | ||||
-rw-r--r-- | src/java/org/apache/fop/area/LineArea.java | 3 | ||||
-rw-r--r-- | src/java/org/apache/fop/area/RenderPagesModel.java | 7 | ||||
-rw-r--r-- | src/java/org/apache/fop/area/Trait.java | 75 | ||||
-rw-r--r-- | src/java/org/apache/fop/traits/BorderProps.java | 3 |
7 files changed, 146 insertions, 26 deletions
diff --git a/src/java/org/apache/fop/area/Area.java b/src/java/org/apache/fop/area/Area.java index df0292bec..b0916abda 100644 --- a/src/java/org/apache/fop/area/Area.java +++ b/src/java/org/apache/fop/area/Area.java @@ -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); + } } /** diff --git a/src/java/org/apache/fop/area/AreaTreeHandler.java b/src/java/org/apache/fop/area/AreaTreeHandler.java index df7b74fb3..09d8cb80b 100644 --- a/src/java/org/apache/fop/area/AreaTreeHandler.java +++ b/src/java/org/apache/fop/area/AreaTreeHandler.java @@ -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) { @@ -127,6 +126,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. * * @return AreaTreeModel the model being used for this area tree diff --git a/src/java/org/apache/fop/area/CachedRenderPagesModel.java b/src/java/org/apache/fop/area/CachedRenderPagesModel.java index 171b071c6..6692b5f5b 100644 --- a/src/java/org/apache/fop/area/CachedRenderPagesModel.java +++ b/src/java/org/apache/fop/area/CachedRenderPagesModel.java @@ -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. @@ -18,9 +18,11 @@ 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(); + } } diff --git a/src/java/org/apache/fop/area/LineArea.java b/src/java/org/apache/fop/area/LineArea.java index 7b0037506..4f13ad65f 100644 --- a/src/java/org/apache/fop/area/LineArea.java +++ b/src/java/org/apache/fop/area/LineArea.java @@ -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; diff --git a/src/java/org/apache/fop/area/RenderPagesModel.java b/src/java/org/apache/fop/area/RenderPagesModel.java index 8757a1c51..3f44ee736 100644 --- a/src/java/org/apache/fop/area/RenderPagesModel.java +++ b/src/java/org/apache/fop/area/RenderPagesModel.java @@ -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) { diff --git a/src/java/org/apache/fop/area/Trait.java b/src/java/org/apache/fop/area/Trait.java index 249b170cf..a576ede6e 100644 --- a/src/java/org/apache/fop/area/Trait.java +++ b/src/java/org/apache/fop/area/Trait.java @@ -423,6 +423,79 @@ public class Trait implements Serializable { } /** + * 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); } /** diff --git a/src/java/org/apache/fop/traits/BorderProps.java b/src/java/org/apache/fop/traits/BorderProps.java index 34a8d3d3b..8de33920f 100644 --- a/src/java/org/apache/fop/traits/BorderProps.java +++ b/src/java/org/apache/fop/traits/BorderProps.java @@ -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; } |