import org.apache.fop.apps.FormattingResults;
import org.apache.fop.apps.MimeConstants;
import org.apache.fop.apps.PageSequenceResults;
-import org.xml.sax.helpers.DefaultHandler;
/**
* This class demonstrates the conversion of multiple FO files to PDF using FOP.
// Construct fop with desired output format and output stream
fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
- // This will also check the consistency of your setup
- DefaultHandler handler;
- try {
- handler = fop.getDefaultHandler();
- } catch (IllegalStateException e) {
- throw new FOPException(e);
- }
-
// Setup JAXP using identity transformer
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(); // identity transformer
Source src = new StreamSource(fo);
// Resulting SAX events (the generated FO) must be piped through to FOP
- Result res = new SAXResult(handler);
+ Result res = new SAXResult(fop.getDefaultHandler());
// Start XSLT transformation and FOP processing
transformer.transform(src, res);
* @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
* @param ua FOUserAgent object
* @param stream the output stream
+ * @throws FOPException if setting up the DefaultHandler fails
*/
- public Fop(String outputFormat, FOUserAgent ua, OutputStream stream) {
+ public Fop(String outputFormat, FOUserAgent ua, OutputStream stream) throws FOPException {
this.outputFormat = outputFormat;
foUserAgent = ua;
}
this.stream = stream;
+
+ createDefaultHandler();
}
/**
* output format (ex. "application/pdf" for PDF).
* @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
* @param ua FOUserAgent object
+ * @throws FOPException if setting up the DefaultHandler fails
*/
- public Fop(String outputFormat, FOUserAgent ua) {
+ public Fop(String outputFormat, FOUserAgent ua) throws FOPException {
this.outputFormat = outputFormat;
foUserAgent = ua;
if (foUserAgent == null) {
foUserAgent = new FOUserAgent();
}
+
+ createDefaultHandler();
}
/**
* Constructor for FOP with a default FOUserAgent. It uses MIME types to select the
* output format (ex. "application/pdf" for PDF).
* @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
+ * @deprecated Use a constructor with an FOUserAgent instead!
*/
public Fop(String outputFormat) {
- this(outputFormat, null);
+ this.outputFormat = outputFormat;
+ foUserAgent = new FOUserAgent();
}
/**
}
/**
- * Returns a DefaultHandler object used to generate the document.
+ * Creates a DefaultHandler object used to generate the document.
* Note this object implements the ContentHandler interface.
* For processing with a Transformer object, this DefaultHandler object
* can be used in the SAXResult constructor.
* Alternatively, for processing with a SAXParser, this object can be
* used as the DefaultHandler argument to its parse() methods.
*
- * @return a SAX DefaultHandler for handling the SAX events.
+ * @throws FOPException if setting up the DefaultHandler fails
+ */
+ private void createDefaultHandler() throws FOPException {
+ this.foTreeBuilder = new FOTreeBuilder(outputFormat, foUserAgent, stream);
+ }
+
+ /**
+ * Returns the DefaultHandler object used to generate the document.
+ * Checking for null and the exception is only for the deprecated constructor.
+ * @return the SAX DefaultHandler for handling the SAX events.
* @throws FOPException if setting up the DefaultHandler fails
*/
public DefaultHandler getDefaultHandler() throws FOPException {
if (foTreeBuilder == null) {
- this.foTreeBuilder = new FOTreeBuilder(outputFormat, foUserAgent, stream);
+ createDefaultHandler();
}
return this.foTreeBuilder;
}
* use the constants defined in {@link MimeConstants}.
* @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
* @return the new Fop instance
+ * @throws FOPException when the constructor fails
*/
- public Fop newFop(String outputFormat) {
+ public Fop newFop(String outputFormat) throws FOPException {
return new Fop(outputFormat, newFOUserAgent());
}
* @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
* @param userAgent the user agent that will be used to control the rendering run
* @return the new Fop instance
+ * @throws FOPException when the constructor fails
*/
- public Fop newFop(String outputFormat, FOUserAgent userAgent) {
+ public Fop newFop(String outputFormat, FOUserAgent userAgent) throws FOPException {
if (userAgent == null) {
throw new NullPointerException("The userAgent parameter must not be null!");
}
* @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
* @param stream the output stream
* @return the new Fop instance
+ * @throws FOPException when the constructor fails
*/
- public Fop newFop(String outputFormat, OutputStream stream) {
+ public Fop newFop(String outputFormat, OutputStream stream) throws FOPException {
return new Fop(outputFormat, newFOUserAgent(), stream);
}
* @param userAgent the user agent that will be used to control the rendering run
* @param stream the output stream
* @return the new Fop instance
+ * @throws FOPException when the constructor fails
*/
- public Fop newFop(String outputFormat, FOUserAgent userAgent, OutputStream stream) {
+ public Fop newFop(String outputFormat, FOUserAgent userAgent, OutputStream stream) throws FOPException {
if (userAgent == null) {
throw new NullPointerException("The userAgent parameter must not be null!");
}
* instance instead of the default ones created internally by FOP.
* @param userAgent the user agent that will be used to control the rendering run
* @return the new Fop instance
+ * @throws FOPException when the constructor fails
*/
- public Fop newFop(FOUserAgent userAgent) {
+ public Fop newFop(FOUserAgent userAgent) throws FOPException {
if (userAgent.getRendererOverride() == null
&& userAgent.getFOEventHandlerOverride() == null) {
throw new IllegalStateException("Either the overriding renderer or the overriding"
*/
public void renderTo(FOUserAgent userAgent, String outputFormat, OutputStream out)
throws FOPException {
-
- Fop fop = new Fop(outputFormat, userAgent);
+
+ Fop fop;
if (out != null) {
- fop.setOutputStream(out);
+ fop = new Fop(outputFormat, userAgent, out);
+ } else {
+ fop = new Fop(outputFormat, userAgent);
}
// if base URL was not explicitly set in FOUserAgent, obtain here
/*
- * Copyright 2004-2005 The Apache Software Foundation.
+ * Copyright 2004-2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
if (out == null
&& userAgent.getRendererOverride() == null
&& rendMaker.needsOutputStream()) {
- throw new IllegalStateException(
+ throw new FOPException(
"OutputStream has not been set");
}
//Found a Renderer so we need to construct an AreaTreeHandler.