KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ImagesApp


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: ImagesApp.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
49  */

50 public class ImagesApp
51 {
52
53
54     /**
55      *
56      */

57     private static final String JavaDoc TASK_FILL = "fill";
58     private static final String JavaDoc TASK_PRINT = "print";
59     private static final String JavaDoc TASK_PDF = "pdf";
60     private static final String JavaDoc TASK_XML = "xml";
61     private static final String JavaDoc TASK_XML_EMBED = "xmlEmbed";
62     private static final String JavaDoc TASK_HTML = "html";
63     private static final String JavaDoc TASK_RTF = "rtf";
64     private static final String JavaDoc TASK_XLS = "xls";
65     private static final String JavaDoc TASK_JXL = "jxl";
66     private static final String JavaDoc TASK_CSV = "csv";
67     private static final String JavaDoc TASK_RUN = "run";
68     
69     
70     /**
71      *
72      */

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

236     private static void usage()
237     {
238         System.out.println( "ImagesApp usage:" );
239         System.out.println( "\tjava ImagesApp -Ttask -Ffile" );
240         System.out.println( "\tTasks : fill | print | pdf | xml | xmlEmbed | html | rtf | xls | jxl | csv | run" );
241     }
242
243
244 }
245
Popular Tags