KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.util.Collection JavaDoc;
34
35 import net.sf.jasperreports.engine.design.JRDefaultCompiler;
36 import net.sf.jasperreports.engine.design.JRVerifier;
37 import net.sf.jasperreports.engine.design.JasperDesign;
38 import net.sf.jasperreports.engine.util.JRLoader;
39 import net.sf.jasperreports.engine.util.JRSaver;
40 import net.sf.jasperreports.engine.xml.JRXmlLoader;
41 import net.sf.jasperreports.engine.xml.JRXmlWriter;
42
43
44 /**
45  * Façade class for compiling report designs into the ready-to-fill form
46  * and for getting the XML representation of report design objects for
47  * storage or network transfer.
48  *
49  * Report compilation using this class is delegated to the
50  * {@link net.sf.jasperreports.engine.design.JRDefaultCompiler}.
51  *
52  * @see net.sf.jasperreports.engine.design.JasperDesign
53  * @see net.sf.jasperreports.engine.JasperReport
54  * @see net.sf.jasperreports.engine.design.JRDefaultCompiler
55  * @see net.sf.jasperreports.engine.design.JRVerifier
56  * @see net.sf.jasperreports.engine.xml.JRXmlLoader
57  * @see net.sf.jasperreports.engine.xml.JRXmlWriter
58  * @see net.sf.jasperreports.engine.util.JRLoader
59  * @see net.sf.jasperreports.engine.util.JRSaver
60  * @author Teodor Danciu (teodord@users.sourceforge.net)
61  * @version $Id: JasperCompileManager.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
62  */

63 public class JasperCompileManager
64 {
65
66
67     /**
68      * Compiles the XML report design file specified by the parameter.
69      * The result of this operation is another file that will contain the serialized
70      * {@link net.sf.jasperreports.engine.JasperReport} object representing the compiled report design,
71      * having the same name as the report design as declared in the XML plus the <code>*.jasper</code> extension,
72      * located in the same directory as the XML source file.
73      *
74      * @param sourceFileName XML source file name
75      * @return resulting file name containing a serialized {@link net.sf.jasperreports.engine.JasperReport} object
76      */

77     public static String JavaDoc compileReportToFile(String JavaDoc sourceFileName) throws JRException
78     {
79         File JavaDoc sourceFile = new File JavaDoc(sourceFileName);
80
81         JasperDesign jasperDesign = JRXmlLoader.load(sourceFileName);
82
83         File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperDesign.getName() + ".jasper");
84         String JavaDoc destFileName = destFile.toString();
85
86         compileReportToFile(jasperDesign, destFileName);
87         
88         return destFileName;
89     }
90
91
92     /**
93      * Compiles the XML report design file received as the first parameter, placing the result
94      * in the file specified by the second parameter.
95      * The resulting file will contain a serialized instance of a
96      * {@link net.sf.jasperreports.engine.JasperReport} object representing
97      * the compiled report design.
98      *
99      * @param sourceFileName XML source file name
100      * @param destFileName file name to place the result into
101      */

102     public static void compileReportToFile(
103         String JavaDoc sourceFileName,
104         String JavaDoc destFileName
105         ) throws JRException
106     {
107         JasperDesign jasperDesign = JRXmlLoader.load(sourceFileName);
108
109         compileReportToFile(jasperDesign, destFileName);
110     }
111
112
113     /**
114      * Compiles the report design object received as the first parameter, placing the result
115      * in the file specified by the second parameter.
116      * The resulting file will contain a serialized instance of a
117      * {@link net.sf.jasperreports.engine.JasperReport} object representing the compiled report design.
118      *
119      * @param jasperDesign source report design object
120      * @param destFileName file name to place the compiled report design into
121      */

122     public static void compileReportToFile(
123         JasperDesign jasperDesign,
124         String JavaDoc destFileName
125         ) throws JRException
126     {
127         JasperReport jasperReport = JRDefaultCompiler.getInstance().compileReport(jasperDesign);
128
129         JRSaver.saveObject(jasperReport, destFileName);
130     }
131
132
133     /**
134      * Compiles the XML report design file received as parameter, and returns
135      * the compiled report design object.
136      *
137      * @param sourceFileName XML source file name
138      * @return compiled report design object
139      */

140     public static JasperReport compileReport(String JavaDoc sourceFileName) throws JRException
141     {
142         JasperDesign jasperDesign = JRXmlLoader.load(sourceFileName);
143
144         return compileReport(jasperDesign);
145     }
146
147
148     /**
149      * Compiles the XML representation of the report design read from the supplied input stream and
150      * writes the generated compiled report design object to the output stream specified
151      * by the second parameter.
152      *
153      * @param inputStream XML source input stream
154      * @param outputStream output stream to write the compiled report design to
155      */

156     public static void compileReportToStream(
157         InputStream JavaDoc inputStream,
158         OutputStream JavaDoc outputStream
159         ) throws JRException
160     {
161         JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
162
163         compileReportToStream(jasperDesign, outputStream);
164     }
165
166
167     /**
168      * Compiles the report design object represented by the first parameter and
169      * writes the generated compiled report design object to the output stream specified
170      * by the second parameter.
171      *
172      * @param jasperDesign source report design object
173      * @param outputStream output stream to write the compiled report design to
174      */

175     public static void compileReportToStream(
176         JasperDesign jasperDesign,
177         OutputStream JavaDoc outputStream
178         ) throws JRException
179     {
180         JasperReport jasperReport = JRDefaultCompiler.getInstance().compileReport(jasperDesign);
181
182         JRSaver.saveObject(jasperReport, outputStream);
183     }
184
185
186     /**
187      * Compiles the serialized report design object read from the supplied input stream and
188      * returns the generated compiled report design object.
189      *
190      * @param inputStream XML source input stream
191      * @return compiled report design object
192      */

193     public static JasperReport compileReport(InputStream JavaDoc inputStream) throws JRException
194     {
195         JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
196
197         return compileReport(jasperDesign);
198     }
199
200
201     /**
202      * Compiles the report design object received as parameter and
203      * returns the generated compiled report design object.
204      *
205      * @param jasperDesign source report design object
206      * @return compiled report design object
207      * @see net.sf.jasperreports.engine.design.JRDefaultCompiler
208      */

209     public static JasperReport compileReport(JasperDesign jasperDesign) throws JRException
210     {
211         return JRDefaultCompiler.getInstance().compileReport(jasperDesign);
212     }
213
214
215     /**
216      * Verifies the validity and consistency of the report design object.
217      * Returns a collection of error messages (String), if problems are found in the report design.
218      *
219      * @param jasperDesign report design object to verify
220      * @return collection of String messages if problems are found
221      * @see net.sf.jasperreports.engine.design.JRVerifier
222      */

223     public static Collection JavaDoc verifyDesign(JasperDesign jasperDesign)
224     {
225         return JRVerifier.verifyDesign(jasperDesign);
226     }
227
228
229     /**
230      * Generates the XML representation of the report design loaded from the specified filename.
231      * The result of this operation is an "UTF-8" encoded XML file having the same name as
232      * the report design, plus the <code>*.jasper.jrxml</code> extension, located in the same directory as
233      * the source file.
234      *
235      * @param sourceFileName source file name containing the report design object
236      * @return XML representation of the report design
237      */

238     public static String JavaDoc writeReportToXmlFile(
239         String JavaDoc sourceFileName
240         ) throws JRException
241     {
242         File JavaDoc sourceFile = new File JavaDoc(sourceFileName);
243
244         /* We need the report name. */
245         JRReport report = (JRReport)JRLoader.loadObject(sourceFile);
246
247         File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), report.getName() + ".jasper.jrxml");
248         String JavaDoc destFileName = destFile.toString();
249         
250         writeReportToXmlFile(
251             report,
252             destFileName
253             );
254         
255         return destFileName;
256     }
257
258
259     /**
260      * Generates the XML representation of the report design loaded from the first file parameter
261      * and place it in the file specified by the second parameter. The result is "UTF-8" encoded.
262      *
263      * @param sourceFileName source file name containing the report design object
264      * @param destFileName output file name to write the XML report design representation to
265      */

266     public static void writeReportToXmlFile(
267         String JavaDoc sourceFileName,
268         String JavaDoc destFileName
269         ) throws JRException
270     {
271         JRReport report = (JRReport)JRLoader.loadObject(sourceFileName);
272
273         writeReportToXmlFile(
274             report,
275             destFileName
276             );
277     }
278
279     
280     /**
281      * Generates the XML representation of the report design supplied as the first parameter
282      * and place it in the file specified by the second parameter. The result is "UTF-8" encoded.
283      *
284      * @param report source report design object
285      * @param destFileName output file name to write the XML report design representation to
286      * @see net.sf.jasperreports.engine.xml.JRXmlWriter
287      */

288     public static void writeReportToXmlFile(
289         JRReport report,
290         String JavaDoc destFileName
291         ) throws JRException
292     {
293         JRXmlWriter.writeReport(
294             report,
295             destFileName,
296             "UTF-8"
297             );
298     }
299
300
301     /**
302      * Generates the XML representation of the serialized report design object read from the supplied
303      * input stream abd writes it to the specified output stream, using the "UTF-8" encoding.
304      *
305      * @param inputStream source input stream to read the report design object from
306      * @param outputStream output stream to write the XML report design representation to
307      */

308     public static void writeReportToXmlStream(
309         InputStream JavaDoc inputStream,
310         OutputStream JavaDoc outputStream
311         ) throws JRException
312     {
313         JRReport report = (JRReport)JRLoader.loadObject(inputStream);
314
315         writeReportToXmlStream(report, outputStream);
316     }
317
318     
319     /**
320      * Generates the XML representation of the report design object supplied as parameter
321      * and writes it to the specified output stream, using the "UTF-8" encoding.
322      *
323      * @param report source report design object
324      * @param outputStream output stream to write the XML report design representation to
325      * @see net.sf.jasperreports.engine.xml.JRXmlWriter
326      */

327     public static void writeReportToXmlStream(
328         JRReport report,
329         OutputStream JavaDoc outputStream
330         ) throws JRException
331     {
332         JRXmlWriter.writeReport(
333             report,
334             outputStream,
335             "UTF-8"
336             );
337     }
338
339
340     /**
341      * Generates the XML representation of the report design object supplied as parameter
342      * using the "UTF-8" enconding.
343      *
344      * @param report source report design object
345      * @return XML representation of the report design
346      * @see net.sf.jasperreports.engine.xml.JRXmlWriter
347      */

348     public static String JavaDoc writeReportToXml(JRReport report)
349     {
350         return JRXmlWriter.writeReport(report, "UTF-8");
351     }
352
353
354 }
355
Popular Tags