From de7495a8e754284b9c1aa2f5aa1b39d1b9e442be Mon Sep 17 00:00:00 2001 From: Joerg Pietschmann Date: Sun, 23 Feb 2003 20:24:30 +0000 Subject: [PATCH] Made new constructors for XSLTInputHandler and TraxInputHandler actually construct something usable. PR:17118 Submitted by: i.pavkovic@bmiag.de git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/fop-0_20_2-maintain@195977 13f79535-47bb-0310-9956-ffa450edef68 --- src/org/apache/fop/apps/TraxInputHandler.java | 96 ++++++++++--------- 1 file changed, 53 insertions(+), 43 deletions(-) diff --git a/src/org/apache/fop/apps/TraxInputHandler.java b/src/org/apache/fop/apps/TraxInputHandler.java index 63df336c9..df6cd5d3f 100644 --- a/src/org/apache/fop/apps/TraxInputHandler.java +++ b/src/org/apache/fop/apps/TraxInputHandler.java @@ -1,6 +1,6 @@ /* * $Id$ - * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. + * Copyright (C) 2001-2003 The Apache Software Foundation. All rights reserved. * For details on use and redistribution please refer to the * LICENSE file included with these sources. */ @@ -32,48 +32,40 @@ import java.io.IOException; import java.io.File; /** - * XSLTInputHandler basically takes an xmlfile and transforms it with an xsltfile - * and the resulting xsl:fo document is input for Fop. + * XSLTInputHandler basically takes an xml source and transforms it with + * an xslt source and the resulting xsl:fo document is input for Fop. */ public class TraxInputHandler extends InputHandler { - - File xmlfile, xsltfile; private Transformer transformer; - private Source xmlSource; + private StreamSource xmlSource; + private Source xsltSource; 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); - } + throws FOPException { + xmlSource = new StreamSource(xmlfile); + xsltSource = new StreamSource(xsltfile); + initTransformer(); } 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); - } + throws FOPException { + this.xmlSource = new StreamSource(xmlURL); + this.xsltSource = new StreamSource(xsltURL); + initTransformer(); } public TraxInputHandler(InputSource xmlSource, InputSource xsltSource) - throws FOPException { - this.xmlSource = new StreamSource(xmlSource.getByteStream(), - xmlSource.getSystemId()); + 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 { - transformer = TransformerFactory.newInstance().newTransformer( - new StreamSource(xsltSource.getByteStream(), - xsltSource.getSystemId())); + transformer = TransformerFactory.newInstance().newTransformer (xsltSource); } catch( Exception ex) { throw new FOPException(ex); @@ -85,17 +77,21 @@ public class TraxInputHandler extends InputHandler { * @deprecated */ public InputSource getInputSource() { - return fileInputSource(xmlfile); + InputSource is = new InputSource(); + is.setByteStream(xmlSource.getInputStream()); + is.setSystemId(xmlSource.getSystemId()); + return is; } /** - * overwrites this method of the super class and returns an XMLFilter instead of a + * 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 { - return this.getXMLFilter(xmlfile, xsltfile); + return this.getXMLFilter(xsltSource); } /** @@ -106,22 +102,22 @@ public class TraxInputHandler extends InputHandler { * @param xmlfile The xmlfile containing the text data * @param xsltfile An xslt stylesheet * @return XMLFilter an XMLFilter which can be chained together with other XMLReaders or XMLFilters - */ - public static XMLFilter getXMLFilter(File xmlfile, - File xsltfile) throws FOPException { + */ + private static XMLFilter getXMLFilter(Source xsltSource) + throws FOPException { try { // Instantiate a TransformerFactory. TransformerFactory tFactory = TransformerFactory.newInstance(); - // Determine whether the TransformerFactory supports The use uf SAXSource - // and SAXResult + // Determine whether the TransformerFactory supports the + // use of SAXSource and SAXResult if (tFactory.getFeature(SAXSource.FEATURE) - && tFactory.getFeature(SAXResult.FEATURE)) { + && tFactory.getFeature(SAXResult.FEATURE)) { // Cast the TransformerFactory to SAXTransformerFactory. SAXTransformerFactory saxTFactory = ((SAXTransformerFactory)tFactory); // Create an XMLFilter for each stylesheet. XMLFilter xmlfilter = - saxTFactory.newXMLFilter(new StreamSource(xsltfile)); + saxTFactory.newXMLFilter(xsltSource); // Create an XMLReader. XMLReader parser = createParser(); @@ -129,7 +125,7 @@ public class TraxInputHandler extends InputHandler { throw new FOPException("Unable to create SAX parser"); } - // xmlFilter1 uses the XMLReader as its reader. + // xmlFilter uses the XMLReader as its reader. xmlfilter.setParent(parser); return xmlfilter; } else { @@ -143,6 +139,21 @@ public class TraxInputHandler extends InputHandler { } } + + /** + * 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 xmlfile The xmlfile containing the text data + * @param xsltfile An xslt stylesheet + * @return XMLFilter an XMLFilter which can be chained together with other XMLReaders or XMLFilters + */ + public static XMLFilter getXMLFilter(File xmlfile, + File xsltfile) throws FOPException { + return getXMLFilter(new StreamSource(xsltfile)); + } + public void run(Driver driver) throws FOPException { try { transformer.transform(xmlSource, @@ -157,4 +168,3 @@ public class TraxInputHandler extends InputHandler { } } - -- 2.39.5