001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine;
029:
030: import java.io.ByteArrayOutputStream;
031: import java.io.File;
032: import java.io.InputStream;
033: import java.io.OutputStream;
034:
035: import net.sf.jasperreports.engine.export.JRHtmlExporter;
036: import net.sf.jasperreports.engine.export.JRPdfExporter;
037: import net.sf.jasperreports.engine.export.JRXmlExporter;
038: import net.sf.jasperreports.engine.export.JRXmlExporterParameter;
039: import net.sf.jasperreports.engine.util.JRLoader;
040:
041: /**
042: * Façade class for exporting generated reports into more popular
043: * formats such as PDF, HTML and XML.
044: * This class contains convenience methods for exporting to only these 3 formats.
045: * <p>
046: * For exporting to XLS and CSV format or for using special exporter parameters,
047: * the specific exporter class should be used directly.
048: *
049: * @see net.sf.jasperreports.engine.JasperPrint
050: * @see net.sf.jasperreports.engine.export.JRHtmlExporter
051: * @see net.sf.jasperreports.engine.export.JRPdfExporter
052: * @see net.sf.jasperreports.engine.export.JRXmlExporter
053: * @see net.sf.jasperreports.engine.export.JRXlsExporter
054: * @see net.sf.jasperreports.engine.export.JRCsvExporter
055: * @author Teodor Danciu (teodord@users.sourceforge.net)
056: * @version $Id: JasperExportManager.java 1229 2006-04-19 10:27:35Z teodord $
057: */
058: public class JasperExportManager {
059:
060: /**
061: * Exports the generated report file specified by the parameter into PDF format.
062: * The resulting PDF file has the same name as the report object inside the source file,
063: * plus the <code>*.pdf</code> extension and it is located in the same directory as the source file.
064: *
065: * @param sourceFileName source file containing the generated report
066: * @return resulting PDF file name
067: * @see net.sf.jasperreports.engine.export.JRPdfExporter
068: */
069: public static String exportReportToPdfFile(String sourceFileName)
070: throws JRException {
071: File sourceFile = new File(sourceFileName);
072:
073: /* We need the report name. */
074: JasperPrint jasperPrint = (JasperPrint) JRLoader
075: .loadObject(sourceFile);
076:
077: File destFile = new File(sourceFile.getParent(), jasperPrint
078: .getName()
079: + ".pdf");
080: String destFileName = destFile.toString();
081:
082: exportReportToPdfFile(jasperPrint, destFileName);
083:
084: return destFileName;
085: }
086:
087: /**
088: * Exports the generated report file specified by the first parameter into PDF format,
089: * the result being placed in the second file parameter.
090: *
091: * @param sourceFileName source file containing the generated report
092: * @param destFileName file name to place the PDF content into
093: * @see net.sf.jasperreports.engine.export.JRPdfExporter
094: */
095: public static void exportReportToPdfFile(String sourceFileName,
096: String destFileName) throws JRException {
097: JasperPrint jasperPrint = (JasperPrint) JRLoader
098: .loadObject(sourceFileName);
099:
100: exportReportToPdfFile(jasperPrint, destFileName);
101: }
102:
103: /**
104: * Exports the generated report file specified by the first parameter into PDF format,
105: * the result being placed in the second file parameter.
106: *
107: * @param jasperPrint report object to export
108: * @param destFileName file name to place the PDF content into
109: * @see net.sf.jasperreports.engine.export.JRPdfExporter
110: */
111: public static void exportReportToPdfFile(JasperPrint jasperPrint,
112: String destFileName) throws JRException {
113: /* */
114: JRPdfExporter exporter = new JRPdfExporter();
115:
116: exporter.setParameter(JRExporterParameter.JASPER_PRINT,
117: jasperPrint);
118: exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,
119: destFileName);
120:
121: exporter.exportReport();
122: }
123:
124: /**
125: * Exports the generated report read from the supplied input stream into PDF format and
126: * writes the results to the output stream specified by the second parameter.
127: *
128: * @param inputStream input stream to read the generated report object from
129: * @param outputStream output stream to write the resulting PDF content to
130: * @see net.sf.jasperreports.engine.export.JRPdfExporter
131: */
132: public static void exportReportToPdfStream(InputStream inputStream,
133: OutputStream outputStream) throws JRException {
134: JasperPrint jasperPrint = (JasperPrint) JRLoader
135: .loadObject(inputStream);
136:
137: exportReportToPdfStream(jasperPrint, outputStream);
138: }
139:
140: /**
141: * Exports the generated report object received as first parameter into PDF format and
142: * writes the results to the output stream specified by the second parameter.
143: *
144: * @param jasperPrint report object to export
145: * @param outputStream output stream to write the resulting PDF content to
146: * @see net.sf.jasperreports.engine.export.JRPdfExporter
147: */
148: public static void exportReportToPdfStream(JasperPrint jasperPrint,
149: OutputStream outputStream) throws JRException {
150: JRPdfExporter exporter = new JRPdfExporter();
151:
152: exporter.setParameter(JRExporterParameter.JASPER_PRINT,
153: jasperPrint);
154: exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
155: outputStream);
156:
157: exporter.exportReport();
158: }
159:
160: /**
161: * Exports the generated report object received as parameter into PDF format and
162: * returns the binary content as a byte array.
163: *
164: * @param jasperPrint report object to export
165: * @return byte array representing the resulting PDF content
166: * @see net.sf.jasperreports.engine.export.JRPdfExporter
167: */
168: public static byte[] exportReportToPdf(JasperPrint jasperPrint)
169: throws JRException {
170: ByteArrayOutputStream baos = new ByteArrayOutputStream();
171:
172: JRPdfExporter exporter = new JRPdfExporter();
173:
174: exporter.setParameter(JRExporterParameter.JASPER_PRINT,
175: jasperPrint);
176: exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
177:
178: exporter.exportReport();
179:
180: return baos.toByteArray();
181: }
182:
183: /**
184: * Exports the generated report file specified by the parameter into XML format.
185: * The resulting XML file has the same name as the report object inside the source file,
186: * plus the <code>*.jrpxml</code> extension and it is located in the same directory as the source file.
187: * <p>
188: * When exporting to XML format, the images can be either embedded in the XML content
189: * itself using the Base64 encoder or be referenced as external resources.
190: * If not embedded, the images are placed as distinct files inside a directory
191: * having the same name as the XML destination file, plus the "_files" suffix.
192: *
193: * @param sourceFileName source file containing the generated report
194: * @param isEmbeddingImages flag that indicates whether the images should be embedded in the
195: * XML content itself using the Base64 encoder or be referenced as external resources
196: * @return XML representation of the generated report
197: * @see net.sf.jasperreports.engine.export.JRPdfExporter
198: */
199: public static String exportReportToXmlFile(String sourceFileName,
200: boolean isEmbeddingImages) throws JRException {
201: File sourceFile = new File(sourceFileName);
202:
203: /* We need the report name. */
204: JasperPrint jasperPrint = (JasperPrint) JRLoader
205: .loadObject(sourceFile);
206:
207: File destFile = new File(sourceFile.getParent(), jasperPrint
208: .getName()
209: + ".jrpxml");
210: String destFileName = destFile.toString();
211:
212: exportReportToXmlFile(jasperPrint, destFileName,
213: isEmbeddingImages);
214:
215: return destFileName;
216: }
217:
218: /**
219: * Exports the generated report file specified by the first parameter into XML format,
220: * placing the result into the second file parameter.
221: * <p>
222: * If not embedded into the XML content itself using the Base64 encoder,
223: * the images are placed as distinct files inside a directory having the same name
224: * as the XML destination file, plus the "_files" suffix.
225: *
226: * @param sourceFileName source file containing the generated report
227: * @param destFileName file name to place the XML representation into
228: * @param isEmbeddingImages flag that indicates whether the images should be embedded in the
229: * XML content itself using the Base64 encoder or be referenced as external resources
230: * @see net.sf.jasperreports.engine.export.JRPdfExporter
231: */
232: public static void exportReportToXmlFile(String sourceFileName,
233: String destFileName, boolean isEmbeddingImages)
234: throws JRException {
235: JasperPrint jasperPrint = (JasperPrint) JRLoader
236: .loadObject(sourceFileName);
237:
238: exportReportToXmlFile(jasperPrint, destFileName,
239: isEmbeddingImages);
240: }
241:
242: /**
243: * Exports the generated report object received as parameter into XML format,
244: * placing the result into the second file parameter.
245: * <p>
246: * If not embedded into the XML content itself using the Base64 encoder,
247: * the images are placed as distinct files inside a directory having the same name
248: * as the XML destination file, plus the "_files" suffix.
249: *
250: * @param jasperPrint report object to export
251: * @param destFileName file name to place the XML representation into
252: * @param isEmbeddingImages flag that indicates whether the images should be embedded in the
253: * XML content itself using the Base64 encoder or be referenced as external resources
254: *
255: * @see net.sf.jasperreports.engine.export.JRPdfExporter
256: */
257: public static void exportReportToXmlFile(JasperPrint jasperPrint,
258: String destFileName, boolean isEmbeddingImages)
259: throws JRException {
260: JRXmlExporter exporter = new JRXmlExporter();
261:
262: exporter.setParameter(JRExporterParameter.JASPER_PRINT,
263: jasperPrint);
264: exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,
265: destFileName);
266: exporter.setParameter(
267: JRXmlExporterParameter.IS_EMBEDDING_IMAGES,
268: isEmbeddingImages ? Boolean.TRUE : Boolean.FALSE);
269:
270: exporter.exportReport();
271: }
272:
273: /**
274: * Exports the generated report object read from the supplied input stream into XML format,
275: * and writes the result to the output stream specified by the second parameter.
276: * The images are embedded into the XML content itself using the Base64 encoder.
277: *
278: * @param inputStream input stream to read the generated report object from
279: * @param outputStream output stream to write the resulting XML representation to
280: * @see net.sf.jasperreports.engine.export.JRPdfExporter
281: */
282: public static void exportReportToXmlStream(InputStream inputStream,
283: OutputStream outputStream) throws JRException {
284: JasperPrint jasperPrint = (JasperPrint) JRLoader
285: .loadObject(inputStream);
286:
287: exportReportToXmlStream(jasperPrint, outputStream);
288: }
289:
290: /**
291: * Exports the generated report object supplied as the first parameter into XML format,
292: * and writes the result to the output stream specified by the second parameter.
293: * The images are embedded into the XML content itself using the Base64 encoder.
294: *
295: * @param jasperPrint report object to export
296: * @param outputStream output stream to write the resulting XML representation to
297: * @see net.sf.jasperreports.engine.export.JRPdfExporter
298: */
299: public static void exportReportToXmlStream(JasperPrint jasperPrint,
300: OutputStream outputStream) throws JRException {
301: JRXmlExporter exporter = new JRXmlExporter();
302:
303: exporter.setParameter(JRExporterParameter.JASPER_PRINT,
304: jasperPrint);
305: exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
306: outputStream);
307:
308: exporter.exportReport();
309: }
310:
311: /**
312: * Exports the generated report object supplied as parameter into XML format
313: * and returs the result as String.
314: * The images are embedded into the XML content itself using the Base64 encoder.
315: *
316: * @param jasperPrint report object to export
317: * @return XML representation of the generated report
318: * @see net.sf.jasperreports.engine.export.JRPdfExporter
319: */
320: public static String exportReportToXml(JasperPrint jasperPrint)
321: throws JRException {
322: StringBuffer sbuffer = new StringBuffer();
323:
324: JRXmlExporter exporter = new JRXmlExporter();
325:
326: exporter.setParameter(JRExporterParameter.JASPER_PRINT,
327: jasperPrint);
328: exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER,
329: sbuffer);
330:
331: exporter.exportReport();
332:
333: return sbuffer.toString();
334: }
335:
336: /**
337: * Exports the generated report file specified by the parameter into HTML format.
338: * The resulting HTML file has the same name as the report object inside the source file,
339: * plus the <code>*.html</code> extension and it is located in the same directory as the source file.
340: * The images are placed as distinct files inside a directory having the same name
341: * as the HTML destination file, plus the "_files" suffix.
342: *
343: * @param sourceFileName source file containing the generated report
344: * @return resulting HTML file name
345: * @see net.sf.jasperreports.engine.export.JRHtmlExporter
346: */
347: public static String exportReportToHtmlFile(String sourceFileName)
348: throws JRException {
349: File sourceFile = new File(sourceFileName);
350:
351: /* We need the report name. */
352: JasperPrint jasperPrint = (JasperPrint) JRLoader
353: .loadObject(sourceFile);
354:
355: File destFile = new File(sourceFile.getParent(), jasperPrint
356: .getName()
357: + ".html");
358: String destFileName = destFile.toString();
359:
360: exportReportToHtmlFile(jasperPrint, destFileName);
361:
362: return destFileName;
363: }
364:
365: /**
366: * Exports the generated report file specified by the first parameter into HTML format,
367: * placing the result into the second file parameter.
368: * <p>
369: * The images are placed as distinct files inside a directory having the same name
370: * as the HTML destination file, plus the "_files" suffix.
371: *
372: * @param sourceFileName source file containing the generated report
373: * @param destFileName file name to place the HTML content into
374: * @see net.sf.jasperreports.engine.export.JRPdfExporter
375: */
376: public static void exportReportToHtmlFile(String sourceFileName,
377: String destFileName) throws JRException {
378: JasperPrint jasperPrint = (JasperPrint) JRLoader
379: .loadObject(sourceFileName);
380:
381: exportReportToHtmlFile(jasperPrint, destFileName);
382: }
383:
384: /**
385: * Exports the generated report object received as parameter into HTML format,
386: * placing the result into the second file parameter.
387: * <p>
388: * The images are placed as distinct files inside a directory having the same name
389: * as the HTML destination file, plus the "_files" suffix.
390: *
391: * @param jasperPrint report object to export
392: * @param destFileName file name to place the HTML content into
393: * @see net.sf.jasperreports.engine.export.JRPdfExporter
394: */
395: public static void exportReportToHtmlFile(JasperPrint jasperPrint,
396: String destFileName) throws JRException {
397: JRHtmlExporter exporter = new JRHtmlExporter();
398:
399: exporter.setParameter(JRExporterParameter.JASPER_PRINT,
400: jasperPrint);
401: exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,
402: destFileName);
403:
404: exporter.exportReport();
405: }
406:
407: }
|