001: /*
002: * Copyright 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.javadoc;
027:
028: import java.io.PrintWriter;
029: import java.text.MessageFormat;
030: import java.util.ResourceBundle;
031: import java.util.MissingResourceException;
032:
033: import com.sun.javadoc.*;
034:
035: import com.sun.tools.javac.util.Context;
036:
037: import com.sun.tools.javac.util.Log; // Access to 'javac' output streams
038:
039: /**
040: * Utility for integrating with javadoc tools and for localization.
041: * Handle Resources. Access to error and warning counts.
042: * Message formatting.
043: * <br>
044: * Also provides implementation for DocErrorReporter.
045: *
046: * @see java.util.ResourceBundle
047: * @see java.text.MessageFormat
048: * @author Neal Gafter (rewrite)
049: */
050: public class Messager extends Log implements DocErrorReporter {
051:
052: /** Get the current messager, which is also the compiler log. */
053: public static Messager instance0(Context context) {
054: Log instance = context.get(logKey);
055: if (instance == null || !(instance instanceof Messager))
056: throw new InternalError("no messager instance!");
057: return (Messager) instance;
058: }
059:
060: public static void preRegister(final Context context,
061: final String programName) {
062: context.put(logKey, new Context.Factory<Log>() {
063: public Log make() {
064: return new Messager(context, programName);
065: }
066: });
067: }
068:
069: public static void preRegister(final Context context,
070: final String programName, final PrintWriter errWriter,
071: final PrintWriter warnWriter, final PrintWriter noticeWriter) {
072: context.put(logKey, new Context.Factory<Log>() {
073: public Log make() {
074: return new Messager(context, programName, errWriter,
075: warnWriter, noticeWriter);
076: }
077: });
078: }
079:
080: public class ExitJavadoc extends Error {
081: private static final long serialVersionUID = 0;
082: }
083:
084: private final String programName;
085:
086: private ResourceBundle messageRB = null;
087:
088: /** The default writer for diagnostics
089: */
090: static final PrintWriter defaultErrWriter = new PrintWriter(
091: System.err);
092: static final PrintWriter defaultWarnWriter = new PrintWriter(
093: System.err);
094: static final PrintWriter defaultNoticeWriter = new PrintWriter(
095: System.out);
096:
097: /**
098: * Constructor
099: * @param programName Name of the program (for error messages).
100: */
101: protected Messager(Context context, String programName) {
102: this (context, programName, defaultErrWriter, defaultWarnWriter,
103: defaultNoticeWriter);
104: }
105:
106: /**
107: * Constructor
108: * @param programName Name of the program (for error messages).
109: * @param errWriter Stream for error messages
110: * @param warnWriter Stream for warnings
111: * @param noticeWriter Stream for other messages
112: */
113: protected Messager(Context context, String programName,
114: PrintWriter errWriter, PrintWriter warnWriter,
115: PrintWriter noticeWriter) {
116: super (context, errWriter, warnWriter, noticeWriter);
117: this .programName = programName;
118: }
119:
120: /**
121: * Reset resource bundle, eg. locale has changed.
122: */
123: public void reset() {
124: messageRB = null;
125: }
126:
127: /**
128: * Get string from ResourceBundle, initialize ResourceBundle
129: * if needed.
130: */
131: private String getString(String key) {
132: ResourceBundle messageRB = this .messageRB;
133: if (messageRB == null) {
134: try {
135: this .messageRB = messageRB = ResourceBundle
136: .getBundle("com.sun.tools.javadoc.resources.javadoc");
137: } catch (MissingResourceException e) {
138: throw new Error(
139: "Fatal: Resource for javadoc is missing");
140: }
141: }
142: return messageRB.getString(key);
143: }
144:
145: /**
146: * get and format message string from resource
147: *
148: * @param key selects message from resource
149: */
150: String getText(String key) {
151: return getText(key, (String) null);
152: }
153:
154: /**
155: * get and format message string from resource
156: *
157: * @param key selects message from resource
158: * @param a1 first argument
159: */
160: String getText(String key, String a1) {
161: return getText(key, a1, null);
162: }
163:
164: /**
165: * get and format message string from resource
166: *
167: * @param key selects message from resource
168: * @param a1 first argument
169: * @param a2 second argument
170: */
171: String getText(String key, String a1, String a2) {
172: return getText(key, a1, a2, null);
173: }
174:
175: /**
176: * get and format message string from resource
177: *
178: * @param key selects message from resource
179: * @param a1 first argument
180: * @param a2 second argument
181: * @param a3 third argument
182: */
183: String getText(String key, String a1, String a2, String a3) {
184: return getText(key, a1, a2, a3, null);
185: }
186:
187: /**
188: * get and format message string from resource
189: *
190: * @param key selects message from resource
191: * @param a1 first argument
192: * @param a2 second argument
193: * @param a3 third argument
194: * @param a4 fourth argument
195: */
196: String getText(String key, String a1, String a2, String a3,
197: String a4) {
198: try {
199: String message = getString(key);
200: String[] args = new String[4];
201: args[0] = a1;
202: args[1] = a2;
203: args[2] = a3;
204: args[3] = a4;
205: return MessageFormat.format(message, (Object[]) args);
206: } catch (MissingResourceException e) {
207: return "********** Resource for javadoc is broken. There is no "
208: + key + " key in resource.";
209: }
210: }
211:
212: /**
213: * Print error message, increment error count.
214: * Part of DocErrorReporter.
215: *
216: * @param msg message to print
217: */
218: public void printError(String msg) {
219: printError(null, msg);
220: }
221:
222: /**
223: * Print error message, increment error count.
224: * Part of DocErrorReporter.
225: *
226: * @param pos the position where the error occurs
227: * @param msg message to print
228: */
229: public void printError(SourcePosition pos, String msg) {
230: String prefix = (pos == null) ? programName : pos.toString();
231: errWriter.println(prefix + ": " + getText("javadoc.error")
232: + " - " + msg);
233: errWriter.flush();
234: prompt();
235: nerrors++;
236: }
237:
238: /**
239: * Print warning message, increment warning count.
240: * Part of DocErrorReporter.
241: *
242: * @param msg message to print
243: */
244: public void printWarning(String msg) {
245: printWarning(null, msg);
246: }
247:
248: /**
249: * Print warning message, increment warning count.
250: * Part of DocErrorReporter.
251: *
252: * @param pos the position where the error occurs
253: * @param msg message to print
254: */
255: public void printWarning(SourcePosition pos, String msg) {
256: String prefix = (pos == null) ? programName : pos.toString();
257: warnWriter.println(prefix + ": " + getText("javadoc.warning")
258: + " - " + msg);
259: warnWriter.flush();
260: nwarnings++;
261: }
262:
263: /**
264: * Print a message.
265: * Part of DocErrorReporter.
266: *
267: * @param msg message to print
268: */
269: public void printNotice(String msg) {
270: printNotice(null, msg);
271: }
272:
273: /**
274: * Print a message.
275: * Part of DocErrorReporter.
276: *
277: * @param pos the position where the error occurs
278: * @param msg message to print
279: */
280: public void printNotice(SourcePosition pos, String msg) {
281: if (pos == null)
282: noticeWriter.println(msg);
283: else
284: noticeWriter.println(pos + ": " + msg);
285: noticeWriter.flush();
286: }
287:
288: /**
289: * Print error message, increment error count.
290: *
291: * @param key selects message from resource
292: */
293: public void error(SourcePosition pos, String key) {
294: printError(pos, getText(key));
295: }
296:
297: /**
298: * Print error message, increment error count.
299: *
300: * @param key selects message from resource
301: * @param a1 first argument
302: */
303: public void error(SourcePosition pos, String key, String a1) {
304: printError(pos, getText(key, a1));
305: }
306:
307: /**
308: * Print error message, increment error count.
309: *
310: * @param key selects message from resource
311: * @param a1 first argument
312: * @param a2 second argument
313: */
314: public void error(SourcePosition pos, String key, String a1,
315: String a2) {
316: printError(pos, getText(key, a1, a2));
317: }
318:
319: /**
320: * Print error message, increment error count.
321: *
322: * @param key selects message from resource
323: * @param a1 first argument
324: * @param a2 second argument
325: * @param a3 third argument
326: */
327: public void error(SourcePosition pos, String key, String a1,
328: String a2, String a3) {
329: printError(pos, getText(key, a1, a2, a3));
330: }
331:
332: /**
333: * Print warning message, increment warning count.
334: *
335: * @param key selects message from resource
336: */
337: public void warning(SourcePosition pos, String key) {
338: printWarning(pos, getText(key));
339: }
340:
341: /**
342: * Print warning message, increment warning count.
343: *
344: * @param key selects message from resource
345: * @param a1 first argument
346: */
347: public void warning(SourcePosition pos, String key, String a1) {
348: printWarning(pos, getText(key, a1));
349: }
350:
351: /**
352: * Print warning message, increment warning count.
353: *
354: * @param key selects message from resource
355: * @param a1 first argument
356: * @param a2 second argument
357: */
358: public void warning(SourcePosition pos, String key, String a1,
359: String a2) {
360: printWarning(pos, getText(key, a1, a2));
361: }
362:
363: /**
364: * Print warning message, increment warning count.
365: *
366: * @param key selects message from resource
367: * @param a1 first argument
368: * @param a2 second argument
369: * @param a3 third argument
370: */
371: public void warning(SourcePosition pos, String key, String a1,
372: String a2, String a3) {
373: printWarning(pos, getText(key, a1, a2, a3));
374: }
375:
376: /**
377: * Print warning message, increment warning count.
378: *
379: * @param key selects message from resource
380: * @param a1 first argument
381: * @param a2 second argument
382: * @param a3 third argument
383: */
384: public void warning(SourcePosition pos, String key, String a1,
385: String a2, String a3, String a4) {
386: printWarning(pos, getText(key, a1, a2, a3, a4));
387: }
388:
389: /**
390: * Print a message.
391: *
392: * @param key selects message from resource
393: */
394: public void notice(String key) {
395: printNotice(getText(key));
396: }
397:
398: /**
399: * Print a message.
400: *
401: * @param key selects message from resource
402: * @param a1 first argument
403: */
404: public void notice(String key, String a1) {
405: printNotice(getText(key, a1));
406: }
407:
408: /**
409: * Print a message.
410: *
411: * @param key selects message from resource
412: * @param a1 first argument
413: * @param a2 second argument
414: */
415: public void notice(String key, String a1, String a2) {
416: printNotice(getText(key, a1, a2));
417: }
418:
419: /**
420: * Print a message.
421: *
422: * @param key selects message from resource
423: * @param a1 first argument
424: * @param a2 second argument
425: * @param a3 third argument
426: */
427: public void notice(String key, String a1, String a2, String a3) {
428: printNotice(getText(key, a1, a2, a3));
429: }
430:
431: /**
432: * Return total number of errors, including those recorded
433: * in the compilation log.
434: */
435: public int nerrors() {
436: return nerrors;
437: }
438:
439: /**
440: * Return total number of warnings, including those recorded
441: * in the compilation log.
442: */
443: public int nwarnings() {
444: return nwarnings;
445: }
446:
447: /**
448: * Print exit message.
449: */
450: public void exitNotice() {
451: int nerrors = nerrors();
452: int nwarnings = nwarnings();
453: if (nerrors > 0) {
454: notice((nerrors > 1) ? "main.errors" : "main.error", ""
455: + nerrors);
456: }
457: if (nwarnings > 0) {
458: notice((nwarnings > 1) ? "main.warnings" : "main.warning",
459: "" + nwarnings);
460: }
461: }
462:
463: /**
464: * Force program exit, e.g., from a fatal error.
465: * <p>
466: * TODO: This method does not really belong here.
467: */
468: public void exit() {
469: throw new ExitJavadoc();
470: }
471:
472: }
|