KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > apps > TraxInputHandler


1 /*
2  * $Id: TraxInputHandler.java,v 1.5.2.4 2003/02/25 10:18:32 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.apps;
52
53
54 // Imported TraX classes
55
import javax.xml.transform.TransformerFactory JavaDoc;
56 import javax.xml.transform.Transformer JavaDoc;
57 import javax.xml.transform.Source JavaDoc;
58 import javax.xml.transform.stream.StreamSource JavaDoc;
59 import javax.xml.transform.sax.SAXResult JavaDoc;
60 import javax.xml.transform.sax.SAXSource JavaDoc;
61 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
62
63 // Imported SAX classes
64
import org.xml.sax.InputSource JavaDoc;
65 import org.xml.sax.XMLReader JavaDoc;
66 import org.xml.sax.XMLFilter JavaDoc;
67
68 // Imported java.io classes
69
import java.io.File JavaDoc;
70
71 /**
72  * XSLTInputHandler basically takes an xml source and transforms it with
73  * an xslt source and the resulting xsl:fo document is input for Fop.
74  */

75 public class TraxInputHandler extends InputHandler {
76     private Transformer JavaDoc transformer;
77     private StreamSource JavaDoc xmlSource;
78     private Source JavaDoc xsltSource;
79
80     public TraxInputHandler(File JavaDoc xmlfile, File JavaDoc xsltfile)
81         throws FOPException {
82         xmlSource = new StreamSource JavaDoc(xmlfile);
83         xsltSource = new StreamSource JavaDoc(xsltfile);
84         initTransformer();
85     }
86
87     public TraxInputHandler(String JavaDoc xmlURL, String JavaDoc xsltURL)
88         throws FOPException {
89         this.xmlSource = new StreamSource JavaDoc(xmlURL);
90         this.xsltSource = new StreamSource JavaDoc(xsltURL);
91         initTransformer();
92     }
93
94     public TraxInputHandler(InputSource JavaDoc xmlSource, InputSource JavaDoc xsltSource)
95         throws FOPException {
96         this.xmlSource = new StreamSource JavaDoc(xmlSource.getByteStream(),
97                                            xmlSource.getSystemId());
98         this.xsltSource = new StreamSource JavaDoc(xsltSource.getByteStream(),
99                                            xsltSource.getSystemId());
100         initTransformer();
101     }
102     
103     private void initTransformer() throws FOPException {
104         try {
105             transformer = TransformerFactory.newInstance().newTransformer (xsltSource);
106         }
107         catch( Exception JavaDoc ex) {
108             throw new FOPException(ex);
109         }
110     }
111
112     /**
113      * Overwrites the method of the super class to return the xmlfile.
114      * Use run(Driver driver) instead.
115      * @deprecated
116      */

117     public InputSource JavaDoc getInputSource() {
118         InputSource JavaDoc is = new InputSource JavaDoc();
119         is.setByteStream(xmlSource.getInputStream());
120         is.setSystemId(xmlSource.getSystemId());
121         return is;
122     }
123
124     /**
125      * Overwrites this method of the super class and returns an
126      * XMLFilter instead of a simple XMLReader which allows chaining
127      * of transformations.
128      * Use run(Driver driver) instead.
129      * @deprecated
130      *
131      */

132     public XMLReader JavaDoc getParser() throws FOPException {
133         return this.getXMLFilter(xsltSource);
134     }
135
136     /**
137      * Creates from the transformer an instance of an XMLFilter which
138      * then can be used in a chain with the XMLReader passed to Driver. This way
139      * during the conversion of the xml file + xslt stylesheet the resulting
140      * data is fed into Fop. This should help to avoid memory problems
141      * @param xmlfile The xmlfile containing the text data
142      * @param xsltfile An xslt stylesheet
143      * @return XMLFilter an XMLFilter which can be chained together with other XMLReaders or XMLFilters
144     */

145     private static XMLFilter JavaDoc getXMLFilter(Source JavaDoc xsltSource)
146         throws FOPException {
147         try {
148             // Instantiate a TransformerFactory.
149
TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
150             // Determine whether the TransformerFactory supports the
151
// use of SAXSource and SAXResult
152
if (tFactory.getFeature(SAXSource.FEATURE)
153                 && tFactory.getFeature(SAXResult.FEATURE)) {
154                 // Cast the TransformerFactory to SAXTransformerFactory.
155
SAXTransformerFactory JavaDoc saxTFactory =
156                     ((SAXTransformerFactory JavaDoc)tFactory);
157                 // Create an XMLFilter for each stylesheet.
158
XMLFilter JavaDoc xmlfilter =
159                     saxTFactory.newXMLFilter(xsltSource);
160
161                 // Create an XMLReader.
162
XMLReader JavaDoc parser = createParser();
163                 if (parser == null) {
164                     throw new FOPException("Unable to create SAX parser");
165                 }
166
167                 // xmlFilter uses the XMLReader as its reader.
168
xmlfilter.setParent(parser);
169                 return xmlfilter;
170             } else {
171                 throw new FOPException("Your parser doesn't support the features SAXSource and SAXResult."
172                                        + "\nMake sure you are using a xsl parser which supports TrAX");
173             }
174         } catch (FOPException fex) {
175             throw fex;
176         } catch (Exception JavaDoc ex) {
177             throw new FOPException(ex);
178         }
179     }
180
181
182     /**
183      * Creates from the transformer an instance of an XMLFilter which
184      * then can be used in a chain with the XMLReader passed to Driver. This way
185      * during the conversion of the xml file + xslt stylesheet the resulting
186      * data is fed into Fop. This should help to avoid memory problems
187      * @param xmlfile The xmlfile containing the text data
188      * @param xsltfile An xslt stylesheet
189      * @return XMLFilter an XMLFilter which can be chained together with other XMLReaders or XMLFilters
190     */

191     public static XMLFilter JavaDoc getXMLFilter(File JavaDoc xmlfile,
192                                          File JavaDoc xsltfile) throws FOPException {
193         return getXMLFilter(new StreamSource JavaDoc(xsltfile));
194     }
195
196     public void run(Driver driver) throws FOPException {
197         try {
198             transformer.transform(xmlSource,
199                                   new SAXResult JavaDoc(driver.getContentHandler()));
200         } catch (Exception JavaDoc ex) {
201             throw new FOPException(ex);
202         }
203     }
204
205     public void setParameter(String JavaDoc name, Object JavaDoc value) {
206         transformer.setParameter(name, value);
207     }
208
209 }
210
Popular Tags