KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JFlex > Out


1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * JFlex 1.4.1 *
3  * Copyright (C) 1998-2004 Gerwin Klein <lsf@jflex.de> *
4  * All rights reserved. *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License. See the file *
8  * COPYRIGHT for more information. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License along *
16  * with this program; if not, write to the Free Software Foundation, Inc., *
17  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
18  * *
19  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

20
21 package JFlex;
22
23
24 import java.io.*;
25 import java.awt.TextArea JavaDoc;
26
27
28 /**
29  * In this class all output to the java console is filtered.
30  *
31  * Use the switches verbose, time and DUMP at compile time to determine
32  * the verbosity of JFlex output. There is no switch for
33  * suppressing error messages. verbose and time can be overridden
34  * by command line paramters.
35  *
36  * Redirects output to a TextArea in GUI mode.
37  *
38  * Counts error and warning messages.
39  *
40  * @author Gerwin Klein
41  * @version JFlex 1.4.1, $Revision: 2.9 $, $Date: 2004/11/06 23:03:30 $
42  */

43 public final class Out {
44
45   /** platform dependent newline sequence */
46   public static final String JavaDoc NL = System.getProperty("line.separator");
47   
48   /** count total warnings */
49   private static int warnings;
50
51   /** count total errors */
52   private static int errors;
53
54   /** output device */
55   private static StdOutWriter out = new StdOutWriter();
56
57
58   /**
59    * Switches to GUI mode if <code>text</code> is not <code>null</code>
60    *
61    * @param text the message TextArea of the JFlex GUI
62    */

63   public static void setGUIMode(TextArea JavaDoc text) {
64     out.setGUIMode(text);
65   }
66   
67   /**
68    * Sets a new output stream and switches to non-gui mode.
69    *
70    * @param stream the new output stream
71    */

72   public static void setOutputStream(OutputStream stream) {
73     out = new StdOutWriter(stream);
74     out.setGUIMode(null);
75   }
76
77   /**
78    * Report time statistic data.
79    *
80    * @param message the message to be printed
81    * @param time elapsed time
82    */

83   public static void time(ErrorMessages message, Timer time) {
84     if (Options.time) {
85       String JavaDoc msg = ErrorMessages.get(message, time.toString());
86       out.println(msg);
87     }
88   }
89   
90   /**
91    * Report time statistic data.
92    *
93    * @param message the message to be printed
94    */

95   public static void time(String JavaDoc message) {
96     if (Options.time) {
97       out.println(message);
98     }
99   }
100
101   /**
102    * Report generation progress.
103    *
104    * @param message the message to be printed
105    */

106   public static void println(String JavaDoc message) {
107     if (Options.verbose)
108       out.println(message);
109   }
110
111   /**
112    * Report generation progress.
113    *
114    * @param message the message to be printed
115    * @param data data to be inserted into the message
116    */

117   public static void println(ErrorMessages message, String JavaDoc data) {
118     if (Options.verbose) {
119       out.println(ErrorMessages.get(message,data));
120     }
121   }
122
123   /**
124    * Report generation progress.
125    *
126    * @param message the message to be printed
127    * @param data data to be inserted into the message
128    */

129   public static void println(ErrorMessages message, int data) {
130     if (Options.verbose) {
131       out.println(ErrorMessages.get(message,data));
132     }
133   }
134
135   /**
136    * Report generation progress.
137    *
138    * @param message the message to be printed
139    */

140   public static void print(String JavaDoc message) {
141     if (Options.verbose) out.print(message);
142   }
143
144   /**
145    * Dump debug information to System.out
146    *
147    * Use like this
148    *
149    * <code>if (Out.DEBUG) Out.debug(message)</code>
150    *
151    * to save performance during normal operation (when DEBUG
152    * is turned off).
153    */

154   public static void debug(String JavaDoc message) {
155     if (Options.DEBUG) System.out.println(message);
156   }
157
158
159   /**
160    * All parts of JFlex, that want to provide dump information
161    * should use this method for their output.
162    *
163    * @message the message to be printed
164    */

165   public static void dump(String JavaDoc message) {
166     if (Options.dump) out.println(message);
167   }
168
169   
170   /**
171    * All parts of JFlex, that want to report error messages
172    * should use this method for their output.
173    *
174    * @message the message to be printed
175    */

176   private static void err(String JavaDoc message) {
177     out.println(message);
178   }
179   
180   
181   /**
182    * throws a GeneratorException if there are any errors recorded
183    */

184   public static void checkErrors() {
185     if (errors > 0) throw new GeneratorException();
186   }
187   
188
189   /**
190    * print error and warning statistics
191    */

192   public static void statistics() {
193     StringBuffer JavaDoc line = new StringBuffer JavaDoc(errors+" error");
194     if (errors != 1) line.append("s");
195
196     line.append(", "+warnings+" warning");
197     if (warnings != 1) line.append("s");
198
199     line.append(".");
200     err(line.toString());
201   }
202
203
204   /**
205    * reset error and warning counters
206    */

207   public static void resetCounters() {
208     errors = 0;
209     warnings = 0;
210   }
211
212   
213   /**
214    * print a warning without position information
215    *
216    * @param message the warning message
217    */

218   public static void warning(String JavaDoc message) {
219     warnings++;
220
221     err(NL+"Warning : "+message);
222   }
223
224
225   /**
226    * print a warning with line information
227    *
228    * @param message code of the warning message
229    * @param line the line information
230    *
231    * @see ErrorMessages
232    */

233   public static void warning(ErrorMessages message, int line) {
234     warnings++;
235
236     String JavaDoc msg = NL+"Warning";
237     if (line > 0) msg = msg+" in line "+(line+1);
238
239     err(msg+": "+ErrorMessages.get(message));
240   }
241
242
243   /**
244    * print warning message with location information
245    *
246    * @param file the file the warning is issued for
247    * @param message the code of the message to print
248    * @param line the line number of the position
249    * @param column the column of the position
250    */

251   public static void warning(File file, ErrorMessages message, int line, int column) {
252
253     String JavaDoc msg = NL+"Warning";
254     if (file != null) msg += " in file \""+file+"\"";
255     if (line >= 0) msg = msg+" (line "+(line+1)+")";
256
257     try {
258       err(msg+": "+NL+ErrorMessages.get(message));
259     }
260     catch (ArrayIndexOutOfBoundsException JavaDoc e) {
261       err(msg);
262     }
263
264     warnings++;
265
266     if (line >= 0) {
267       if (column >= 0)
268         showPosition(file, line, column);
269       else
270         showPosition(file, line);
271     }
272   }
273
274   
275   /**
276    * print error message (string)
277    *
278    * @param message the message to print
279    */

280   public static void error(String JavaDoc message) {
281     errors++;
282     err(NL+message);
283   }
284
285
286   /**
287    * print error message (code)
288    *
289    * @param message the code of the error message
290    *
291    * @see ErrorMessages
292    */

293   public static void error(ErrorMessages message) {
294     errors++;
295     err(NL+"Error: "+ErrorMessages.get(message) );
296   }
297
298
299   /**
300    * print error message with data
301    *
302    * @param data data to insert into the message
303    * @param message the code of the error message
304    *
305    * @see ErrorMessages
306    */

307   public static void error(ErrorMessages message, String JavaDoc data) {
308     errors++;
309     err(NL+"Error: "+ ErrorMessages.get(message,data));
310   }
311
312
313   /**
314    * IO error message for a file (displays file
315    * name in parentheses).
316    *
317    * @param message the code of the error message
318    * @param file the file it occurred for
319    */

320   public static void error(ErrorMessages message, File file) {
321     errors++;
322     err(NL+"Error: "+ErrorMessages.get(message)+" ("+file+")");
323   }
324
325
326   /**
327    * print error message with location information
328    *
329    * @param file the file the error occurred for
330    * @param message the code of the error message to print
331    * @param line the line number of error position
332    * @param column the column of error position
333    */

334   public static void error(File file, ErrorMessages message, int line, int column) {
335
336     String JavaDoc msg = NL+"Error";
337     if (file != null) msg += " in file \""+file+"\"";
338     if (line >= 0) msg = msg+" (line "+(line+1)+")";
339
340     try {
341       err(msg+": "+NL+ErrorMessages.get(message));
342     }
343     catch (ArrayIndexOutOfBoundsException JavaDoc e) {
344       err(msg);
345     }
346
347     errors++;
348
349     if (line >= 0) {
350       if (column >= 0)
351         showPosition(file, line, column);
352       else
353         showPosition(file, line);
354     }
355   }
356
357
358   /**
359    * prints a line of a file with marked position.
360    *
361    * @param file the file of which to show the line
362    * @param line the line to show
363    * @param column the column in which to show the marker
364    */

365   public static void showPosition(File file, int line, int column) {
366     try {
367       String JavaDoc ln = getLine(file, line);
368       if (ln != null) {
369         err( ln );
370
371         if (column < 0) return;
372         
373         String JavaDoc t = "^";
374         for (int i = 0; i < column; i++) t = " "+t;
375         
376         err(t);
377       }
378     }
379     catch (IOException e) {
380       /* silently ignore IO errors, don't show anything */
381     }
382   }
383
384
385   /**
386    * print a line of a file
387    *
388    * @param file the file to show
389    * @param line the line number
390    */

391   public static void showPosition(File file, int line) {
392     try {
393       String JavaDoc ln = getLine(file, line);
394       if (ln != null) err(ln);
395     }
396     catch (IOException e) {
397       /* silently ignore IO errors, don't show anything */
398     }
399   }
400
401
402   /**
403    * get one line from a file
404    *
405    * @param file the file to read
406    * @param line the line number to get
407    *
408    * @throw IOException if any error occurs
409    */

410   private static String JavaDoc getLine(File file, int line) throws IOException {
411     BufferedReader reader = new BufferedReader(new FileReader(file));
412
413     String JavaDoc msg = "";
414
415     for (int i = 0; i <= line; i++)
416       msg = reader.readLine();
417
418     reader.close();
419     
420     return msg;
421   }
422
423
424   /**
425    * Print system information (e.g. in case of unexpected exceptions)
426    */

427   public static void printSystemInfo() {
428     err("Java version: "+System.getProperty("java.version"));
429     err("Runtime name: "+System.getProperty("java.runtime.name"));
430     err("Vendor: "+System.getProperty("java.vendor"));
431     err("VM version: "+System.getProperty("java.vm.version"));
432     err("VM vendor: "+System.getProperty("java.vm.vendor"));
433     err("VM name: "+System.getProperty("java.vm.name"));
434     err("VM info: "+System.getProperty("java.vm.info"));
435     err("OS name: "+System.getProperty("os.name"));
436     err("OS arch: "+System.getProperty("os.arch"));
437     err("OS version: "+System.getProperty("os.version"));
438     err("Encoding: "+System.getProperty("file.encoding"));
439     err("JFlex version: "+Main.version);
440   }
441
442
443   /**
444    * Request a bug report for an unexpected Exception/Error.
445    */

446   public static void requestBugReport(Error JavaDoc e) {
447     err("An unexpected error occurred. Please send a report of this to");
448     err("<bugs@jflex.de> and include the following information:");
449     err("");
450     printSystemInfo();
451     err("Exception:");
452     e.printStackTrace(out);
453     err("");
454     err("Please also include a specification (as small as possible)");
455     err("that triggers this error. You may also want to check at");
456     err("http://www.jflex.de if there is a newer version available");
457     err("that doesn't have this problem");
458     err("");
459     err("Thanks for your support.");
460   }
461 }
462
Popular Tags