1 package jimm.datavision; 2 import jimm.datavision.source.charsep.CharSepSource; 3 import jimm.datavision.layout.*; 4 import jimm.datavision.layout.swing.SwingLE; 5 import jimm.datavision.layout.pdf.PDFLE; 6 import jimm.datavision.gui.DesignWin; 7 import jimm.datavision.gui.StartupDialog; 8 import jimm.util.XMLWriter; 9 import jimm.util.Getopts; 10 import jimm.util.I18N; 11 import java.io.*; 12 import java.util.Locale ; 13 14 21 public class DataVision { 22 23 protected static final String DEFAULT_CHARACTER_SEPARATOR = ","; 24 25 char layoutEngineChoice; 26 String layoutEngineFileName; 27 String dbPassword; 28 String paramXMLFile; 29 int numReports; 30 boolean usesGUI; 31 String charSepFile; 32 char sepChar; 33 34 42 public static void main(String [] args) { 43 Getopts g = new Getopts("a:c:d:e:f:g:h:i:l:np:qr:s:wx:", args); 44 if (g.error()) usage(null); 47 DataVision dv = new DataVision(); 48 49 if (g.hasOption('g') || g.hasOption('i')) 51 I18N.setLanguage(new Locale (g.option('g', "").toLowerCase(), 52 g.option('i', "").toUpperCase())); 53 54 dv.layoutEngineChoiceFromOptions(g); 55 dv.dataSourceFromOptions(g); 56 57 if (dv.hasLayoutEngine()) { 58 if (dv.usesGUI()) ErrorHandler.useGUI(true); 60 else if (!g.hasOption('n') && !g.hasOption('p') && !g.hasOption('e')) 61 usage(I18N.get("DataVision.n_or_p")); 62 } 63 64 dv.paramXMLFile = g.option('r', null); 66 if (g.argc() == 0) { 67 if (startupDialog()) return; 69 70 if (dv.hasLayoutEngine()) usage(I18N.get("DataVision.xml_req")); 72 else { 73 try { 74 dv.designReport(g, null); 75 } 76 catch (Exception e) { ErrorHandler.error(e); 78 } 79 } 80 } 81 else { dv.numReports = g.argc(); 83 for (int i = 0; i < g.argc(); ++i) { 84 File f = new File(g.argv(i)); 85 try { 86 if (dv.hasLayoutEngine()) 87 dv.runReport(g, f); 88 else 89 dv.designReport(g, f); 90 } 91 catch (Exception e) { ErrorHandler.error(e); 93 } 94 } 95 } 96 97 if (dv.hasLayoutEngine() && dv.getLayoutEngineChoice() != 'w') 102 System.exit(0); 103 } 104 105 void designReport(Getopts g, File reportXMLFile) throws FileNotFoundException { 106 DesignWin win = new DesignWin(reportXMLFile, dbPassword); 107 Report report = win.getReport(); 108 109 if (charSepFile != null) { CharSepSource src = (CharSepSource)report.getDataSource(); 111 src.setSepChar(sepChar); 112 src.setInput(charSepFile); 113 } 114 115 if (g.hasOption('q')) 116 report.setCaseSensitiveDatabaseNames(false); 117 } 118 119 125 static boolean startupDialog() { 126 StartupDialog sd = new StartupDialog(); 128 129 String selectedFile = sd.getSelectedFile(); 131 132 if (selectedFile == null) 135 return true; 136 137 if (!selectedFile.equalsIgnoreCase(StartupDialog.NEW_REPORT_STRING)) { 142 String [] newArgs = { selectedFile }; 143 main(newArgs); 144 return true; 145 } 146 147 return false; } 149 150 void runReport(Getopts g, File reportXMLFile) throws Exception { 151 Report report = new Report(); 152 153 if (dbPassword != null) 154 report.setDatabasePassword(dbPassword); 155 156 report.read(reportXMLFile); 158 if (paramXMLFile != null) report.setParameterXMLInput(new File(paramXMLFile)); 160 161 if (charSepFile != null) { CharSepSource src = (CharSepSource)report.getDataSource(); 163 src.setSepChar(sepChar); 164 src.setInput(charSepFile); 165 } 166 167 if (g.hasOption('q')) 168 report.setCaseSensitiveDatabaseNames(false); 169 170 report.setLayoutEngine(createLayoutEngine(reportXMLFile, g)); 171 report.runReport(); 172 } 173 174 175 boolean hasLayoutEngine() { return layoutEngineChoice != '\0'; } 176 char getLayoutEngineChoice() { return layoutEngineChoice; } 177 178 boolean usesGUI() { return usesGUI; } 179 180 void layoutEngineChoiceFromOptions(Getopts g) { 181 String errMsg = I18N.get("DataVision.le_one"); 182 183 layoutEngineChoice = '\0'; 184 if (g.hasOption('c')) { 185 layoutEngineChoice = 'c'; 186 layoutEngineFileName = g.option('c', null); 187 } 188 if (g.hasOption('d')) { 189 if (layoutEngineChoice != '\0') usage(errMsg); 190 layoutEngineChoice = 'd'; 191 layoutEngineFileName = g.option('d', null); 192 } 193 if (g.hasOption('f')) { 194 if (layoutEngineChoice != '\0') usage(errMsg); 195 layoutEngineChoice = 'f'; 196 layoutEngineFileName = g.option('f', null); 197 } 198 if (g.hasOption('h')) { 199 if (layoutEngineChoice != '\0') usage(errMsg); 200 layoutEngineChoice = 'h'; 201 layoutEngineFileName = g.option('h', null); 202 } 203 if (g.hasOption('l')) { 204 if (layoutEngineChoice != '\0') usage(errMsg); 205 layoutEngineChoice = 'l'; 206 layoutEngineFileName = g.option('l', null); 207 } 208 if (g.hasOption('x')) { 209 if (layoutEngineChoice != '\0') usage(errMsg); 210 layoutEngineChoice = 'x'; 211 layoutEngineFileName = g.option('x', null); 212 } 213 if (g.hasOption('w')) { 214 if (layoutEngineChoice != '\0') usage(errMsg); 215 layoutEngineChoice = 'w'; 216 usesGUI = true; 217 } 218 } 219 220 LayoutEngine createLayoutEngine(File f, Getopts g) throws IOException 221 { 222 LayoutEngine le = null; 223 224 String fileName = f.getName(); 226 String fileNameSansExtension = null; 227 if (layoutEngineFileName != null) { 228 int pos = fileName.lastIndexOf('.'); 229 if (pos == -1) 230 fileNameSansExtension = fileName; 231 else 232 fileNameSansExtension = fileName.substring(0, pos); 233 } 234 235 String outFileName = null; 236 PrintWriter out = null; 237 switch (layoutEngineChoice) { 238 case 'c': 239 char sep = g.option('s', DEFAULT_CHARACTER_SEPARATOR).charAt(0); 240 if (layoutEngineFileName != null) 241 outFileName = layoutEngineFileName; 242 else { 243 outFileName = fileNameSansExtension 244 + (sep == ',' ? ".csv" : ".tab"); 245 if (outFileName.equals(fileName)) 246 outFileName = fileNameSansExtension + "_out" 247 + (sep == ',' ? ".csv" : ".tab"); 248 } 249 out = new PrintWriter(new FileWriter(outFileName)); 250 le = new CharSepLE(out, sep); 251 break; 252 case 'd': 253 if (layoutEngineFileName != null) 254 outFileName = layoutEngineFileName; 255 else { 256 outFileName = fileNameSansExtension + ".sgml"; 257 if (outFileName.equals(fileName)) 258 outFileName = fileNameSansExtension + "_out.sgml"; 259 } 260 out = new PrintWriter(new FileWriter(outFileName)); 261 le = new DocBookLE(out); 262 break; 263 case 'f': 264 if (layoutEngineFileName != null) 265 outFileName = layoutEngineFileName; 266 else { 267 outFileName = fileNameSansExtension + ".pdf"; 268 if (outFileName.equals(fileName)) 269 outFileName = fileNameSansExtension + "_out.pdf"; 270 } 271 OutputStream outStream = new FileOutputStream(outFileName); 272 le = new PDFLE(outStream); 273 break; 274 case 'h': 275 if (layoutEngineFileName != null) 276 outFileName = layoutEngineFileName; 277 else { 278 outFileName = fileNameSansExtension + ".html"; 279 if (outFileName.equals(fileName)) 280 outFileName = fileNameSansExtension + "_out.html"; 281 } 282 out = new PrintWriter(new FileWriter(outFileName)); 283 le = new HTMLLE(out); 284 break; 285 case 'l': 286 if (layoutEngineFileName != null) 287 outFileName = layoutEngineFileName; 288 else { 289 outFileName = fileNameSansExtension + ".tex"; 290 if (outFileName.equals(fileName)) 291 outFileName = fileNameSansExtension + "_out.tex"; 292 } 293 out = new PrintWriter(new FileWriter(outFileName)); 294 le = new LaTeXLE(out); 295 break; 296 case 'x': 297 if (layoutEngineFileName != null) 298 outFileName = layoutEngineFileName; 299 else { 300 outFileName = fileNameSansExtension + ".xml"; 301 if (outFileName.equals(fileName)) 302 outFileName = fileNameSansExtension + "_out.xml"; 303 } 304 XMLWriter iout = new XMLWriter(new FileWriter(outFileName)); 305 le = new XMLLE(iout); 306 break; 307 case 'w': 308 le = new SwingLE() { 309 public void close() { super.close(); 311 swingLayoutEngineClosed(); 312 } 313 }; 314 break; 315 } 316 return le; 317 } 318 319 323 protected void swingLayoutEngineClosed() { 324 if (--numReports == 0) 325 System.exit(0); 326 } 327 328 331 protected void dataSourceFromOptions(Getopts g) { 332 if (g.hasOption('n')) { if (g.hasOption('p') || g.hasOption('e')) 334 usage(I18N.get("DataVision.n_and_p")); 335 dbPassword = ""; 336 } 337 else if (g.hasOption('p')) { 338 if (g.hasOption('n') || g.hasOption('e')) 339 usage(I18N.get("DataVision.n_and_p")); 340 dbPassword = g.option('p', null); } 342 else if (g.hasOption('e')) { 343 if (g.hasOption('n') || g.hasOption('p')) 344 usage(I18N.get("DataVision.n_and_p")); 345 charSepFile = g.option('e'); 346 sepChar = g.option('a', DEFAULT_CHARACTER_SEPARATOR).charAt(0); 347 } 348 } 349 350 public String toString() { 351 return "DataVision [layoutEngineChoice=" + layoutEngineChoice 352 + ", dbPassword = " + dbPassword 353 + ", paramXMLFile = " + paramXMLFile 354 + ", numReports = " + numReports 355 + ", usesGUI = " + usesGUI 356 + ", charSepFile = " + charSepFile 357 + ", sepChar = " + sepChar 358 + "]"; 359 } 360 361 367 public static void usage(String errMsg) { 368 if (errMsg != null) 369 System.err.println(errMsg); 370 System.err.println(I18N.get("DataVision.usage")); 371 372 System.exit(1); 373 } 374 375 } 376 | Popular Tags |