1 28 import java.io.File ; 29 import java.util.HashMap ; 30 import java.util.Locale ; 31 import java.util.Map ; 32 33 import javax.swing.JOptionPane ; 34 35 import net.sf.jasperreports.engine.JREmptyDataSource; 36 import net.sf.jasperreports.engine.JRException; 37 import net.sf.jasperreports.engine.JRExporterParameter; 38 import net.sf.jasperreports.engine.JRParameter; 39 import net.sf.jasperreports.engine.JasperExportManager; 40 import net.sf.jasperreports.engine.JasperFillManager; 41 import net.sf.jasperreports.engine.JasperPrint; 42 import net.sf.jasperreports.engine.export.JExcelApiExporter; 43 import net.sf.jasperreports.engine.export.JRCsvExporter; 44 import net.sf.jasperreports.engine.export.JRRtfExporter; 45 import net.sf.jasperreports.engine.export.JRXlsExporter; 46 import net.sf.jasperreports.engine.export.JRXlsExporterParameter; 47 import net.sf.jasperreports.engine.util.JRLoader; 48 import net.sf.jasperreports.view.JasperViewer; 49 50 51 55 public class I18nApp 56 { 57 58 59 62 private static final String TASK_FILL = "fill"; 63 private static final String TASK_FILL_DEFAULT = "fillDefault"; 64 private static final String TASK_VIEW = "view"; 65 private static final String TASK_PDF = "pdf"; 66 private static final String TASK_XML = "xml"; 67 private static final String TASK_XML_EMBED = "xmlEmbed"; 68 private static final String TASK_HTML = "html"; 69 private static final String TASK_RTF = "rtf"; 70 private static final String TASK_XLS = "xls"; 71 private static final String TASK_JXL = "jxl"; 72 private static final String TASK_CSV = "csv"; 73 74 75 78 public static void main(String [] args) 79 { 80 if(args.length != 2) 81 { 82 usage(); 83 return; 84 } 85 86 String taskName = args[0]; 87 String fileName = args[1]; 88 89 try 90 { 91 if (TASK_FILL.equals(taskName)) 92 { 93 Locale locale = chooseLocale(); 94 if (locale != null) 95 { 96 Map parameters = new HashMap (); 98 parameters.put("number", new Double (1234567 + Math.random())); 99 parameters.put(JRParameter.REPORT_LOCALE, locale); 101 long start = System.currentTimeMillis(); 102 JasperFillManager.fillReportToFile(fileName, parameters, new JREmptyDataSource()); 103 System.err.println("Filling time : " + (System.currentTimeMillis() - start)); 104 } 105 System.exit(0); 106 } 107 else if (TASK_FILL_DEFAULT.equals(taskName)) 108 { 109 Map parameters = new HashMap (); 110 parameters.put("number", new Double (1234567 + Math.random())); 111 long start = System.currentTimeMillis(); 112 JasperFillManager.fillReportToFile(fileName, parameters, new JREmptyDataSource()); 113 System.err.println("Filling time : " + (System.currentTimeMillis() - start)); 114 System.exit(0); 115 } 116 else if (TASK_VIEW.equals(taskName)) 117 { 118 Locale locale = chooseLocale(); 119 if (locale != null) 120 { 121 JasperViewer.viewReport(fileName, false, true, locale); 122 } 123 else 124 { 125 System.exit(0); 126 } 127 } 128 else if (TASK_PDF.equals(taskName)) 129 { 130 long start = System.currentTimeMillis(); 131 JasperExportManager.exportReportToPdfFile(fileName); 132 System.err.println("PDF creation time : " + (System.currentTimeMillis() - start)); 133 System.exit(0); 134 } 135 else if (TASK_XML.equals(taskName)) 136 { 137 long start = System.currentTimeMillis(); 138 JasperExportManager.exportReportToXmlFile(fileName, false); 139 System.err.println("XML creation time : " + (System.currentTimeMillis() - start)); 140 System.exit(0); 141 } 142 else if (TASK_XML_EMBED.equals(taskName)) 143 { 144 long start = System.currentTimeMillis(); 145 JasperExportManager.exportReportToXmlFile(fileName, true); 146 System.err.println("XML creation time : " + (System.currentTimeMillis() - start)); 147 System.exit(0); 148 } 149 else if (TASK_HTML.equals(taskName)) 150 { 151 long start = System.currentTimeMillis(); 152 JasperExportManager.exportReportToHtmlFile(fileName); 153 System.err.println("HTML creation time : " + (System.currentTimeMillis() - start)); 154 System.exit(0); 155 } 156 else if (TASK_RTF.equals(taskName)) 157 { 158 long start = System.currentTimeMillis(); 159 160 File sourceFile = new File (fileName); 161 162 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 163 164 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".rtf"); 165 166 JRRtfExporter exporter = new JRRtfExporter(); 167 168 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 169 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 170 171 exporter.exportReport(); 172 173 System.err.println("RTF creation time : " + (System.currentTimeMillis() - start)); 174 System.exit(0); 175 } 176 else if (TASK_XLS.equals(taskName)) 177 { 178 long start = System.currentTimeMillis(); 179 180 File sourceFile = new File (fileName); 181 182 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 183 184 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".xls"); 185 186 JRXlsExporter exporter = new JRXlsExporter(); 187 188 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 189 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 190 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); 191 192 exporter.exportReport(); 193 194 System.err.println("XLS creation time : " + (System.currentTimeMillis() - start)); 195 System.exit(0); 196 } 197 else if (TASK_JXL.equals(taskName)) 198 { 199 long start = System.currentTimeMillis(); 200 201 File sourceFile = new File (fileName); 202 203 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 204 205 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".jxl.xls"); 206 207 JExcelApiExporter exporter = new JExcelApiExporter(); 208 209 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 210 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 211 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); 212 213 exporter.exportReport(); 214 215 System.err.println("XLS creation time : " + (System.currentTimeMillis() - start)); 216 System.exit(0); 217 } 218 else if (TASK_CSV.equals(taskName)) 219 { 220 long start = System.currentTimeMillis(); 221 222 File sourceFile = new File (fileName); 223 224 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 225 226 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".csv"); 227 228 JRCsvExporter exporter = new JRCsvExporter(); 229 230 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 231 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 232 233 exporter.exportReport(); 234 235 System.err.println("CSV creation time : " + (System.currentTimeMillis() - start)); 236 System.exit(0); 237 } 238 else 239 { 240 usage(); 241 System.exit(0); 242 } 243 } 244 catch (JRException e) 245 { 246 e.printStackTrace(); 247 System.exit(1); 248 } 249 catch (Exception e) 250 { 251 e.printStackTrace(); 252 System.exit(1); 253 } 254 } 255 256 257 260 private static Locale chooseLocale() 261 { 262 LocaleWrapper[] locales = 263 new LocaleWrapper[] 264 { 265 new LocaleWrapper(new Locale ("cs", "CZ")), 266 new LocaleWrapper(Locale.GERMANY), 267 new LocaleWrapper(Locale.US), 268 new LocaleWrapper(Locale.FRANCE), 269 new LocaleWrapper(new Locale ("pt", "PT")), 270 new LocaleWrapper(new Locale ("ro", "RO")), 271 new LocaleWrapper(new Locale ("sk", "SK")) 272 }; 273 274 LocaleWrapper locale = 275 (LocaleWrapper)JOptionPane.showInputDialog( 276 null, 277 "Choose the locale", 278 "Locale", 279 JOptionPane.PLAIN_MESSAGE, 280 null, 281 locales, 282 null 283 ); 284 285 if (locale != null) 286 return locale.getLocale(); 287 288 return null; 289 } 290 291 294 private static void usage() 295 { 296 System.out.println( "I18nApp usage:" ); 297 System.out.println( "\tjava I18nApp task file" ); 298 System.out.println( "\tTasks : fill | fillDefault | view | pdf | xml | xmlEmbed | html | rtf | xls | jxl | csv" ); 299 } 300 301 302 } 303 304 class LocaleWrapper 305 { 306 Locale locale = null; 307 308 public LocaleWrapper(Locale locale) 309 { 310 this.locale = locale; 311 } 312 313 public Locale getLocale() 314 { 315 return locale; 316 } 317 318 public String toString() 319 { 320 return locale.toString() + " - " + locale.getDisplayName(locale); 321 } 322 } 323 | Popular Tags |