jeremias    2003/06/02 15:24:02

  Modified:    src/java/org/apache/fop/apps TraxInputHandler.java
                        InputHandler.java XSLTInputHandler.java
                        CommandLineOptions.java FOInputHandler.java
  Log:
  Take over InputHandler code from maintenance branch.
  Makes the org.apache.fop.tools.xslt package superfluous.
  (#20398)
  Submitted by: Glen Mazza <[EMAIL PROTECTED]>
  
  Revision  Changes    Path
  1.2       +77 -13    xml-fop/src/java/org/apache/fop/apps/TraxInputHandler.java
  
  Index: TraxInputHandler.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/apps/TraxInputHandler.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TraxInputHandler.java     11 Mar 2003 13:05:28 -0000      1.1
  +++ TraxInputHandler.java     2 Jun 2003 22:24:02 -0000       1.2
  @@ -54,6 +54,8 @@
   import java.io.File;
   
   // Imported TraX classes
  +import javax.xml.transform.Source;
  +import javax.xml.transform.Transformer;
   import javax.xml.transform.TransformerFactory;
   import javax.xml.transform.stream.StreamSource;
   import javax.xml.transform.sax.SAXResult;
  @@ -71,23 +73,66 @@
    */
   public class TraxInputHandler extends InputHandler {
   
  -    private File xmlfile, xsltfile;
  +    private Transformer transformer;
  +    private StreamSource xmlSource;
  +    private Source xsltSource;
   
       /**
  -     * Main constructor
  +     * Constructor with files as input.
        * @param xmlfile XML file 
        * @param xsltfile XSLT file
  +     * @throws FOPException if initializing the Transformer fails
        */
  -    public TraxInputHandler(File xmlfile, File xsltfile) {
  -        this.xmlfile = xmlfile;
  -        this.xsltfile = xsltfile;
  +    public TraxInputHandler(File xmlfile, File xsltfile) throws FOPException {
  +        this.xmlSource  = new StreamSource(xmlfile);
  +        this.xsltSource = new StreamSource(xsltfile);
  +        initTransformer();
  +    }
  +
  +    /**
  +     * Constructor with URIs/URLs as input.
  +     * @param xmlURL XML URL
  +     * @param xsltURL XSLT URL
  +     * @throws FOPException if initializing the Transformer fails
  +     */
  +    public TraxInputHandler(String xmlURL, String xsltURL) throws FOPException {
  +        this.xmlSource  = new StreamSource(xmlURL);
  +        this.xsltSource = new StreamSource(xsltURL);
  +        initTransformer();
  +    }
  +
  +    /**
  +     * Constructor with InputSources as input.
  +     * @param xmlSource XML InputSource
  +     * @param xsltSource XSLT InputSource
  +     * @throws FOPException if initializing the Transformer fails
  +     */
  +    public TraxInputHandler(InputSource xmlSource, InputSource xsltSource) 
  +                throws FOPException {
  +        this.xmlSource  = new StreamSource(xmlSource.getByteStream(),
  +                                           xmlSource.getSystemId());
  +        this.xsltSource = new StreamSource(xsltSource.getByteStream(),
  +                                           xsltSource.getSystemId());
  +        initTransformer();
  +    }
  +    
  +    private void initTransformer() throws FOPException {
  +        try {
  +            this.transformer = 
  +                TransformerFactory.newInstance().newTransformer(xsltSource);
  +        } catch (Exception ex) {
  +            throw new FOPException(ex);
  +        }
       }
   
       /**
        * @see org.apache.fop.apps.InputHandler#getInputSource()
        */
       public InputSource getInputSource() {
  -        return fileInputSource(xmlfile);
  +        InputSource is = new InputSource();
  +        is.setByteStream(xmlSource.getInputStream());
  +        is.setSystemId(xmlSource.getSystemId());
  +        return is;
       }
   
       /**
  @@ -96,7 +141,7 @@
        * @see org.apache.fop.apps.InputHandler#getParser()
        */
       public XMLReader getParser() throws FOPException {
  -        return this.getXMLFilter(xmlfile, xsltfile);
  +        return getXMLFilter(xsltSource);
       }
   
       /**
  @@ -104,14 +149,12 @@
        * then can be used in a chain with the XMLReader passed to Driver. This way
        * during the conversion of the xml file + xslt stylesheet the resulting
        * data is fed into Fop. This should help to avoid memory problems
  -     * @param xmlfile The xmlfile containing the text data
  -     * @param xsltfile An xslt stylesheet
  +     * @param xsltSource An xslt stylesheet
        * @return an XMLFilter which can be chained together with other 
        * XMLReaders or XMLFilters
        * @throws FOPException if setting up the XMLFilter fails
        */
  -    public static XMLFilter getXMLFilter(File xmlfile,
  -                                         File xsltfile) throws FOPException {
  +    public static XMLFilter getXMLFilter(Source xsltSource) throws FOPException {
           try {
               // Instantiate  a TransformerFactory.
               TransformerFactory tFactory = TransformerFactory.newInstance();
  @@ -124,7 +167,7 @@
                       ((SAXTransformerFactory)tFactory);
                   // Create an XMLFilter for each stylesheet.
                   XMLFilter xmlfilter =
  -                    saxTFactory.newXMLFilter(new StreamSource(xsltfile));
  +                    saxTFactory.newXMLFilter(xsltSource);
   
                   // Create an XMLReader.
                   XMLReader parser = createParser();
  @@ -148,5 +191,26 @@
           }
       }
   
  -}
  +    /**
  +     * @see org.apache.fop.apps.InputHandler#run(Driver)
  +     */
  +    public void run(Driver driver) throws FOPException {
  +        try {
  +            transformer.transform(xmlSource,
  +                                  new SAXResult(driver.getContentHandler()));
  +        } catch (Exception ex) {
  +            throw new FOPException(ex);
  +        }
  +    }
  +
  +    /**
  +     * Sets an XSLT parameter.
  +     * @param name the name of the parameter
  +     * @param value the value of the parameter
  +     */
  +    public void setParameter(String name, Object value) {
  +        transformer.setParameter(name, value);
  +    }
  +
   
  +}
  \ No newline at end of file
  
  
  
  1.2       +8 -0      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.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- InputHandler.java 11 Mar 2003 13:05:28 -0000      1.1
  +++ InputHandler.java 2 Jun 2003 22:24:02 -0000       1.2
  @@ -129,5 +129,13 @@
               throw new FOPException("Coudn't create XMLReader", pce);
           }
       }
  +    
  +    /**
  +     * Runs this InputHandler through the Driver.
  +     * @param driver Driver instance to use
  +     * @throws FOPException if processing this InputHandler fails
  +     */
  +    public abstract void run(Driver driver) throws FOPException;
  +    
   }
   
  
  
  
  1.3       +48 -95    xml-fop/src/java/org/apache/fop/apps/XSLTInputHandler.java
  
  Index: XSLTInputHandler.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/apps/XSLTInputHandler.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- XSLTInputHandler.java     25 Apr 2003 07:06:43 -0000      1.2
  +++ XSLTInputHandler.java     2 Jun 2003 22:24:02 -0000       1.3
  @@ -52,131 +52,84 @@
   
   // Imported java.io classes
   import java.io.File;
  -import java.lang.reflect.InvocationTargetException;
  -import java.lang.reflect.Method;
   
   // Imported SAX classes
   import org.xml.sax.InputSource;
   import org.xml.sax.XMLReader;
   
  -// FOP
  -import org.apache.fop.tools.xslt.XSLTransform;
  -
   /**
    * XSLTInputHandler basically takes an XML file and transforms it with an XSLT
    * file and the resulting XSL-FO document is input for FOP.
  - * (todo) add URL constructor
    */
   public class XSLTInputHandler extends InputHandler {
   
  -    private File xmlfile, xsltfile;
  -    private boolean useOldTransform = false;
  -    private boolean gotParser = false;
  +    private TraxInputHandler traxInputHandler;
   
       /**
  -     * Main constructor
  +     * Constructor for files as input
        * @param xmlfile XML file
        * @param xsltfile XSLT file
  +     * @throws FOPException if initializing the Transformer fails
        */
  -    public XSLTInputHandler(File xmlfile, File xsltfile) {
  -        this.xmlfile = xmlfile;
  -        this.xsltfile = xsltfile;
  +    public XSLTInputHandler(File xmlfile, File xsltfile) throws FOPException {
  +        this.traxInputHandler = new TraxInputHandler(xmlfile, xsltfile);
       }
   
       /**
  -     * @see org.apache.fop.apps.InputHandler#getInputSource()
  +     * Constructor with URIs/URLs as input.
  +     * @param xmlURL XML URL
  +     * @param xsltURL XSLT URL
  +     * @throws FOPException if initializing the Transformer fails
        */
  -    public InputSource getInputSource() {
  -        if (!gotParser) {
  -            throw new IllegalStateException("The method getParser() must be "
  -                    + "called and the parser used when using XSLTInputHandler");
  -        }
  -        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 java.io.FileWriter(tmpFile);
  -                } else {
  -                    writer = new java.io.StringWriter();
  -                }
  -
  -                XSLTransform.transform(xmlfile.getCanonicalPath(),
  -                                       xsltfile.getCanonicalPath(), writer);
  -
  -                writer.flush();
  -                writer.close();
  -
  -                if (tmpFile != null) {
  -                    reader = new java.io.FileReader(tmpFile);
  -                } else {
  -                    // create a input source containing the xsl:fo file which can 
be fed to Fop
  -                    reader = new java.io.StringReader(writer.toString());
  -                }
  -                return new InputSource(reader);
  -            } catch (Exception ex) {
  -                ex.printStackTrace();
  -                /**(todo) do proper logging of exceptions */
  -                return null;
  -            }
  -        } else {
  -            return fileInputSource(xmlfile);
  -        }
  +    public XSLTInputHandler(String xmlURL, String xsltURL) throws FOPException {
  +        traxInputHandler = new TraxInputHandler(xmlURL, xsltURL);
  +    }
   
  +    /**
  +     * Constructor with InputSources as input.
  +     * @param xmlSource XML InputSource
  +     * @param xsltSource XSLT InputSource
  +     * @throws FOPException if initializing the Transformer fails
  +     */
  +    public XSLTInputHandler(InputSource xmlSource, InputSource xsltSource)
  +                throws FOPException {
  +        traxInputHandler = new TraxInputHandler(xmlSource, xsltSource);
       }
   
       /**
  -     * 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
  -     * @return the created <code>XMLReader</code>
  -     * @throws FOPException if getting the parser fails
  +     * Get the InputSource.
  +     * @return the InputSource
  +     * @deprecated Use TraxInputHandler run(Driver driver) instead.
  +     */
  +    public InputSource getInputSource() {
  +        return traxInputHandler.getInputSource();
  +    }
  +
  +    /**
  +     * Get the parser, actually an XML filter.
        * @see org.apache.fop.apps.InputHandler#getParser()
  +     * @deprecated Use TraxInputHandler run(Driver driver) instead.
        */
       public XMLReader getParser() throws FOPException {
  -        gotParser = true;
  +        return traxInputHandler.getParser();
  +    }
   
  -        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;
  +    /**
  +     * @see org.apache.fop.apps.InputHandler#run(Driver)
  +     */
  +    public void run(Driver driver) throws FOPException {
  +        traxInputHandler.run(driver);
  +    }
   
  +    /**
  +     * Sets an XSLT parameter.
  +     * @param name the name of the parameter
  +     * @param value the value of the parameter
  +     */
  +    public void setParameter(String name, Object value) {
  +        traxInputHandler.setParameter(name, value);
       }
   
   }
  +
   
  
  
  
  1.2       +2 -1      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.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- CommandLineOptions.java   11 Mar 2003 13:05:28 -0000      1.1
  +++ CommandLineOptions.java   2 Jun 2003 22:24:02 -0000       1.2
  @@ -420,8 +420,9 @@
       /**
        * Get the input handler.
        * @return the input handler
  +     * @throws FOPException if creating the InputHandler fails
        */
  -    public InputHandler getInputHandler() {
  +    public InputHandler getInputHandler() throws FOPException {
           switch (inputmode) {
           case FO_INPUT:
               return new FOInputHandler(fofile);
  
  
  
  1.2       +8 -0      xml-fop/src/java/org/apache/fop/apps/FOInputHandler.java
  
  Index: FOInputHandler.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/apps/FOInputHandler.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- FOInputHandler.java       11 Mar 2003 13:05:28 -0000      1.1
  +++ FOInputHandler.java       2 Jun 2003 22:24:02 -0000       1.2
  @@ -100,5 +100,13 @@
           return super.createParser();
       }
   
  +    /**
  +     * @see org.apache.fop.apps.InputHandler#run(Driver)
  +     */
  +    public void run(Driver driver) throws FOPException {
  +        throw new FOPException("not implemented: FOInputHandler.run(Driver)");
  +    }
  +
  +
   }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to