diff options
Diffstat (limited to 'src/java/org/apache')
-rw-r--r-- | src/java/org/apache/fop/apps/Driver.java | 54 | ||||
-rw-r--r-- | src/java/org/apache/fop/apps/FOFileHandler.java | 51 | ||||
-rw-r--r-- | src/java/org/apache/fop/apps/Fop.java | 2 | ||||
-rw-r--r-- | src/java/org/apache/fop/apps/InputHandler.java | 16 | ||||
-rw-r--r-- | src/java/org/apache/fop/apps/XSLTInputHandler.java | 102 | ||||
-rw-r--r-- | src/java/org/apache/fop/render/awt/viewer/PreviewDialog.java | 2 | ||||
-rw-r--r-- | src/java/org/apache/fop/tools/TestConverter.java | 2 | ||||
-rw-r--r-- | src/java/org/apache/fop/tools/anttasks/Fop.java | 2 |
8 files changed, 75 insertions, 156 deletions
diff --git a/src/java/org/apache/fop/apps/Driver.java b/src/java/org/apache/fop/apps/Driver.java index aecf72518..149ee1f47 100644 --- a/src/java/org/apache/fop/apps/Driver.java +++ b/src/java/org/apache/fop/apps/Driver.java @@ -19,13 +19,9 @@ package org.apache.fop.apps; // Java -import java.io.IOException; import java.io.OutputStream; // XML -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; // FOP @@ -138,54 +134,4 @@ public class Driver implements Constants { public DefaultHandler getDefaultHandler() throws FOPException { return new FOTreeBuilder(renderType, foUserAgent, stream); } - - /** - * Render the FO document read by a SAX Parser from an InputHandler - * @param inputHandler the input handler containing the source and - * parser information. - * @throws FOPException if anything goes wrong. - */ - public synchronized void render(InputHandler inputHandler) - throws FOPException { - XMLReader parser = inputHandler.getParser(); - foUserAgent.setBaseURL(inputHandler.getBaseURL()); - render(parser, inputHandler.getInputSource()); - } - - /** - * This is the main render() method. The other render() methods are for - * convenience, and normalize to this form, then run this. - * Renders the FO document read by a SAX Parser from an InputSource. - * For versions not needing an FO Tree (e.g., Alt-Design), override this. - * - * @param parser the SAX parser. - * @param source the input source the parser reads from. - * @throws FOPException if anything goes wrong. - */ - public synchronized void render(XMLReader parser, InputSource source) - throws FOPException { - parser.setContentHandler(getDefaultHandler()); - - try { - /** - The following statement triggers virtually all of the processing - for this document. The SAX parser fires events that are handled by - the appropriate InputHandler object, which means that you will need - to look in those objects to see where FOP picks up control of - processing again. For Structure Renderers (e.g. MIF & RTF), the SAX - events are handled directly. For Layout Renderers (e.g. PDF & - PostScript), an Area Tree is managed by the AreaTreeHandler. - */ - parser.parse(source); - } catch (SAXException e) { - if (e.getException() instanceof FOPException) { - // Undo exception tunneling. - throw (FOPException)e.getException(); - } else { - throw new FOPException(e); - } - } catch (IOException e) { - throw new FOPException(e); - } - } } diff --git a/src/java/org/apache/fop/apps/FOFileHandler.java b/src/java/org/apache/fop/apps/FOFileHandler.java index d2a500b80..2819d2aab 100644 --- a/src/java/org/apache/fop/apps/FOFileHandler.java +++ b/src/java/org/apache/fop/apps/FOFileHandler.java @@ -15,21 +15,27 @@ */ /* $Id$ */ - + package org.apache.fop.apps; +// Java +import java.io.File; +import java.net.URL; + // Imported SAX classes import org.xml.sax.InputSource; -import org.xml.sax.XMLReader; import org.xml.sax.SAXException; -import org.xml.sax.SAXNotSupportedException; +import org.xml.sax.XMLReader; -// java +//JAXP import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.ParserConfigurationException; -import java.io.File; -import java.net.URL; - +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.Source; +import javax.xml.transform.Result; +import javax.xml.transform.sax.SAXSource; +import javax.xml.transform.sax.SAXResult; /** * Manages input if it is an XSL-FO file. @@ -72,12 +78,34 @@ public class FOFileHandler extends InputHandler { } /** - * @see org.apache.fop.apps.InputHandler#getParser() + * @see org.apache.fop.apps.InputHandler#render(Driver) */ - public XMLReader getParser() throws FOPException { - return createParser(); - } + public void render(Driver driver) throws FOPException { + // temporary until baseURL removed from inputHandler objects + if (driver.getUserAgent().getBaseURL() == null) { + driver.getUserAgent().setBaseURL(getBaseURL()); + } + + try { + // Setup JAXP using identity transformer (no stylesheet here) + TransformerFactory factory = TransformerFactory.newInstance(); + Transformer transformer = factory.newTransformer(); + + // Setup input stream + Source src = new SAXSource(getInputSource()); + + // Resulting SAX events (the generated FO) must be piped through to FOP + Result res = new SAXResult(driver.getDefaultHandler()); + + // Start XSLT transformation and FOP processing + transformer.transform(src, res); + + } catch (Exception e) { + throw new FOPException(e); + } + } + /** * Creates <code>XMLReader</code> object using default * <code>SAXParserFactory</code> @@ -109,4 +137,3 @@ public class FOFileHandler extends InputHandler { } } } - diff --git a/src/java/org/apache/fop/apps/Fop.java b/src/java/org/apache/fop/apps/Fop.java index c8e40d1db..dfdf2e6fb 100644 --- a/src/java/org/apache/fop/apps/Fop.java +++ b/src/java/org/apache/fop/apps/Fop.java @@ -52,7 +52,7 @@ public class Fop { options.getOutputFile())); driver.setOutputStream(bos); } - driver.render(foUserAgent.getInputHandler()); + foUserAgent.getInputHandler().render(driver); } finally { if (bos != null) { bos.close(); diff --git a/src/java/org/apache/fop/apps/InputHandler.java b/src/java/org/apache/fop/apps/InputHandler.java index 383e7cd8d..a457a268b 100644 --- a/src/java/org/apache/fop/apps/InputHandler.java +++ b/src/java/org/apache/fop/apps/InputHandler.java @@ -33,12 +33,6 @@ import java.io.File; */ public abstract class InputHandler { - /** - * Get the input source associated with this input handler. - * @return the input source - */ - public abstract InputSource getInputSource(); - protected String baseURL = null; /** @@ -50,11 +44,11 @@ public abstract class InputHandler { } /** - * Get the SAX parser associated with this input handler. - * @return the SAX parser - * @throws FOPException in case of an error determining the SAX parser + * Generate a document, given an initialized Driver object + * @param driver -- Driver object + * @throws FOPException in case of an error during processing */ - public abstract XMLReader getParser() throws FOPException; + public void render(Driver driver) throws FOPException {} /** * Creates an InputSource from a URL. @@ -86,6 +80,4 @@ public abstract class InputHandler { throw new RuntimeException("unexpected MalformedURLException"); } } - } - diff --git a/src/java/org/apache/fop/apps/XSLTInputHandler.java b/src/java/org/apache/fop/apps/XSLTInputHandler.java index 47c7aa6d9..a8bedf260 100644 --- a/src/java/org/apache/fop/apps/XSLTInputHandler.java +++ b/src/java/org/apache/fop/apps/XSLTInputHandler.java @@ -28,23 +28,20 @@ import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.sax.SAXResult; -import javax.xml.transform.sax.SAXSource; -import javax.xml.transform.sax.SAXTransformerFactory; +import javax.xml.transform.stream.StreamSource; +import javax.xml.transform.Result; // Imported SAX classes import org.xml.sax.InputSource; -import org.xml.sax.XMLReader; -import org.xml.sax.XMLFilter; /** * 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; // not yet implemented + private Vector xsltParams = null; /** * Constructor for files as input @@ -108,80 +105,37 @@ public class XSLTInputHandler extends InputHandler { } /** - * @see org.apache.fop.apps.InputHandler#getInputSource() + * @see org.apache.fop.apps.InputHandler#render(Driver) */ - public InputSource getInputSource() { - InputSource is = new InputSource(); - is.setByteStream(xmlSource.getInputStream()); - is.setSystemId(xmlSource.getSystemId()); - return is; - } + public void render(Driver driver) + throws FOPException { - /** - * Overwrites this method of the super class and returns an XMLFilter - * instead of a simple XMLReader which allows chaining of transformations. - * @see org.apache.fop.apps.InputHandler#getParser() - */ - public XMLReader getParser() throws FOPException { - return getXMLFilter(xsltSource, xsltParams); - } + // temporary until baseURL removed from inputHandler objects + if (driver.getUserAgent().getBaseURL() == null) { + driver.getUserAgent().setBaseURL(getBaseURL()); + } - /** - * Creates from the transformer an instance of an XMLFilter which - * 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 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(Source xsltSource, Vector inParams) throws FOPException { try { - // Instantiate a TransformerFactory. - TransformerFactory tFactory = TransformerFactory.newInstance(); - // Determine whether the TransformerFactory supports The use of SAXSource - // and SAXResult - if (tFactory.getFeature(SAXSource.FEATURE) - && tFactory.getFeature(SAXResult.FEATURE)) { - // Cast the TransformerFactory to SAXTransformerFactory. - SAXTransformerFactory saxTFactory = - ((SAXTransformerFactory)tFactory); - // Create an XMLFilter for each stylesheet. - XMLFilter xmlfilter = - saxTFactory.newXMLFilter(xsltSource); - -/* if (inParams != null) { - // parameters currently not settable with an XMLFilter - for (int i = 0; i < nParams; i += 2) { - // setParameter() method doesn't exist - xmlfilter.setParameter((String) inParams.elementAt(i), - (String) inParams.elementAt(i + 1)); - } + // 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)); } -*/ - - // Create an XMLReader. - XMLReader parser = FOFileHandler.createParser(); - if (parser == null) { - throw new FOPException("Unable to create SAX parser"); - } - - // xmlFilter1 uses the XMLReader as its reader. - xmlfilter.setParent(parser); - return xmlfilter; - } else { - throw new FOPException("Your parser doesn't support the " - + "features SAXSource and SAXResult." - + "\nMake sure you are using an XSLT engine which " - + "supports TrAX"); } - } catch (FOPException fe) { - throw fe; - } catch (Exception ex) { - throw new FOPException(ex); + + // Resulting SAX events (the generated FO) must be piped through to FOP + Result res = new SAXResult(driver.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/render/awt/viewer/PreviewDialog.java b/src/java/org/apache/fop/render/awt/viewer/PreviewDialog.java index 40f47be64..6eb868269 100644 --- a/src/java/org/apache/fop/render/awt/viewer/PreviewDialog.java +++ b/src/java/org/apache/fop/render/awt/viewer/PreviewDialog.java @@ -394,7 +394,7 @@ public class PreviewDialog extends JFrame { try { setStatus(translator.getString("Status.Build.FO.tree")); - driver.render(foUserAgent.getInputHandler()); + foUserAgent.getInputHandler().render(driver); setStatus(translator.getString("Status.Show")); } catch (FOPException e) { reportException(e); diff --git a/src/java/org/apache/fop/tools/TestConverter.java b/src/java/org/apache/fop/tools/TestConverter.java index 67d25c4d9..5469e06ad 100644 --- a/src/java/org/apache/fop/tools/TestConverter.java +++ b/src/java/org/apache/fop/tools/TestConverter.java @@ -326,7 +326,7 @@ public class TestConverter { driver.setOutputStream(outStream); logger.debug("ddir:" + destdir + " on:" + outputFile.getName()); - driver.render(inputHandler); + inputHandler.render(driver); outStream.close(); // check difference diff --git a/src/java/org/apache/fop/tools/anttasks/Fop.java b/src/java/org/apache/fop/tools/anttasks/Fop.java index 255bbb0b9..9c641ba08 100644 --- a/src/java/org/apache/fop/tools/anttasks/Fop.java +++ b/src/java/org/apache/fop/tools/anttasks/Fop.java @@ -535,7 +535,7 @@ class FOPTaskStarter { userAgent.setBaseURL(this.baseURL); Driver driver = new Driver(renderer, userAgent); driver.setOutputStream(out); - driver.render(inputHandler); + inputHandler.render(driver); } catch (Exception ex) { throw new BuildException(ex); } finally { |