From 4cd2247565deba8992baa983ba5622620fa6bb5e Mon Sep 17 00:00:00 2001 From: Glen Mazza Date: Fri, 16 Jul 2004 05:10:32 +0000 Subject: [PATCH] Moved user-defined ElementMapping initialization from Driver to FOUserAgent. Moved only "string" method, the version we use internally--probably sufficient for others' work as well. (Note: an additional unused FOUserAgent object will be created in driver during command-line use--cp. its no-param constructor vs. setUserAgent() call in apps.Fop; this will need to be ironed out at some time.) git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@197794 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/fop/apps/Driver.java | 64 +------------------ src/java/org/apache/fop/apps/FOUserAgent.java | 23 +++++++ src/java/org/apache/fop/fo/FOTreeBuilder.java | 45 +++++++------ 3 files changed, 50 insertions(+), 82 deletions(-) diff --git a/src/java/org/apache/fop/apps/Driver.java b/src/java/org/apache/fop/apps/Driver.java index 8dd1a4652..41ae90469 100644 --- a/src/java/org/apache/fop/apps/Driver.java +++ b/src/java/org/apache/fop/apps/Driver.java @@ -30,7 +30,6 @@ import org.xml.sax.XMLReader; // FOP import org.apache.fop.fo.Constants; -import org.apache.fop.fo.ElementMapping; import org.apache.fop.fo.FOTreeBuilder; import org.apache.fop.render.awt.AWTRenderer; @@ -47,11 +46,7 @@ import org.apache.fop.render.awt.AWTRenderer; * render. Methods within FOUserAgent can be called to set the * Renderer to use, the (possibly multiple) ElementMapping(s) to * use and the OutputStream to use to output the results of the - * rendering (where applicable). In the case of - * ElementMapping(s), the Driver may be supplied either with the - * object itself, or the name of the class, in which case Driver will - * instantiate the class itself. The advantage of the latter is it - * enables runtime determination of ElementMapping(s). + * rendering (where applicable). *

* Once the Driver is set up, the render method * is called. The invocation of the method is @@ -67,11 +62,6 @@ import org.apache.fop.render.awt.AWTRenderer; */ public class Driver implements Constants { - /** - * the FO tree builder - */ - private FOTreeBuilder treeBuilder; - /** * the render type code given by setRender */ @@ -91,6 +81,7 @@ public class Driver implements Constants { * Main constructor for the Driver class. */ public Driver() { + foUserAgent = new FOUserAgent(); stream = null; } @@ -113,23 +104,6 @@ public class Driver implements Constants { this.stream = stream; } - private boolean isInitialized() { - return (treeBuilder != null); - } - - /** - * Initializes the Driver object. - */ - private void initialize() { - if (isInitialized()) { - throw new IllegalStateException("Driver already initialized"); - } - treeBuilder = new FOTreeBuilder(); - if (foUserAgent == null) { - foUserAgent = new FOUserAgent(); - } - } - /** * Resets the Driver so it can be reused. Property and element * mappings are reset to defaults. @@ -137,9 +111,6 @@ public class Driver implements Constants { */ public synchronized void reset() { stream = null; - if (treeBuilder != null) { - treeBuilder.reset(); - } } /** @@ -148,10 +119,6 @@ public class Driver implements Constants { * @param agent FOUserAgent to use */ public void setUserAgent(FOUserAgent agent) throws FOPException { - if (foUserAgent != null) { - throw new IllegalStateException("FOUserAgent " + - "instance already set."); - } foUserAgent = agent; } @@ -160,9 +127,6 @@ public class Driver implements Constants { * @return the user agent */ public FOUserAgent getUserAgent() { - if (foUserAgent == null) { - foUserAgent = new FOUserAgent(); - } return foUserAgent; } @@ -208,24 +172,6 @@ public class Driver implements Constants { this.renderType = renderType; } - /** - * Add the given element mapping. - * An element mapping maps element names to Java classes. - * - * @param mapping the element mappingto add - */ - public void addElementMapping(ElementMapping mapping) { - treeBuilder.addElementMapping(mapping); - } - - /** - * Add the element mapping with the given class name. - * @param mappingClassName the class name representing the element mapping. - */ - public void addElementMapping(String mappingClassName) { - treeBuilder.addElementMapping(mappingClassName); - } - /** * Determines which SAX ContentHandler is appropriate for the renderType. * Structure renderers (e.g. MIF & RTF) each have a specialized @@ -236,16 +182,12 @@ public class Driver implements Constants { * @throws FOPException if setting up the ContentHandler fails */ public ContentHandler getContentHandler() throws FOPException { - if (!isInitialized()) { - initialize(); - } if (renderType != RENDER_PRINT && renderType != RENDER_AWT) { validateOutputStream(); } - treeBuilder.initialize(renderType, foUserAgent, stream); - return treeBuilder; + return new FOTreeBuilder(renderType, foUserAgent, stream); } /** diff --git a/src/java/org/apache/fop/apps/FOUserAgent.java b/src/java/org/apache/fop/apps/FOUserAgent.java index e7679259e..c1ae3d3af 100644 --- a/src/java/org/apache/fop/apps/FOUserAgent.java +++ b/src/java/org/apache/fop/apps/FOUserAgent.java @@ -19,6 +19,7 @@ package org.apache.fop.apps; // Java +import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -73,6 +74,9 @@ public class FOUserAgent { private Configuration userConfig = null; private Log log = LogFactory.getLog("FOP"); + /* Additional fo.ElementMapping subclasses set by user */ + private ArrayList additionalElementMappings = null; + /** Producer: Metadata element for the system/software that produces * the document. (Some renderers can store this in the document.) */ @@ -105,6 +109,25 @@ public class FOUserAgent { return inputHandler; } + /** + * Add the element mapping with the given class name. + * @param mappingClassName the class name representing the element mapping. + */ + public void addElementMapping(String mappingClassName) { + if (additionalElementMappings == null) { + additionalElementMappings = new ArrayList(); + } + additionalElementMappings.add(mappingClassName); + } + + /** + * Returns the ArrayList of user-added ElementMapping class names + * @return ArrayList of Strings holding ElementMapping names. + */ + public ArrayList getAdditionalElementMappings() { + return additionalElementMappings; + } + /** * Sets the producer of the document. * @param producer source of document diff --git a/src/java/org/apache/fop/fo/FOTreeBuilder.java b/src/java/org/apache/fop/fo/FOTreeBuilder.java index 0516016a4..c9f566c18 100644 --- a/src/java/org/apache/fop/fo/FOTreeBuilder.java +++ b/src/java/org/apache/fop/fo/FOTreeBuilder.java @@ -24,6 +24,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.Reader; +import java.util.ArrayList; import java.util.Enumeration; import java.util.Iterator; import java.util.List; @@ -88,17 +89,12 @@ public class FOTreeBuilder extends DefaultHandler { private Locator locator; /** - * Default constructor + * FOTreeBuilder constructor + * @param render type as defined in Constants class + * @param foUserAgent in effect for this process + * @param stream OutputStream to direct results */ - public FOTreeBuilder() { - setupDefaultMappings(); - } - - /** - * Creates the FOInputHandler object based on passed-in render type - * @param render type - */ - public void initialize(int renderType, FOUserAgent foUserAgent, + public FOTreeBuilder(int renderType, FOUserAgent foUserAgent, OutputStream stream) throws FOPException { if (renderType == Constants.RENDER_MIF) { foInputHandler = new MIFHandler(foUserAgent, stream); @@ -113,6 +109,18 @@ public class FOTreeBuilder extends DefaultHandler { foInputHandler = new AreaTreeHandler(foUserAgent, renderType, stream); } + + // Add standard element mappings + setupDefaultMappings(); + + // add additional ElementMappings defined within FOUserAgent + ArrayList addlEMs = foUserAgent.getAdditionalElementMappings(); + + if (addlEMs != null) { + for (int i = 0; i < addlEMs.size(); i++) { + addElementMapping((String) addlEMs.get(i)); + } + } } /** @@ -141,17 +149,6 @@ public class FOTreeBuilder extends DefaultHandler { } } - /** - * Add the given element mapping. - * An element mapping maps element names to Java classes. - * - * @param mapping the element mappingto add - */ - public void addElementMapping(ElementMapping mapping) { - this.fobjTable.put(mapping.getNamespaceURI(), mapping.getTable()); - this.namespaces.add(mapping.getNamespaceURI().intern()); - } - /** * Add the element mapping with the given class name. * @param mappingClassName the class name representing the element mapping. @@ -159,6 +156,7 @@ public class FOTreeBuilder extends DefaultHandler { */ public void addElementMapping(String mappingClassName) throws IllegalArgumentException { + try { ElementMapping mapping = (ElementMapping)Class.forName(mappingClassName).newInstance(); @@ -178,6 +176,11 @@ public class FOTreeBuilder extends DefaultHandler { } } + private void addElementMapping(ElementMapping mapping) { + this.fobjTable.put(mapping.getNamespaceURI(), mapping.getTable()); + this.namespaces.add(mapping.getNamespaceURI().intern()); + } + /** * SAX Handler for locator * @see org.xml.sax.ContentHandler#setDocumentLocator(Locator) -- 2.39.5