KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JChartsApp


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 import java.io.File JavaDoc;
29
30 import net.sf.jasperreports.engine.JREmptyDataSource;
31 import net.sf.jasperreports.engine.JRException;
32 import net.sf.jasperreports.engine.JRExporterParameter;
33 import net.sf.jasperreports.engine.JasperExportManager;
34 import net.sf.jasperreports.engine.JasperFillManager;
35 import net.sf.jasperreports.engine.JasperPrint;
36 import net.sf.jasperreports.engine.JasperPrintManager;
37 import net.sf.jasperreports.engine.JasperRunManager;
38 import net.sf.jasperreports.engine.export.JExcelApiExporter;
39 import net.sf.jasperreports.engine.export.JRCsvExporter;
40 import net.sf.jasperreports.engine.export.JRRtfExporter;
41 import net.sf.jasperreports.engine.export.JRXlsExporter;
42 import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
43 import net.sf.jasperreports.engine.util.JRLoader;
44
45
46 /**
47  * @author Teodor Danciu (teodord@users.sourceforge.net)
48  * @version $Id: JChartsApp.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
49  */

50 public class JChartsApp
51 {
52     private static final String JavaDoc TASK_FILL = "fill";
53     private static final String JavaDoc TASK_PRINT = "print";
54     private static final String JavaDoc TASK_PDF = "pdf";
55     private static final String JavaDoc TASK_XML = "xml";
56     private static final String JavaDoc TASK_XML_EMBED = "xmlEmbed";
57     private static final String JavaDoc TASK_HTML = "html";
58     private static final String JavaDoc TASK_RTF = "rtf";
59     private static final String JavaDoc TASK_XLS = "xls";
60     private static final String JavaDoc TASK_JXL = "jxl";
61     private static final String JavaDoc TASK_CSV = "csv";
62     private static final String JavaDoc TASK_RUN = "run";
63     
64     /**
65      *
66      */

67     public static void main(String JavaDoc[] args)
68     {
69         String JavaDoc fileName = null;
70         String JavaDoc taskName = null;
71
72         if(args.length == 0)
73         {
74             usage();
75             return;
76         }
77                 
78         int k = 0;
79         while ( args.length > k )
80         {
81             if ( args[k].startsWith("-T") )
82                 taskName = args[k].substring(2);
83             if ( args[k].startsWith("-F") )
84                 fileName = args[k].substring(2);
85             
86             k++;
87         }
88
89         try
90         {
91             long start = System.currentTimeMillis();
92             if (TASK_FILL.equals(taskName))
93             {
94                 JasperFillManager.fillReportToFile(fileName, null, new JREmptyDataSource());
95                 System.err.println("Filling time : " + (System.currentTimeMillis() - start));
96                 System.exit(0);
97             }
98             else if (TASK_PRINT.equals(taskName))
99             {
100                 JasperPrintManager.printReport(fileName, true);
101                 System.err.println("Printing time : " + (System.currentTimeMillis() - start));
102                 System.exit(0);
103             }
104             else if (TASK_PDF.equals(taskName))
105             {
106                 JasperExportManager.exportReportToPdfFile(fileName);
107                 System.err.println("PDF creation time : " + (System.currentTimeMillis() - start));
108                 System.exit(0);
109             }
110             else if (TASK_RTF.equals(taskName))
111             {
112                 File JavaDoc sourceFile = new File JavaDoc(fileName);
113         
114                 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
115         
116                 File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperPrint.getName() + ".rtf");
117                 
118                 JRRtfExporter exporter = new JRRtfExporter();
119                 
120                 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
121                 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
122                 
123                 exporter.exportReport();
124
125                 System.err.println("XLS creation time : " + (System.currentTimeMillis() - start));
126                 System.exit(0);
127             }
128             else if (TASK_XML.equals(taskName))
129             {
130                 JasperExportManager.exportReportToXmlFile(fileName, false);
131                 System.err.println("XML creation time : " + (System.currentTimeMillis() - start));
132                 System.exit(0);
133             }
134             else if (TASK_XML_EMBED.equals(taskName))
135             {
136                 JasperExportManager.exportReportToXmlFile(fileName, true);
137                 System.err.println("XML creation time : " + (System.currentTimeMillis() - start));
138                 System.exit(0);
139             }
140             else if (TASK_HTML.equals(taskName))
141             {
142                 JasperExportManager.exportReportToHtmlFile(fileName);
143                 System.err.println("HTML creation time : " + (System.currentTimeMillis() - start));
144                 System.exit(0);
145             }
146             else if (TASK_XLS.equals(taskName))
147             {
148                 File JavaDoc sourceFile = new File JavaDoc(fileName);
149         
150                 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
151         
152                 File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperPrint.getName() + ".xls");
153                 
154                 JRXlsExporter exporter = new JRXlsExporter();
155                 
156                 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
157                 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
158                 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
159                 
160                 exporter.exportReport();
161
162                 System.err.println("XLS creation time : " + (System.currentTimeMillis() - start));
163                 System.exit(0);
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                 System.exit(0);
201             }
202             else if (TASK_RUN.equals(taskName))
203             {
204                 JasperRunManager.runReportToPdfFile(fileName, null, new JREmptyDataSource());
205                 System.err.println("PDF running time : " + (System.currentTimeMillis() - start));
206                 System.exit(0);
207             }
208             else
209             {
210                 usage();
211                 System.exit(0);
212             }
213         }
214         catch (JRException e)
215         {
216             e.printStackTrace();
217             System.exit(1);
218         }
219         catch (Exception JavaDoc e)
220         {
221             e.printStackTrace();
222             System.exit(1);
223         }
224     }
225
226
227     /**
228      *
229      */

230     private static void usage()
231     {
232         System.out.println( "JChartsApp usage:" );
233         System.out.println( "\tjava JChartsApp -Ttask -Ffile" );
234         System.out.println( "\tTasks : fill | print | pdf | xml | xmlEmbed | html | rtf | xls | jxl | csv | run" );
235     }
236
237
238 }
239
Popular Tags