pietsch 2002/09/17 15:01:07 Modified: src/org/apache/fop/apps Tag: fop-0_20_2-maintain CommandLineOptions.java FOInputHandler.java InputHandler.java Options.java TraxInputHandler.java XSLTInputHandler.java Log: Preliminarily added additional constructors to Options and InputHandlers to provide more flexibility for data and XSLT input sources. Revision Changes Path No revision No revision 1.14.2.7 +3 -2 xml-fop/src/org/apache/fop/apps/CommandLineOptions.java Index: CommandLineOptions.java =================================================================== RCS file: /home/cvs/xml-fop/src/org/apache/fop/apps/CommandLineOptions.java,v retrieving revision 1.14.2.6 retrieving revision 1.14.2.7 diff -u -r1.14.2.6 -r1.14.2.7 --- CommandLineOptions.java 2 Aug 2002 20:28:46 -0000 1.14.2.6 +++ CommandLineOptions.java 17 Sep 2002 22:01:04 -0000 1.14.2.7 @@ -370,7 +370,8 @@ /** * */ - public InputHandler getInputHandler() { + public InputHandler getInputHandler() + throws FOPException { switch (inputmode) { case FO_INPUT: return new FOInputHandler(fofile); 1.6.2.1 +5 -1 xml-fop/src/org/apache/fop/apps/FOInputHandler.java Index: FOInputHandler.java =================================================================== RCS file: /home/cvs/xml-fop/src/org/apache/fop/apps/FOInputHandler.java,v retrieving revision 1.6 retrieving revision 1.6.2.1 diff -u -r1.6 -r1.6.2.1 --- FOInputHandler.java 21 Aug 2001 06:18:54 -0000 1.6 +++ FOInputHandler.java 17 Sep 2002 22:01:05 -0000 1.6.2.1 @@ -40,5 +40,9 @@ return super.createParser(); } + public void run(Driver driver) throws FOPException { + throw new FOPException("not implemented: FOInputHandler.run(Driver)"); + } + } 1.5.2.2 +2 -1 xml-fop/src/org/apache/fop/apps/InputHandler.java Index: InputHandler.java =================================================================== RCS file: /home/cvs/xml-fop/src/org/apache/fop/apps/InputHandler.java,v retrieving revision 1.5.2.1 retrieving revision 1.5.2.2 diff -u -r1.5.2.1 -r1.5.2.2 --- InputHandler.java 25 Mar 2002 18:08:27 -0000 1.5.2.1 +++ InputHandler.java 17 Sep 2002 22:01:05 -0000 1.5.2.2 @@ -26,6 +26,7 @@ abstract public InputSource getInputSource(); abstract public XMLReader getParser() throws FOPException; + abstract public void run(Driver driver) throws FOPException; static public InputSource urlInputSource(URL url) { 1.9.2.1 +34 -17 xml-fop/src/org/apache/fop/apps/Attic/Options.java Index: Options.java =================================================================== RCS file: /home/cvs/xml-fop/src/org/apache/fop/apps/Attic/Options.java,v retrieving revision 1.9 retrieving revision 1.9.2.1 diff -u -r1.9 -r1.9.2.1 --- Options.java 10 Aug 2001 08:18:43 -0000 1.9 +++ Options.java 17 Sep 2002 22:01:06 -0000 1.9.2.1 @@ -31,6 +31,16 @@ initOptions(); } + public Options(InputStream userConfig) throws FOPException { + this(); + this.loadUserconfiguration(userConfig); + } + + public Options(InputSource userConfig) throws FOPException { + this(); + this.loadUserconfiguration(userConfig); + } + public Options(File userConfigFile) throws FOPException { this(); this.loadUserconfiguration(userConfigFile); @@ -147,24 +157,31 @@ } public void loadUserconfiguration(File userConfigFile) { - // read user configuration file if (userConfigFile != null) { - MessageHandler.logln("reading user configuration file"); - ConfigurationReader reader = - new ConfigurationReader(InputHandler.fileInputSource(userConfigFile)); + loadUserconfiguration(InputHandler.fileInputSource(userConfigFile)); + } + } + + public void loadUserconfiguration(InputStream userConfig) { + loadUserconfiguration(new InputSource(userConfig)); + } + + public void loadUserconfiguration(InputSource userConfigSource) { + // read user configuration + ConfigurationReader reader = + new ConfigurationReader(userConfigSource); + if (errorDump) { + reader.setDumpError(true); + } + try { + reader.start(); + } catch (org.apache.fop.apps.FOPException error) { + MessageHandler.errorln("Could not load user configuration " + + userConfigSource.getSystemId() + " - error: " + + error.getMessage()); + MessageHandler.errorln("using default values"); if (errorDump) { - reader.setDumpError(true); - } - try { - reader.start(); - } catch (org.apache.fop.apps.FOPException error) { - MessageHandler.errorln("Could not load user configuration file " - + userConfigFile + " - error: " - + error.getMessage()); - MessageHandler.errorln("using default values"); - if (errorDump) { - reader.dumpError(error); - } + reader.dumpError(error); } } } 1.5.2.1 +57 -5 xml-fop/src/org/apache/fop/apps/TraxInputHandler.java Index: TraxInputHandler.java =================================================================== RCS file: /home/cvs/xml-fop/src/org/apache/fop/apps/TraxInputHandler.java,v retrieving revision 1.5 retrieving revision 1.5.2.1 diff -u -r1.5 -r1.5.2.1 --- TraxInputHandler.java 21 Aug 2001 06:18:54 -0000 1.5 +++ TraxInputHandler.java 17 Sep 2002 22:01:06 -0000 1.5.2.1 @@ -10,6 +10,8 @@ // Imported TraX classes import javax.xml.transform.TransformerFactory; +import javax.xml.transform.Transformer; +import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.sax.SAXResult; import javax.xml.transform.sax.SAXSource; @@ -36,14 +38,51 @@ public class TraxInputHandler extends InputHandler { File xmlfile, xsltfile; + private Transformer transformer; + private Source xmlSource; - public TraxInputHandler(File xmlfile, File xsltfile) { + public TraxInputHandler(File xmlfile, File xsltfile) + throws FOPException { this.xmlfile = xmlfile; this.xsltfile = xsltfile; + try { + transformer = TransformerFactory.newInstance().newTransformer( + new StreamSource(xsltfile)); + } + catch( Exception ex) { + throw new FOPException(ex); + } + } + + public TraxInputHandler(String xmlURL, String xsltURL) + throws FOPException { + this.xmlSource = new StreamSource(xmlURL); + try { + transformer = TransformerFactory.newInstance().newTransformer( + new StreamSource(xsltURL)); + } + catch( Exception ex) { + throw new FOPException(ex); + } + } + + public TraxInputHandler(InputSource xmlSource, InputSource xsltSource) + throws FOPException { + this.xmlSource = new StreamSource(xmlSource.getByteStream(), + xmlSource.getSystemId()); + try { + transformer = TransformerFactory.newInstance().newTransformer( + new StreamSource(xsltSource.getByteStream(), + xsltSource.getSystemId())); + } + catch( Exception ex) { + throw new FOPException(ex); + } } /** * overwrites the method of the super class to return the xmlfile + * @deprecated */ public InputSource getInputSource() { return fileInputSource(xmlfile); @@ -52,6 +91,7 @@ /** * overwrites this method of the super class and returns an XMLFilter instead of a * simple XMLReader which allows chaining of transformations + * @deprecated * */ public XMLReader getParser() throws FOPException { @@ -96,12 +136,24 @@ throw new FOPException("Your parser doesn't support the features SAXSource and SAXResult." + "\nMake sure you are using a xsl parser which supports TrAX"); } + } catch (FOPException fex) { + throw fex; + } catch (Exception ex) { + throw new FOPException(ex); + } + } + + public void run(Driver driver) throws FOPException { + try { + transformer.transform(xmlSource, + new SAXResult(driver.getContentHandler())); } catch (Exception ex) { - if (ex instanceof FOPException) { - throw (FOPException)ex; - } throw new FOPException(ex); } + } + + public void setParameter(String name, Object value) { + transformer.setParameter(name, value); } } 1.7.2.1 +29 -89 xml-fop/src/org/apache/fop/apps/XSLTInputHandler.java Index: XSLTInputHandler.java =================================================================== RCS file: /home/cvs/xml-fop/src/org/apache/fop/apps/XSLTInputHandler.java,v retrieving revision 1.7 retrieving revision 1.7.2.1 diff -u -r1.7 -r1.7.2.1 --- XSLTInputHandler.java 21 Aug 2001 06:18:54 -0000 1.7 +++ XSLTInputHandler.java 17 Sep 2002 22:01:06 -0000 1.7.2.1 @@ -7,9 +7,6 @@ package org.apache.fop.apps; -import java.lang.reflect.*; - - // Imported SAX classes import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -18,108 +15,51 @@ // Imported java.io classes import java.io.*; -// FOP -import org.apache.fop.tools.xslt.XSLTransform; - /** - * XSLTInputHandler basically takes an xmlfile and transforms it with an xsltfile - * and the resulting xsl:fo document is input for Fop. + * XSLTInputHandler takes an XML input, transforms it with XSLT + * and provides the resulting xsl:fo document as input for the + * FOP driver. */ public class XSLTInputHandler extends InputHandler { + private TraxInputHandler traxInputHandler; - File xmlfile, xsltfile; - boolean useOldTransform = false; + public XSLTInputHandler(File xmlfile, File xsltfile) + throws FOPException { + traxInputHandler = new TraxInputHandler(xmlfile, xsltfile); + } - public XSLTInputHandler(File xmlfile, File xsltfile) { - this.xmlfile = xmlfile; - this.xsltfile = xsltfile; + public XSLTInputHandler(String xmlURL, String xsltURL) + throws FOPException { + traxInputHandler = new TraxInputHandler(xmlURL, xsltURL); + } + + public XSLTInputHandler(InputSource xmlSource, String xsltSource) + throws FOPException { + traxInputHandler = new TraxInputHandler(xmlSource, xsltSource); } /** - * overwrites the method of the super class to return the xmlfile + * Get the InputSource. + * @deprecated */ public InputSource getInputSource() { - if (useOldTransform) { - try { - java.io.Writer writer; - java.io.Reader reader; - File tmpFile = null; - - // create a Writer - // the following is an ugly hack to allow processing of larger files - // if xml file size is larger than 500 kb write the fo:file to disk - if ((xmlfile.length()) > 500000) { - tmpFile = new File(xmlfile.getName() + ".fo.tmp"); - writer = new FileWriter(tmpFile); - } else { - writer = new StringWriter(); - } - - XSLTransform.transform(xmlfile.getCanonicalPath(), - xsltfile.getCanonicalPath(), writer); - - writer.flush(); - writer.close(); - - if (tmpFile != null) { - reader = new FileReader(tmpFile); - } else { - // create a input source containing the xsl:fo file which can be fed to Fop - reader = new StringReader(writer.toString()); - } - return new InputSource(reader); - } catch (Exception ex) { - ex.printStackTrace(); - return null; - } - } else { - return fileInputSource(xmlfile); - } - + return traxInputHandler.getInputSource(); } /** - * This looks to see if the Trax api is supported and uses that to - * get an XMLFilter. Otherwise, it falls back to using DOM documents - * + * Get the parser, actually an XML filter. + * @deprecated */ public XMLReader getParser() throws FOPException { - XMLReader result = null; - try { - // try trax first - Class transformer = - Class.forName("javax.xml.transform.Transformer"); - transformer = - Class.forName("org.apache.fop.apps.TraxInputHandler"); - Class[] argTypes = { - File.class, File.class - }; - Method getFilterMethod = transformer.getMethod("getXMLFilter", - argTypes); - File[] args = { - xmlfile, xsltfile - }; - Object obj = getFilterMethod.invoke(null, args); - if (obj instanceof XMLReader) { - result = (XMLReader)obj; - } - } catch (ClassNotFoundException ex) { - throw new FOPException(ex); - } catch (InvocationTargetException ex) { - throw new FOPException(ex); - } catch (IllegalAccessException ex) { - throw new FOPException(ex); - } catch (NoSuchMethodException ex) { - throw new FOPException(ex); - } - // otherwise, use DOM documents via our XSLTransform tool class old style - if (result == null) { - useOldTransform = true; - result = createParser(); - } - return result; + return traxInputHandler.getParser(); + } + public void run(Driver driver) throws FOPException { + traxInputHandler.run(driver); } + public void setParameter(String name, Object value) { + traxInputHandler.setParameter(name, value); + } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]