1 /*
2  * JasperReports - Free Java Reporting Library.
3  * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4  * http://www.jaspersoft.com
5  *
6  * Unless you have purchased a commercial license agreement from Jaspersoft,
7  * the following license terms apply:
8  *
9  * This program is part of JasperReports.
10  *
11  * JasperReports is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * JasperReports is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23  */

24 package net.sf.jasperreports.engine;
25
26 import java.io.ByteArrayOutputStream;
27 import java.io.File;
28 import java.io.InputStream;
29 import java.io.OutputStream;
30
31 import net.sf.jasperreports.engine.export.HtmlExporter;
32 import net.sf.jasperreports.engine.export.JRPdfExporter;
33 import net.sf.jasperreports.engine.export.JRXmlExporter;
34 import net.sf.jasperreports.engine.util.JRLoader;
35 import net.sf.jasperreports.export.SimpleExporterInput;
36 import net.sf.jasperreports.export.SimpleHtmlExporterOutput;
37 import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
38 import net.sf.jasperreports.export.SimpleXmlExporterOutput;
39
40
41 /**
42  * Facade class for exporting generated reports into more popular
43  * formats such as PDF, HTML and XML.
44  * <p>
45  * This class contains convenience methods for exporting to only these 3 formats.
46  * These methods can process data that comes from different
47  * sources and goes to different destinations (files, input and output streams, etc.).
48  * </p><p>
49  * For exporting to XLS and CSV format or for using special exporter parameters, 
50  * the specific exporter class should be used directly.  
51  * 
52  * @see net.sf.jasperreports.engine.JasperPrint
53  * @see net.sf.jasperreports.engine.export.HtmlExporter
54  * @see net.sf.jasperreports.engine.export.JRPdfExporter
55  * @see net.sf.jasperreports.engine.export.JRXmlExporter
56  * @see net.sf.jasperreports.engine.export.JRXlsExporter
57  * @see net.sf.jasperreports.engine.export.JRCsvExporter
58  * @author Teodor Danciu (teodord@users.sourceforge.net)
59  */

60 public final class JasperExportManager
61 {
62     private JasperReportsContext jasperReportsContext;
63
64
65     /**
66      *
67      */

68     private JasperExportManager(JasperReportsContext jasperReportsContext)
69     {
70         this.jasperReportsContext = jasperReportsContext;
71     }
72     
73     
74     /**
75      *
76      */

77     private static JasperExportManager getDefaultInstance()
78     {
79         return new JasperExportManager(DefaultJasperReportsContext.getInstance());
80     }
81     
82     
83     /**
84      *
85      */

86     public static JasperExportManager getInstance(JasperReportsContext jasperReportsContext)
87     {
88         return new JasperExportManager(jasperReportsContext);
89     }
90     
91     
92     /**
93      * Exports the generated report file specified by the parameter into PDF format.
94      * The resulting PDF file has the same name as the report object inside the source file,
95      * plus the <code>*.pdf</code> extension and it is located in the same directory as the source file.
96      *  
97      * @param sourceFileName source file containing the generated report
98      * @return resulting PDF file name
99      * @see net.sf.jasperreports.engine.export.JRPdfExporter
100      */

101     public String exportToPdfFile(String sourceFileName) throws JRException
102     {
103         File sourceFile = new File(sourceFileName);
104
105         /* We need the report name. */
106         JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
107
108         File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".pdf");
109         String destFileName = destFile.toString();
110         
111         exportToPdfFile(jasperPrint, destFileName);
112         
113         return destFileName;
114     }
115
116
117     /**
118      * Exports the generated report file specified by the first parameter into PDF format,
119      * the result being placed in the second file parameter.
120      *  
121      * @param sourceFileName source file containing the generated report
122      * @param destFileName   file name to place the PDF content into
123      * @see net.sf.jasperreports.engine.export.JRPdfExporter
124      */

125     public void exportToPdfFile(
126         String sourceFileName, 
127         String destFileName
128         ) throws JRException
129     {
130         JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObjectFromFile(sourceFileName);
131
132         exportToPdfFile(jasperPrint, destFileName);
133     }
134
135     
136     /**
137      * Exports the generated report file specified by the first parameter into PDF format,
138      * the result being placed in the second file parameter.
139      *
140      * @param jasperPrint  report object to export 
141      * @param destFileName file name to place the PDF content into
142      * @see net.sf.jasperreports.engine.export.JRPdfExporter
143      */

144     public void exportToPdfFile(
145         JasperPrint jasperPrint, 
146         String destFileName
147         ) throws JRException
148     {
149         /*   */
150         JRPdfExporter exporter = new JRPdfExporter(jasperReportsContext);
151         
152         exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
153         exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(destFileName));
154         
155         exporter.exportReport();
156     }
157
158
159     /**
160      * Exports the generated report read from the supplied input stream into PDF format and
161      * writes the results to the output stream specified by the second parameter.
162      *
163      * @param inputStream  input stream to read the generated report object from
164      * @param outputStream output stream to write the resulting PDF content to
165      * @see net.sf.jasperreports.engine.export.JRPdfExporter
166      */

167     public void exportToPdfStream(
168         InputStream inputStream, 
169         OutputStream outputStream
170         ) throws JRException
171     {
172         JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(inputStream);
173
174         exportToPdfStream(jasperPrint, outputStream);
175     }
176
177     
178     /**
179      * Exports the generated report object received as first parameter into PDF format and
180      * writes the results to the output stream specified by the second parameter.
181      * 
182      * @param jasperPrint  report object to export 
183      * @param outputStream output stream to write the resulting PDF content to
184      * @see net.sf.jasperreports.engine.export.JRPdfExporter
185      */

186     public void exportToPdfStream(
187         JasperPrint jasperPrint, 
188         OutputStream outputStream
189         ) throws JRException
190     {
191         JRPdfExporter exporter = new JRPdfExporter(jasperReportsContext);
192         
193         exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
194         exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
195         
196         exporter.exportReport();
197     }
198
199
200     /**
201      * Exports the generated report object received as parameter into PDF format and
202      * returns the binary content as a byte array.
203      * 
204      * @param jasperPrint report object to export 
205      * @return byte array representing the resulting PDF content 
206      * @see net.sf.jasperreports.engine.export.JRPdfExporter
207      */

208     public byte[] exportToPdf(JasperPrint jasperPrint) throws JRException
209     {
210         ByteArrayOutputStream baos = new ByteArrayOutputStream();
211
212         JRPdfExporter exporter = new JRPdfExporter(jasperReportsContext);
213         
214         exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
215         exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(baos));
216         
217         exporter.exportReport();
218         
219         return baos.toByteArray();
220     }
221
222     
223     /**
224      * Exports the generated report file specified by the parameter into XML format.
225      * The resulting XML file has the same name as the report object inside the source file,
226      * plus the <code>*.jrpxml</code> extension and it is located in the same directory as the source file.
227      * <p>
228      * When exporting to XML format, the images can be either embedded in the XML content
229      * itself using the Base64 encoder or be referenced as external resources.
230      * If not embedded, the images are placed as distinct files inside a directory
231      * having the same name as the XML destination file, plus the "_files" suffix. 
232      * 
233      * @param sourceFileName    source file containing the generated report
234      * @param isEmbeddingImages flag that indicates whether the images should be embedded in the
235      *                          XML content itself using the Base64 encoder or be referenced as external resources
236      * @return XML representation of the generated report
237      * @see net.sf.jasperreports.engine.export.JRPdfExporter
238      */

239     public String exportToXmlFile(
240         String sourceFileName, 
241         boolean isEmbeddingImages
242         ) throws JRException
243     {
244         File sourceFile = new File(sourceFileName);
245
246         /* We need the report name. */
247         JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
248
249         File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".jrpxml");
250         String destFileName = destFile.toString();
251         
252         exportToXmlFile(
253             jasperPrint, 
254             destFileName,
255             isEmbeddingImages
256             );
257         
258         return destFileName;
259     }
260
261
262     /**
263      * Exports the generated report file specified by the first parameter into XML format,
264      * placing the result into the second file parameter.
265      * <p>
266      * If not embedded into the XML content itself using the Base64 encoder, 
267      * the images are placed as distinct files inside a directory having the same name 
268      * as the XML destination file, plus the "_files" suffix. 
269      * 
270      * @param sourceFileName    source file containing the generated report
271      * @param destFileName      file name to place the XML representation into
272      * @param isEmbeddingImages flag that indicates whether the images should be embedded in the
273      *                          XML content itself using the Base64 encoder or be referenced as external resources
274      * @see net.sf.jasperreports.engine.export.JRPdfExporter
275      */

276     public void exportToXmlFile(
277         String sourceFileName, 
278         String destFileName,
279         boolean isEmbeddingImages
280         ) throws JRException
281     {
282         JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObjectFromFile(sourceFileName);
283
284         exportToXmlFile(
285             jasperPrint, 
286             destFileName,
287             isEmbeddingImages
288             );
289     }
290
291     
292     /**
293      * Exports the generated report object received as parameter into XML format,
294      * placing the result into the second file parameter.
295      * <p>
296      * If not embedded into the XML content itself using the Base64 encoder, 
297      * the images are placed as distinct files inside a directory having the same name 
298      * as the XML destination file, plus the "_files" suffix. 
299      * 
300      * @param jasperPrint       report object to export
301      * @param destFileName      file name to place the XML representation into
302      * @param isEmbeddingImages flag that indicates whether the images should be embedded in the
303      *                          XML content itself using the Base64 encoder or be referenced as external resources
304      *  
305      * @see net.sf.jasperreports.engine.export.JRPdfExporter
306      */

307     public void exportToXmlFile(
308         JasperPrint jasperPrint, 
309         String destFileName,
310         boolean isEmbeddingImages
311         ) throws JRException
312     {
313         JRXmlExporter exporter = new JRXmlExporter(jasperReportsContext);
314         
315         exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
316         
317         SimpleXmlExporterOutput xmlOutput = new SimpleXmlExporterOutput(destFileName);
318         xmlOutput.setEmbeddingImages(isEmbeddingImages);
319         exporter.setExporterOutput(xmlOutput);
320         
321         exporter.exportReport();
322     }
323
324
325     /**
326      * Exports the generated report object read from the supplied input stream into XML format,
327      * and writes the result to the output stream specified by the second parameter.
328      * The images are embedded into the XML content itself using the Base64 encoder. 
329      * 
330      * @param inputStream  input stream to read the generated report object from
331      * @param outputStream output stream to write the resulting XML representation to
332      * @see net.sf.jasperreports.engine.export.JRPdfExporter
333      */

334     public void exportToXmlStream(
335         InputStream inputStream, 
336         OutputStream outputStream
337         ) throws JRException
338     {
339         JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(inputStream);
340
341         exportToXmlStream(jasperPrint, outputStream);
342     }
343
344     
345     /**
346      * Exports the generated report object supplied as the first parameter into XML format,
347      * and writes the result to the output stream specified by the second parameter.
348      * The images are embedded into the XML content itself using the Base64 encoder. 
349      * 
350      * @param jasperPrint  report object to export
351      * @param outputStream output stream to write the resulting XML representation to
352      * @see net.sf.jasperreports.engine.export.JRPdfExporter
353      */

354     public void exportToXmlStream(
355         JasperPrint jasperPrint, 
356         OutputStream outputStream
357         ) throws JRException
358     {
359         JRXmlExporter exporter = new JRXmlExporter(jasperReportsContext);
360         
361         exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
362         exporter.setExporterOutput(new SimpleXmlExporterOutput(outputStream));
363         
364         exporter.exportReport();
365     }
366
367
368     /**
369      * Exports the generated report object supplied as parameter into XML format
370      * and returs the result as String.
371      * The images are embedded into the XML content itself using the Base64 encoder. 
372      * 
373      * @param jasperPrint report object to export
374      * @return XML representation of the generated report
375      * @see net.sf.jasperreports.engine.export.JRPdfExporter
376      */

377     public String exportToXml(JasperPrint jasperPrint) throws JRException
378     {
379         StringBuilder sb = new StringBuilder();
380
381         JRXmlExporter exporter = new JRXmlExporter(jasperReportsContext);
382         
383         exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
384         exporter.setExporterOutput(new SimpleXmlExporterOutput(sb));
385         
386         exporter.exportReport();
387         
388         return sb.toString();
389     }
390
391
392     /**
393      * Exports the generated report file specified by the parameter into HTML format.
394      * The resulting HTML file has the same name as the report object inside the source file,
395      * plus the <code>*.html</code> extension and it is located in the same directory as the source file.
396      * The images are placed as distinct files inside a directory having the same name 
397      * as the HTML destination file, plus the "_files" suffix. 
398      * 
399      * @param sourceFileName source file containing the generated report
400      * @return resulting HTML file name
401      * @see net.sf.jasperreports.engine.export.HtmlExporter
402      */

403     public String exportToHtmlFile(
404         String sourceFileName
405         ) throws JRException
406     {
407         File sourceFile = new File(sourceFileName);
408
409         /* We need the report name. */
410         JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
411
412         File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".html");
413         String destFileName = destFile.toString();
414         
415         exportToHtmlFile(
416             jasperPrint, 
417             destFileName
418             );
419         
420         return destFileName;
421     }
422
423
424     /**
425      * Exports the generated report file specified by the first parameter into HTML format,
426      * placing the result into the second file parameter.
427      * <p>
428      * The images are placed as distinct files inside a directory having the same name 
429      * as the HTML destination file, plus the "_files" suffix. 
430      * 
431      * @param sourceFileName source file containing the generated report
432      * @param destFileName   file name to place the HTML content into
433      * @see net.sf.jasperreports.engine.export.JRPdfExporter
434      */

435     public void exportToHtmlFile(
436         String sourceFileName, 
437         String destFileName
438         ) throws JRException
439     {
440         JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObjectFromFile(sourceFileName);
441
442         exportToHtmlFile(
443             jasperPrint, 
444             destFileName
445             );
446     }
447
448     
449     /**
450      * Exports the generated report object received as parameter into HTML format,
451      * placing the result into the second file parameter.
452      * <p>
453      * The images are placed as distinct files inside a directory having the same name 
454      * as the HTML destination file, plus the "_files" suffix. 
455      * 
456      * @param jasperPrint  report object to export
457      * @param destFileName file name to place the HTML content into
458      * @see net.sf.jasperreports.engine.export.JRPdfExporter
459      */

460     public void exportToHtmlFile(
461         JasperPrint jasperPrint, 
462         String destFileName
463         ) throws JRException
464     {
465         HtmlExporter exporter = new HtmlExporter(jasperReportsContext);
466         
467         exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
468         exporter.setExporterOutput(new SimpleHtmlExporterOutput(destFileName));
469         
470         exporter.exportReport();
471     }
472     
473     
474     /**
475      * @see #exportToPdfFile(String)
476      */

477     public static String exportReportToPdfFile(String sourceFileName) throws JRException
478     {
479         return getDefaultInstance().exportToPdfFile(sourceFileName);
480     }
481
482
483     /**
484      * @see #exportToPdfFile(String, String)
485      */

486     public static void exportReportToPdfFile(
487         String sourceFileName, 
488         String destFileName
489         ) throws JRException
490     {
491         getDefaultInstance().exportToPdfFile(sourceFileName, destFileName);
492     }
493
494     
495     /**
496      * @see #exportToPdfFile(JasperPrint, String)
497      */

498     public static void exportReportToPdfFile(
499         JasperPrint jasperPrint, 
500         String destFileName
501         ) throws JRException
502     {
503         getDefaultInstance().exportToPdfFile(jasperPrint, destFileName);
504     }
505
506
507     /**
508      * @see #exportToPdfStream(InputStream, OutputStream)
509      */

510     public static void exportReportToPdfStream(
511         InputStream inputStream, 
512         OutputStream outputStream
513         ) throws JRException
514     {
515         getDefaultInstance().exportToPdfStream(inputStream, outputStream);
516     }
517
518     
519     /**
520      * Exports the generated report object received as first parameter into PDF format and
521      * writes the results to the output stream specified by the second parameter.
522      * 
523      * @param jasperPrint  report object to export 
524      * @param outputStream output stream to write the resulting PDF content to
525      * @see net.sf.jasperreports.engine.export.JRPdfExporter
526      * @see #exportToPdfStream(JasperPrint, OutputStream)
527      */

528     public static void exportReportToPdfStream(
529         JasperPrint jasperPrint, 
530         OutputStream outputStream
531         ) throws JRException
532     {
533         getDefaultInstance().exportToPdfStream(jasperPrint, outputStream);
534     }
535
536
537     /**
538      * @see #exportToPdf(JasperPrint)
539      */

540     public static byte[] exportReportToPdf(JasperPrint jasperPrint) throws JRException
541     {
542         return getDefaultInstance().exportToPdf(jasperPrint);
543     }
544
545     
546     /**
547      * @see #exportToXmlFile(String, String, boolean)
548      */

549     public static String exportReportToXmlFile(
550         String sourceFileName, 
551         boolean isEmbeddingImages
552         ) throws JRException
553     {
554         return getDefaultInstance().exportToXmlFile(sourceFileName, isEmbeddingImages);
555     }
556
557
558     /**
559      * @see #exportToXmlFile(String, String, boolean)
560      */

561     public static void exportReportToXmlFile(
562         String sourceFileName, 
563         String destFileName,
564         boolean isEmbeddingImages
565         ) throws JRException
566     {
567         getDefaultInstance().exportToXmlFile(sourceFileName, destFileName, isEmbeddingImages);
568     }
569
570     
571     /**
572      * @see #exportToXmlFile(JasperPrint, String, boolean)
573      */

574     public static void exportReportToXmlFile(
575         JasperPrint jasperPrint, 
576         String destFileName,
577         boolean isEmbeddingImages
578         ) throws JRException
579     {
580         getDefaultInstance().exportToXmlFile(jasperPrint, destFileName, isEmbeddingImages);
581     }
582
583
584     /**
585      * @see #exportToXmlStream(InputStream, OutputStream)
586      */

587     public static void exportReportToXmlStream(
588         InputStream inputStream, 
589         OutputStream outputStream
590         ) throws JRException
591     {
592         getDefaultInstance().exportToXmlStream(inputStream, outputStream);
593     }
594
595     
596     /**
597      * @see #exportToXmlStream(JasperPrint, OutputStream)
598      */

599     public static void exportReportToXmlStream(
600         JasperPrint jasperPrint, 
601         OutputStream outputStream
602         ) throws JRException
603     {
604         getDefaultInstance().exportToXmlStream(jasperPrint, outputStream);
605     }
606
607
608     /**
609      * @see #exportToXml(JasperPrint)
610      */

611     public static String exportReportToXml(JasperPrint jasperPrint) throws JRException
612     {
613         return getDefaultInstance().exportToXml(jasperPrint);
614     }
615
616
617     /**
618      * @see #exportToHtmlFile(String)
619      */

620     public static String exportReportToHtmlFile(
621         String sourceFileName
622         ) throws JRException
623     {
624         return getDefaultInstance().exportToHtmlFile(sourceFileName);
625     }
626
627
628     /**
629      * @see #exportToHtmlFile(String, String)
630      */

631     public static void exportReportToHtmlFile(
632         String sourceFileName, 
633         String destFileName
634         ) throws JRException
635     {
636         getDefaultInstance().exportToHtmlFile(sourceFileName, destFileName);
637     }
638
639     
640     /**
641      * @see #exportToHtmlFile(JasperPrint, String)
642      */

643     public static void exportReportToHtmlFile(
644         JasperPrint jasperPrint, 
645         String destFileName
646         ) throws JRException
647     {
648         getDefaultInstance().exportToHtmlFile(jasperPrint, destFileName);
649     }
650 }
651