1 28 29 import java.util.HashMap ; 30 import java.util.Map ; 31 32 import net.sf.jasperreports.engine.JRDataSource; 33 import net.sf.jasperreports.engine.JREmptyDataSource; 34 import net.sf.jasperreports.engine.JRException; 35 import net.sf.jasperreports.engine.JRExporterParameter; 36 import net.sf.jasperreports.engine.JRParameter; 37 import net.sf.jasperreports.engine.JasperExportManager; 38 import net.sf.jasperreports.engine.JasperFillManager; 39 import net.sf.jasperreports.engine.JasperPrint; 40 import net.sf.jasperreports.engine.JasperPrintManager; 41 import net.sf.jasperreports.engine.export.JRCsvExporter; 42 import net.sf.jasperreports.engine.fill.JRFileVirtualizer; 43 import net.sf.jasperreports.view.JasperViewer; 44 45 49 public class VirtualizerApp 50 { 51 52 55 private static final String TASK_PRINT = "print"; 56 57 private static final String TASK_PDF = "pdf"; 58 59 private static final String TASK_XML = "xml"; 60 61 private static final String TASK_XML_EMBED = "xmlEmbed"; 62 63 private static final String TASK_HTML = "html"; 64 65 private static final String TASK_CSV = "csv"; 66 67 private static final String TASK_VIEW = "view"; 68 69 private static final String TASK_EXPORT = "export"; 70 71 74 public static void main(String [] args) 75 { 76 String fileName = null; 77 String outFileName = null; 78 String taskName = null; 79 80 81 if (args.length == 0) 82 { 83 usage(); 84 return; 85 } 86 87 int k = 0; 88 while (args.length > k) 89 { 90 if (args[k].startsWith("-T")) 91 taskName = args[k].substring(2); 92 else if (args[k].startsWith("-F")) 93 fileName = args[k].substring(2); 94 else if (args[k].startsWith("-O")) 95 outFileName = args[k].substring(2); 96 97 k++; 98 } 99 100 try 101 { 102 106 JRDataSource ds = new JREmptyDataSource(1000); 108 109 JRFileVirtualizer virtualizer = new JRFileVirtualizer(2, "tmp"); 111 112 JasperPrint jasperPrint = fillReport(fileName, ds, virtualizer); 114 115 116 if (TASK_PRINT.equals(taskName)) 117 { 118 JasperPrintManager.printReport(jasperPrint, true); 119 } 120 else if (TASK_PDF.equals(taskName)) 121 { 122 exportPDF(outFileName, jasperPrint); 123 } 124 else if (TASK_XML.equals(taskName)) 125 { 126 exportXML(outFileName, jasperPrint, false); 127 } 128 else if (TASK_XML_EMBED.equals(taskName)) 129 { 130 exportXML(outFileName, jasperPrint, true); 131 } 132 else if (TASK_HTML.equals(taskName)) 133 { 134 exportHTML(outFileName, jasperPrint); 135 } 136 else if (TASK_CSV.equals(taskName)) 137 { 138 exportCSV(outFileName, jasperPrint); 139 } 140 else if (TASK_EXPORT.equals(taskName)) 141 { 142 exportPDF(outFileName + ".pdf", jasperPrint); 143 exportXML(outFileName + ".jrpxml", jasperPrint, false); 144 exportHTML(outFileName + ".html", jasperPrint); 145 exportCSV(outFileName + ".csv", jasperPrint); 146 147 virtualizer.cleanup(); 149 } 150 else if (TASK_VIEW.equals(taskName)) 151 { 152 JasperViewer.viewReport(jasperPrint, true); 153 } 154 else 155 { 156 usage(); 157 System.exit(0); 158 } 159 } 160 catch (JRException e) 161 { 162 e.printStackTrace(); 163 System.exit(1); 164 } 165 catch (Exception e) 166 { 167 e.printStackTrace(); 168 System.exit(1); 169 } 170 } 171 172 private static void exportCSV(String outFileName, JasperPrint jasperPrint) throws JRException 173 { 174 long start = System.currentTimeMillis(); 175 JRCsvExporter exporter = new JRCsvExporter(); 176 177 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 178 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFileName); 179 180 exporter.exportReport(); 181 182 System.err.println("CSV creation time : " + (System.currentTimeMillis() - start)); 183 } 184 185 private static void exportHTML(String outFileName, JasperPrint jasperPrint) throws JRException 186 { 187 long start = System.currentTimeMillis(); 188 JasperExportManager.exportReportToHtmlFile(jasperPrint, outFileName); 189 System.err.println("HTML creation time : " + (System.currentTimeMillis() - start)); 190 } 191 192 private static void exportXML(String outFileName, JasperPrint jasperPrint, boolean embedded) throws JRException 193 { 194 long start = System.currentTimeMillis(); 195 JasperExportManager.exportReportToXmlFile(jasperPrint, outFileName, embedded); 196 System.err.println("XML creation time : " + (System.currentTimeMillis() - start)); 197 } 198 199 private static void exportPDF(String outFileName, JasperPrint jasperPrint) throws JRException 200 { 201 long start = System.currentTimeMillis(); 202 JasperExportManager.exportReportToPdfFile(jasperPrint, outFileName); 203 System.err.println("PDF creation time : " + (System.currentTimeMillis() - start)); 204 } 205 206 207 208 private static JasperPrint fillReport(String fileName, JRDataSource dataSource, JRFileVirtualizer virtualizer) throws JRException 209 { 210 long start = System.currentTimeMillis(); 211 212 Map parameters = new HashMap (); 214 parameters.put(JRParameter.REPORT_VIRTUALIZER, virtualizer); 215 216 JasperPrint jasperPrint = JasperFillManager.fillReport(fileName, parameters, dataSource); 217 218 virtualizer.setReadOnly(true); 219 220 System.err.println("Filling time : " + (System.currentTimeMillis() - start)); 221 return jasperPrint; 222 } 223 224 227 private static void usage() 228 { 229 System.out.println("VirtualizerApp usage:"); 230 System.out.println("\tjava VirtualizerApp -Ttask -Ffile"); 231 System.out.println("\tTasks : print | pdf | xml | xmlEmbed | html | csv | export | view"); 232 } 233 234 } 235 | Popular Tags |