KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > StyledTextApp


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

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

63     private static final String JavaDoc TASK_FILL = "fill";
64     private static final String JavaDoc TASK_PRINT = "print";
65     private static final String JavaDoc TASK_PDF = "pdf";
66     private static final String JavaDoc TASK_RTF = "rtf";
67     private static final String JavaDoc TASK_XML = "xml";
68     private static final String JavaDoc TASK_XML_EMBED = "xmlEmbed";
69     private static final String JavaDoc TASK_HTML = "html";
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     private static final String JavaDoc TASK_RUN = "run";
74     
75     
76     /**
77      *
78      */

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

261     private static void usage()
262     {
263         System.out.println( "StyledTextApp usage:" );
264         System.out.println( "\tjava StyledTextApp -Ttask -Ffile" );
265         System.out.println( "\tTasks : fill | print | pdf | xml | xmlEmbed | html | rtf | xls | jxl | csv | run" );
266     }
267
268
269 }
270
Popular Tags