KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > DataVision


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 JavaDoc;
13
14 /**
15  * This is the DataVision application class. It opens a design window
16  * for each report specified on the command line. If none are specified,
17  * it openes a new design window on a new, empty report.
18  *
19  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
20  */

21 public class DataVision {
22
23 protected static final String JavaDoc DEFAULT_CHARACTER_SEPARATOR = ",";
24
25 char layoutEngineChoice;
26 String JavaDoc layoutEngineFileName;
27 String JavaDoc dbPassword;
28 String JavaDoc paramXMLFile;
29 int numReports;
30 boolean usesGUI;
31 String JavaDoc charSepFile;
32 char sepChar;
33
34 /**
35  * This main application method opens a design window for each report
36  * specified on the command line. If none are specified, it openes a new
37  * design window on a new, empty report.
38  *
39  * @param args command line array; each element is assumed to be a report
40  * file name.
41  */

42 public static void main(String JavaDoc[] args) {
43     Getopts g = new Getopts("a:c:d:e:f:g:h:i:l:np:qr:s:wx:", args);
44     if (g.error()) // Any bad command line argument?
45
usage(null); // If so, whine and exit
46

47     DataVision dv = new DataVision();
48
49     // Language
50
if (g.hasOption('g') || g.hasOption('i'))
51     I18N.setLanguage(new Locale JavaDoc(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()) // Can ask for password via GUI
59
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); // Parameter XML file name or null
65

66     if (g.argc() == 0) {
67     if (startupDialog()) // Returns true if we should exit
68
return;
69
70     if (dv.hasLayoutEngine()) // Have layout engine but no file
71
usage(I18N.get("DataVision.xml_req"));
72     else {
73         try {
74         dv.designReport(g, null);
75         }
76         catch (Exception JavaDoc e) { // Global catch-all
77
ErrorHandler.error(e);
78         }
79     }
80     }
81     else { // Loop over input files
82
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 JavaDoc e) { // Global catch-all
92
ErrorHandler.error(e);
93         }
94     }
95     }
96
97     // For some odd reason, on Mac OS X with Java 1.4.1 the app hangs
98
// below, just before the last closing brace when run from the command
99
// line unless I add this System.exit(). I hate fixing bugs without
100
// knowing what is going wrong.
101
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) { // Must come after file read
110
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 /**
120  * Shows startup dialog and returns <code>true</code> if we should exit
121  * the application.
122  *
123  * @return <code>true</code> if we should exit the application
124  */

125 static boolean startupDialog() {
126     // Show the startup dialog and await return from it
127
StartupDialog sd = new StartupDialog();
128
129     // Get the file the user selected on the startup dialog, if any
130
String JavaDoc selectedFile = sd.getSelectedFile();
131
132     // If they clicked the close window button, we'll get back null, and we'll
133
// return true so the app exits.
134
if (selectedFile == null)
135     return true;
136
137     // If we DID NOT get the value "*StartANewReport*", then they must have
138
// selected a file, so let's insert it into the parameters array and call
139
// back to this main method so it looks like we started the app with the
140
// file in the command line
141
if (!selectedFile.equalsIgnoreCase(StartupDialog.NEW_REPORT_STRING)) {
142     String JavaDoc[] newArgs = { selectedFile };
143     main(newArgs);
144     return true;
145     }
146
147     return false; // The user didn't select a file
148
}
149
150 void runReport(Getopts g, File reportXMLFile) throws Exception JavaDoc {
151     Report report = new Report();
152
153     if (dbPassword != null)
154     report.setDatabasePassword(dbPassword);
155
156     report.read(reportXMLFile); // Must come after password set
157

158     if (paramXMLFile != null) // Must come after file read
159
report.setParameterXMLInput(new File(paramXMLFile));
160
161     if (charSepFile != null) { // Must come after file read
162
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 JavaDoc 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     // Create new file name.
225
String JavaDoc fileName = f.getName();
226     String JavaDoc 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 JavaDoc 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() { // Override close() to possibly exit app
310
super.close();
311         swingLayoutEngineClosed();
312         }
313         };
314     break;
315     }
316     return le;
317 }
318
319 /**
320  * We arrange this method to be called by a Swing layout engine when
321  * it closes. If appropriate, exit the app.
322  */

323 protected void swingLayoutEngineClosed() {
324     if (--numReports == 0)
325     System.exit(0);
326 }
327
328 /**
329  * Use the options related to the data source defined in the report XML file.
330  */

331 protected void dataSourceFromOptions(Getopts g) {
332     if (g.hasOption('n')) { // No password/empty password
333
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); // Grab password; default is null
341
}
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 JavaDoc 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 /**
362  * Prints a usage message and an optional extra error message to System.err
363  * and exits.
364  *
365  * @param errMsg string to print; may be <code>null</code>
366  */

367 public static void usage(String JavaDoc 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