Browse Source

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
tags/fop-0_92-beta
Jeremias Maerki 18 years ago
parent
commit
16890a0b27

+ 7
- 2
src/java/org/apache/fop/area/Area.java View 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);
}
}

/**

+ 16
- 4
src/java/org/apache/fop/area/AreaTreeHandler.java View 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.
*

+ 42
- 13
src/java/org/apache/fop/area/CachedRenderPagesModel.java View 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.
@@ -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();
}
}


+ 2
- 1
src/java/org/apache/fop/area/LineArea.java View 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;

+ 3
- 4
src/java/org/apache/fop/area/RenderPagesModel.java View 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) {

+ 74
- 1
src/java/org/apache/fop/area/Trait.java View 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);
}

/**

+ 2
- 1
src/java/org/apache/fop/traits/BorderProps.java View 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;
}


Loading…
Cancel
Save