KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > NoReportApp


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
29 import java.io.File JavaDoc;
30 import java.text.SimpleDateFormat JavaDoc;
31 import java.util.Date JavaDoc;
32
33 import net.sf.jasperreports.engine.JRAlignment;
34 import net.sf.jasperreports.engine.JRException;
35 import net.sf.jasperreports.engine.JRExporterParameter;
36 import net.sf.jasperreports.engine.JRImage;
37 import net.sf.jasperreports.engine.JRImageRenderer;
38 import net.sf.jasperreports.engine.JRPrintImage;
39 import net.sf.jasperreports.engine.JRPrintLine;
40 import net.sf.jasperreports.engine.JRPrintPage;
41 import net.sf.jasperreports.engine.JRPrintText;
42 import net.sf.jasperreports.engine.JasperExportManager;
43 import net.sf.jasperreports.engine.JasperPrint;
44 import net.sf.jasperreports.engine.JasperPrintManager;
45 import net.sf.jasperreports.engine.base.JRBasePrintImage;
46 import net.sf.jasperreports.engine.base.JRBasePrintLine;
47 import net.sf.jasperreports.engine.base.JRBasePrintPage;
48 import net.sf.jasperreports.engine.base.JRBasePrintText;
49 import net.sf.jasperreports.engine.design.JRDesignStyle;
50 import net.sf.jasperreports.engine.export.JExcelApiExporter;
51 import net.sf.jasperreports.engine.export.JRCsvExporter;
52 import net.sf.jasperreports.engine.export.JRRtfExporter;
53 import net.sf.jasperreports.engine.export.JRXlsExporter;
54 import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
55 import net.sf.jasperreports.engine.util.JRLoader;
56 import net.sf.jasperreports.engine.util.JRSaver;
57
58
59 /**
60  * @author Teodor Danciu (teodord@users.sourceforge.net)
61  * @version $Id: NoReportApp.java 1507 2006-11-27 17:12:17 +0200 (Mon, 27 Nov 2006) teodord $
62  */

63 public class NoReportApp
64 {
65
66
67     /**
68      *
69      */

70     private static final String JavaDoc TASK_FILL = "fill";
71     private static final String JavaDoc TASK_PRINT = "print";
72     private static final String JavaDoc TASK_PDF = "pdf";
73     private static final String JavaDoc TASK_RTF = "rtf";
74     private static final String JavaDoc TASK_XML = "xml";
75     private static final String JavaDoc TASK_XML_EMBED = "xmlEmbed";
76     private static final String JavaDoc TASK_HTML = "html";
77     private static final String JavaDoc TASK_XLS = "xls";
78     private static final String JavaDoc TASK_JXL = "jxl";
79     private static final String JavaDoc TASK_CSV = "csv";
80     
81     
82     /**
83      *
84      */

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

243     private static void usage()
244     {
245         System.out.println( "NoReportApp usage:" );
246         System.out.println( "\tjava NoReportApp -Ttask -Ffile" );
247         System.out.println( "\tTasks : fill | print | pdf | xml | xmlEmbed | html | rtf | xls | jxl | csv" );
248     }
249
250
251     /**
252      *
253      */

254     private static JasperPrint getJasperPrint() throws JRException
255     {
256         //JasperPrint
257
JasperPrint jasperPrint = new JasperPrint();
258         jasperPrint.setName("NoReport");
259         jasperPrint.setPageWidth(595);
260         jasperPrint.setPageHeight(842);
261         
262         //Fonts
263
JRDesignStyle normalStyle = new JRDesignStyle();
264         normalStyle.setName("Arial_Normal");
265         normalStyle.setDefault(true);
266         normalStyle.setFontName("Arial");
267         normalStyle.setFontSize(8);
268         normalStyle.setPdfFontName("Helvetica");
269         normalStyle.setPdfEncoding("Cp1252");
270         normalStyle.setPdfEmbedded(false);
271         jasperPrint.addStyle(normalStyle);
272
273         JRDesignStyle boldStyle = new JRDesignStyle();
274         boldStyle.setName("Arial_Bold");
275         boldStyle.setFontName("Arial");
276         boldStyle.setFontSize(8);
277         boldStyle.setBold(true);
278         boldStyle.setPdfFontName("Helvetica-Bold");
279         boldStyle.setPdfEncoding("Cp1252");
280         boldStyle.setPdfEmbedded(false);
281         jasperPrint.addStyle(boldStyle);
282
283         JRDesignStyle italicStyle = new JRDesignStyle();
284         italicStyle.setName("Arial_Italic");
285         italicStyle.setFontName("Arial");
286         italicStyle.setFontSize(8);
287         italicStyle.setItalic(true);
288         italicStyle.setPdfFontName("Helvetica-Oblique");
289         italicStyle.setPdfEncoding("Cp1252");
290         italicStyle.setPdfEmbedded(false);
291         jasperPrint.addStyle(italicStyle);
292         
293         JRPrintPage page = new JRBasePrintPage();
294
295         JRPrintLine line = new JRBasePrintLine(jasperPrint.getDefaultStyleProvider());
296         line.setX(40);
297         line.setY(50);
298         line.setWidth(515);
299         line.setHeight(0);
300         page.addElement(line);
301         
302         JRPrintImage image = new JRBasePrintImage(jasperPrint.getDefaultStyleProvider());
303         image.setX(45);
304         image.setY(55);
305         image.setWidth(165);
306         image.setHeight(40);
307         image.setScaleImage(JRImage.SCALE_IMAGE_CLIP);
308         image.setRenderer(
309             JRImageRenderer.getInstance(
310                 JRLoader.loadBytesFromLocation("jasperreports.gif")
311                 )
312             );
313         page.addElement(image);
314
315         JRPrintText text = new JRBasePrintText(jasperPrint.getDefaultStyleProvider());
316         text.setX(210);
317         text.setY(55);
318         text.setWidth(345);
319         text.setHeight(30);
320         text.setTextHeight(text.getHeight());
321         text.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_RIGHT);
322         text.setLineSpacingFactor(1.3133681f);
323         text.setLeadingOffset(-4.955078f);
324         text.setStyle(boldStyle);
325         text.setFontSize(18);
326         text.setText("JasperReports Project Description");
327         page.addElement(text);
328
329         text = new JRBasePrintText(jasperPrint.getDefaultStyleProvider());
330         text.setX(210);
331         text.setY(85);
332         text.setWidth(325);
333         text.setHeight(15);
334         text.setTextHeight(text.getHeight());
335         text.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_RIGHT);
336         text.setLineSpacingFactor(1.329241f);
337         text.setLeadingOffset(-4.076172f);
338         text.setStyle(italicStyle);
339         text.setFontSize(12);
340         text.setText((new SimpleDateFormat JavaDoc("EEE, MMM d, yyyy")).format(new Date JavaDoc()));
341         page.addElement(text);
342
343         text = new JRBasePrintText(jasperPrint.getDefaultStyleProvider());
344         text.setX(40);
345         text.setY(150);
346         text.setWidth(515);
347         text.setHeight(200);
348         text.setTextHeight(text.getHeight());
349         text.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_JUSTIFIED);
350         text.setLineSpacingFactor(1.329241f);
351         text.setLeadingOffset(-4.076172f);
352         text.setStyle(normalStyle);
353         text.setFontSize(14);
354         text.setText(
355             "JasperReports is a powerful report-generating tool that has the ability to deliver rich content onto the screen, to the printer or into PDF, HTML, XLS, CSV or XML files.\n\n" +
356             "It is entirely written in Java and can be used in a variety of Java enabled applications, including J2EE or Web applications, to generate dynamic content.\n\n" +
357             "Its main purpose is to help creating page oriented, ready to print documents in a simple and flexible manner."
358             );
359         page.addElement(text);
360
361         jasperPrint.addPage(page);
362
363         return jasperPrint;
364     }
365     
366
367 }
368
Popular Tags