1 28 import java.io.BufferedInputStream ; 29 import java.io.File ; 30 import java.io.FileInputStream ; 31 import java.util.HashMap ; 32 import java.util.Locale ; 33 import java.util.TimeZone ; 34 import java.text.SimpleDateFormat ; 35 import java.util.Map ; 36 37 import org.w3c.dom.Document ; 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 63 public class XmlDataSourceApp 64 { 65 66 67 70 private static final String TASK_FILL = "fill"; 71 private static final String TASK_PRINT = "print"; 72 private static final String TASK_PDF = "pdf"; 73 private static final String TASK_RTF = "rtf"; 74 private static final String TASK_XML = "xml"; 75 private static final String TASK_XML_EMBED = "xmlEmbed"; 76 private static final String TASK_HTML = "html"; 77 private static final String TASK_XLS = "xls"; 78 private static final String TASK_JXL = "jxl"; 79 private static final String TASK_CSV = "csv"; 80 private static final String TASK_RUN = "run"; 81 82 83 86 public static void main(String [] args) 87 { 88 String fileName = null; 89 String 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 params = new HashMap (); 114 Document document = JRXmlUtils.parse(new File ("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 sourceFile = new File (fileName); 140 141 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 142 143 File destFile = new File (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 sourceFile = new File (fileName); 176 177 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 178 179 File destFile = new File (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 sourceFile = new File (fileName); 195 196 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 197 198 File destFile = new File (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 sourceFile = new File (fileName); 214 215 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 216 217 File destFile = new File (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 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 params = new HashMap (); 233 Document document = JRXmlUtils.parse(new File ("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 e) 252 { 253 e.printStackTrace(); 254 System.exit(1); 255 } 256 } 257 258 259 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 |