KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > JasperExportManager


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine;
29
30 import java.io.ByteArrayOutputStream JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.OutputStream JavaDoc;
34
35 import net.sf.jasperreports.engine.export.JRHtmlExporter;
36 import net.sf.jasperreports.engine.export.JRPdfExporter;
37 import net.sf.jasperreports.engine.export.JRXmlExporter;
38 import net.sf.jasperreports.engine.export.JRXmlExporterParameter;
39 import net.sf.jasperreports.engine.util.JRLoader;
40
41
42 /**
43  * Façade class for exporting generated reports into more popular
44  * formats such as PDF, HTML and XML.
45  * This class contains convenience methods for exporting to only these 3 formats.
46  * <p>
47  * For exporting to XLS and CSV format or for using special exporter parameters,
48  * the specific exporter class should be used directly.
49  *
50  * @see net.sf.jasperreports.engine.JasperPrint
51  * @see net.sf.jasperreports.engine.export.JRHtmlExporter
52  * @see net.sf.jasperreports.engine.export.JRPdfExporter
53  * @see net.sf.jasperreports.engine.export.JRXmlExporter
54  * @see net.sf.jasperreports.engine.export.JRXlsExporter
55  * @see net.sf.jasperreports.engine.export.JRCsvExporter
56  * @author Teodor Danciu (teodord@users.sourceforge.net)
57  * @version $Id: JasperExportManager.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
58  */

59 public class JasperExportManager
60 {
61
62
63     /**
64      * Exports the generated report file specified by the parameter into PDF format.
65      * The resulting PDF file has the same name as the report object inside the source file,
66      * plus the <code>*.pdf</code> extension and it is located in the same directory as the source file.
67      *
68      * @param sourceFileName source file containing the generated report
69      * @return resulting PDF file name
70      * @see net.sf.jasperreports.engine.export.JRPdfExporter
71      */

72     public static String JavaDoc exportReportToPdfFile(String JavaDoc sourceFileName) throws JRException
73     {
74         File JavaDoc sourceFile = new File JavaDoc(sourceFileName);
75
76         /* We need the report name. */
77         JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
78
79         File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperPrint.getName() + ".pdf");
80         String JavaDoc destFileName = destFile.toString();
81         
82         exportReportToPdfFile(jasperPrint, destFileName);
83         
84         return destFileName;
85     }
86
87
88     /**
89      * Exports the generated report file specified by the first parameter into PDF format,
90      * the result being placed in the second file parameter.
91      *
92      * @param sourceFileName source file containing the generated report
93      * @param destFileName file name to place the PDF content into
94      * @see net.sf.jasperreports.engine.export.JRPdfExporter
95      */

96     public static void exportReportToPdfFile(
97         String JavaDoc sourceFileName,
98         String JavaDoc destFileName
99         ) throws JRException
100     {
101         JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFileName);
102
103         exportReportToPdfFile(jasperPrint, destFileName);
104     }
105
106     
107     /**
108      * Exports the generated report file specified by the first parameter into PDF format,
109      * the result being placed in the second file parameter.
110      *
111      * @param jasperPrint report object to export
112      * @param destFileName file name to place the PDF content into
113      * @see net.sf.jasperreports.engine.export.JRPdfExporter
114      */

115     public static void exportReportToPdfFile(
116         JasperPrint jasperPrint,
117         String JavaDoc destFileName
118         ) throws JRException
119     {
120         /* */
121         JRPdfExporter exporter = new JRPdfExporter();
122         
123         exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
124         exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFileName);
125         
126         exporter.exportReport();
127     }
128
129
130     /**
131      * Exports the generated report read from the supplied input stream into PDF format and
132      * writes the results to the output stream specified by the second parameter.
133      *
134      * @param inputStream input stream to read the generated report object from
135      * @param outputStream output stream to write the resulting PDF content to
136      * @see net.sf.jasperreports.engine.export.JRPdfExporter
137      */

138     public static void exportReportToPdfStream(
139         InputStream JavaDoc inputStream,
140         OutputStream JavaDoc outputStream
141         ) throws JRException
142     {
143         JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(inputStream);
144
145         exportReportToPdfStream(jasperPrint, outputStream);
146     }
147
148     
149     /**
150      * Exports the generated report object received as first parameter into PDF format and
151      * writes the results to the output stream specified by the second parameter.
152      *
153      * @param jasperPrint report object to export
154      * @param outputStream output stream to write the resulting PDF content to
155      * @see net.sf.jasperreports.engine.export.JRPdfExporter
156      */

157     public static void exportReportToPdfStream(
158         JasperPrint jasperPrint,
159         OutputStream JavaDoc outputStream
160         ) throws JRException
161     {
162         JRPdfExporter exporter = new JRPdfExporter();
163         
164         exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
165         exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
166         
167         exporter.exportReport();
168     }
169
170
171     /**
172      * Exports the generated report object received as parameter into PDF format and
173      * returns the binary content as a byte array.
174      *
175      * @param jasperPrint report object to export
176      * @return byte array representing the resulting PDF content
177      * @see net.sf.jasperreports.engine.export.JRPdfExporter
178      */

179     public static byte[] exportReportToPdf(JasperPrint jasperPrint) throws JRException
180     {
181         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
182
183         JRPdfExporter exporter = new JRPdfExporter();
184         
185         exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
186         exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
187         
188         exporter.exportReport();
189         
190         return baos.toByteArray();
191     }
192
193     
194     /**
195      * Exports the generated report file specified by the parameter into XML format.
196      * The resulting XML file has the same name as the report object inside the source file,
197      * plus the <code>*.jrpxml</code> extension and it is located in the same directory as the source file.
198      * <p>
199      * When exporting to XML format, the images can be either embedded in the XML content
200      * itself using the Base64 encoder or be referenced as external resources.
201      * If not embedded, the images are placed as distinct files inside a directory
202      * having the same name as the XML destination file, plus the "_files" suffix.
203      *
204      * @param sourceFileName source file containing the generated report
205      * @param isEmbeddingImages flag that indicates whether the images should be embedded in the
206      * XML content itself using the Base64 encoder or be referenced as external resources
207      * @return XML representation of the generated report
208      * @see net.sf.jasperreports.engine.export.JRPdfExporter
209      */

210     public static String JavaDoc exportReportToXmlFile(
211         String JavaDoc sourceFileName,
212         boolean isEmbeddingImages
213         ) throws JRException
214     {
215         File JavaDoc sourceFile = new File JavaDoc(sourceFileName);
216
217         /* We need the report name. */
218         JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
219
220         File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperPrint.getName() + ".jrpxml");
221         String JavaDoc destFileName = destFile.toString();
222         
223         exportReportToXmlFile(
224             jasperPrint,
225             destFileName,
226             isEmbeddingImages
227             );
228         
229         return destFileName;
230     }
231
232
233     /**
234      * Exports the generated report file specified by the first parameter into XML format,
235      * placing the result into the second file parameter.
236      * <p>
237      * If not embedded into the XML content itself using the Base64 encoder,
238      * the images are placed as distinct files inside a directory having the same name
239      * as the XML destination file, plus the "_files" suffix.
240      *
241      * @param sourceFileName source file containing the generated report
242      * @param destFileName file name to place the XML representation into
243      * @param isEmbeddingImages flag that indicates whether the images should be embedded in the
244      * XML content itself using the Base64 encoder or be referenced as external resources
245      * @see net.sf.jasperreports.engine.export.JRPdfExporter
246      */

247     public static void exportReportToXmlFile(
248         String JavaDoc sourceFileName,
249         String JavaDoc destFileName,
250         boolean isEmbeddingImages
251         ) throws JRException
252     {
253         JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFileName);
254
255         exportReportToXmlFile(
256             jasperPrint,
257             destFileName,
258             isEmbeddingImages
259             );
260     }
261
262     
263     /**
264      * Exports the generated report object received as parameter into XML format,
265      * placing the result into the second file parameter.
266      * <p>
267      * If not embedded into the XML content itself using the Base64 encoder,
268      * the images are placed as distinct files inside a directory having the same name
269      * as the XML destination file, plus the "_files" suffix.
270      *
271      * @param jasperPrint report object to export
272      * @param destFileName file name to place the XML representation into
273      * @param isEmbeddingImages flag that indicates whether the images should be embedded in the
274      * XML content itself using the Base64 encoder or be referenced as external resources
275      *
276      * @see net.sf.jasperreports.engine.export.JRPdfExporter
277      */

278     public static void exportReportToXmlFile(
279         JasperPrint jasperPrint,
280         String JavaDoc destFileName,
281         boolean isEmbeddingImages
282         ) throws JRException
283     {
284         JRXmlExporter exporter = new JRXmlExporter();
285         
286         exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
287         exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFileName);
288         exporter.setParameter(JRXmlExporterParameter.IS_EMBEDDING_IMAGES,
289             isEmbeddingImages ? Boolean.TRUE : Boolean.FALSE);
290         
291         exporter.exportReport();
292     }
293
294
295     /**
296      * Exports the generated report object read from the supplied input stream into XML format,
297      * and writes the result to the output stream specified by the second parameter.
298      * The images are embedded into the XML content itself using the Base64 encoder.
299      *
300      * @param inputStream input stream to read the generated report object from
301      * @param outputStream output stream to write the resulting XML representation to
302      * @see net.sf.jasperreports.engine.export.JRPdfExporter
303      */

304     public static void exportReportToXmlStream(
305         InputStream JavaDoc inputStream,
306         OutputStream JavaDoc outputStream
307         ) throws JRException
308     {
309         JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(inputStream);
310
311         exportReportToXmlStream(jasperPrint, outputStream);
312     }
313
314     
315     /**
316      * Exports the generated report object supplied as the first parameter into XML format,
317      * and writes the result to the output stream specified by the second parameter.
318      * The images are embedded into the XML content itself using the Base64 encoder.
319      *
320      * @param jasperPrint report object to export
321      * @param outputStream output stream to write the resulting XML representation to
322      * @see net.sf.jasperreports.engine.export.JRPdfExporter
323      */

324     public static void exportReportToXmlStream(
325         JasperPrint jasperPrint,
326         OutputStream JavaDoc outputStream
327         ) throws JRException
328     {
329         JRXmlExporter exporter = new JRXmlExporter();
330         
331         exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
332         exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
333         
334         exporter.exportReport();
335     }
336
337
338     /**
339      * Exports the generated report object supplied as parameter into XML format
340      * and returs the result as String.
341      * The images are embedded into the XML content itself using the Base64 encoder.
342      *
343      * @param jasperPrint report object to export
344      * @return XML representation of the generated report
345      * @see net.sf.jasperreports.engine.export.JRPdfExporter
346      */

347     public static String JavaDoc exportReportToXml(JasperPrint jasperPrint) throws JRException
348     {
349         StringBuffer JavaDoc sbuffer = new StringBuffer JavaDoc();
350
351         JRXmlExporter exporter = new JRXmlExporter();
352         
353         exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
354         exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, sbuffer);
355         
356         exporter.exportReport();
357         
358         return sbuffer.toString();
359     }
360
361
362     /**
363      * Exports the generated report file specified by the parameter into HTML format.
364      * The resulting HTML file has the same name as the report object inside the source file,
365      * plus the <code>*.html</code> extension and it is located in the same directory as the source file.
366      * The images are placed as distinct files inside a directory having the same name
367      * as the HTML destination file, plus the "_files" suffix.
368      *
369      * @param sourceFileName source file containing the generated report
370      * @return resulting HTML file name
371      * @see net.sf.jasperreports.engine.export.JRHtmlExporter
372      */

373     public static String JavaDoc exportReportToHtmlFile(
374         String JavaDoc sourceFileName
375         ) throws JRException
376     {
377         File JavaDoc sourceFile = new File JavaDoc(sourceFileName);
378
379         /* We need the report name. */
380         JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
381
382         File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperPrint.getName() + ".html");
383         String JavaDoc destFileName = destFile.toString();
384         
385         exportReportToHtmlFile(
386             jasperPrint,
387             destFileName
388             );
389         
390         return destFileName;
391     }
392
393
394     /**
395      * Exports the generated report file specified by the first parameter into HTML format,
396      * placing the result into the second file parameter.
397      * <p>
398      * The images are placed as distinct files inside a directory having the same name
399      * as the HTML destination file, plus the "_files" suffix.
400      *
401      * @param sourceFileName source file containing the generated report
402      * @param destFileName file name to place the HTML content into
403      * @see net.sf.jasperreports.engine.export.JRPdfExporter
404      */

405     public static void exportReportToHtmlFile(
406         String JavaDoc sourceFileName,
407         String JavaDoc destFileName
408         ) throws JRException
409     {
410         JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFileName);
411
412         exportReportToHtmlFile(
413             jasperPrint,
414             destFileName
415             );
416     }
417
418     
419     /**
420      * Exports the generated report object received as parameter into HTML format,
421      * placing the result into the second file parameter.
422      * <p>
423      * The images are placed as distinct files inside a directory having the same name
424      * as the HTML destination file, plus the "_files" suffix.
425      *
426      * @param jasperPrint report object to export
427      * @param destFileName file name to place the HTML content into
428      * @see net.sf.jasperreports.engine.export.JRPdfExporter
429      */

430     public static void exportReportToHtmlFile(
431         JasperPrint jasperPrint,
432         String JavaDoc destFileName
433         ) throws JRException
434     {
435         JRHtmlExporter exporter = new JRHtmlExporter();
436         
437         exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
438         exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFileName);
439         
440         exporter.exportReport();
441     }
442
443
444 }
445
Popular Tags