1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
3
* Copyright (C) 1998-2004 Gerwin Klein <lsf@jflex.de> *
4
* All rights reserved. *
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. *
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. *
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 *
19
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
21
package weka.core.parser.JFlex;
25
import java.awt.TextArea;
29
* In this class all output to the java console is filtered.
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.
36
* Redirects output to a TextArea in GUI mode.
38
* Counts error and warning messages.
40
* @author Gerwin Klein
41
* @version JFlex 1.4.1, $Revision: 1.1 $, $Date: 2008/05/09 09:14:10 $
43
public final class Out {
45
/** platform dependent newline sequence */
46
public static final String NL = System.getProperty("line.separator");
48
/** count total warnings */
49
private static int warnings;
51
/** count total errors */
52
private static int errors;
55
private static StdOutWriter out = new StdOutWriter();
59
* Switches to GUI mode if <code>text</code> is not <code>null</code>
61
* @param text the message TextArea of the JFlex GUI
63
public static void setGUIMode(TextArea text) {
68
* Sets a new output stream and switches to non-gui mode.
70
* @param stream the new output stream
72
public static void setOutputStream(OutputStream stream) {
73
out = new StdOutWriter(stream);
78
* Report time statistic data.
80
* @param message the message to be printed
81
* @param time elapsed time
83
public static void time(ErrorMessages message, Timer time) {
85
String msg = ErrorMessages.get(message, time.toString());
91
* Report time statistic data.
93
* @param message the message to be printed
95
public static void time(String message) {
102
* Report generation progress.
104
* @param message the message to be printed
106
public static void println(String message) {
108
out.println(message);
112
* Report generation progress.
114
* @param message the message to be printed
115
* @param data data to be inserted into the message
117
public static void println(ErrorMessages message, String data) {
118
if (Options.verbose) {
119
out.println(ErrorMessages.get(message,data));
124
* Report generation progress.
126
* @param message the message to be printed
127
* @param data data to be inserted into the message
129
public static void println(ErrorMessages message, int data) {
130
if (Options.verbose) {
131
out.println(ErrorMessages.get(message,data));
136
* Report generation progress.
138
* @param message the message to be printed
140
public static void print(String message) {
141
if (Options.verbose) out.print(message);
145
* Dump debug information to System.out
149
* <code>if (Out.DEBUG) Out.debug(message)</code>
151
* to save performance during normal operation (when DEBUG
154
public static void debug(String message) {
155
if (Options.DEBUG) System.out.println(message);
160
* All parts of JFlex, that want to provide dump information
161
* should use this method for their output.
163
* @message the message to be printed
165
public static void dump(String message) {
166
if (Options.dump) out.println(message);
171
* All parts of JFlex, that want to report error messages
172
* should use this method for their output.
174
* @message the message to be printed
176
private static void err(String message) {
177
out.println(message);
182
* throws a GeneratorException if there are any errors recorded
184
public static void checkErrors() {
185
if (errors > 0) throw new GeneratorException();
190
* print error and warning statistics
192
public static void statistics() {
193
StringBuffer line = new StringBuffer(errors+" error");
194
if (errors != 1) line.append("s");
196
line.append(", "+warnings+" warning");
197
if (warnings != 1) line.append("s");
200
err(line.toString());
205
* reset error and warning counters
207
public static void resetCounters() {
214
* print a warning without position information
216
* @param message the warning message
218
public static void warning(String message) {
221
err(NL+"Warning : "+message);
226
* print a warning with line information
228
* @param message code of the warning message
229
* @param line the line information
233
public static void warning(ErrorMessages message, int line) {
236
String msg = NL+"Warning";
237
if (line > 0) msg = msg+" in line "+(line+1);
239
err(msg+": "+ErrorMessages.get(message));
244
* print warning message with location information
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
251
public static void warning(File file, ErrorMessages message, int line, int column) {
253
String msg = NL+"Warning";
254
if (file != null) msg += " in file \""+file+"\"";
255
if (line >= 0) msg = msg+" (line "+(line+1)+")";
258
err(msg+": "+NL+ErrorMessages.get(message));
260
catch (ArrayIndexOutOfBoundsException e) {
268
showPosition(file, line, column);
270
showPosition(file, line);
276
* print error message (string)
278
* @param message the message to print
280
public static void error(String message) {
287
* print error message (code)
289
* @param message the code of the error message
293
public static void error(ErrorMessages message) {
295
err(NL+"Error: "+ErrorMessages.get(message) );
300
* print error message with data
302
* @param data data to insert into the message
303
* @param message the code of the error message
307
public static void error(ErrorMessages message, String data) {
309
err(NL+"Error: "+ ErrorMessages.get(message,data));
314
* IO error message for a file (displays file
315
* name in parentheses).
317
* @param message the code of the error message
318
* @param file the file it occurred for
320
public static void error(ErrorMessages message, File file) {
322
err(NL+"Error: "+ErrorMessages.get(message)+" ("+file+")");
327
* print error message with location information
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
334
public static void error(File file, ErrorMessages message, int line, int column) {
336
String msg = NL+"Error";
337
if (file != null) msg += " in file \""+file+"\"";
338
if (line >= 0) msg = msg+" (line "+(line+1)+")";
341
err(msg+": "+NL+ErrorMessages.get(message));
343
catch (ArrayIndexOutOfBoundsException e) {
351
showPosition(file, line, column);
353
showPosition(file, line);
359
* prints a line of a file with marked position.
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
365
public static void showPosition(File file, int line, int column) {
367
String ln = getLine(file, line);
371
if (column < 0) return;
374
for (int i = 0; i < column; i++) t = " "+t;
379
catch (IOException e) {
380
/* silently ignore IO errors, don't show anything */
386
* print a line of a file
388
* @param file the file to show
389
* @param line the line number
391
public static void showPosition(File file, int line) {
393
String ln = getLine(file, line);
394
if (ln != null) err(ln);
396
catch (IOException e) {
397
/* silently ignore IO errors, don't show anything */
403
* get one line from a file
405
* @param file the file to read
406
* @param line the line number to get
408
* @throw IOException if any error occurs
410
private static String getLine(File file, int line) throws IOException {
411
BufferedReader reader = new BufferedReader(new FileReader(file));
415
for (int i = 0; i <= line; i++)
416
msg = reader.readLine();
425
* Print system information (e.g. in case of unexpected exceptions)
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);
444
* Request a bug report for an unexpected Exception/Error.
446
public static void requestBugReport(Error e) {
447
err("An unexpected error occurred. Please send a report of this to");
448
err("<bugs@jflex.de> and include the following information:");
452
e.printStackTrace(out);
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");
459
err("Thanks for your support.");