/**
* Primary class that drives overall FOP process.
* <P>
- * The simplest way to use this is to instantiate it with the
- * InputSource and OutputStream, then set the renderer desired, and
- * calling run();
+ * JAXP is the standard method of embedding FOP in Java programs.
+ * Please check our embedding page (http://xml.apache.org/fop/embedding.html)
+ * for samples (these are also available within the distribution in
+ * FOP_DIR\examples\embedding)
* <P>
- * Here is an example use of Driver which outputs PDF:
- *
- * <PRE>
- * Driver driver = new Driver(new InputSource (args[0]),
- * new FileOutputStream(args[1]));
- * driver.setRenderer(RENDER_PDF);
- * driver.run();
- * </PRE>
* If necessary, calling classes can call into the lower level
* methods to setup and
* render. Methods within FOUserAgent can be called to set the
* is called. The invocation of the method is
* render(Parser, InputSource).
* <P>
- * A third possibility may be used to build the FO Tree, namely
- * calling getContentHandler() and firing the SAX events yourself.
- * This will work either for DOM Tree input or SAX. See the embed
- * examples on the FOP web page for more details or in the
- * examples\embedding directory of the main distribution for more details.
- * <P>
- * Once the FO Tree is built, the format() and render() methods may be
- * called in that order.
- * <P>
* Here is an example use of Driver which outputs to AWT:
*
* <PRE>
*/
private int renderType = NOT_SET;
- /**
- * the source of the FO file
- */
- private InputSource source;
-
/**
* the stream to use to output the results of the renderer
*/
* @param source InputSource to take the XSL-FO input from
* @param stream Target output stream
*/
- public Driver(InputSource source, OutputStream stream) {
+ public Driver(OutputStream stream) {
this();
- this.source = source;
this.stream = stream;
}
* The output stream is cleared. The renderer is cleared.
*/
public synchronized void reset() {
- source = null;
stream = null;
if (treeBuilder != null) {
treeBuilder.reset();
}
}
- /**
- * Set the source for the FO document. This can be a normal SAX
- * InputSource, or an DocumentInputSource containing a DOM document.
- * @see DocumentInputSource
- */
- public void setInputSource(InputSource source) {
- this.source = source;
- }
-
/**
* Method to set the rendering type desired. Must be one of
* <ul>
throw new FOPException(e);
}
}
-
- /**
- * Runs the formatting and rendering process using the previously set
- * parser, input source, renderer and output stream.
- * If no parser was set, get a default SAX parser.
- * @throws IOException in case of IO errors.
- * @throws FOPException if anything else goes wrong.
- */
- public synchronized void run() throws IOException, FOPException {
- if (!isInitialized()) {
- initialize();
- }
-
- if (renderType == NOT_SET) {
- renderType = RENDER_PDF;
- }
-
- if (source == null) {
- throw new FOPException("InputSource is not set.");
- }
-
- render(FOFileHandler.createParser(), source);
- }
}
if (foParam != null) {
InputStream file = new java.io.FileInputStream(foParam);
- renderFO(new InputSource(file), response);
+ renderFO(file, response);
} else if ((xmlParam != null) && (xsltParam != null)) {
renderXML(new File(xmlParam), new File(xsltParam), response);
} else {
* @param response Response to write to
* @throws ServletException In case of a problem
*/
- public void renderFO(InputSource foFile,
+ public void renderFO(InputStream foFile,
HttpServletResponse response) throws ServletException {
try {
- Driver driver = new Driver(foFile, null);
+ Driver driver = new Driver();
driver.setRenderer(Driver.RENDER_PRINT);
- driver.run();
+ // Setup JAXP
+ TransformerFactory factory = TransformerFactory.newInstance();
+ Transformer transformer = factory.newTransformer(); //identity transformer
+
+ // Setup input for XSLT transformation
+ Source src = new StreamSource(foFile);
+
+ // Resulting SAX events (the generated FO) must be piped through to FOP
+ Result res = new SAXResult(driver.getContentHandler());
+
+ // Start XSLT transformation and FOP processing
+ transformer.transform(src, res);
+
reportOK (response);
} catch (Exception ex) {
throw new ServletException(ex);
super(name);
}
- /**
- * Tests Driver with its special constructor for FO-->PDF conversion.
- * @throws Exception if anything fails
- */
- public void testFO2PDFWithConstructorSetup() throws Exception {
- File foFile = new File(getBaseDir(), "test/xml/bugtests/block.fo");
- ByteArrayOutputStream baout = new ByteArrayOutputStream();
- Driver driver = new Driver(
- new InputSource(foFile.toURL().toExternalForm()),
- baout);
-
- driver.setRenderer(Driver.RENDER_PDF);
- driver.run();
- assertTrue("Generated PDF has zero length", baout.size() > 0);
- }
-
- /**
- * Tests Driver with InputSource and OutputStream.
- * @throws Exception if anything fails
- */
- public void testFO2PDFWithInputSource() throws Exception {
- File foFile = new File(getBaseDir(), "test/xml/bugtests/block.fo");
- ByteArrayOutputStream baout = new ByteArrayOutputStream();
- Driver driver = new Driver();
-
- driver.setInputSource(new InputSource(foFile.toURL().toExternalForm()));
- driver.setOutputStream(baout);
- driver.setRenderer(Driver.RENDER_PDF);
- driver.run();
- assertTrue("Generated PDF has zero length", baout.size() > 0);
- }
-
private Document loadDocument(File foFile)
throws TransformerException {
TransformerFactory factory = TransformerFactory.newInstance();