// 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;
* 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).
* <P>
* Once the Driver is set up, the render method
* is called. The invocation of the method is
*/
public class Driver implements Constants {
- /**
- * the FO tree builder
- */
- private FOTreeBuilder treeBuilder;
-
/**
* the render type code given by setRender
*/
* Main constructor for the Driver class.
*/
public Driver() {
+ foUserAgent = new FOUserAgent();
stream = null;
}
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.
*/
public synchronized void reset() {
stream = null;
- if (treeBuilder != null) {
- treeBuilder.reset();
- }
}
/**
* @param agent FOUserAgent to use
*/
public void setUserAgent(FOUserAgent agent) throws FOPException {
- if (foUserAgent != null) {
- throw new IllegalStateException("FOUserAgent " +
- "instance already set.");
- }
foUserAgent = agent;
}
* @return the user agent
*/
public FOUserAgent getUserAgent() {
- if (foUserAgent == null) {
- foUserAgent = new FOUserAgent();
- }
return foUserAgent;
}
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
* @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);
}
/**
package org.apache.fop.apps;
// Java
+import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
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.)
*/
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
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;
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);
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));
+ }
+ }
}
/**
}
}
- /**
- * 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.
*/
public void addElementMapping(String mappingClassName)
throws IllegalArgumentException {
+
try {
ElementMapping mapping =
(ElementMapping)Class.forName(mappingClassName).newInstance();
}
}
+ 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)