KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > ui > jasperreports > JasperReportsUtils


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.ui.jasperreports;
18
19 import java.io.OutputStream JavaDoc;
20 import java.io.Writer JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import net.sf.jasperreports.engine.JRDataSource;
25 import net.sf.jasperreports.engine.JRDataSourceProvider;
26 import net.sf.jasperreports.engine.JRException;
27 import net.sf.jasperreports.engine.JRExporter;
28 import net.sf.jasperreports.engine.JRExporterParameter;
29 import net.sf.jasperreports.engine.JasperFillManager;
30 import net.sf.jasperreports.engine.JasperPrint;
31 import net.sf.jasperreports.engine.JasperReport;
32 import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;
33 import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
34 import net.sf.jasperreports.engine.export.JRCsvExporter;
35 import net.sf.jasperreports.engine.export.JRHtmlExporter;
36 import net.sf.jasperreports.engine.export.JRPdfExporter;
37 import net.sf.jasperreports.engine.export.JRXlsExporter;
38
39 /**
40  * Utility methods for working with JasperReports. Provides a set of convenience
41  * methods for generating reports in a CSV, HTML, PDF and XLS formats.
42  *
43  * @author Rob Harrop
44  * @author Juergen Hoeller
45  * @since 1.1.3
46  */

47 public abstract class JasperReportsUtils {
48
49     /**
50      * Convert the given report data value to a <code>JRDataSource</code>.
51      * <p>In the default implementation, a <code>JRDataSource</code>,
52      * <code>java.util.Collection</code> or object array is detected.
53      * The latter are converted to <code>JRBeanCollectionDataSource</code>
54      * or <code>JRBeanArrayDataSource</code>, respectively.
55      * @param value the report data value to convert
56      * @return the JRDataSource
57      * @throws IllegalArgumentException if the value could not be converted
58      * @see net.sf.jasperreports.engine.JRDataSource
59      * @see net.sf.jasperreports.engine.data.JRBeanCollectionDataSource
60      * @see net.sf.jasperreports.engine.data.JRBeanArrayDataSource
61      */

62     public static JRDataSource convertReportData(Object JavaDoc value) throws IllegalArgumentException JavaDoc {
63         if (value instanceof JRDataSource) {
64             return (JRDataSource) value;
65         }
66         else if (value instanceof Collection JavaDoc) {
67             return new JRBeanCollectionDataSource((Collection JavaDoc) value);
68         }
69         else if (value instanceof Object JavaDoc[]) {
70             return new JRBeanArrayDataSource((Object JavaDoc[]) value);
71         }
72         else if (value instanceof JRDataSourceProvider) {
73             return null;
74         }
75         else {
76             throw new IllegalArgumentException JavaDoc("Value [" + value + "] cannot be converted to a JRDataSource");
77         }
78     }
79
80     /**
81      * Render the supplied <code>JasperPrint</code> instance using the
82      * supplied <code>JRAbstractExporter</code> instance and write the results
83      * to the supplied <code>Writer</code>.
84      * <p>Make sure that the <code>JRAbstractExporter</code> implementation
85      * you supply is capable of writing to a <code>Writer</code>.
86      * @param exporter the <code>JRAbstractExporter</code> to use to render the report
87      * @param print the <code>JasperPrint</code> instance to render
88      * @param writer the <code>Writer</code> to write the result to
89      * @throws JRException if rendering failed
90      */

91     public static void render(JRExporter exporter, JasperPrint print, Writer JavaDoc writer)
92             throws JRException {
93         exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
94         exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, writer);
95         exporter.exportReport();
96     }
97
98     /**
99      * Render the supplied <code>JasperPrint</code> instance using the
100      * supplied <code>JRAbstractExporter</code> instance and write the results
101      * to the supplied <code>OutputStream</code>.
102      * <p>Make sure that the <code>JRAbstractExporter</code> implementation you
103      * supply is capable of writing to a <code>OutputStream</code>.
104      * @param exporter the <code>JRAbstractExporter</code> to use to render the report
105      * @param print the <code>JasperPrint</code> instance to render
106      * @param outputStream the <code>OutputStream</code> to write the result to
107      * @throws JRException if rendering failed
108      */

109     public static void render(JRExporter exporter, JasperPrint print, OutputStream JavaDoc outputStream)
110             throws JRException {
111         exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
112         exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
113         exporter.exportReport();
114     }
115
116     /**
117      * Render a report in CSV format using the supplied report data.
118      * Writes the results to the supplied <code>Writer</code>.
119      * @param report the <code>JasperReport</code> instance to render
120      * @param parameters the parameters to use for rendering
121      * @param writer the <code>Writer</code> to write the rendered report to
122      * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code>
123      * or object array (converted accordingly), representing the report data to read
124      * fields from
125      * @throws JRException if rendering failed
126      * @see #convertReportData
127      */

128     public static void renderAsCsv(JasperReport report, Map JavaDoc parameters, Object JavaDoc reportData, Writer JavaDoc writer)
129             throws JRException {
130         JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
131         render(new JRCsvExporter(), print, writer);
132     }
133
134     /**
135      * Render a report in CSV format using the supplied report data.
136      * Writes the results to the supplied <code>Writer</code>.
137      * @param report the <code>JasperReport</code> instance to render
138      * @param parameters the parameters to use for rendering
139      * @param writer the <code>Writer</code> to write the rendered report to
140      * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code>
141      * or object array (converted accordingly), representing the report data to read
142      * fields from
143      * @param exporterParameters a {@link Map} of {@link JRExporterParameter exporter parameters}
144      * @throws JRException if rendering failed
145      * @see #convertReportData
146      */

147     public static void renderAsCsv(JasperReport report, Map JavaDoc parameters, Object JavaDoc reportData, Writer JavaDoc writer,
148                                    Map JavaDoc exporterParameters) throws JRException {
149         JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
150         JRCsvExporter exporter = new JRCsvExporter();
151         exporter.setParameters(exporterParameters);
152         render(exporter, print, writer);
153     }
154
155     /**
156      * Render a report in HTML format using the supplied report data.
157      * Writes the results to the supplied <code>Writer</code>.
158      * @param report the <code>JasperReport</code> instance to render
159      * @param parameters the parameters to use for rendering
160      * @param writer the <code>Writer</code> to write the rendered report to
161      * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code>
162      * or object array (converted accordingly), representing the report data to read
163      * fields from
164      * @throws JRException if rendering failed
165      * @see #convertReportData
166      */

167     public static void renderAsHtml(JasperReport report, Map JavaDoc parameters, Object JavaDoc reportData, Writer JavaDoc writer)
168             throws JRException {
169         JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
170         render(new JRHtmlExporter(), print, writer);
171     }
172
173     /**
174      * Render a report in HTML format using the supplied report data.
175      * Writes the results to the supplied <code>Writer</code>.
176      * @param report the <code>JasperReport</code> instance to render
177      * @param parameters the parameters to use for rendering
178      * @param writer the <code>Writer</code> to write the rendered report to
179      * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code>
180      * or object array (converted accordingly), representing the report data to read
181      * fields from
182      * @param exporterParameters a {@link Map} of {@link JRExporterParameter exporter parameters}
183      * @throws JRException if rendering failed
184      * @see #convertReportData
185      */

186     public static void renderAsHtml(JasperReport report, Map JavaDoc parameters, Object JavaDoc reportData, Writer JavaDoc writer,
187                                     Map JavaDoc exporterParameters) throws JRException {
188         JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
189         JRHtmlExporter exporter = new JRHtmlExporter();
190         exporter.setParameters(exporterParameters);
191         render(exporter, print, writer);
192     }
193
194     /**
195      * Render a report in PDF format using the supplied report data.
196      * Writes the results to the supplied <code>OutputStream</code>.
197      * @param report the <code>JasperReport</code> instance to render
198      * @param parameters the parameters to use for rendering
199      * @param stream the <code>OutputStream</code> to write the rendered report to
200      * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code>
201      * or object array (converted accordingly), representing the report data to read
202      * fields from
203      * @throws JRException if rendering failed
204      * @see #convertReportData
205      */

206     public static void renderAsPdf(JasperReport report, Map JavaDoc parameters, Object JavaDoc reportData, OutputStream JavaDoc stream)
207             throws JRException {
208
209         JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
210         render(new JRPdfExporter(), print, stream);
211     }
212
213     /**
214      * Render a report in PDF format using the supplied report data.
215      * Writes the results to the supplied <code>OutputStream</code>.
216      * @param report the <code>JasperReport</code> instance to render
217      * @param parameters the parameters to use for rendering
218      * @param stream the <code>OutputStream</code> to write the rendered report to
219      * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code>
220      * or object array (converted accordingly), representing the report data to read
221      * fields from
222      * @param exporterParameters a {@link Map} of {@link JRExporterParameter exporter parameters}
223      * @throws JRException if rendering failed
224      * @see #convertReportData
225      */

226     public static void renderAsPdf(JasperReport report, Map JavaDoc parameters, Object JavaDoc reportData, OutputStream JavaDoc stream,
227                                    Map JavaDoc exporterParameters) throws JRException {
228         JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
229         JRPdfExporter exporter = new JRPdfExporter();
230         exporter.setParameters(exporterParameters);
231         render(exporter, print, stream);
232     }
233
234     /**
235      * Render a report in XLS format using the supplied report data.
236      * Writes the results to the supplied <code>OutputStream</code>.
237      * @param report the <code>JasperReport</code> instance to render
238      * @param parameters the parameters to use for rendering
239      * @param stream the <code>OutputStream</code> to write the rendered report to
240      * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code>
241      * or object array (converted accordingly), representing the report data to read
242      * fields from
243      * @throws JRException if rendering failed
244      * @see #convertReportData
245      */

246     public static void renderAsXls(JasperReport report, Map JavaDoc parameters, Object JavaDoc reportData, OutputStream JavaDoc stream)
247             throws JRException {
248
249         JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
250         render(new JRXlsExporter(), print, stream);
251     }
252
253     /**
254      * Render a report in XLS format using the supplied report data.
255      * Writes the results to the supplied <code>OutputStream</code>.
256      * @param report the <code>JasperReport</code> instance to render
257      * @param parameters the parameters to use for rendering
258      * @param stream the <code>OutputStream</code> to write the rendered report to
259      * @param reportData a <code>JRDataSource</code>, <code>java.util.Collection</code>
260      * or object array (converted accordingly), representing the report data to read
261      * fields from
262      * @param exporterParameters a {@link Map} of {@link JRExporterParameter exporter parameters}
263      * @throws JRException if rendering failed
264      * @see #convertReportData
265      */

266     public static void renderAsXls(JasperReport report, Map JavaDoc parameters, Object JavaDoc reportData, OutputStream JavaDoc stream,
267                                    Map JavaDoc exporterParameters) throws JRException {
268
269         JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData));
270         JRXlsExporter exporter = new JRXlsExporter();
271         exporter.setParameters(exporterParameters);
272         render(exporter, print, stream);
273     }
274 }
275
Popular Tags