KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ScriptletApp


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.sql.Connection JavaDoc;
30 import java.sql.DriverManager JavaDoc;
31 import java.sql.SQLException JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import net.sf.jasperreports.engine.JRException;
36 import net.sf.jasperreports.engine.JRExporterParameter;
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.JasperRunManager;
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
49
50 /**
51  * @author Teodor Danciu (teodord@users.sourceforge.net)
52  * @version $Id: ScriptletApp.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
53  */

54 public class ScriptletApp
55 {
56
57
58     /**
59      *
60      */

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

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

252     private static void usage()
253     {
254         System.out.println( "ScriptletApp usage:" );
255         System.out.println( "\tjava ScriptletApp -Ttask -Ffile" );
256         System.out.println( "\tTasks : fill | print | pdf | xml | xmlEmbed | html | rtf | xls | jxl | csv | run" );
257     }
258
259
260     /**
261      *
262      */

263     private static Connection JavaDoc getConnection() throws ClassNotFoundException JavaDoc, SQLException JavaDoc
264     {
265         //Change these settings according to your local configuration
266
String JavaDoc driver = "org.hsqldb.jdbcDriver";
267         String JavaDoc connectString = "jdbc:hsqldb:hsql://localhost";
268         String JavaDoc user = "sa";
269         String JavaDoc password = "";
270
271
272         Class.forName(driver);
273         Connection JavaDoc conn = DriverManager.getConnection(connectString, user, password);
274         return conn;
275     }
276
277
278 }
279
Popular Tags