1 28 import java.io.File ; 29 import java.sql.Connection ; 30 import java.sql.DriverManager ; 31 import java.sql.SQLException ; 32 import java.util.HashMap ; 33 import java.util.Map ; 34 35 import net.sf.jasperreports.engine.JRException; 36 import net.sf.jasperreports.engine.JRExporterParameter; 37 import net.sf.jasperreports.engine.JasperCompileManager; 38 import net.sf.jasperreports.engine.JasperExportManager; 39 import net.sf.jasperreports.engine.JasperFillManager; 40 import net.sf.jasperreports.engine.JasperPrint; 41 import net.sf.jasperreports.engine.JasperPrintManager; 42 import net.sf.jasperreports.engine.JasperRunManager; 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.export.JRTextExporter; 48 import net.sf.jasperreports.engine.export.JRTextExporterParameter; 49 import net.sf.jasperreports.engine.util.JRLoader; 50 51 52 56 public class TextApp 57 { 58 59 60 63 private static final String TASK_COMPILE = "compile"; 64 private static final String TASK_FILL = "fill"; 65 private static final String TASK_PRINT = "print"; 66 private static final String TASK_PDF = "pdf"; 67 private static final String TASK_RTF = "rtf"; 68 private static final String TASK_TEXT = "text"; 69 70 71 74 public static void main(String [] args) 75 { 76 String fileName = null; 77 String taskName = null; 78 79 if(args.length == 0) 80 { 81 usage(); 82 return; 83 } 84 85 int k = 0; 86 while ( args.length > k ) 87 { 88 if ( args[k].startsWith("-T") ) 89 taskName = args[k].substring(2); 90 if ( args[k].startsWith("-F") ) 91 fileName = args[k].substring(2); 92 93 k++; 94 } 95 96 try 97 { 98 long start = System.currentTimeMillis(); 99 if (TASK_COMPILE.equals(taskName)) 100 { 101 JasperCompileManager.compileReportToFile(fileName); 102 System.err.println("Compile time : " + (System.currentTimeMillis() - start)); 103 System.exit(0); 104 } 105 else if (TASK_FILL.equals(taskName)) 106 { 107 Map parameters = new HashMap (); 109 parameters.put("ReportTitle", "Address Report"); 110 parameters.put("FilterClause", "'Boston', 'Chicago', 'Oslo'"); 111 parameters.put("OrderClause", "City"); 112 113 JasperFillManager.fillReportToFile(fileName, parameters, getConnection()); 114 System.err.println("Filling time : " + (System.currentTimeMillis() - start)); 115 System.exit(0); 116 } 117 else if (TASK_PRINT.equals(taskName)) 118 { 119 JasperPrintManager.printReport(fileName, true); 120 System.err.println("Printing time : " + (System.currentTimeMillis() - start)); 121 System.exit(0); 122 } 123 else if (TASK_TEXT.equals(taskName)) 124 { 125 JRTextExporter exporter = new JRTextExporter(); 126 File sourceFile = new File (fileName); 127 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 128 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".txt"); 129 130 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 131 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 132 exporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH, new Integer (10)); 133 exporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT, new Integer (10)); 134 exporter.exportReport(); 135 136 System.err.println("Text creation time : " + (System.currentTimeMillis() - start)); 137 System.exit(0); 138 } 139 else if (TASK_PDF.equals(taskName)) 140 { 141 JasperExportManager.exportReportToPdfFile(fileName); 142 System.err.println("PDF creation time : " + (System.currentTimeMillis() - start)); 143 System.exit(0); 144 } 145 else if (TASK_RTF.equals(taskName)) 146 { 147 File sourceFile = new File (fileName); 148 149 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 150 151 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".rtf"); 152 153 JRRtfExporter exporter = new JRRtfExporter(); 154 155 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 156 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); 157 158 exporter.exportReport(); 159 160 System.err.println("RTF creation time : " + (System.currentTimeMillis() - start)); 161 System.exit(0); 162 } 163 else 164 { 165 usage(); 166 System.exit(0); 167 } 168 } 169 catch (JRException e) 170 { 171 e.printStackTrace(); 172 System.exit(1); 173 } 174 catch (Exception e) 175 { 176 e.printStackTrace(); 177 System.exit(1); 178 } 179 } 180 181 182 185 private static void usage() 186 { 187 System.out.println( "TextApp usage:" ); 188 System.out.println( "\tjava TextApp -Ttask -Ffile" ); 189 System.out.println( "\tTasks : compile | fill | print | pdf | text" ); 190 } 191 192 193 196 private static Connection getConnection() throws ClassNotFoundException , SQLException  197 { 198 String driver = "org.hsqldb.jdbcDriver"; 200 String connectString = "jdbc:hsqldb:hsql://localhost"; 201 String user = "sa"; 202 String password = ""; 203 204 205 Class.forName(driver); 206 Connection conn = DriverManager.getConnection(connectString, user, password); 207 return conn; 208 } 209 210 211 } 212
| Popular Tags
|