KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > XmlDataSourceApp


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.BufferedInputStream JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Locale JavaDoc;
33 import java.util.TimeZone JavaDoc;
34 import java.text.SimpleDateFormat JavaDoc;
35 import java.util.Map JavaDoc;
36
37 import org.w3c.dom.Document JavaDoc;
38
39 import net.sf.jasperreports.engine.JRException;
40 import net.sf.jasperreports.engine.JRExporterParameter;
41 import net.sf.jasperreports.engine.JRParameter;
42 import net.sf.jasperreports.engine.JasperExportManager;
43 import net.sf.jasperreports.engine.JasperFillManager;
44 import net.sf.jasperreports.engine.JasperPrint;
45 import net.sf.jasperreports.engine.JasperPrintManager;
46 import net.sf.jasperreports.engine.JasperRunManager;
47 import net.sf.jasperreports.engine.data.JRXmlDataSource;
48 import net.sf.jasperreports.engine.export.JExcelApiExporter;
49 import net.sf.jasperreports.engine.export.JRCsvExporter;
50 import net.sf.jasperreports.engine.export.JRCsvExporterParameter;
51 import net.sf.jasperreports.engine.export.JRRtfExporter;
52 import net.sf.jasperreports.engine.export.JRXlsExporter;
53 import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
54 import net.sf.jasperreports.engine.query.JRXPathQueryExecuterFactory;
55 import net.sf.jasperreports.engine.util.JRLoader;
56 import net.sf.jasperreports.engine.util.JRXmlUtils;
57
58
59 /**
60  * @author Teodor Danciu (teodord@users.sourceforge.net)
61  * @version $Id: XmlDataSourceApp.java 1540 2006-12-22 17:15:44 +0200 (Fri, 22 Dec 2006) teodord $
62  */

63 public class XmlDataSourceApp
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     private static final String JavaDoc TASK_RUN = "run";
81     
82     
83     /**
84      *
85      */

86     public static void main(String JavaDoc[] args)
87     {
88         String JavaDoc fileName = null;
89         String JavaDoc taskName = null;
90
91         if(args.length == 0)
92         {
93             usage();
94             return;
95         }
96                 
97         int k = 0;
98         while ( args.length > k )
99         {
100             if ( args[k].startsWith("-T") )
101                 taskName = args[k].substring(2);
102             if ( args[k].startsWith("-F") )
103                 fileName = args[k].substring(2);
104             
105             k++;
106         }
107
108         try
109         {
110             long start = System.currentTimeMillis();
111             if (TASK_FILL.equals(taskName))
112             {
113                 Map JavaDoc params = new HashMap JavaDoc();
114                 Document JavaDoc document = JRXmlUtils.parse(new File JavaDoc("northwind.xml"));
115                 params.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, document);
116                 params.put(JRXPathQueryExecuterFactory.XML_DATE_PATTERN, "yyyy-MM-dd");
117                 params.put(JRXPathQueryExecuterFactory.XML_NUMBER_PATTERN, "#,##0.##");
118                 params.put(JRXPathQueryExecuterFactory.XML_LOCALE, Locale.ENGLISH);
119                 params.put(JRParameter.REPORT_LOCALE, Locale.US);
120                 
121                 JasperFillManager.fillReportToFile(fileName, params);
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                 //exporter.setParameter(JRCsvExporterParameter.FIELD_DELIMITER, "|");
224

225                 exporter.exportReport();
226
227                 System.err.println("CSV creation time : " + (System.currentTimeMillis() - start));
228                 System.exit(0);
229             }
230             else if (TASK_RUN.equals(taskName))
231             {
232                 Map JavaDoc params = new HashMap JavaDoc();
233                 Document JavaDoc document = JRXmlUtils.parse(new File JavaDoc("northwind.xml"));
234                 params.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, document);
235                 
236                 JasperRunManager.runReportToPdfFile(fileName, params);
237                 System.err.println("PDF running time : " + (System.currentTimeMillis() - start));
238                 System.exit(0);
239             }
240             else
241             {
242                 usage();
243                 System.exit(0);
244             }
245         }
246         catch (JRException e)
247         {
248             e.printStackTrace();
249             System.exit(1);
250         }
251         catch (Exception JavaDoc e)
252         {
253             e.printStackTrace();
254             System.exit(1);
255         }
256     }
257
258
259     /**
260      *
261      */

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