* limitations under the License.
*/
-/* $Id: Area.java,v 1.2 2004/02/27 17:41:26 jeremias Exp $ */
+/* $Id$ */
package org.apache.fop.area;
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
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);
+ }
}
/**
// 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;
OutputStream stream) throws FOPException {
super(userAgent);
- model = new RenderPagesModel(userAgent, outputFormat, fontInfo,
- stream);
+ setupModel(userAgent, outputFormat, stream);
lmMaker = userAgent.getLayoutManagerMakerOverride();
if (lmMaker == null) {
}
}
+ /**
+ * 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.
*
/*
- * 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;
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"));
}
/**
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);
}
if (newpage != null && newpage.getPage() != null) {
savePage(newpage);
+ newpage.clear();
}
return renderer.supportsOutOfOrder() || prepared.isEmpty();
}
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();
+ }
}
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;
* 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;
/*
- * 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.
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
// 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) {
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.
* @param color The color to set
*/
public void setColor(ColorType color) {
- this.color = color;
+ this.color = Color.makeSerializable(color);
}
/**
package org.apache.fop.traits;
+import org.apache.fop.area.Trait;
import org.apache.fop.datatypes.ColorType;
import org.apache.fop.fo.Constants;
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;
}