From 8a045dbc8d61f3ae7750715cd61c626fb48f775b Mon Sep 17 00:00:00 2001 From: Glen Mazza Date: Fri, 25 Jun 2004 23:35:00 +0000 Subject: [PATCH] 1.) Moved the CommandLineOptions' InputHandler object into FOUserAgent, allowing for a no-parameter constructor for AWTRenderer (like the other renderers). (Code is not yet ideal in AWTRenderer, but will do the task.) 2.) AWT renderer handling now more similar to the other renderers (simplifications in Driver, Fop). 3.) Driver.getRenderer() removed from API. (Renderer configuration now mostly done through FOUserAgent, and setup of renderer prior to calling setRenderer().) 4.) Validity checking added to region-after, -start, -end, and -before. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@197744 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/fop/apps/CommandLineOptions.java | 27 +++++++++------ src/java/org/apache/fop/apps/Driver.java | 11 ++---- src/java/org/apache/fop/apps/FOUserAgent.java | 21 +++++++++++- src/java/org/apache/fop/apps/Fop.java | 34 ++++++++----------- .../org/apache/fop/apps/XSLTInputHandler.java | 12 +++---- .../org/apache/fop/fo/pagination/Region.java | 13 ++++++- .../apache/fop/fo/pagination/RegionBody.java | 9 ----- .../apache/fop/render/awt/AWTRenderer.java | 17 +++------- .../apache/fop/servlet/FopPrintServlet.java | 9 ++--- .../org/apache/fop/BasicDriverTestCase.java | 11 ------ .../org/apache/fop/GenericFOPTestCase.java | 5 ++- 11 files changed, 80 insertions(+), 89 deletions(-) diff --git a/src/java/org/apache/fop/apps/CommandLineOptions.java b/src/java/org/apache/fop/apps/CommandLineOptions.java index 6a3adbfc2..2497131cb 100644 --- a/src/java/org/apache/fop/apps/CommandLineOptions.java +++ b/src/java/org/apache/fop/apps/CommandLineOptions.java @@ -114,6 +114,8 @@ public class CommandLineOptions { printUsage(); throw e; } + + foUserAgent.setInputHandler(createInputHandler()); } /** @@ -386,6 +388,11 @@ public class CommandLineOptions { throw new FOPException("No output file specified"); } + if ((outputmode == AWT_OUTPUT || outputmode == PRINT_OUTPUT) && outfile != null) { + throw new FOPException("Output file may not be specified " + + "for AWT or PRINT output"); + } + if (inputmode == XSLT_INPUT) { // check whether xml *and* xslt file have been set if (xmlfile == null) { @@ -466,18 +473,18 @@ public class CommandLineOptions { } /** - * Get the input handler. - * @return the input handler - * @throws FOPException if creating the InputHandler fails + * Create an InputHandler object based on command-line parameters + * @return a new InputHandler instance + * @throws IllegalStateException if invalid/missing parameters */ - public InputHandler getInputHandler() throws FOPException { + private InputHandler createInputHandler() throws IllegalArgumentException { switch (inputmode) { - case FO_INPUT: - return new FOFileHandler(fofile); - case XSLT_INPUT: - return new XSLTInputHandler(xmlfile, xsltfile, xsltParams); - default: - throw new FOPException("Invalid inputmode setting!"); + case FO_INPUT: + return new FOFileHandler(fofile); + case XSLT_INPUT: + return new XSLTInputHandler(xmlfile, xsltfile, xsltParams); + default: + throw new IllegalArgumentException("Error creating InputHandler object."); } } diff --git a/src/java/org/apache/fop/apps/Driver.java b/src/java/org/apache/fop/apps/Driver.java index 0dd53cff8..2a96d8039 100644 --- a/src/java/org/apache/fop/apps/Driver.java +++ b/src/java/org/apache/fop/apps/Driver.java @@ -310,7 +310,8 @@ public class Driver { setRenderer("org.apache.fop.render.pdf.PDFRenderer"); break; case RENDER_AWT: - throw new IllegalArgumentException("Use renderer form of setRenderer() for AWT"); + setRenderer("org.apache.fop.render.awt.AWTRenderer"); + break; case RENDER_PRINT: setRenderer("org.apache.fop.render.awt.AWTPrintRenderer"); break; @@ -355,14 +356,6 @@ public class Driver { this.renderer = renderer; } - /** - * Returns the currently active renderer. - * @return the renderer - */ - public Renderer getRenderer() { - return renderer; - } - /** * Set the class name of the Renderer to use as well as the * producer string for those renderers that can make use of it. diff --git a/src/java/org/apache/fop/apps/FOUserAgent.java b/src/java/org/apache/fop/apps/FOUserAgent.java index 15bc99628..b84d17bbc 100644 --- a/src/java/org/apache/fop/apps/FOUserAgent.java +++ b/src/java/org/apache/fop/apps/FOUserAgent.java @@ -58,7 +58,9 @@ public class FOUserAgent { private PDFEncryptionParams pdfEncryptionParams; private float px2mm = 0.35277777777777777778f; //72dpi (=25.4/dpi) private HashMap rendererOptions = new java.util.HashMap(); - + private InputHandler inputHandler = null; + + /** Producer: Metadata element for the system/software that produces * the document. (Some renderers can store this in the document.) */ @@ -74,6 +76,23 @@ public class FOUserAgent { */ protected Date creationDate = null; + /** + * Sets the InputHandler object for this process + * @param inputHandler holding input file name information + */ + public void setInputHandler(InputHandler inputHandler) { + this.inputHandler = inputHandler; + } + + /** + * Returns the apps.InputHandler object created during command-line + * processing + * @return InputHandler object + */ + public InputHandler getInputHandler() { + return inputHandler; + } + /** * Sets the producer of the document. * @param producer source of document diff --git a/src/java/org/apache/fop/apps/Fop.java b/src/java/org/apache/fop/apps/Fop.java index a7b8eb06e..7376c9bbe 100644 --- a/src/java/org/apache/fop/apps/Fop.java +++ b/src/java/org/apache/fop/apps/Fop.java @@ -37,35 +37,29 @@ public class Fop { */ public static void main(String[] args) { CommandLineOptions options = null; - InputHandler inputHandler = null; + FOUserAgent foUserAgent = null; BufferedOutputStream bos = null; try { Driver driver = new Driver(); options = new CommandLineOptions(args); - driver.setUserAgent(options.getFOUserAgent()); - inputHandler = options.getInputHandler(); + foUserAgent = options.getFOUserAgent(); + driver.setUserAgent(foUserAgent); + driver.setRenderer(options.getRenderer()); try { - if (options.getOutputMode() == CommandLineOptions.AWT_OUTPUT) { - driver.setRenderer(new AWTRenderer(inputHandler)); - } else { - driver.setRenderer(options.getRenderer()); - - if (options.getOutputFile() != null) { - bos = new BufferedOutputStream(new FileOutputStream( - options.getOutputFile())); - driver.setOutputStream(bos); - } - } - - driver.render(inputHandler); - } finally { - if (bos != null) { - bos.close(); + if (options.getOutputFile() != null) { + bos = new BufferedOutputStream(new FileOutputStream( + options.getOutputFile())); + driver.setOutputStream(bos); } - } + driver.render(foUserAgent.getInputHandler()); + } finally { + if (bos != null) { + bos.close(); + } + } // System.exit(0) called to close AWT/SVG-created threads, if any. // AWTRenderer closes with window shutdown, so exit() should not diff --git a/src/java/org/apache/fop/apps/XSLTInputHandler.java b/src/java/org/apache/fop/apps/XSLTInputHandler.java index 88771f6b3..47c7aa6d9 100644 --- a/src/java/org/apache/fop/apps/XSLTInputHandler.java +++ b/src/java/org/apache/fop/apps/XSLTInputHandler.java @@ -54,7 +54,7 @@ public class XSLTInputHandler extends InputHandler { * name, value, ...) for XSL stylesheet * @throws FOPException if initializing the Transformer fails */ - public XSLTInputHandler(File xmlfile, File xsltfile, Vector params) throws FOPException { + public XSLTInputHandler(File xmlfile, File xsltfile, Vector params) { this.xmlSource = new StreamSource(xmlfile); this.xsltSource = new StreamSource(xsltfile); try { @@ -71,9 +71,8 @@ public class XSLTInputHandler extends InputHandler { * @param xmlfile XML file * @param xsltfile XSLT file * @throws FOPException if initializing the Transformer fails - * @deprecated Use JAXP instead. */ - public XSLTInputHandler(File xmlfile, File xsltfile) throws FOPException { + public XSLTInputHandler(File xmlfile, File xsltfile) { this.xmlSource = new StreamSource(xmlfile); this.xsltSource = new StreamSource(xsltfile); try { @@ -89,9 +88,8 @@ public class XSLTInputHandler extends InputHandler { * @param xmlURL XML URL * @param xsltURL XSLT URL * @throws FOPException if initializing the Transformer fails - * @deprecated Use JAXP instead. */ - public XSLTInputHandler(String xmlURL, String xsltURL) throws FOPException { + public XSLTInputHandler(String xmlURL, String xsltURL) { this.xmlSource = new StreamSource(xmlURL); this.xsltSource = new StreamSource(xsltURL); } @@ -101,10 +99,8 @@ public class XSLTInputHandler extends InputHandler { * @param xmlSource XML InputSource * @param xsltSource XSLT InputSource * @throws FOPException if initializing the Transformer fails - * @deprecated Use JAXP instead. */ - public XSLTInputHandler(InputSource xmlSource, InputSource xsltSource) - throws FOPException { + public XSLTInputHandler(InputSource xmlSource, InputSource xsltSource) { this.xmlSource = new StreamSource(xmlSource.getByteStream(), xmlSource.getSystemId()); this.xsltSource = new StreamSource(xsltSource.getByteStream(), diff --git a/src/java/org/apache/fop/fo/pagination/Region.java b/src/java/org/apache/fop/fo/pagination/Region.java index 1540682a5..31ac2eb13 100644 --- a/src/java/org/apache/fop/fo/pagination/Region.java +++ b/src/java/org/apache/fop/fo/pagination/Region.java @@ -20,12 +20,15 @@ package org.apache.fop.fo.pagination; import java.awt.Rectangle; +// XML +import org.xml.sax.Attributes; +import org.xml.sax.Locator; + import org.apache.fop.apps.FOPException; import org.apache.fop.datatypes.FODimension; import org.apache.fop.fo.FONode; import org.apache.fop.fo.FOTreeVisitor; import org.apache.fop.fo.FObj; -import org.xml.sax.Attributes; /** * This is an abstract base class for pagination regions @@ -61,6 +64,14 @@ public abstract class Region extends FObj { regionId = id; } + /** + * @see org.apache.fop.fo.FONode#validateChildNode(Locator, String, String) + * XSL/FOP Content Model: empty + */ + protected void validateChildNode(Locator loc, String nsURI, String localName) { + invalidChildError(loc, nsURI, localName); + } + /** * @see org.apache.fop.fo.FObj#addProperties */ diff --git a/src/java/org/apache/fop/fo/pagination/RegionBody.java b/src/java/org/apache/fop/fo/pagination/RegionBody.java index 15ed0cc39..c0855abaa 100644 --- a/src/java/org/apache/fop/fo/pagination/RegionBody.java +++ b/src/java/org/apache/fop/fo/pagination/RegionBody.java @@ -23,7 +23,6 @@ import java.awt.Rectangle; // XML import org.xml.sax.Attributes; -import org.xml.sax.Locator; // FOP import org.apache.fop.datatypes.ColorType; @@ -50,14 +49,6 @@ public class RegionBody extends Region { super(parent, Region.BODY_CODE); } - /** - * @see org.apache.fop.fo.FONode#validateChildNode(Locator, String, String) - * XSL/FOP Content Model: empty - */ - protected void validateChildNode(Locator loc, String nsURI, String localName) { - invalidChildError(loc, nsURI, localName); - } - /** * @see org.apache.fop.fo.pagination.Region#getViewportRectangle(FODimension) */ diff --git a/src/java/org/apache/fop/render/awt/AWTRenderer.java b/src/java/org/apache/fop/render/awt/AWTRenderer.java index f4f934528..5b174646c 100644 --- a/src/java/org/apache/fop/render/awt/AWTRenderer.java +++ b/src/java/org/apache/fop/render/awt/AWTRenderer.java @@ -47,6 +47,7 @@ import java.util.Vector; import org.apache.fop.fonts.FontInfo; import org.apache.fop.apps.FOPException; +import org.apache.fop.apps.FOUserAgent; import org.apache.fop.apps.InputHandler; import org.apache.fop.area.Area; import org.apache.fop.area.Page; @@ -80,12 +81,6 @@ public class AWTRenderer extends AbstractRenderer implements Printable, Pageable /** Font configuration */ protected FontInfo fontInfo; - /** - The InputHandler associated with this Renderer. - Sent to the PreviewDialog for document reloading. - */ - private InputHandler inputHandler; - /** * The resource bundle used for AWT messages. */ @@ -102,15 +97,13 @@ public class AWTRenderer extends AbstractRenderer implements Printable, Pageable */ protected PreviewDialog frame; - public AWTRenderer(InputHandler handler) { - inputHandler = handler; + public AWTRenderer() { translator = new Translator(); - createPreviewDialog(inputHandler); } - public AWTRenderer() { - translator = new Translator(); - createPreviewDialog(null); + public void setUserAgent(FOUserAgent foUserAgent) { + super.setUserAgent(foUserAgent); + createPreviewDialog(foUserAgent.getInputHandler()); } /** diff --git a/src/java/org/apache/fop/servlet/FopPrintServlet.java b/src/java/org/apache/fop/servlet/FopPrintServlet.java index f873812aa..687019042 100644 --- a/src/java/org/apache/fop/servlet/FopPrintServlet.java +++ b/src/java/org/apache/fop/servlet/FopPrintServlet.java @@ -32,7 +32,6 @@ import org.apache.commons.logging.impl.SimpleLog; import org.apache.commons.logging.Log; import org.apache.fop.apps.Driver; import org.apache.fop.apps.XSLTInputHandler; -import org.apache.fop.render.awt.AWTPrintRenderer; import org.xml.sax.InputSource; /** @@ -132,9 +131,7 @@ public class FopPrintServlet extends HttpServlet { HttpServletResponse response) throws ServletException { try { Driver driver = new Driver(foFile, null); - AWTPrintRenderer renderer = new AWTPrintRenderer(); - - driver.setRenderer(renderer); + driver.setRenderer(Driver.RENDER_PRINT); driver.run(); reportOK (response); @@ -153,9 +150,7 @@ public class FopPrintServlet extends HttpServlet { HttpServletResponse response) throws ServletException { try { Driver driver = new Driver(); - AWTPrintRenderer renderer = new AWTPrintRenderer(); - - driver.setRenderer(renderer); + driver.setRenderer(Driver.RENDER_PRINT); driver.render(input.getParser(), input.getInputSource()); reportOK (response); diff --git a/test/java/org/apache/fop/BasicDriverTestCase.java b/test/java/org/apache/fop/BasicDriverTestCase.java index ab158a396..c512a50ec 100644 --- a/test/java/org/apache/fop/BasicDriverTestCase.java +++ b/test/java/org/apache/fop/BasicDriverTestCase.java @@ -33,7 +33,6 @@ import javax.xml.transform.stream.StreamSource; import org.xml.sax.InputSource; -import org.apache.commons.logging.impl.NoOpLog; import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.fop.apps.Driver; import org.apache.fop.apps.InputHandler; @@ -47,8 +46,6 @@ import org.w3c.dom.Document; */ public class BasicDriverTestCase extends AbstractFOPTestCase { - private NoOpLog logger = new NoOpLog(); - /** * @see junit.framework.TestCase#TestCase(String) */ @@ -67,7 +64,6 @@ public class BasicDriverTestCase extends AbstractFOPTestCase { new InputSource(foFile.toURL().toExternalForm()), baout); - driver.setLogger(this.logger); driver.setRenderer(Driver.RENDER_PDF); driver.run(); assertTrue("Generated PDF has zero length", baout.size() > 0); @@ -82,7 +78,6 @@ public class BasicDriverTestCase extends AbstractFOPTestCase { ByteArrayOutputStream baout = new ByteArrayOutputStream(); Driver driver = new Driver(); - driver.setLogger(this.logger); driver.setInputSource(new InputSource(foFile.toURL().toExternalForm())); driver.setOutputStream(baout); driver.setRenderer(Driver.RENDER_PDF); @@ -109,7 +104,6 @@ public class BasicDriverTestCase extends AbstractFOPTestCase { ByteArrayOutputStream baout = new ByteArrayOutputStream(); Driver driver = new Driver(); - driver.setLogger(this.logger); driver.setOutputStream(baout); driver.setRenderer(Driver.RENDER_PDF); driver.render(loadDocument(foFile)); @@ -125,7 +119,6 @@ public class BasicDriverTestCase extends AbstractFOPTestCase { ByteArrayOutputStream baout = new ByteArrayOutputStream(); Driver driver = new Driver(); - driver.setLogger(this.logger); driver.setOutputStream(baout); driver.setRenderer(Driver.RENDER_PDF); SAXParserFactory factory = SAXParserFactory.newInstance(); @@ -145,7 +138,6 @@ public class BasicDriverTestCase extends AbstractFOPTestCase { File foFile = new File(getBaseDir(), "test/xml/bugtests/block.fo"); ByteArrayOutputStream baout = new ByteArrayOutputStream(); Driver driver = new Driver(); - driver.setLogger(this.logger); driver.setOutputStream(baout); driver.setRenderer(Driver.RENDER_PDF); @@ -166,7 +158,6 @@ public class BasicDriverTestCase extends AbstractFOPTestCase { File foFile = new File(getBaseDir(), "test/xml/bugtests/block.fo"); ByteArrayOutputStream baout = new ByteArrayOutputStream(); Driver driver = new Driver(); - driver.setLogger(this.logger); driver.setOutputStream(baout); driver.setRenderer(Driver.RENDER_PS); @@ -187,7 +178,6 @@ public class BasicDriverTestCase extends AbstractFOPTestCase { File foFile = new File(getBaseDir(), "test/xml/bugtests/block.fo"); ByteArrayOutputStream baout = new ByteArrayOutputStream(); Driver driver = new Driver(); - driver.setLogger(this.logger); driver.setOutputStream(baout); driver.setRenderer(Driver.RENDER_RTF); @@ -209,7 +199,6 @@ public class BasicDriverTestCase extends AbstractFOPTestCase { File xsltFile = new File(getBaseDir(), "test/xsl/doc.xsl"); ByteArrayOutputStream baout = new ByteArrayOutputStream(); Driver driver = new Driver(); - driver.setLogger(this.logger); driver.setOutputStream(baout); driver.setRenderer(Driver.RENDER_PDF); diff --git a/test/java/org/apache/fop/GenericFOPTestCase.java b/test/java/org/apache/fop/GenericFOPTestCase.java index 568855849..370b012a9 100644 --- a/test/java/org/apache/fop/GenericFOPTestCase.java +++ b/test/java/org/apache/fop/GenericFOPTestCase.java @@ -31,6 +31,7 @@ import junit.framework.TestCase; import junit.framework.TestSuite; import org.apache.fop.apps.Driver; +import org.apache.fop.apps.FOUserAgent; import org.apache.fop.render.pdf.PDFRenderer; import org.apache.fop.util.DigestFilter; import org.xml.sax.InputSource; @@ -112,7 +113,9 @@ public final class GenericFOPTestCase extends TestCase { private void renderPDF(String fo, String digestIn, String digestOut) throws Exception { PDFRenderer renderer = new PDFRenderer(); - renderer.setCreationDate(new Date(10000)); + FOUserAgent foUserAgent = new FOUserAgent(); + foUserAgent.setCreationDate(new Date(10000)); + renderer.setUserAgent(foUserAgent); MessageDigest outDigest = MessageDigest.getInstance("MD5"); ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); DigestOutputStream out = -- 2.39.5