KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > I18nApp


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 import java.util.HashMap JavaDoc;
30 import java.util.Locale JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.swing.JOptionPane JavaDoc;
34
35 import net.sf.jasperreports.engine.JREmptyDataSource;
36 import net.sf.jasperreports.engine.JRException;
37 import net.sf.jasperreports.engine.JRExporterParameter;
38 import net.sf.jasperreports.engine.JRParameter;
39 import net.sf.jasperreports.engine.JasperExportManager;
40 import net.sf.jasperreports.engine.JasperFillManager;
41 import net.sf.jasperreports.engine.JasperPrint;
42 import net.sf.jasperreports.engine.export.JExcelApiExporter;
43 import net.sf.jasperreports.engine.export.JRCsvExporter;
44 import net.sf.jasperreports.engine.export.JRRtfExporter;
45 import net.sf.jasperreports.engine.export.JRXlsExporter;
46 import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
47 import net.sf.jasperreports.engine.util.JRLoader;
48 import net.sf.jasperreports.view.JasperViewer;
49
50
51 /**
52  * @author Teodor Danciu (teodord@users.sourceforge.net)
53  * @version $Id: I18nApp.java 1463 2006-11-06 16:27:04 +0200 (Mon, 06 Nov 2006) shertage $
54  */

55 public class I18nApp
56 {
57
58
59     /**
60      *
61      */

62     private static final String JavaDoc TASK_FILL = "fill";
63     private static final String JavaDoc TASK_FILL_DEFAULT = "fillDefault";
64     private static final String JavaDoc TASK_VIEW = "view";
65     private static final String JavaDoc TASK_PDF = "pdf";
66     private static final String JavaDoc TASK_XML = "xml";
67     private static final String JavaDoc TASK_XML_EMBED = "xmlEmbed";
68     private static final String JavaDoc TASK_HTML = "html";
69     private static final String JavaDoc TASK_RTF = "rtf";
70     private static final String JavaDoc TASK_XLS = "xls";
71     private static final String JavaDoc TASK_JXL = "jxl";
72     private static final String JavaDoc TASK_CSV = "csv";
73     
74     
75     /**
76      *
77      */

78     public static void main(String JavaDoc[] args)
79     {
80         if(args.length != 2)
81         {
82             usage();
83             return;
84         }
85
86         String JavaDoc taskName = args[0];
87         String JavaDoc fileName = args[1];
88
89         try
90         {
91             if (TASK_FILL.equals(taskName))
92             {
93                 Locale JavaDoc locale = chooseLocale();
94                 if (locale != null)
95                 {
96 // Object[] aw = new Object[] {new Double(1000000.45), "$", "Ferrari", new Integer(20),new Integer(88)};
97
Map JavaDoc parameters = new HashMap JavaDoc();
98                     parameters.put("number", new Double JavaDoc(1234567 + Math.random()));
99 // parameters.put("array", aw);
100
parameters.put(JRParameter.REPORT_LOCALE, locale);
101                     long start = System.currentTimeMillis();
102                     JasperFillManager.fillReportToFile(fileName, parameters, new JREmptyDataSource());
103                     System.err.println("Filling time : " + (System.currentTimeMillis() - start));
104                 }
105                 System.exit(0);
106             }
107             else if (TASK_FILL_DEFAULT.equals(taskName))
108             {
109                 Map JavaDoc parameters = new HashMap JavaDoc();
110                 parameters.put("number", new Double JavaDoc(1234567 + Math.random()));
111                 long start = System.currentTimeMillis();
112                 JasperFillManager.fillReportToFile(fileName, parameters, new JREmptyDataSource());
113                 System.err.println("Filling time : " + (System.currentTimeMillis() - start));
114                 System.exit(0);
115             }
116             else if (TASK_VIEW.equals(taskName))
117             {
118                 Locale JavaDoc locale = chooseLocale();
119                 if (locale != null)
120                 {
121                     JasperViewer.viewReport(fileName, false, true, locale);
122                 }
123                 else
124                 {
125                     System.exit(0);
126                 }
127             }
128             else if (TASK_PDF.equals(taskName))
129             {
130                 long start = System.currentTimeMillis();
131                 JasperExportManager.exportReportToPdfFile(fileName);
132                 System.err.println("PDF creation time : " + (System.currentTimeMillis() - start));
133                 System.exit(0);
134             }
135             else if (TASK_XML.equals(taskName))
136             {
137                 long start = System.currentTimeMillis();
138                 JasperExportManager.exportReportToXmlFile(fileName, false);
139                 System.err.println("XML creation time : " + (System.currentTimeMillis() - start));
140                 System.exit(0);
141             }
142             else if (TASK_XML_EMBED.equals(taskName))
143             {
144                 long start = System.currentTimeMillis();
145                 JasperExportManager.exportReportToXmlFile(fileName, true);
146                 System.err.println("XML creation time : " + (System.currentTimeMillis() - start));
147                 System.exit(0);
148             }
149             else if (TASK_HTML.equals(taskName))
150             {
151                 long start = System.currentTimeMillis();
152                 JasperExportManager.exportReportToHtmlFile(fileName);
153                 System.err.println("HTML creation time : " + (System.currentTimeMillis() - start));
154                 System.exit(0);
155             }
156             else if (TASK_RTF.equals(taskName))
157             {
158                 long start = System.currentTimeMillis();
159
160                 File JavaDoc sourceFile = new File JavaDoc(fileName);
161         
162                 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
163         
164                 File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperPrint.getName() + ".rtf");
165                 
166                 JRRtfExporter exporter = new JRRtfExporter();
167                 
168                 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
169                 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
170                 
171                 exporter.exportReport();
172
173                 System.err.println("RTF creation time : " + (System.currentTimeMillis() - start));
174                 System.exit(0);
175             }
176             else if (TASK_XLS.equals(taskName))
177             {
178                 long start = System.currentTimeMillis();
179
180                 File JavaDoc sourceFile = new File JavaDoc(fileName);
181         
182                 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
183         
184                 File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperPrint.getName() + ".xls");
185                 
186                 JRXlsExporter exporter = new JRXlsExporter();
187                 
188                 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
189                 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
190                 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
191                 
192                 exporter.exportReport();
193
194                 System.err.println("XLS creation time : " + (System.currentTimeMillis() - start));
195                 System.exit(0);
196             }
197             else if (TASK_JXL.equals(taskName))
198             {
199                 long start = System.currentTimeMillis();
200
201                 File JavaDoc sourceFile = new File JavaDoc(fileName);
202
203                 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
204
205                 File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperPrint.getName() + ".jxl.xls");
206
207                 JExcelApiExporter exporter = new JExcelApiExporter();
208
209                 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
210                 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
211                 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
212
213                 exporter.exportReport();
214
215                 System.err.println("XLS creation time : " + (System.currentTimeMillis() - start));
216                 System.exit(0);
217             }
218             else if (TASK_CSV.equals(taskName))
219             {
220                 long start = System.currentTimeMillis();
221
222                 File JavaDoc sourceFile = new File JavaDoc(fileName);
223         
224                 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
225         
226                 File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperPrint.getName() + ".csv");
227                 
228                 JRCsvExporter exporter = new JRCsvExporter();
229                 
230                 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
231                 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
232                 
233                 exporter.exportReport();
234
235                 System.err.println("CSV creation time : " + (System.currentTimeMillis() - start));
236                 System.exit(0);
237             }
238             else
239             {
240                 usage();
241                 System.exit(0);
242             }
243         }
244         catch (JRException e)
245         {
246             e.printStackTrace();
247             System.exit(1);
248         }
249         catch (Exception JavaDoc e)
250         {
251             e.printStackTrace();
252             System.exit(1);
253         }
254     }
255
256
257     /**
258      *
259      */

260     private static Locale JavaDoc chooseLocale()
261     {
262         LocaleWrapper[] locales =
263             new LocaleWrapper[]
264             {
265                 new LocaleWrapper(new Locale JavaDoc("cs", "CZ")),
266                 new LocaleWrapper(Locale.GERMANY),
267                 new LocaleWrapper(Locale.US),
268                 new LocaleWrapper(Locale.FRANCE),
269                 new LocaleWrapper(new Locale JavaDoc("pt", "PT")),
270                 new LocaleWrapper(new Locale JavaDoc("ro", "RO")),
271                 new LocaleWrapper(new Locale JavaDoc("sk", "SK"))
272             };
273                                       
274         LocaleWrapper locale =
275             (LocaleWrapper)JOptionPane.showInputDialog(
276                 null,
277                 "Choose the locale",
278                 "Locale",
279                 JOptionPane.PLAIN_MESSAGE,
280                 null,
281                 locales,
282                 null
283                 );
284         
285         if (locale != null)
286             return locale.getLocale();
287         
288         return null;
289     }
290
291     /**
292      *
293      */

294     private static void usage()
295     {
296         System.out.println( "I18nApp usage:" );
297         System.out.println( "\tjava I18nApp task file" );
298         System.out.println( "\tTasks : fill | fillDefault | view | pdf | xml | xmlEmbed | html | rtf | xls | jxl | csv" );
299     }
300
301
302 }
303
304 class LocaleWrapper
305 {
306     Locale JavaDoc locale = null;
307     
308     public LocaleWrapper(Locale JavaDoc locale)
309     {
310         this.locale = locale;
311     }
312     
313     public Locale JavaDoc getLocale()
314     {
315         return locale;
316     }
317     
318     public String JavaDoc toString()
319     {
320         return locale.toString() + " - " + locale.getDisplayName(locale);
321     }
322 }
323
Popular Tags