KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TableOfContentsApp


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.Collection JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Map JavaDoc;
37
38 import net.sf.jasperreports.engine.JRException;
39 import net.sf.jasperreports.engine.JRExporterParameter;
40 import net.sf.jasperreports.engine.JRPrintElement;
41 import net.sf.jasperreports.engine.JRPrintPage;
42 import net.sf.jasperreports.engine.JRPrintText;
43 import net.sf.jasperreports.engine.JasperExportManager;
44 import net.sf.jasperreports.engine.JasperFillManager;
45 import net.sf.jasperreports.engine.JasperPrint;
46 import net.sf.jasperreports.engine.JasperPrintManager;
47 import net.sf.jasperreports.engine.JasperReport;
48 import net.sf.jasperreports.engine.export.JExcelApiExporter;
49 import net.sf.jasperreports.engine.export.JRCsvExporter;
50 import net.sf.jasperreports.engine.export.JRRtfExporter;
51 import net.sf.jasperreports.engine.export.JRXlsExporter;
52 import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
53 import net.sf.jasperreports.engine.util.JRLoader;
54 import net.sf.jasperreports.engine.util.JRSaver;
55
56
57 /**
58  * @author Teodor Danciu (teodord@users.sourceforge.net)
59  * @version $Id: TableOfContentsApp.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
60  */

61 public class TableOfContentsApp
62 {
63
64
65     /**
66      *
67      */

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

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

267     private static void usage()
268     {
269         System.out.println( "TableOfContentsApp usage:" );
270         System.out.println( "\tjava TableOfContentsApp -Ttask -Ffile" );
271         System.out.println( "\tTasks : fill | print | pdf | xml | xmlEmbed | html | rtf | xls | jxl | csv | run" );
272     }
273
274
275     /**
276      *
277      */

278     private static Connection JavaDoc getConnection() throws ClassNotFoundException JavaDoc, SQLException JavaDoc
279     {
280         //Change these settings according to your local configuration
281
String JavaDoc driver = "org.hsqldb.jdbcDriver";
282         String JavaDoc connectString = "jdbc:hsqldb:hsql://localhost";
283         String JavaDoc user = "sa";
284         String JavaDoc password = "";
285
286
287         Class.forName(driver);
288         Connection JavaDoc conn = DriverManager.getConnection(connectString, user, password);
289         return conn;
290     }
291
292
293     /**
294      *
295      */

296     private static JasperPrint moveTableOfContents(JasperPrint jasperPrint)
297     {
298         if (jasperPrint != null)
299         {
300             List JavaDoc pages = jasperPrint.getPages();
301             if (pages != null && pages.size() > 0)
302             {
303                 String JavaDoc key = "HIDDEN TEXT TO MARK THE BEGINNING OF THE TABEL OF CONTENTS";
304                 JRPrintPage page = null;
305                 Collection JavaDoc elements = null;
306                 Iterator JavaDoc it = null;
307                 JRPrintElement element = null;
308                 int i = pages.size() - 1;
309                 boolean isFound = false;
310                 while(i >= 0 && !isFound)
311                 {
312                     page = (JRPrintPage)pages.get(i);
313                     elements = page.getElements();
314
315                     if (elements != null && elements.size() > 0)
316                     {
317                         it = elements.iterator();
318                         
319                         while(it.hasNext() && !isFound)
320                         {
321                             element = (JRPrintElement)it.next();
322                             
323                             if (element instanceof JRPrintText)
324                             {
325                                 if ( key.equals( ((JRPrintText)element).getText() ) )
326                                 {
327                                     isFound = true;
328                                     break;
329                                 }
330                             }
331                         }
332                     }
333                     
334                     i--;
335                 }
336                 
337                 if (isFound)
338                 {
339                     for(int j = i + 1; j < pages.size(); j++)
340                     {
341                         jasperPrint.addPage(j - i - 1, jasperPrint.removePage(j));
342                     }
343                 }
344             }
345         }
346         
347         return jasperPrint;
348     }
349
350
351 }
352
Popular Tags