From d918e59ad096f78692e1177646e719110762aea9 Mon Sep 17 00:00:00 2001 From: Glen Mazza Date: Sun, 25 Jul 2004 17:04:44 +0000 Subject: [PATCH] Took advantage of the Transformer similarities between FO input and (XSL, XSLT) input to combine the two into InputHandler. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@197840 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/fop/apps/CommandLineOptions.java | 4 +- .../org/apache/fop/apps/FOFileHandler.java | 81 --------------- .../org/apache/fop/apps/InputHandler.java | 93 +++++++++++++++-- .../org/apache/fop/apps/XSLTInputHandler.java | 99 ------------------- src/java/org/apache/fop/fo/FObj.java | 2 +- .../org/apache/fop/tools/TestConverter.java | 10 +- .../org/apache/fop/tools/anttasks/Fop.java | 3 +- .../org/apache/fop/BasicDriverTestCase.java | 3 +- 8 files changed, 93 insertions(+), 202 deletions(-) delete mode 100644 src/java/org/apache/fop/apps/FOFileHandler.java delete mode 100644 src/java/org/apache/fop/apps/XSLTInputHandler.java diff --git a/src/java/org/apache/fop/apps/CommandLineOptions.java b/src/java/org/apache/fop/apps/CommandLineOptions.java index e26e767be..d2f4cdc57 100644 --- a/src/java/org/apache/fop/apps/CommandLineOptions.java +++ b/src/java/org/apache/fop/apps/CommandLineOptions.java @@ -483,9 +483,9 @@ public class CommandLineOptions implements Constants { private InputHandler createInputHandler() throws IllegalArgumentException { switch (inputmode) { case FO_INPUT: - return new FOFileHandler(fofile); + return new InputHandler(fofile); case XSLT_INPUT: - return new XSLTInputHandler(xmlfile, xsltfile, xsltParams); + return new InputHandler(xmlfile, xsltfile, xsltParams); default: throw new IllegalArgumentException("Error creating InputHandler object."); } diff --git a/src/java/org/apache/fop/apps/FOFileHandler.java b/src/java/org/apache/fop/apps/FOFileHandler.java deleted file mode 100644 index b159e5086..000000000 --- a/src/java/org/apache/fop/apps/FOFileHandler.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 1999-2004 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* $Id$ */ - -package org.apache.fop.apps; - -// Java -import java.io.File; - -// JAXP -import javax.xml.transform.Result; -import javax.xml.transform.Source; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.sax.SAXResult; -import javax.xml.transform.stream.StreamSource; - -// Imported SAX classes -import org.xml.sax.InputSource; - -/** - * Manages input if it is an XSL-FO file. - */ -public class FOFileHandler extends InputHandler { - private StreamSource fofile = null; - - /** - * Create a FOFileHandler for a file. - * @param fofile the file to read the FO document. - */ - public FOFileHandler(File fofile) { - this.fofile = new StreamSource(fofile); - - try { - baseURL = - new File(fofile.getAbsolutePath()).getParentFile().toURL().toExternalForm(); - } catch (Exception e) { - baseURL = ""; - } - } - - /** - * @see org.apache.fop.apps.InputHandler#render(Fop) - */ - public void render(Fop fop) throws FOPException { - - // temporary until baseURL removed from inputHandler objects - if (fop.getUserAgent().getBaseURL() == null) { - fop.getUserAgent().setBaseURL(getBaseURL()); - } - - try { - // Setup JAXP using identity transformer (no stylesheet here) - TransformerFactory factory = TransformerFactory.newInstance(); - Transformer transformer = factory.newTransformer(); - - // Resulting SAX events (the generated FO) must be piped through to FOP - Result res = new SAXResult(fop.getDefaultHandler()); - - // Start XSLT transformation and FOP processing - transformer.transform(fofile, res); - - } catch (Exception e) { - throw new FOPException(e); - } - } -} diff --git a/src/java/org/apache/fop/apps/InputHandler.java b/src/java/org/apache/fop/apps/InputHandler.java index e094168ed..7af24101d 100644 --- a/src/java/org/apache/fop/apps/InputHandler.java +++ b/src/java/org/apache/fop/apps/InputHandler.java @@ -18,19 +18,47 @@ package org.apache.fop.apps; +// Imported java.io classes +import java.io.File; +import java.util.Vector; + +// Imported TraX classes +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.sax.SAXResult; +import javax.xml.transform.stream.StreamSource; + /** - * Abstract super class for input handlers. + * Class for handling files input from command line + * either with XML and XSLT files (and optionally xsl + * parameters) or FO File input alone */ -public abstract class InputHandler { - - protected String baseURL = null; +public class InputHandler { + private File sourcefile = null; // either FO or XML/XSLT usage + private File stylesheet = null; // for XML/XSLT usage + private Vector xsltParams = null; // for XML/XSLT usage /** - * Get the base URL associated with this input source - * @return the input source + * Constructor for XML->XSLT->FO input + * @param xmlfile XML file + * @param xsltfile XSLT file + * @param params Vector of command-line parameters (name, value, + * name, value, ...) for XSL stylesheet, null if none + */ + public InputHandler(File xmlfile, File xsltfile, Vector params) { + sourcefile = xmlfile; + stylesheet = xsltfile; + xsltParams = params; + } + + /** + * Constructor for FO input + * @param fofile the file to read the FO document. */ - public String getBaseURL() { - return baseURL; + public InputHandler(File fofile) { + sourcefile = fofile; } /** @@ -38,6 +66,53 @@ public abstract class InputHandler { * @param fop -- Fop object * @throws FOPException in case of an error during processing */ - public void render(Fop fop) throws FOPException {} + public void render(Fop fop) throws FOPException { + // if base URL was not explicitly set in FOUserAgent, obtain here + if (fop.getUserAgent().getBaseURL() == null) { + String baseURL = null; + + try { + baseURL = + new File(sourcefile.getAbsolutePath()). + getParentFile().toURL().toExternalForm(); + } catch (Exception e) { + baseURL = ""; + } + fop.getUserAgent().setBaseURL(baseURL); + } + + try { + // Setup XSLT + TransformerFactory factory = TransformerFactory.newInstance(); + Transformer transformer; + + if (stylesheet == null) { // FO Input + transformer = factory.newTransformer(); + } else { // XML/XSLT input + transformer = factory.newTransformer(new StreamSource( + stylesheet)); + + // Set the value of parameters, if any, defined for stylesheet + if (xsltParams != null) { + for (int i = 0; i < xsltParams.size(); i += 2) { + transformer.setParameter((String) xsltParams.elementAt(i), + (String) xsltParams.elementAt(i + 1)); + } + } + } + + // Create a SAXSource from the input Source file + Source src = new StreamSource(sourcefile); + + // Resulting SAX events (the generated FO) must be piped through to FOP + Result res = new SAXResult(fop.getDefaultHandler()); + + // Start XSLT transformation and FOP processing + transformer.transform(src, res); + + } catch (Exception e) { + throw new FOPException(e); + } + } } diff --git a/src/java/org/apache/fop/apps/XSLTInputHandler.java b/src/java/org/apache/fop/apps/XSLTInputHandler.java deleted file mode 100644 index f30ecc9e5..000000000 --- a/src/java/org/apache/fop/apps/XSLTInputHandler.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright 1999-2004 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* $Id$ */ - -package org.apache.fop.apps; - -// Imported java.io classes -import java.io.File; -import java.util.Vector; - -// Imported TraX classes -import javax.xml.transform.Result; -import javax.xml.transform.Source; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.sax.SAXResult; -import javax.xml.transform.stream.StreamSource; - -// Imported SAX classes -import org.xml.sax.InputSource; - -/** - * XSLTInputHandler basically takes an XML file and transforms it with an XSLT - * file and the resulting XSL-FO document is input for FOP. - */ -public class XSLTInputHandler extends InputHandler { - private StreamSource xmlSource; - private Source xsltSource; - private Vector xsltParams = null; - - /** - * Constructor for files as input - * @param xmlfile XML file - * @param xsltfile XSLT file - * @param params Vector of command-line parameters (name, value, - * name, value, ...) for XSL stylesheet, null if none - * @throws FOPException if initializing the Transformer fails - */ - public XSLTInputHandler(File xmlfile, File xsltfile, Vector params) { - this.xmlSource = new StreamSource(xmlfile); - this.xsltSource = new StreamSource(xsltfile); - try { - baseURL = - new File(xmlfile.getAbsolutePath()).getParentFile().toURL().toExternalForm(); - } catch (Exception e) { - baseURL = ""; - } - xsltParams = params; - } - - /** - * @see org.apache.fop.apps.InputHandler#render(Fop) - */ - public void render(Fop fop) - throws FOPException { - - // temporary until baseURL removed from inputHandler objects - if (fop.getUserAgent().getBaseURL() == null) { - fop.getUserAgent().setBaseURL(getBaseURL()); - } - - try { - // Setup XSLT - TransformerFactory factory = TransformerFactory.newInstance(); - Transformer transformer = factory.newTransformer(xsltSource); - - // Set the value of parameters, if any, defined for stylesheet - if (xsltParams != null) { - for (int i = 0; i < xsltParams.size(); i += 2) { - transformer.setParameter((String) xsltParams.elementAt(i), - (String) xsltParams.elementAt(i + 1)); - } - } - - // Resulting SAX events (the generated FO) must be piped through to FOP - Result res = new SAXResult(fop.getDefaultHandler()); - - // Start XSLT transformation and FOP processing - transformer.transform(xmlSource, res); - - } catch (Exception e) { - throw new FOPException(e); - } - } -} diff --git a/src/java/org/apache/fop/fo/FObj.java b/src/java/org/apache/fop/fo/FObj.java index a61790606..a6d3739c4 100644 --- a/src/java/org/apache/fop/fo/FObj.java +++ b/src/java/org/apache/fop/fo/FObj.java @@ -57,7 +57,7 @@ public class FObj extends FONode implements Constants { protected Map layoutDimension = null; /** During input FO validation, certain FO's are not valid as - child nodes if this FO is a descendant of an Out Of Line + child nodes if they would be a descendant of an Out Of Line Formatting Object as defined in specification. See Section 6.2 of 1.0/1.2 spec for more information. */ diff --git a/src/java/org/apache/fop/tools/TestConverter.java b/src/java/org/apache/fop/tools/TestConverter.java index 66f7b36f2..5090575f5 100644 --- a/src/java/org/apache/fop/tools/TestConverter.java +++ b/src/java/org/apache/fop/tools/TestConverter.java @@ -27,10 +27,8 @@ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.fop.apps.Fop; -import org.apache.fop.apps.FOFileHandler; import org.apache.fop.apps.FOUserAgent; import org.apache.fop.apps.InputHandler; -import org.apache.fop.apps.XSLTInputHandler; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; @@ -296,11 +294,11 @@ public class TestConverter { InputHandler inputHandler = null; if (xsl == null) { - inputHandler = new FOFileHandler(xmlFile); + inputHandler = new InputHandler(xmlFile); } else { - inputHandler = new XSLTInputHandler(xmlFile, - new File(baseDir + "/" - + xsl), null); + inputHandler = new InputHandler(xmlFile, + new File(baseDir + "/" + + xsl), null); } FOUserAgent userAgent = new FOUserAgent(); diff --git a/src/java/org/apache/fop/tools/anttasks/Fop.java b/src/java/org/apache/fop/tools/anttasks/Fop.java index 633b37b1d..c60bb9f06 100644 --- a/src/java/org/apache/fop/tools/anttasks/Fop.java +++ b/src/java/org/apache/fop/tools/anttasks/Fop.java @@ -35,7 +35,6 @@ import java.util.List; // FOP import org.apache.fop.apps.InputHandler; -import org.apache.fop.apps.FOFileHandler; import org.apache.fop.fo.Constants; import org.apache.fop.apps.FOPException; import org.apache.fop.apps.FOUserAgent; @@ -517,7 +516,7 @@ class FOPTaskStarter { private void render(File foFile, File outFile, int renderer) throws FOPException { - InputHandler inputHandler = new FOFileHandler(foFile); + InputHandler inputHandler = new InputHandler(foFile); OutputStream out = null; try { diff --git a/test/java/org/apache/fop/BasicDriverTestCase.java b/test/java/org/apache/fop/BasicDriverTestCase.java index d1c2f2d9e..e6dc07d27 100644 --- a/test/java/org/apache/fop/BasicDriverTestCase.java +++ b/test/java/org/apache/fop/BasicDriverTestCase.java @@ -37,7 +37,6 @@ import org.xml.sax.InputSource; import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.fop.apps.Fop; import org.apache.fop.apps.InputHandler; -import org.apache.fop.apps.XSLTInputHandler; import org.w3c.dom.Document; /** @@ -151,7 +150,7 @@ public class BasicDriverTestCase extends AbstractFOPTestCase { Fop fop = new Fop(Fop.RENDER_PDF); fop.setOutputStream(baout); - InputHandler handler = new XSLTInputHandler(xmlFile, xsltFile, null); + InputHandler handler = new InputHandler(xmlFile, xsltFile, null); handler.render(fop); assertTrue("Generated PDF has zero length", baout.size() > 0); -- 2.39.5