1 22 package org.aspectj.tools.ajdoc; 23 24 import org.aspectj.compiler.base.ErrorHandler; 25 import org.aspectj.compiler.base.InternalCompilerError; 26 27 import com.sun.javadoc.DocErrorReporter; 28 29 import java.io.PrintWriter ; 30 import java.lang.reflect.InvocationTargetException ; 31 import java.text.MessageFormat ; 32 import java.util.ArrayList ; 33 import java.util.List ; 34 import java.util.MissingResourceException ; 35 import java.util.ResourceBundle ; 36 37 42 public class ErrPrinter 43 extends ErrorHandler 44 implements DocErrorReporter 45 { 46 47 48 public final static ErrPrinter instance = new ErrPrinter(); 49 50 private String programName; 51 private PrintWriter out; 52 private PrintWriter err; 53 private ResourceBundle bundle; 54 private List keys = new ArrayList (); 55 private List msgs = new ArrayList (); 56 57 static int cnt = 0; 58 59 67 public ErrPrinter(String programName, 68 PrintWriter err, 69 PrintWriter out) { 70 super(err); 71 this.programName = programName; 72 this.err = err; 73 this.out = out; 74 try { 75 bundle = ResourceBundle.getBundle 76 ("org.aspectj.tools.ajdoc.resources.ajdoc"); 77 } catch (MissingResourceException e) { 78 throw new Error ("Can't find ajdoc.properties: " + e); 79 } 80 } 81 82 88 public ErrPrinter(String programName) { 89 this(programName, 90 new PrintWriter (System.err, true), 91 new PrintWriter (System.out, true)); 92 } 93 94 98 public ErrPrinter() { 99 this("ajdoc"); 100 } 101 102 107 public void printError(String error) { 108 errors++; 109 err.println(error); 110 err.flush(); 111 } 112 113 118 public void printWarning(String warning) { 119 warnings++; 120 err.println(warning); 121 err.flush(); 122 } 123 124 129 public void printNotice(String notice) { 130 out.println(notice); 131 out.flush(); 132 } 133 134 139 public int getNumErrors() { 140 return errors; 141 } 142 143 148 public int getNumWarnings() { 149 return warnings; 150 } 151 152 157 public List getKeys() { 158 return new ArrayList (keys); 159 } 160 161 166 public List getMsgs() { 167 return new ArrayList (msgs); 168 } 169 170 179 public synchronized Throwable invocationTargetException 180 (InvocationTargetException e, 181 String classname, 182 String methodName) { 183 Throwable t = e.getTargetException(); 184 if (t != null) { 185 if (t instanceof OutOfMemoryError ) { 186 error("out_of_memory"); 187 } else { 188 error("exception_thrown", "", classname, methodName, t+""); 189 t.printStackTrace(); 190 } 191 } 192 return t != null ? t : e; 193 } 194 195 201 public synchronized void internalError(String key, Throwable t) { 202 internalError(key, "", t); 203 } 204 205 212 public synchronized void internalError(String key, String s0, Throwable t) { 213 if (t instanceof InternalCompilerError) { 214 t = ((InternalCompilerError)t).uncaughtThrowable; 215 } 216 error(key, s0, t != null ? t.getMessage() : ""); 217 if (t != null) t.printStackTrace(); 218 internalError(t, null); 219 } 220 221 222 229 public final int error(String key) { 230 printError(text(key)); 231 return errors; 232 } 233 234 243 public final int error(String key, String s0) { 244 printError(text(key,s0)); 245 return errors; 246 } 247 248 258 public final int error(String key, String s0, String s1) { 259 printError(text(key,s0,s1)); 260 return errors; 261 } 262 263 274 public final int error(String key, String s0, String s1, 275 String s2) { 276 printError(text(key,s0,s1,s2)); 277 return errors; 278 } 279 291 public final int error(String key, String s0, String s1, 292 String s2, String cookieMonster) { 293 printError(text(key,s0,s1,s2,cookieMonster)); 294 return errors; 295 } 296 297 306 public final int ex(Throwable t, String key) { 307 error(key); 308 if (t != null) t.printStackTrace(); 309 return errors; 310 } 311 312 323 public final int ex(Throwable t, String key, String s0) { 324 error(key,s0); 325 if (t != null) t.printStackTrace(); 326 return errors; 327 } 328 329 341 public final int ex(Throwable t, String key, String s0, String s1) { 342 error(key,s0,s1); 343 if (t != null) t.printStackTrace(); 344 return errors; 345 } 346 347 360 public final int ex(Throwable t, String key, String s0, String s1, 361 String s2) { 362 error(key,s0,s1,s2); 363 if (t != null) t.printStackTrace(); 364 return errors; 365 } 366 367 381 public final int ex(Throwable t, String key, String s0, String s1, 382 String s2, String snuffulufugus) { 383 error(key,s0,s1,s2,snuffulufugus); 384 if (t != null) t.printStackTrace(); 385 return errors; 386 } 387 388 395 public final int warning(String key) { 396 printWarning(text(key)); 397 return warnings; 398 } 399 400 409 public final int warning(String key, String s0) { 410 printWarning(text(key,s0)); 411 return warnings; 412 } 413 414 424 public final int warning(String key, String s0, String s1) { 425 printWarning(text(key,s0,s1)); 426 return warnings; 427 } 428 429 440 public final int warning(String key, String s0, String s1, 441 String s2) { 442 printWarning(text(key,s0,s1,s2)); 443 return warnings; 444 } 445 446 458 public final int warning(String key, String s0, String s1, 459 String s2, String josefStalin) { 460 printWarning(text(key,s0,s1,s2,josefStalin)); 461 return warnings; 462 } 463 464 469 public final void notice(String key) { 470 printNotice(text(key)); 471 } 472 473 480 public final void notice(String key, String s0) { 481 printNotice(text(key,s0)); 482 } 483 484 492 public final void notice(String key, String s0, String s1) { 493 printNotice(text(key,s0,s1)); 494 } 495 496 505 public final void notice(String key, String s0, String s1, 506 String s2) { 507 printNotice(text(key,s0,s1,s2)); 508 } 509 510 520 public final void notice(String key, String s0, String s1, 521 String s2, String bigbird) { 522 printNotice(text(key,s0,s1,s2,bigbird)); 523 } 524 525 531 protected final String text(String key) { 532 return text(key, ""); 533 } 534 535 543 protected final String text(String key, String s0) { 544 return text(key, s0, ""); 545 } 546 547 556 protected final String text(String key, String s0, String s1) { 557 return text(key, s0, s1, ""); 558 } 559 560 570 protected final String text(String key, String s0, String s1, 571 String s2) { 572 return text(key, s0, s1, s2, ""); 573 } 574 575 586 protected final String text(String key, String s0, String s1, 587 String s2, String oscarTheGrouch) { 588 return text(key, new String []{s0,s1,s2,oscarTheGrouch}); 589 } 590 591 601 protected final String text(String key, String [] args) { 602 String msg = MessageFormat.format(string(key), args); 603 msgs.add(msg); 604 return msg; 605 } 606 607 613 protected final String string(String key) { 614 keys.add(key); 615 try { 616 return bundle.getString(key); 617 } catch (MissingResourceException e) { 618 throw new Error ("Can't find " + key + " in " + bundle); 619 } 620 } 621 PrintWriter getErr() { 622 return err; 623 } 624 PrintWriter getOut() { 625 return out; 626 } 627 } 628 | Popular Tags |