1 20 21 package JFlex; 22 23 24 import java.io.*; 25 import java.awt.TextArea ; 26 27 28 43 public final class Out { 44 45 46 public static final String NL = System.getProperty("line.separator"); 47 48 49 private static int warnings; 50 51 52 private static int errors; 53 54 55 private static StdOutWriter out = new StdOutWriter(); 56 57 58 63 public static void setGUIMode(TextArea text) { 64 out.setGUIMode(text); 65 } 66 67 72 public static void setOutputStream(OutputStream stream) { 73 out = new StdOutWriter(stream); 74 out.setGUIMode(null); 75 } 76 77 83 public static void time(ErrorMessages message, Timer time) { 84 if (Options.time) { 85 String msg = ErrorMessages.get(message, time.toString()); 86 out.println(msg); 87 } 88 } 89 90 95 public static void time(String message) { 96 if (Options.time) { 97 out.println(message); 98 } 99 } 100 101 106 public static void println(String message) { 107 if (Options.verbose) 108 out.println(message); 109 } 110 111 117 public static void println(ErrorMessages message, String data) { 118 if (Options.verbose) { 119 out.println(ErrorMessages.get(message,data)); 120 } 121 } 122 123 129 public static void println(ErrorMessages message, int data) { 130 if (Options.verbose) { 131 out.println(ErrorMessages.get(message,data)); 132 } 133 } 134 135 140 public static void print(String message) { 141 if (Options.verbose) out.print(message); 142 } 143 144 154 public static void debug(String message) { 155 if (Options.DEBUG) System.out.println(message); 156 } 157 158 159 165 public static void dump(String message) { 166 if (Options.dump) out.println(message); 167 } 168 169 170 176 private static void err(String message) { 177 out.println(message); 178 } 179 180 181 184 public static void checkErrors() { 185 if (errors > 0) throw new GeneratorException(); 186 } 187 188 189 192 public static void statistics() { 193 StringBuffer line = new StringBuffer (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 207 public static void resetCounters() { 208 errors = 0; 209 warnings = 0; 210 } 211 212 213 218 public static void warning(String message) { 219 warnings++; 220 221 err(NL+"Warning : "+message); 222 } 223 224 225 233 public static void warning(ErrorMessages message, int line) { 234 warnings++; 235 236 String msg = NL+"Warning"; 237 if (line > 0) msg = msg+" in line "+(line+1); 238 239 err(msg+": "+ErrorMessages.get(message)); 240 } 241 242 243 251 public static void warning(File file, ErrorMessages message, int line, int column) { 252 253 String 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 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 280 public static void error(String message) { 281 errors++; 282 err(NL+message); 283 } 284 285 286 293 public static void error(ErrorMessages message) { 294 errors++; 295 err(NL+"Error: "+ErrorMessages.get(message) ); 296 } 297 298 299 307 public static void error(ErrorMessages message, String data) { 308 errors++; 309 err(NL+"Error: "+ ErrorMessages.get(message,data)); 310 } 311 312 313 320 public static void error(ErrorMessages message, File file) { 321 errors++; 322 err(NL+"Error: "+ErrorMessages.get(message)+" ("+file+")"); 323 } 324 325 326 334 public static void error(File file, ErrorMessages message, int line, int column) { 335 336 String 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 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 365 public static void showPosition(File file, int line, int column) { 366 try { 367 String ln = getLine(file, line); 368 if (ln != null) { 369 err( ln ); 370 371 if (column < 0) return; 372 373 String t = "^"; 374 for (int i = 0; i < column; i++) t = " "+t; 375 376 err(t); 377 } 378 } 379 catch (IOException e) { 380 381 } 382 } 383 384 385 391 public static void showPosition(File file, int line) { 392 try { 393 String ln = getLine(file, line); 394 if (ln != null) err(ln); 395 } 396 catch (IOException e) { 397 398 } 399 } 400 401 402 410 private static String getLine(File file, int line) throws IOException { 411 BufferedReader reader = new BufferedReader(new FileReader(file)); 412 413 String 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 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 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:"); 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
|