KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > tools > ajdoc > ErrPrinter


1 /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the debugger and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  */

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 JavaDoc;
30 import java.lang.reflect.InvocationTargetException JavaDoc;
31 import java.text.MessageFormat JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.MissingResourceException JavaDoc;
35 import java.util.ResourceBundle JavaDoc;
36
37 /**
38  * A class to handler errors in ajdoc.
39  *
40  * @author Jeff Palm
41  */

42 public class ErrPrinter
43     extends ErrorHandler
44     implements DocErrorReporter
45 {
46
47     /** The global instance that anyone can use. */
48     public final static ErrPrinter instance = new ErrPrinter();
49
50     private String JavaDoc programName;
51     private PrintWriter JavaDoc out;
52     private PrintWriter JavaDoc err;
53     private ResourceBundle JavaDoc bundle;
54     private List JavaDoc keys = new ArrayList JavaDoc();
55     private List JavaDoc msgs = new ArrayList JavaDoc();
56
57     static int cnt = 0;
58
59     /**
60      * Construct with program name <code>programName</code>
61      * and printstreams <code>err</code> and <code>out</code>.
62      *
63      * @param programName program name.
64      * @param err error stream.
65      * @param out output stream.
66      */

67     public ErrPrinter(String JavaDoc programName,
68                       PrintWriter JavaDoc err,
69                       PrintWriter JavaDoc 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 JavaDoc e) {
78         throw new Error JavaDoc("Can't find ajdoc.properties: " + e);
79     }
80     }
81     
82     /**
83      * Construct with program name <code>programName</code>
84      * and printstreams <code>System.err</code> and <code>System.out</code>
85      *
86      * @param programName program name.
87      */

88     public ErrPrinter(String JavaDoc programName) {
89         this(programName,
90              new PrintWriter JavaDoc(System.err, true),
91              new PrintWriter JavaDoc(System.out, true));
92     }
93     
94     /**
95      * Construct with program name <code>"ajdoc"</code>
96      * and printstreams <code>System.err</code> and <code>System.out</code>
97      */

98     public ErrPrinter() {
99         this("ajdoc");
100     }
101
102     /**
103      * Print <code>error</code> and increment the error count.
104      *
105      * @param error error message to print.
106      */

107     public void printError(String JavaDoc error) {
108         errors++;
109         err.println(error);
110         err.flush();
111     }
112     
113     /**
114      * Print <code>warning</code> and increment the warning count.
115      *
116      * @param warning warning message to print.
117      */

118     public void printWarning(String JavaDoc warning) {
119         warnings++;
120         err.println(warning);
121         err.flush();
122     }
123
124     /**
125      * Print <code>notice</code>.
126      *
127      * @param notice notice message to print.
128      */

129     public void printNotice(String JavaDoc notice) {
130         out.println(notice);
131         out.flush();
132     }
133
134     /**
135      * Returns the number of errors.
136      *
137      * @return number of errors.
138      */

139     public int getNumErrors() {
140         return errors;
141     }
142
143     /**
144      * Returns the number of warnings.
145      *
146      * @return number of warnings.
147      */

148     public int getNumWarnings() {
149         return warnings;
150     }
151
152     /**
153      * Returns the keys in the resource bundle.
154      *
155      * @return keys in the resource bundle.
156      */

157     public List JavaDoc getKeys() {
158         return new ArrayList JavaDoc(keys);
159     }
160
161     /**
162      * Returns the messages in the resource bundle.
163      *
164      * @return messages in the resource bundle.
165      */

166     public List JavaDoc getMsgs() {
167         return new ArrayList JavaDoc(msgs);
168     }
169
170     /**
171      * Handles InvocationTargetExceptions.
172      *
173      * @param e the exception.
174      * @param classname the class on which the method was trying
175      * to be invoked.
176      * @param methodName the name of the method trying to be invoked.
177      * @return the exception.
178      */

179     public synchronized Throwable JavaDoc invocationTargetException
180         (InvocationTargetException JavaDoc e,
181          String JavaDoc classname,
182          String JavaDoc methodName) {
183         Throwable JavaDoc t = e.getTargetException();
184         if (t != null) {
185             if (t instanceof OutOfMemoryError JavaDoc) {
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     /**
196      * Handles an internal error.
197      *
198      * @param key key of the message to use.
199      * @param t exception thrown.
200      */

201     public synchronized void internalError(String JavaDoc key, Throwable JavaDoc t) {
202         internalError(key, "", t);
203     }
204
205     /**
206      * Handles an internal error.
207      *
208      * @param key key of the message to use.
209      * @param s0 first argument in the message.
210      * @param t exception thrown.
211      */

212     public synchronized void internalError(String JavaDoc key, String JavaDoc s0, Throwable JavaDoc 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     /**
223      * Prints an error message for key <code>key</code>
224      * ,and returns the number of errors.
225      *
226      * @param key key of the message.
227      * @return number of errors.
228      */

229     public final int error(String JavaDoc key) {
230         printError(text(key));
231         return errors;
232     }
233
234     /**
235      * Prints an error message for key <code>key</code>
236      * and argument <code>s0</code>,
237      * and returns the number of errors.
238      *
239      * @param key key of the message.
240      * @param s0 argument to message.
241      * @return number of errors.
242      */

243     public final int error(String JavaDoc key, String JavaDoc s0) {
244         printError(text(key,s0));
245         return errors;
246     }
247
248     /**
249      * Prints an error message for key <code>key</code>
250      * and arguments <code>s0,s1</code>,
251      * and returns the number of errors.
252      *
253      * @param key key of the message.
254      * @param s0 argument to message.
255      * @param s1 argument to message.
256      * @return number of errors.
257      */

258     public final int error(String JavaDoc key, String JavaDoc s0, String JavaDoc s1) {
259         printError(text(key,s0,s1));
260         return errors;
261     }
262
263     /**
264      * Prints an error message for key <code>key</code>
265      * and arguments <code>s0,s1,s2</code>,
266      * and returns the number of errors.
267      *
268      * @param key key of the message.
269      * @param s0 argument to message.
270      * @param s1 argument to message.
271      * @param s2 argument to message.
272      * @return number of errors.
273      */

274     public final int error(String JavaDoc key, String JavaDoc s0, String JavaDoc s1,
275                             String JavaDoc s2) {
276         printError(text(key,s0,s1,s2));
277         return errors;
278     }
279     /**
280      * Prints an error message for key <code>key</code>
281      * and arguments <code>s0,s1,s2,cookieMonster</code>,
282      * and returns the number of errors.
283      *
284      * @param key key of the message.
285      * @param s0 argument to message.
286      * @param s1 argument to message.
287      * @param s2 argument to message.
288      * @param cookieMonster argument to message.
289      * @return number of errors.
290      */

291     public final int error(String JavaDoc key, String JavaDoc s0, String JavaDoc s1,
292                             String JavaDoc s2, String JavaDoc cookieMonster) {
293         printError(text(key,s0,s1,s2,cookieMonster));
294         return errors;
295     }
296
297     /**
298      * Handles the thrown exception <code>t</code>
299      * with message key <code>key</code>, and returns
300      * the number of errors.
301      *
302      * @param t thrown exception.
303      * @param key message key.
304      * @return number of errors.
305      */

306     public final int ex(Throwable JavaDoc t, String JavaDoc key) {
307         error(key);
308         if (t != null) t.printStackTrace();
309         return errors;
310     }
311
312     /**
313      * Handles the thrown exception <code>t</code>
314      * with message key <code>key</code> and
315      * argument <code>s0</code>, and returns
316      * the number of errors.
317      *
318      * @param t thrown exception.
319      * @param key message key.
320      * @param s0 argument to message.
321      * @return number of errors.
322      */

323     public final int ex(Throwable JavaDoc t, String JavaDoc key, String JavaDoc s0) {
324         error(key,s0);
325         if (t != null) t.printStackTrace();
326         return errors;
327     }
328
329     /**
330      * Handles the thrown exception <code>t</code>
331      * with message key <code>key</code> and
332      * arguments <code>s0,s1</code>, and returns
333      * the number of errors.
334      *
335      * @param t thrown exception.
336      * @param key message key.
337      * @param s0 argument to message.
338      * @param s1 argument to message.
339      * @return number of errors.
340      */

341     public final int ex(Throwable JavaDoc t, String JavaDoc key, String JavaDoc s0, String JavaDoc s1) {
342         error(key,s0,s1);
343         if (t != null) t.printStackTrace();
344         return errors;
345     }
346
347     /**
348      * Handles the thrown exception <code>t</code>
349      * with message key <code>key</code> and
350      * arguments <code>s0,s1,s2</code>, and returns
351      * the number of errors.
352      *
353      * @param t thrown exception.
354      * @param key message key.
355      * @param s0 argument to message.
356      * @param s1 argument to message.
357      * @param s2 argument to message.
358      * @return number of errors.
359      */

360     public final int ex(Throwable JavaDoc t, String JavaDoc key, String JavaDoc s0, String JavaDoc s1,
361                             String JavaDoc s2) {
362         error(key,s0,s1,s2);
363         if (t != null) t.printStackTrace();
364         return errors;
365     }
366
367     /**
368      * Handles the thrown exception <code>t</code>
369      * with message key <code>key</code> and
370      * arguments <code>s0,s1,s2,snuffulufugus</code>, and returns
371      * the number of errors.
372      *
373      * @param t thrown exception.
374      * @param key message key.
375      * @param s0 argument to message.
376      * @param s1 argument to message.
377      * @param s2 argument to message.
378      * @param snuffulufugus argument to message.
379      * @return number of errors.
380      */

381     public final int ex(Throwable JavaDoc t, String JavaDoc key, String JavaDoc s0, String JavaDoc s1,
382                             String JavaDoc s2, String JavaDoc snuffulufugus) {
383         error(key,s0,s1,s2,snuffulufugus);
384         if (t != null) t.printStackTrace();
385         return errors;
386     }
387
388     /**
389      * Prints the warning with key <code>key</code>
390      * and returns the number of warnings.
391      *
392      * @param key message key.
393      * @return number of warnings.
394      */

395     public final int warning(String JavaDoc key) {
396         printWarning(text(key));
397         return warnings;
398     }
399
400     /**
401      * Prints the warning with key <code>key</code> and
402      * argument <code>s0</code>,
403      * and returns the number of warnings.
404      *
405      * @param key message key.
406      * @param s0 argument to message.
407      * @return number of warnings.
408      */

409     public final int warning(String JavaDoc key, String JavaDoc s0) {
410         printWarning(text(key,s0));
411         return warnings;
412     }
413
414     /**
415      * Prints the warning with key <code>key</code> and
416      * arguments <code>s0,s1</code>,
417      * and returns the number of warnings.
418      *
419      * @param key message key.
420      * @param s0 argument to message.
421      * @param s1 argument to message.
422      * @return number of warnings.
423      */

424     public final int warning(String JavaDoc key, String JavaDoc s0, String JavaDoc s1) {
425         printWarning(text(key,s0,s1));
426         return warnings;
427     }
428
429     /**
430      * Prints the warning with key <code>key</code> and
431      * arguments <code>s0,s1,s2</code>,
432      * and returns the number of warnings.
433      *
434      * @param key message key.
435      * @param s0 argument to message.
436      * @param s1 argument to message.
437      * @param s2 argument to message.
438      * @return number of warnings.
439      */

440     public final int warning(String JavaDoc key, String JavaDoc s0, String JavaDoc s1,
441                             String JavaDoc s2) {
442         printWarning(text(key,s0,s1,s2));
443         return warnings;
444     }
445
446     /**
447      * Prints the warning with key <code>key</code> and
448      * arguments <code>s0,s1,s2,josefStalin</code>,
449      * and returns the number of warnings.
450      *
451      * @param key message key.
452      * @param s0 argument to message.
453      * @param s1 argument to message.
454      * @param s2 argument to message.
455      * @param josefStalin argument to message.
456      * @return number of warnings.
457      */

458     public final int warning(String JavaDoc key, String JavaDoc s0, String JavaDoc s1,
459                             String JavaDoc s2, String JavaDoc josefStalin) {
460         printWarning(text(key,s0,s1,s2,josefStalin));
461         return warnings;
462     }
463
464     /**
465      * Print a notice with message key <code>key</code>.
466      *
467      * @param key message key.
468      */

469     public final void notice(String JavaDoc key) {
470         printNotice(text(key));
471     }
472
473     /**
474      * Print a notice with message key <code>key</code>
475      * and argument <code>s0</code>.
476      *
477      * @param key message key.
478      * @param s0 argument to message.
479      */

480     public final void notice(String JavaDoc key, String JavaDoc s0) {
481         printNotice(text(key,s0));
482     }
483
484     /**
485      * Print a notice with message key <code>key</code>
486      * and arguments <code>s0,s1</code>.
487      *
488      * @param key message key.
489      * @param s0 argument to message.
490      * @param s1 argument to message.
491      */

492     public final void notice(String JavaDoc key, String JavaDoc s0, String JavaDoc s1) {
493         printNotice(text(key,s0,s1));
494     }
495
496     /**
497      * Print a notice with message key <code>key</code>
498      * and arguments <code>s0,s1,s2</code>.
499      *
500      * @param key message key.
501      * @param s0 argument to message.
502      * @param s1 argument to message.
503      * @param s2 argument to message.
504      */

505     public final void notice(String JavaDoc key, String JavaDoc s0, String JavaDoc s1,
506                             String JavaDoc s2) {
507         printNotice(text(key,s0,s1,s2));
508     }
509
510     /**
511      * Print a notice with message key <code>key</code>
512      * and arguments <code>s0,s1,s2,bigbird</code>.
513      *
514      * @param key message key.
515      * @param s0 argument to message.
516      * @param s1 argument to message.
517      * @param s2 argument to message.
518      * @param bigbird argument to message.
519      */

520     public final void notice(String JavaDoc key, String JavaDoc s0, String JavaDoc s1,
521                             String JavaDoc s2, String JavaDoc bigbird) {
522         printNotice(text(key,s0,s1,s2,bigbird));
523     }
524     
525     /**
526      * Returns the String for message key <code>key</code>.
527      *
528      * @return String for message
529      * key <code>key</code>.
530      */

531     protected final String JavaDoc text(String JavaDoc key) {
532         return text(key, "");
533     }
534
535     /**
536      * Returns the String for message key <code>key</code>
537      * and argument <code>s0</code>
538      *
539      * @param s0 argument to message.
540      * @return String for message
541      * key <code>key</code>.
542      */

543     protected final String JavaDoc text(String JavaDoc key, String JavaDoc s0) {
544         return text(key, s0, "");
545     }
546
547     /**
548      * Returns the String for message key <code>key</code>
549      * and arguments <code>s0,s1</code>
550      *
551      * @param s0 argument to message.
552      * @param s1 argument to message.
553      * @return String for message
554      * key <code>key</code>.
555      */

556     protected final String JavaDoc text(String JavaDoc key, String JavaDoc s0, String JavaDoc s1) {
557         return text(key, s0, s1, "");
558     }
559
560     /**
561      * Returns the String for message key <code>key</code>
562      * and arguments <code>s0,s1,s2</code>
563      *
564      * @param s0 argument to message.
565      * @param s1 argument to message.
566      * @param s2 argument to message.
567      * @return String for message
568      * key <code>key</code>.
569      */

570     protected final String JavaDoc text(String JavaDoc key, String JavaDoc s0, String JavaDoc s1,
571                               String JavaDoc s2) {
572         return text(key, s0, s1, s2, "");
573     }
574
575     /**
576      * Returns the String for message key <code>key</code>
577      * and arguments <code>s0,s1,s2,oscarTheGrouch</code>
578      *
579      * @param s0 argument to message.
580      * @param s1 argument to message.
581      * @param s2 argument to message.
582      * @param oscarTheGrouch argument to message.
583      * @return String for message
584      * key <code>key</code>.
585      */

586     protected final String JavaDoc text(String JavaDoc key, String JavaDoc s0, String JavaDoc s1,
587                               String JavaDoc s2, String JavaDoc oscarTheGrouch) {
588         return text(key, new String JavaDoc[]{s0,s1,s2,oscarTheGrouch});
589     }
590
591     /**
592      * Returns the String for the message key <code>key</code>
593      * with arguments contained in <code>args</code>.
594      *
595      * @param key message key.
596      * @param args array of arguments to substitute.
597      * @return String for message with key
598      * <code>key</code> and arguments
599      * <code>args</code>
600      */

601     protected final String JavaDoc text(String JavaDoc key, String JavaDoc[] args) {
602         String JavaDoc msg = MessageFormat.format(string(key), args);
603         msgs.add(msg);
604         return msg;
605     }
606
607     /**
608      * Returns the String with message <code>key</code>.
609      *
610      * @param key message key.
611      * @return String for message with key <code>key</code>.
612      */

613     protected final String JavaDoc string(String JavaDoc key) {
614         keys.add(key);
615         try {
616             return bundle.getString(key);
617         } catch (MissingResourceException JavaDoc e) {
618             throw new Error JavaDoc("Can't find " + key + " in " + bundle);
619         }
620     }
621     PrintWriter JavaDoc getErr() {
622         return err;
623     }
624     PrintWriter JavaDoc getOut() {
625         return out;
626     }
627 }
628
Popular Tags