Ensure the consistency of the created Fop object


git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_API_Finalization@383973 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/examples/embedding/java/embedding/MultipleFO2PDF.java b/examples/embedding/java/embedding/MultipleFO2PDF.java
index 6d268d4..09dfb77 100644
--- a/examples/embedding/java/embedding/MultipleFO2PDF.java
+++ b/examples/embedding/java/embedding/MultipleFO2PDF.java
@@ -43,7 +43,6 @@
 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.
@@ -83,14 +82,6 @@
             // 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
@@ -99,7 +90,7 @@
             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);
diff --git a/src/java/org/apache/fop/apps/Fop.java b/src/java/org/apache/fop/apps/Fop.java
index 7066edc..f4e4c04 100644
--- a/src/java/org/apache/fop/apps/Fop.java
+++ b/src/java/org/apache/fop/apps/Fop.java
@@ -64,8 +64,9 @@
      * @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;
@@ -74,6 +75,8 @@
         }
         
         this.stream = stream;
+        
+        createDefaultHandler();
     }
 
     /**
@@ -81,23 +84,28 @@
      * 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();
     }
 
     /**
@@ -118,19 +126,28 @@
     }
 
     /**
-     * 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;
     }
diff --git a/src/java/org/apache/fop/apps/FopFactory.java b/src/java/org/apache/fop/apps/FopFactory.java
index 9d34ce6..e7849a8 100644
--- a/src/java/org/apache/fop/apps/FopFactory.java
+++ b/src/java/org/apache/fop/apps/FopFactory.java
@@ -133,8 +133,9 @@
      * 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());
     }
 
@@ -149,8 +150,9 @@
      * @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!");
         }
@@ -166,8 +168,9 @@
      * @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);
     }
 
@@ -184,8 +187,9 @@
      * @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!");
         }
@@ -199,8 +203,9 @@
      * 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"
diff --git a/src/java/org/apache/fop/cli/InputHandler.java b/src/java/org/apache/fop/cli/InputHandler.java
index 554f5d5..5fbea4e 100644
--- a/src/java/org/apache/fop/cli/InputHandler.java
+++ b/src/java/org/apache/fop/cli/InputHandler.java
@@ -85,10 +85,12 @@
      */
     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
diff --git a/src/java/org/apache/fop/render/RendererFactory.java b/src/java/org/apache/fop/render/RendererFactory.java
index c6ed0f6..c351344 100644
--- a/src/java/org/apache/fop/render/RendererFactory.java
+++ b/src/java/org/apache/fop/render/RendererFactory.java
@@ -1,5 +1,5 @@
 /*
- * 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.
@@ -248,7 +248,7 @@
                     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.