KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > cli > InputHandler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: InputHandler.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.cli;
21
22 // Imported java.io classes
23
import java.io.File JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 // Imported TraX classes
28
import javax.xml.transform.ErrorListener JavaDoc;
29 import javax.xml.transform.Result JavaDoc;
30 import javax.xml.transform.Source JavaDoc;
31 import javax.xml.transform.Transformer JavaDoc;
32 import javax.xml.transform.TransformerException JavaDoc;
33 import javax.xml.transform.TransformerFactory JavaDoc;
34 import javax.xml.transform.sax.SAXResult JavaDoc;
35 import javax.xml.transform.stream.StreamResult JavaDoc;
36 import javax.xml.transform.stream.StreamSource JavaDoc;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.apache.fop.apps.FOPException;
41 import org.apache.fop.apps.FOUserAgent;
42 import org.apache.fop.apps.Fop;
43 import org.apache.fop.apps.FopFactory;
44 import org.apache.fop.render.awt.viewer.Renderable;
45
46 /**
47  * Class for handling files input from command line
48  * either with XML and XSLT files (and optionally xsl
49  * parameters) or FO File input alone
50  */

51 public class InputHandler implements ErrorListener JavaDoc, Renderable {
52      
53     private File JavaDoc sourcefile = null; // either FO or XML/XSLT usage
54
private File JavaDoc stylesheet = null; // for XML/XSLT usage
55
private Vector JavaDoc xsltParams = null; // for XML/XSLT usage
56

57     /** the logger */
58     protected Log log = LogFactory.getLog(InputHandler.class);
59     
60     /**
61      * Constructor for XML->XSLT->FO input
62      * @param xmlfile XML file
63      * @param xsltfile XSLT file
64      * @param params Vector of command-line parameters (name, value,
65      * name, value, ...) for XSL stylesheet, null if none
66      */

67     public InputHandler(File JavaDoc xmlfile, File JavaDoc xsltfile, Vector JavaDoc params) {
68         sourcefile = xmlfile;
69         stylesheet = xsltfile;
70         xsltParams = params;
71     }
72
73     /**
74      * Constructor for FO input
75      * @param fofile the file to read the FO document.
76      */

77     public InputHandler(File JavaDoc fofile) {
78         sourcefile = fofile;
79     }
80
81     /**
82      * Generate a document, given an initialized Fop object
83      * @param userAgent the user agent
84      * @param outputFormat the output format to generate (MIME type, see MimeConstants)
85      * @param out the output stream to write the generated output to (may be null if not applicable)
86      * @throws FOPException in case of an error during processing
87      */

88     public void renderTo(FOUserAgent userAgent, String JavaDoc outputFormat, OutputStream JavaDoc out)
89                 throws FOPException {
90
91         FopFactory factory = userAgent.getFactory();
92         Fop fop;
93         if (out != null) {
94             fop = factory.newFop(outputFormat, userAgent, out);
95         } else {
96             fop = factory.newFop(outputFormat, userAgent);
97         }
98
99         // if base URL was not explicitly set in FOUserAgent, obtain here
100
if (fop.getUserAgent().getBaseURL() == null) {
101             String JavaDoc baseURL = null;
102
103             try {
104                 baseURL = new File JavaDoc(sourcefile.getAbsolutePath()).
105                         getParentFile().toURL().toExternalForm();
106             } catch (Exception JavaDoc e) {
107                 baseURL = "";
108             }
109             fop.getUserAgent().setBaseURL(baseURL);
110         }
111
112         // Resulting SAX events (the generated FO) must be piped through to FOP
113
Result JavaDoc res = new SAXResult JavaDoc(fop.getDefaultHandler());
114
115         transformTo(res);
116     }
117     
118     /** @see org.apache.fop.render.awt.viewer.Renderable */
119     public void renderTo(FOUserAgent userAgent, String JavaDoc outputFormat) throws FOPException {
120         renderTo(userAgent, outputFormat, null);
121     }
122
123     /**
124      * In contrast to render(Fop) this method only performs the XSLT stage and saves the
125      * intermediate XSL-FO file to the output file.
126      * @param out OutputStream to write the transformation result to.
127      * @throws FOPException in case of an error during processing
128      */

129     public void transformTo(OutputStream JavaDoc out) throws FOPException {
130         Result JavaDoc res = new StreamResult JavaDoc(out);
131         transformTo(res);
132     }
133     
134     /**
135      * Transforms the input document to the input format expected by FOP using XSLT.
136      * @param result the Result object where the result of the XSL transformation is sent to
137      * @throws FOPException in case of an error during processing
138      */

139     protected void transformTo(Result JavaDoc result) throws FOPException {
140         try {
141             // Setup XSLT
142
TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
143             Transformer JavaDoc transformer;
144             
145             if (stylesheet == null) { // FO Input
146
transformer = factory.newTransformer();
147             } else { // XML/XSLT input
148
transformer = factory.newTransformer(new StreamSource JavaDoc(
149                     stylesheet));
150             
151                 // Set the value of parameters, if any, defined for stylesheet
152
if (xsltParams != null) {
153                     for (int i = 0; i < xsltParams.size(); i += 2) {
154                         transformer.setParameter((String JavaDoc) xsltParams.elementAt(i),
155                             (String JavaDoc) xsltParams.elementAt(i + 1));
156                     }
157                 }
158             }
159             transformer.setErrorListener(this);
160
161             // Create a SAXSource from the input Source file
162
Source JavaDoc src = new StreamSource JavaDoc(sourcefile);
163
164             // Start XSLT transformation and FOP processing
165
transformer.transform(src, result);
166
167         } catch (Exception JavaDoc e) {
168             throw new FOPException(e);
169         }
170     }
171
172     // --- Implementation of the ErrorListener interface ---
173

174     /**
175      * @see javax.xml.transform.ErrorListener#warning(javax.xml.transform.TransformerException)
176      */

177     public void warning(TransformerException JavaDoc exc) {
178         log.warn(exc.toString());
179     }
180
181     /**
182      * @see javax.xml.transform.ErrorListener#error(javax.xml.transform.TransformerException)
183      */

184     public void error(TransformerException JavaDoc exc) {
185         log.error(exc.toString());
186     }
187
188     /**
189      * @see javax.xml.transform.ErrorListener#fatalError(javax.xml.transform.TransformerException)
190      */

191     public void fatalError(TransformerException JavaDoc exc)
192             throws TransformerException JavaDoc {
193         throw exc;
194     }
195
196 }
197
Popular Tags