Source code: org/javahispano/canyamo/services/presentation/xml/PDFTransformer.java
1 /*
2 Cañamo, portal framework
3 Copyright (c) 2002
4 Alberto Molpeceres, javaHispano (http://www.javahispano.org)
5 All rights reserved.
6 .
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
11 Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14 Neither the name of Alberto Molpeceres, javaHispano nor the names of its
15 contributors may be used to endorse or promote products derived from
16 this software without specific prior written permission.
17 .
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
22 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 package org.javahispano.canyamo.services.presentation.xml;
31
32 import java.io.*;
33
34 import javax.xml.transform.stream.StreamSource;
35 import javax.xml.transform.stream.StreamResult;
36 import javax.xml.transform.Source;
37 import javax.xml.transform.Result;
38
39 import org.xml.sax.InputSource;
40
41 import org.apache.fop.messaging.MessageHandler;
42 import org.apache.fop.apps.*;
43 import org.apache.fop.apps.Driver;
44 import org.apache.fop.messaging.MessageHandler;
45
46 //import org.apache.avalon.framework.logger.ConsoleLogger;
47 //import org.apache.avalon.framework.logger.Logger;
48
49
50 /**
51 * This class transforms XML files to PDF using XSL stylesheets.
52 * It's a singleton so only one instance will be created in the virtual
53 * machine. This transformer can ouput the transformation result as
54 * files or as an OuputStream. This second option typically will be used
55 * to redirect PDF output to a browse via response object.
56 * <P>
57 * This class initally doesn't support Xalan sources ( DOMSource, SAXSource )
58 * but adding this support is mostly trival and will be similar to
59 * HTMLTransformer
60 * <P>
61 * This class supports the concept of stylesheet templates. You can use the
62 * <I>createTemplate</I> methods for storing stylesheets within the
63 * HTMLTransformer cache. These stylesheets will be reused each time
64 * so the overall performance increases.
65 *
66 * Here is a simple example of using PDF Transformer in a servlet:
67 *<P>
68 * <blockquote>
69 * <code>
70 * PDFTransformer pdfTransformer = PDFTransformer.newInstance();<BR>
71 * OutputStream out = response.getOutputStream();<BR>
72 * pdfTransformer.transformToFile("article.xml","article.xsl",out);<BR>
73 * </code>
74 * </blockquote>
75 *<P>
76 * If you would like to generate a file instead of using an output stream
77 *<P>
78 * <blockquote>
79 * <code>
80 * PDFTransformer pdfTransformer = PDFTransformer.newInstance();<BR>
81 * OutputStream out = response.getOutputStream();<BR>
82 * pdfTransformer.transform("article.xml","article.xsl","article.pdf");<BR>
83 * </code>
84 * </blockquote>
85 *
86 * @author <a href=mailto:martin@javahispano.com>Martín Pérez Mariñán</a>
87 * @created 22-09-2002
88 * @version 1.0
89 */
90 public class PDFTransformer {
91
92 /**
93 * Description of the Field
94 */
95 private static PDFTransformer singleton;
96 /**
97 * Description of the Field
98 */
99 private static HTMLTransformer htmlTransformer;
100
101
102 /**
103 * Constructor
104 */
105 private PDFTransformer() {
106
107 htmlTransformer = HTMLTransformer.newInstance();
108
109 }
110
111
112 /**
113 * PDFTransformer will be a singleton object. This methods return
114 * the unique instance of this class and creates it if necessary
115 *
116 * @return PDFTransformer singleton object
117 */
118 public static PDFTransformer newInstance() {
119
120 if (singleton == null) {
121 singleton = new PDFTransformer();
122 }
123 return singleton;
124 }
125
126
127 /**
128 * This method creates a template from a source stylesheet so
129 * the styleshhet can be reused.
130 * <P>
131 * Typically will be used from within a document management system
132 * to store the common stylesheets (articles, news, etc.) used to
133 * transform documents.
134 * <P>
135 * Using this method ensures that stylesheets will be reused between
136 * transformations
137 *
138 * @param source stylesheet's source
139 * @exception TransformerException Description of Exception
140 */
141 public void createTemplates(String source)
142 throws TransformerException {
143
144 htmlTransformer.createTemplates(source);
145 }
146
147
148 /**
149 * This method creates a template from a source stylesheet so
150 * the styleshhet can be reused.
151 * <P>
152 * Typically will be used from within a document management system
153 * to store the common stylesheets (articles, news, etc.) used to
154 * transform documents.
155 * <P>
156 * Using this method ensures that stylesheets will be reused between
157 * transformations
158 *
159 * @param source stylesheet's source
160 * @exception TransformerException Description of Exception
161 */
162 public void createTemplates(Source source)
163 throws TransformerException {
164
165 htmlTransformer.createTemplates(source);
166 }
167
168
169 /**
170 * Transforms an XML source into a PDF using an XSL source. The PDF
171 * is stored in the OuputStream specified. This can be util for example
172 * for redirecting a PDF file via Servlet's response object
173 *
174 * @param xmlSource XML source
175 * @param xslSource XSL source
176 * @param out OuptutStream in which will be stored the PDF file
177 * @throws TransformerException if some error happens in transformation
178 * process
179 */
180 public void transform(String xmlSource, String xslSource,
181 OutputStream out)
182 throws TransformerException {
183
184 try {
185 ByteArrayOutputStream baos = new ByteArrayOutputStream();
186 StreamResult result = new StreamResult(baos);
187
188 htmlTransformer.transform(new StreamSource(xmlSource),
189 htmlTransformer.getTransformer(xslSource),
190 result);
191 byte[] fo =
192 ((ByteArrayOutputStream) result.getOutputStream()).toByteArray();
193 ByteArrayInputStream bais = new ByteArrayInputStream(fo);
194
195 Driver driver = new Driver(new InputSource(bais), out);
196
197 // driver.setLogger(log);
198 driver.setRenderer(Driver.RENDER_PDF);
199 driver.run();
200 } catch (Exception ioe) {
201 throw new TransformerException(ioe);
202 }
203 }
204
205
206 /**
207 * Transforms an XML source into a PDF using an XSL source. The PDF
208 * is stored in the specified file.
209 *
210 * @param xmlSource XML source
211 * @param xslSource XSL source
212 * @param file output PDF file
213 * @throws TransformerException if some error happens in transformation
214 * process
215 */
216 public void transformToFile(String xmlSource, String xslSource,
217 String file)
218 throws TransformerException {
219
220 try {
221 transform(xmlSource, xslSource, new FileOutputStream(new File(file)));
222 } catch (FileNotFoundException fnfe) {
223 throw new TransformerException(fnfe);
224 }
225 }
226 }
227
228