gmazza 2004/07/25 10:04:44 Modified: src/java/org/apache/fop/apps CommandLineOptions.java InputHandler.java src/java/org/apache/fop/fo FObj.java src/java/org/apache/fop/tools TestConverter.java src/java/org/apache/fop/tools/anttasks Fop.java test/java/org/apache/fop BasicDriverTestCase.java Removed: src/java/org/apache/fop/apps FOFileHandler.java XSLTInputHandler.java Log: Took advantage of the Transformer similarities between FO input and (XSL, XSLT) input to combine the two into InputHandler. Revision Changes Path 1.27 +2 -2 xml-fop/src/java/org/apache/fop/apps/CommandLineOptions.java Index: CommandLineOptions.java =================================================================== RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/apps/CommandLineOptions.java,v retrieving revision 1.26 retrieving revision 1.27 diff -u -r1.26 -r1.27 --- CommandLineOptions.java 24 Jul 2004 05:47:44 -0000 1.26 +++ CommandLineOptions.java 25 Jul 2004 17:04:44 -0000 1.27 @@ -483,9 +483,9 @@ 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."); } 1.15 +85 -10 xml-fop/src/java/org/apache/fop/apps/InputHandler.java Index: InputHandler.java =================================================================== RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/apps/InputHandler.java,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- InputHandler.java 25 Jul 2004 01:47:47 -0000 1.14 +++ InputHandler.java 25 Jul 2004 17:04:44 -0000 1.15 @@ -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 @@ * @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); + } + } } 1.54 +1 -1 xml-fop/src/java/org/apache/fop/fo/FObj.java Index: FObj.java =================================================================== RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/fo/FObj.java,v retrieving revision 1.53 retrieving revision 1.54 diff -u -r1.53 -r1.54 --- FObj.java 25 Jul 2004 16:11:04 -0000 1.53 +++ FObj.java 25 Jul 2004 17:04:44 -0000 1.54 @@ -57,7 +57,7 @@ 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. */ 1.21 +4 -6 xml-fop/src/java/org/apache/fop/tools/TestConverter.java Index: TestConverter.java =================================================================== RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/tools/TestConverter.java,v retrieving revision 1.20 retrieving revision 1.21 diff -u -r1.20 -r1.21 --- TestConverter.java 25 Jul 2004 01:47:47 -0000 1.20 +++ TestConverter.java 25 Jul 2004 17:04:44 -0000 1.21 @@ -27,10 +27,8 @@ 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 @@ 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(); 1.19 +1 -2 xml-fop/src/java/org/apache/fop/tools/anttasks/Fop.java Index: Fop.java =================================================================== RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/tools/anttasks/Fop.java,v retrieving revision 1.18 retrieving revision 1.19 diff -u -r1.18 -r1.19 --- Fop.java 24 Jul 2004 05:47:45 -0000 1.18 +++ Fop.java 25 Jul 2004 17:04:44 -0000 1.19 @@ -35,7 +35,6 @@ // 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 @@ 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 { 1.14 +2 -3 xml-fop/test/java/org/apache/fop/BasicDriverTestCase.java Index: BasicDriverTestCase.java =================================================================== RCS file: /home/cvs/xml-fop/test/java/org/apache/fop/BasicDriverTestCase.java,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- BasicDriverTestCase.java 25 Jul 2004 01:47:47 -0000 1.13 +++ BasicDriverTestCase.java 25 Jul 2004 17:04:44 -0000 1.14 @@ -37,7 +37,6 @@ 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 @@ 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);
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]