}
foInputHandler = new FOTreeHandler(foUserAgent, rendererType,
- stream, true);
+ stream);
}
treeBuilder.setFOInputHandler(foInputHandler);
*/
/* $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;
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;
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
* 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);
}
}
model.endDocument();
+ try {
+ renderer.stopRenderer();
+ } catch (IOException ex) {
+ throw new SAXException(ex);
+ }
}
/**
package org.apache.fop.fo;
// Java
-import java.io.IOException;
import java.io.OutputStream;
import java.util.HashSet;
import java.util.Iterator;
import org.apache.fop.layoutmgr.InlineStackingLayoutManager;
import org.apache.fop.layoutmgr.LMiter;
import org.apache.fop.layoutmgr.PageLayoutManager;
-import org.apache.fop.render.Renderer;
/**
private long initialMemory;
/**
- * Keep track of time used by renderer.
+ * Keep track of time used in rendering
*/
private long startTime;
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
*/
* @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
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;
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;
* @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);
}
}
* 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) {
}
rend.stopRenderer();
- os.close();
} catch (Exception e) {
logger.error("error rendering output", e);
}
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;
}
/**
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 =
Element root = null;
root = doc.getDocumentElement();
- areaTree = new AreaTree(renderer);
+ areaTree = new AreaTree(foUserAgent, renderType, fontInfo, outputStream);
areaTree.setTreeModel(model);
readAreaTree(root);