KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TextApp


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.JasperCompileManager;
38 import net.sf.jasperreports.engine.JasperExportManager;
39 import net.sf.jasperreports.engine.JasperFillManager;
40 import net.sf.jasperreports.engine.JasperPrint;
41 import net.sf.jasperreports.engine.JasperPrintManager;
42 import net.sf.jasperreports.engine.JasperRunManager;
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.export.JRTextExporter;
48 import net.sf.jasperreports.engine.export.JRTextExporterParameter;
49 import net.sf.jasperreports.engine.util.JRLoader;
50
51
52 /**
53  * @author Teodor Danciu (teodord@users.sourceforge.net)
54  * @version $Id: TextApp.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
55  */

56 public class TextApp
57 {
58
59
60     /**
61      *
62      */

63     private static final String JavaDoc TASK_COMPILE = "compile";
64     private static final String JavaDoc TASK_FILL = "fill";
65     private static final String JavaDoc TASK_PRINT = "print";
66     private static final String JavaDoc TASK_PDF = "pdf";
67     private static final String JavaDoc TASK_RTF = "rtf";
68     private static final String JavaDoc TASK_TEXT = "text";
69
70     
71     /**
72      *
73      */

74     public static void main(String JavaDoc[] args)
75     {
76         String JavaDoc fileName = null;
77         String JavaDoc taskName = null;
78
79         if(args.length == 0)
80         {
81             usage();
82             return;
83         }
84                 
85         int k = 0;
86         while ( args.length > k )
87         {
88             if ( args[k].startsWith("-T") )
89                 taskName = args[k].substring(2);
90             if ( args[k].startsWith("-F") )
91                 fileName = args[k].substring(2);
92             
93             k++;
94         }
95
96         try
97         {
98             long start = System.currentTimeMillis();
99             if (TASK_COMPILE.equals(taskName))
100             {
101                 JasperCompileManager.compileReportToFile(fileName);
102                 System.err.println("Compile time : " + (System.currentTimeMillis() - start));
103                 System.exit(0);
104             }
105             else if (TASK_FILL.equals(taskName))
106             {
107                 //Preparing parameters
108
Map JavaDoc parameters = new HashMap JavaDoc();
109                 parameters.put("ReportTitle", "Address Report");
110                 parameters.put("FilterClause", "'Boston', 'Chicago', 'Oslo'");
111                 parameters.put("OrderClause", "City");
112
113                 JasperFillManager.fillReportToFile(fileName, parameters, getConnection());
114                 System.err.println("Filling time : " + (System.currentTimeMillis() - start));
115                 System.exit(0);
116             }
117             else if (TASK_PRINT.equals(taskName))
118             {
119                 JasperPrintManager.printReport(fileName, true);
120                 System.err.println("Printing time : " + (System.currentTimeMillis() - start));
121                 System.exit(0);
122             }
123             else if (TASK_TEXT.equals(taskName))
124             {
125                 JRTextExporter exporter = new JRTextExporter();
126                 File JavaDoc sourceFile = new File JavaDoc(fileName);
127                 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
128                 File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperPrint.getName() + ".txt");
129
130                 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
131                 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
132                 exporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH, new Integer JavaDoc(10));
133                 exporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT, new Integer JavaDoc(10));
134                 exporter.exportReport();
135
136                 System.err.println("Text creation time : " + (System.currentTimeMillis() - start));
137                 System.exit(0);
138             }
139             else if (TASK_PDF.equals(taskName))
140             {
141                 JasperExportManager.exportReportToPdfFile(fileName);
142                 System.err.println("PDF creation time : " + (System.currentTimeMillis() - start));
143                 System.exit(0);
144             }
145             else if (TASK_RTF.equals(taskName))
146             {
147                 File JavaDoc sourceFile = new File JavaDoc(fileName);
148         
149                 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
150         
151                 File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperPrint.getName() + ".rtf");
152                 
153                 JRRtfExporter exporter = new JRRtfExporter();
154                 
155                 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
156                 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
157                 
158                 exporter.exportReport();
159
160                 System.err.println("RTF creation time : " + (System.currentTimeMillis() - start));
161                 System.exit(0);
162             }
163             else
164             {
165                 usage();
166                 System.exit(0);
167             }
168         }
169         catch (JRException e)
170         {
171             e.printStackTrace();
172             System.exit(1);
173         }
174         catch (Exception JavaDoc e)
175         {
176             e.printStackTrace();
177             System.exit(1);
178         }
179     }
180
181
182     /**
183      *
184      */

185     private static void usage()
186     {
187         System.out.println( "TextApp usage:" );
188         System.out.println( "\tjava TextApp -Ttask -Ffile" );
189         System.out.println( "\tTasks : compile | fill | print | pdf | text" );
190     }
191
192
193     /**
194      *
195      */

196     private static Connection JavaDoc getConnection() throws ClassNotFoundException JavaDoc, SQLException JavaDoc
197     {
198         //Change these settings according to your local configuration
199
String JavaDoc driver = "org.hsqldb.jdbcDriver";
200         String JavaDoc connectString = "jdbc:hsqldb:hsql://localhost";
201         String JavaDoc user = "sa";
202         String JavaDoc password = "";
203
204
205         Class.forName(driver);
206         Connection JavaDoc conn = DriverManager.getConnection(connectString, user, password);
207         return conn;
208     }
209
210
211 }
212
Popular Tags