KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > VirtualizerApp


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
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import net.sf.jasperreports.engine.JRDataSource;
33 import net.sf.jasperreports.engine.JREmptyDataSource;
34 import net.sf.jasperreports.engine.JRException;
35 import net.sf.jasperreports.engine.JRExporterParameter;
36 import net.sf.jasperreports.engine.JRParameter;
37 import net.sf.jasperreports.engine.JasperExportManager;
38 import net.sf.jasperreports.engine.JasperFillManager;
39 import net.sf.jasperreports.engine.JasperPrint;
40 import net.sf.jasperreports.engine.JasperPrintManager;
41 import net.sf.jasperreports.engine.export.JRCsvExporter;
42 import net.sf.jasperreports.engine.fill.JRFileVirtualizer;
43 import net.sf.jasperreports.view.JasperViewer;
44
45 /**
46  * @author Teodor Danciu (teodord@users.sourceforge.net)
47  * @version $Id: VirtualizerApp.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
48  */

49 public class VirtualizerApp
50 {
51
52     /**
53      *
54      */

55     private static final String JavaDoc TASK_PRINT = "print";
56
57     private static final String JavaDoc TASK_PDF = "pdf";
58     
59     private static final String JavaDoc TASK_XML = "xml";
60
61     private static final String JavaDoc TASK_XML_EMBED = "xmlEmbed";
62
63     private static final String JavaDoc TASK_HTML = "html";
64
65     private static final String JavaDoc TASK_CSV = "csv";
66     
67     private static final String JavaDoc TASK_VIEW = "view";
68     
69     private static final String JavaDoc TASK_EXPORT = "export";
70
71     /**
72      *
73      */

74     public static void main(String JavaDoc[] args)
75     {
76         String JavaDoc fileName = null;
77         String JavaDoc outFileName = null;
78         String JavaDoc taskName = null;
79         
80
81         if (args.length == 0)
82         {
83             usage();
84             return;
85         }
86
87         int k = 0;
88         while (args.length > k)
89         {
90             if (args[k].startsWith("-T"))
91                 taskName = args[k].substring(2);
92             else if (args[k].startsWith("-F"))
93                 fileName = args[k].substring(2);
94             else if (args[k].startsWith("-O"))
95                 outFileName = args[k].substring(2);
96
97             k++;
98         }
99
100         try
101         {
102             // Virtualization works only with in memory JasperPrint objects.
103
// All the operations will first fill the report and then export
104
// the filled object.
105

106             // creating the data source
107
JRDataSource ds = new JREmptyDataSource(1000);
108             
109             // creating the virtualizer
110
JRFileVirtualizer virtualizer = new JRFileVirtualizer(2, "tmp");
111             
112             // filling the report
113
JasperPrint jasperPrint = fillReport(fileName, ds, virtualizer);
114
115
116             if (TASK_PRINT.equals(taskName))
117             {
118                 JasperPrintManager.printReport(jasperPrint, true);
119             }
120             else if (TASK_PDF.equals(taskName))
121             {
122                 exportPDF(outFileName, jasperPrint);
123             }
124             else if (TASK_XML.equals(taskName))
125             {
126                 exportXML(outFileName, jasperPrint, false);
127             }
128             else if (TASK_XML_EMBED.equals(taskName))
129             {
130                 exportXML(outFileName, jasperPrint, true);
131             }
132             else if (TASK_HTML.equals(taskName))
133             {
134                 exportHTML(outFileName, jasperPrint);
135             }
136             else if (TASK_CSV.equals(taskName))
137             {
138                 exportCSV(outFileName, jasperPrint);
139             }
140             else if (TASK_EXPORT.equals(taskName))
141             {
142                 exportPDF(outFileName + ".pdf", jasperPrint);
143                 exportXML(outFileName + ".jrpxml", jasperPrint, false);
144                 exportHTML(outFileName + ".html", jasperPrint);
145                 exportCSV(outFileName + ".csv", jasperPrint);
146                 
147                 // manually cleaning up
148
virtualizer.cleanup();
149             }
150             else if (TASK_VIEW.equals(taskName))
151             {
152                 JasperViewer.viewReport(jasperPrint, true);
153             }
154             else
155             {
156                 usage();
157                 System.exit(0);
158             }
159         }
160         catch (JRException e)
161         {
162             e.printStackTrace();
163             System.exit(1);
164         }
165         catch (Exception JavaDoc e)
166         {
167             e.printStackTrace();
168             System.exit(1);
169         }
170     }
171
172     private static void exportCSV(String JavaDoc outFileName, JasperPrint jasperPrint) throws JRException
173     {
174         long start = System.currentTimeMillis();
175         JRCsvExporter exporter = new JRCsvExporter();
176
177         exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
178         exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFileName);
179
180         exporter.exportReport();
181
182         System.err.println("CSV creation time : " + (System.currentTimeMillis() - start));
183     }
184
185     private static void exportHTML(String JavaDoc outFileName, JasperPrint jasperPrint) throws JRException
186     {
187         long start = System.currentTimeMillis();
188         JasperExportManager.exportReportToHtmlFile(jasperPrint, outFileName);
189         System.err.println("HTML creation time : " + (System.currentTimeMillis() - start));
190     }
191
192     private static void exportXML(String JavaDoc outFileName, JasperPrint jasperPrint, boolean embedded) throws JRException
193     {
194         long start = System.currentTimeMillis();
195         JasperExportManager.exportReportToXmlFile(jasperPrint, outFileName, embedded);
196         System.err.println("XML creation time : " + (System.currentTimeMillis() - start));
197     }
198
199     private static void exportPDF(String JavaDoc outFileName, JasperPrint jasperPrint) throws JRException
200     {
201         long start = System.currentTimeMillis();
202         JasperExportManager.exportReportToPdfFile(jasperPrint, outFileName);
203         System.err.println("PDF creation time : " + (System.currentTimeMillis() - start));
204     }
205     
206
207
208     private static JasperPrint fillReport(String JavaDoc fileName, JRDataSource dataSource, JRFileVirtualizer virtualizer) throws JRException
209     {
210         long start = System.currentTimeMillis();
211
212         // Preparing parameters
213
Map JavaDoc parameters = new HashMap JavaDoc();
214         parameters.put(JRParameter.REPORT_VIRTUALIZER, virtualizer);
215
216         JasperPrint jasperPrint = JasperFillManager.fillReport(fileName, parameters, dataSource);
217         
218         virtualizer.setReadOnly(true);
219
220         System.err.println("Filling time : " + (System.currentTimeMillis() - start));
221         return jasperPrint;
222     }
223
224     /**
225      *
226      */

227     private static void usage()
228     {
229         System.out.println("VirtualizerApp usage:");
230         System.out.println("\tjava VirtualizerApp -Ttask -Ffile");
231         System.out.println("\tTasks : print | pdf | xml | xmlEmbed | html | csv | export | view");
232     }
233
234 }
235
Popular Tags