import org.apache.fop.fo.FOTreeBuilder;
import org.apache.fop.fo.FOInputHandler;
import org.apache.fop.fo.FOTreeHandler;
-import org.apache.fop.render.Renderer;
import org.apache.fop.render.awt.AWTRenderer;
import org.apache.fop.render.mif.MIFHandler;
import org.apache.fop.render.rtf.RTFHandler;
* driver.setRenderer(RENDER_PDF);
* driver.run();
* </PRE>
- * If neccessary, calling classes can call into the lower level
+ * If necessary, calling classes can call into the lower level
* methods to setup and
- * render. Methods can be called to set the
+ * 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 the Renderer and
+ * 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 Renderer and ElementMapping(s).
+ * enables runtime determination of ElementMapping(s).
* <P>
* Once the Driver is set up, the render method
* is called. Depending on whether DOM or SAX is being used, the
*
* <PRE>
* Driver driver = new Driver();
- * driver.setRenderer(new org.apache.fop.render.awt.AWTRenderer(translator));
+ * driver.setRenderer(RENDER_AWT);
* driver.render(parser, fileInputSource(args[0]));
* </PRE>
*/
*/
private int rendererType = NOT_SET;
- /**
- * the renderer to use to output the area tree
- */
- private Renderer renderer;
-
/**
* the SAX ContentHandler
*/
/**
* The system resources that FOP will use
*/
- private FOUserAgent userAgent = null;
+ private FOUserAgent foUserAgent = null;
/**
* Main constructor for the Driver class.
*/
public Driver(AWTRenderer renderer) {
this();
- setRenderer(renderer);
+ rendererType = RENDER_AWT;
+ foUserAgent = renderer.getUserAgent();
}
/**
treeBuilder = new FOTreeBuilder();
}
+ /**
+ * Resets the Driver so it can be reused. Property and element
+ * mappings are reset to defaults.
+ * The output stream is cleared. The renderer is cleared.
+ */
+ public synchronized void reset() {
+ source = null;
+ stream = null;
+ reader = null;
+ if (treeBuilder != null) {
+ treeBuilder.reset();
+ }
+ }
+
/**
* Optionally sets the FOUserAgent instance for FOP to use. The Driver
* class sets up its own FOUserAgent if none is set through this method.
* @param agent FOUserAgent to use
*/
- public void setUserAgent(FOUserAgent agent) {
- userAgent = agent;
- if (renderer != null) {
- renderer.setUserAgent(userAgent);
+ 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 (userAgent == null) {
- userAgent = new FOUserAgent();
- }
- return userAgent;
- }
-
- /**
- * Resets the Driver so it can be reused. Property and element
- * mappings are reset to defaults.
- * The output stream is cleared. The renderer is cleared.
- */
- public synchronized void reset() {
- source = null;
- stream = null;
- reader = null;
- if (treeBuilder != null) {
- treeBuilder.reset();
+ if (foUserAgent == null) {
+ foUserAgent = new FOUserAgent();
}
+ return foUserAgent;
}
/**
}
/**
- * Method to set the rendering type to use. Must be one of
+ * Method to set the rendering type desired. Must be one of
* <ul>
- * <li>RENDER_PDF</li>
- * <li>RENDER_AWT</li>
- * <li>RENDER_PRINT</li>
- * <li>RENDER_MIF</li>
- * <li>RENDER_XML</li>
- * <li>RENDER_PCL</li>
- * <li>RENDER_PS</li>
- * <li>RENDER_TXT</li>
- * <li>RENDER_SVG</li>
- * <li>RENDER_RTF</li>
+ * <li>Driver.RENDER_PDF</li>
+ * <li>Driver.RENDER_AWT</li>
+ * <li>Driver.RENDER_PRINT</li>
+ * <li>Driver.RENDER_MIF</li>
+ * <li>Driver.RENDER_XML</li>
+ * <li>Driver.RENDER_PCL</li>
+ * <li>Driver.RENDER_PS</li>
+ * <li>Driver.RENDER_TXT</li>
+ * <li>Driver.RENDER_SVG</li>
+ * <li>Driver.RENDER_RTF</li>
* </ul>
- * @param renderer the type of renderer to use
- * @throws IllegalArgumentException if an unsupported renderer type was required.
+ * @param renderType the type of renderer to use
+ * @throws IllegalArgumentException if an unsupported renderer type was requested.
*/
- public void setRenderer(int renderer) throws IllegalArgumentException {
- rendererType = renderer;
- switch (renderer) {
- case RENDER_PDF:
- setRenderer(new org.apache.fop.render.pdf.PDFRenderer());
- break;
- case RENDER_AWT:
- setRenderer(new org.apache.fop.render.awt.AWTRenderer());
- break;
- case RENDER_PRINT:
- setRenderer(new org.apache.fop.render.awt.AWTPrintRenderer());
- break;
- case RENDER_PCL:
- setRenderer(new org.apache.fop.render.pcl.PCLRenderer());
- break;
- case RENDER_PS:
- setRenderer(new org.apache.fop.render.ps.PSRenderer());
- break;
- case RENDER_TXT:
- setRenderer(new org.apache.fop.render.txt.TXTRenderer());
- break;
- case RENDER_MIF:
- //foInputHandler will be set later
- break;
- case RENDER_XML:
- setRenderer(new org.apache.fop.render.xml.XMLRenderer());
- break;
- case RENDER_SVG:
- setRenderer(new org.apache.fop.render.svg.SVGRenderer());
- break;
- case RENDER_RTF:
- //foInputHandler will be set later
- break;
- default:
+ public void setRenderer(int renderType) throws IllegalArgumentException {
+ if (renderType < RENDER_MIN_CONST || renderType > RENDER_MAX_CONST) {
rendererType = NOT_SET;
- throw new IllegalArgumentException("Unknown renderer type " + renderer);
+ throw new IllegalArgumentException(
+ "Invalid renderer ID#" + renderType);
}
- }
- /**
- * Set the Renderer to use.
- * @param renderer the renderer instance to use
- */
- private void setRenderer(Renderer renderer) {
- renderer.setUserAgent(getUserAgent());
- this.renderer = renderer;
+ this.rendererType = renderType;
}
/**
// TODO: - do this stuff in a better way
// PIJ: I guess the structure handler should be created by the renderer.
if (rendererType == RENDER_MIF) {
- foInputHandler = new MIFHandler(userAgent, stream);
+ foInputHandler = new MIFHandler(foUserAgent, stream);
} else if (rendererType == RENDER_RTF) {
- foInputHandler = new RTFHandler(userAgent, stream);
+ foInputHandler = new RTFHandler(foUserAgent, stream);
} else {
- if (renderer == null) {
+ if (rendererType == NOT_SET) {
throw new IllegalStateException(
- "Renderer not set when using standard foInputHandler");
+ "Renderer must be set using setRenderer(int renderType)");
}
- foInputHandler = new FOTreeHandler(userAgent, renderer, stream, true);
+ foInputHandler = new FOTreeHandler(foUserAgent, rendererType,
+ stream, true);
}
treeBuilder.setFOInputHandler(foInputHandler);
public synchronized void render(InputHandler inputHandler)
throws FOPException {
XMLReader parser = inputHandler.getParser();
- userAgent.setBaseURL(inputHandler.getBaseURL());
+ foUserAgent.setBaseURL(inputHandler.getBaseURL());
render(parser, inputHandler.getInputSource());
}
/**
* Runs the formatting and renderering process using the previously set
* parser, input source, renderer and output stream.
- * If the renderer was not set, default to PDF.
* If no parser was set, and the input source is not a dom document,
* get a default SAX parser.
* @throws IOException in case of IO errors.
initialize();
}
- if (renderer == null && rendererType != RENDER_RTF
- && rendererType != RENDER_MIF) {
- setRenderer(RENDER_PDF);
+ if (rendererType == NOT_SET) {
+ rendererType = RENDER_PDF;
}
if (source == null) {
* @param store if true then use the store pages model and keep the
* area tree in memory
*/
- public FOTreeHandler(FOUserAgent userAgent, Renderer renderer,
+ public FOTreeHandler(FOUserAgent userAgent, int renderType,
OutputStream stream, boolean store) throws FOPException {
super(userAgent);
- this.renderer = renderer;
+
+ if (foUserAgent.getRendererOverride() != null) {
+ renderer = foUserAgent.getRendererOverride();
+ } else {
+ renderer = createRenderer(renderType);
+ renderer.setUserAgent(foUserAgent);
+ }
+
areaTree = new AreaTree(renderer);
try {
}
}
+ /**
+ * 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.