KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Java5App


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.io.File JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32
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.JasperCompileManager;
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.JasperReport;
42 import net.sf.jasperreports.engine.JasperRunManager;
43 import net.sf.jasperreports.engine.export.JExcelApiExporter;
44 import net.sf.jasperreports.engine.export.JRCsvExporter;
45 import net.sf.jasperreports.engine.export.JRRtfExporter;
46 import net.sf.jasperreports.engine.export.JRXlsExporter;
47 import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
48 import net.sf.jasperreports.engine.util.JRLoader;
49 import net.sf.jasperreports.engine.util.JRSaver;
50 import net.sf.jasperreports.engine.xml.JRXmlWriter;
51
52 /**
53  * @author Lucian Chirita (lucianc@users.sourceforge.net)
54  * @version $Id: Java5App.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
55  */

56 public class Java5App
57 {
58     
59     private static final String JavaDoc TASK_FILL = "fill";
60     private static final String JavaDoc TASK_PRINT = "print";
61     private static final String JavaDoc TASK_PDF = "pdf";
62     private static final String JavaDoc TASK_XML = "xml";
63     private static final String JavaDoc TASK_XML_EMBED = "xmlEmbed";
64     private static final String JavaDoc TASK_HTML = "html";
65     private static final String JavaDoc TASK_RTF = "rtf";
66     private static final String JavaDoc TASK_XLS = "xls";
67     private static final String JavaDoc TASK_JXL = "jxl";
68     private static final String JavaDoc TASK_CSV = "csv";
69     private static final String JavaDoc TASK_RUN = "run";
70
71     
72     public static void main(String JavaDoc[] args)
73     {
74         String JavaDoc fileName = null;
75         String JavaDoc taskName = null;
76
77         if (args.length == 0)
78         {
79             usage();
80             return;
81         }
82
83         int k = 0;
84         while (args.length > k)
85         {
86             if (args[k].startsWith("-T"))
87                 taskName = args[k].substring(2);
88             if (args[k].startsWith("-F"))
89                 fileName = args[k].substring(2);
90
91             k++;
92         }
93
94         try
95         {
96             long start = System.currentTimeMillis();
97             if (TASK_FILL.equals(taskName))
98             {
99                 Map JavaDoc<String JavaDoc,Object JavaDoc> parameters = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
100                 parameters.put("greeting", Greeting.bye);
101                 
102                 JasperFillManager.fillReportToFile(fileName, parameters, new JREmptyDataSource());
103                 System.err.println("Filling time : " + (System.currentTimeMillis() - start));
104             }
105             else if (TASK_PRINT.equals(taskName))
106             {
107                 JasperPrintManager.printReport(fileName, true);
108                 System.err.println("Printing time : " + (System.currentTimeMillis() - start));
109             }
110             else if (TASK_PDF.equals(taskName))
111             {
112                 JasperExportManager.exportReportToPdfFile(fileName);
113                 System.err.println("PDF creation time : " + (System.currentTimeMillis() - start));
114             }
115             else if (TASK_XML.equals(taskName))
116             {
117                 JasperExportManager.exportReportToXmlFile(fileName, false);
118                 System.err.println("XML creation time : " + (System.currentTimeMillis() - start));
119             }
120             else if (TASK_XML_EMBED.equals(taskName))
121             {
122                 JasperExportManager.exportReportToXmlFile(fileName, true);
123                 System.err.println("XML creation time : " + (System.currentTimeMillis() - start));
124             }
125             else if (TASK_HTML.equals(taskName))
126             {
127                 JasperExportManager.exportReportToHtmlFile(fileName);
128                 System.err.println("HTML creation time : " + (System.currentTimeMillis() - start));
129             }
130             else if (TASK_RTF.equals(taskName))
131             {
132                 File JavaDoc sourceFile = new File JavaDoc(fileName);
133
134                 JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile);
135
136                 File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperPrint.getName() + ".rtf");
137
138                 JRRtfExporter exporter = new JRRtfExporter();
139
140                 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
141                 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
142
143                 exporter.exportReport();
144
145                 System.err.println("RTF creation time : " + (System.currentTimeMillis() - start));
146             }
147             else if (TASK_XLS.equals(taskName))
148             {
149                 File JavaDoc sourceFile = new File JavaDoc(fileName);
150
151                 JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile);
152
153                 File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperPrint.getName() + ".xls");
154
155                 JRXlsExporter exporter = new JRXlsExporter();
156
157                 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
158                 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
159                 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
160
161                 exporter.exportReport();
162
163                 System.err.println("XLS creation time : " + (System.currentTimeMillis() - start));
164             }
165             else if (TASK_JXL.equals(taskName))
166             {
167                 File JavaDoc sourceFile = new File JavaDoc(fileName);
168
169                 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
170
171                 File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperPrint.getName() + ".jxl.xls");
172
173                 JExcelApiExporter exporter = new JExcelApiExporter();
174
175                 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
176                 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
177                 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
178
179                 exporter.exportReport();
180
181                 System.err.println("XLS creation time : " + (System.currentTimeMillis() - start));
182                 System.exit(0);
183             }
184             else if (TASK_CSV.equals(taskName))
185             {
186                 File JavaDoc sourceFile = new File JavaDoc(fileName);
187
188                 JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile);
189
190                 File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperPrint.getName() + ".csv");
191
192                 JRCsvExporter exporter = new JRCsvExporter();
193
194                 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
195                 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
196
197                 exporter.exportReport();
198
199                 System.err.println("CSV creation time : " + (System.currentTimeMillis() - start));
200             }
201             else if (TASK_RUN.equals(taskName))
202             {
203                 Map JavaDoc<String JavaDoc,Object JavaDoc> parameters = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
204                 parameters.put("greeting", Greeting.bye);
205                                 
206                 JasperRunManager.runReportToPdfFile(fileName, parameters, new JREmptyDataSource());
207                 System.err.println("PDF running time : " + (System.currentTimeMillis() - start));
208                 System.exit(0);
209             }
210             else
211             {
212                 usage();
213             }
214         }
215         catch (JRException e)
216         {
217             e.printStackTrace();
218             System.exit(1);
219         }
220         catch (Exception JavaDoc e)
221         {
222             e.printStackTrace();
223             System.exit(1);
224         }
225     }
226
227     private static void usage()
228     {
229         System.out.println("Java5App usage:");
230         System.out.println("\tjava GroovyApp -Ttask -Ffile");
231         System.out.println("\tTasks : fill | print | pdf | xml | xmlEmbed | html | rtf | xls | jxl | csv | run");
232     }
233
234 }
235
Popular Tags