src/share/classes/com/sun/tools/javadoc/Messager.java

changeset 1411
467f4f754368
parent 1359
25e14ad23cef
child 1413
bdcef2ef52d2
equal deleted inserted replaced
1410:bfec2a1cc869 1411:467f4f754368
23 * questions. 23 * questions.
24 */ 24 */
25 25
26 package com.sun.tools.javadoc; 26 package com.sun.tools.javadoc;
27 27
28 import java.io.PrintWriter; // Access to 'javac' output streams 28 import java.io.PrintWriter;
29 import java.text.MessageFormat; 29 import java.text.MessageFormat;
30 import java.util.MissingResourceException; 30 import java.util.Locale;
31 import java.util.ResourceBundle; 31 import java.util.ResourceBundle;
32 32
33 import com.sun.javadoc.*; 33 import com.sun.javadoc.*;
34 import com.sun.tools.javac.util.Context; 34 import com.sun.tools.javac.util.Context;
35 import com.sun.tools.javac.util.JCDiagnostic;
36 import com.sun.tools.javac.util.JavacMessages;
35 import com.sun.tools.javac.util.Log; 37 import com.sun.tools.javac.util.Log;
36 38
37 /** 39 /**
38 * Utility for integrating with javadoc tools and for localization. 40 * Utility for integrating with javadoc tools and for localization.
39 * Handle Resources. Access to error and warning counts. 41 * Handle Resources. Access to error and warning counts.
49 * @see java.util.ResourceBundle 51 * @see java.util.ResourceBundle
50 * @see java.text.MessageFormat 52 * @see java.text.MessageFormat
51 * @author Neal Gafter (rewrite) 53 * @author Neal Gafter (rewrite)
52 */ 54 */
53 public class Messager extends Log implements DocErrorReporter { 55 public class Messager extends Log implements DocErrorReporter {
56 public static final SourcePosition NOPOS = null;
54 57
55 /** Get the current messager, which is also the compiler log. */ 58 /** Get the current messager, which is also the compiler log. */
56 public static Messager instance0(Context context) { 59 public static Messager instance0(Context context) {
57 Log instance = context.get(logKey); 60 Log instance = context.get(logKey);
58 if (instance == null || !(instance instanceof Messager)) 61 if (instance == null || !(instance instanceof Messager))
89 private static final long serialVersionUID = 0; 92 private static final long serialVersionUID = 0;
90 } 93 }
91 94
92 final String programName; 95 final String programName;
93 96
94 private ResourceBundle messageRB = null; 97 private Locale locale;
98 private final JavacMessages messages;
99 private final JCDiagnostic.Factory javadocDiags;
95 100
96 /** The default writer for diagnostics 101 /** The default writer for diagnostics
97 */ 102 */
98 static final PrintWriter defaultErrWriter = new PrintWriter(System.err); 103 static final PrintWriter defaultErrWriter = new PrintWriter(System.err);
99 static final PrintWriter defaultWarnWriter = new PrintWriter(System.err); 104 static final PrintWriter defaultWarnWriter = new PrintWriter(System.err);
119 String programName, 124 String programName,
120 PrintWriter errWriter, 125 PrintWriter errWriter,
121 PrintWriter warnWriter, 126 PrintWriter warnWriter,
122 PrintWriter noticeWriter) { 127 PrintWriter noticeWriter) {
123 super(context, errWriter, warnWriter, noticeWriter); 128 super(context, errWriter, warnWriter, noticeWriter);
129 messages = JavacMessages.instance(context);
130 messages.add("com.sun.tools.javadoc.resources.javadoc");
131 javadocDiags = new JCDiagnostic.Factory(messages, "javadoc");
124 this.programName = programName; 132 this.programName = programName;
125 } 133 }
126 134
127 @Override 135 @Override
128 protected int getDefaultMaxErrors() { 136 protected int getDefaultMaxErrors() {
132 @Override 140 @Override
133 protected int getDefaultMaxWarnings() { 141 protected int getDefaultMaxWarnings() {
134 return Integer.MAX_VALUE; 142 return Integer.MAX_VALUE;
135 } 143 }
136 144
137 /** 145 public void setLocale(Locale locale) {
138 * Reset resource bundle, eg. locale has changed. 146 this.locale = locale;
139 */
140 public void reset() {
141 messageRB = null;
142 }
143
144 /**
145 * Get string from ResourceBundle, initialize ResourceBundle
146 * if needed.
147 */
148 private String getString(String key) {
149 if (messageRB == null) {
150 try {
151 messageRB = ResourceBundle.getBundle(
152 "com.sun.tools.javadoc.resources.javadoc");
153 } catch (MissingResourceException e) {
154 throw new Error("Fatal: Resource for javadoc is missing");
155 }
156 }
157 return messageRB.getString(key);
158 } 147 }
159 148
160 /** 149 /**
161 * get and format message string from resource 150 * get and format message string from resource
162 * 151 *
163 * @param key selects message from resource 152 * @param key selects message from resource
164 */ 153 * @param args arguments for the message
165 String getText(String key) { 154 */
166 return getText(key, (String)null); 155 String getText(String key, Object... args) {
167 } 156 return messages.getLocalizedString(locale, key, args);
168
169 /**
170 * get and format message string from resource
171 *
172 * @param key selects message from resource
173 * @param a1 first argument
174 */
175 String getText(String key, String a1) {
176 return getText(key, a1, null);
177 }
178
179 /**
180 * get and format message string from resource
181 *
182 * @param key selects message from resource
183 * @param a1 first argument
184 * @param a2 second argument
185 */
186 String getText(String key, String a1, String a2) {
187 return getText(key, a1, a2, null);
188 }
189
190 /**
191 * get and format message string from resource
192 *
193 * @param key selects message from resource
194 * @param a1 first argument
195 * @param a2 second argument
196 * @param a3 third argument
197 */
198 String getText(String key, String a1, String a2, String a3) {
199 return getText(key, a1, a2, a3, null);
200 }
201
202 /**
203 * get and format message string from resource
204 *
205 * @param key selects message from resource
206 * @param a1 first argument
207 * @param a2 second argument
208 * @param a3 third argument
209 * @param a4 fourth argument
210 */
211 String getText(String key, String a1, String a2, String a3,
212 String a4) {
213 try {
214 String message = getString(key);
215 String[] args = new String[4];
216 args[0] = a1;
217 args[1] = a2;
218 args[2] = a3;
219 args[3] = a4;
220 return MessageFormat.format(message, (Object[])args);
221 } catch (MissingResourceException e) {
222 return "********** Resource for javadoc is broken. There is no " +
223 key + " key in resource.";
224 }
225 } 157 }
226 158
227 /** 159 /**
228 * Print error message, increment error count. 160 * Print error message, increment error count.
229 * Part of DocErrorReporter. 161 * Part of DocErrorReporter.
305 /** 237 /**
306 * Print error message, increment error count. 238 * Print error message, increment error count.
307 * 239 *
308 * @param key selects message from resource 240 * @param key selects message from resource
309 */ 241 */
310 public void error(SourcePosition pos, String key) { 242 public void error(SourcePosition pos, String key, Object... args) {
311 printError(pos, getText(key)); 243 printError(pos, getText(key, args));
312 }
313
314 /**
315 * Print error message, increment error count.
316 *
317 * @param key selects message from resource
318 * @param a1 first argument
319 */
320 public void error(SourcePosition pos, String key, String a1) {
321 printError(pos, getText(key, a1));
322 }
323
324 /**
325 * Print error message, increment error count.
326 *
327 * @param key selects message from resource
328 * @param a1 first argument
329 * @param a2 second argument
330 */
331 public void error(SourcePosition pos, String key, String a1, String a2) {
332 printError(pos, getText(key, a1, a2));
333 }
334
335 /**
336 * Print error message, increment error count.
337 *
338 * @param key selects message from resource
339 * @param a1 first argument
340 * @param a2 second argument
341 * @param a3 third argument
342 */
343 public void error(SourcePosition pos, String key, String a1, String a2, String a3) {
344 printError(pos, getText(key, a1, a2, a3));
345 } 244 }
346 245
347 /** 246 /**
348 * Print warning message, increment warning count. 247 * Print warning message, increment warning count.
349 * 248 *
350 * @param key selects message from resource 249 * @param key selects message from resource
351 */ 250 */
352 public void warning(SourcePosition pos, String key) { 251 public void warning(SourcePosition pos, String key, Object... args) {
353 printWarning(pos, getText(key)); 252 printWarning(pos, getText(key, args));
354 }
355
356 /**
357 * Print warning message, increment warning count.
358 *
359 * @param key selects message from resource
360 * @param a1 first argument
361 */
362 public void warning(SourcePosition pos, String key, String a1) {
363 printWarning(pos, getText(key, a1));
364 }
365
366 /**
367 * Print warning message, increment warning count.
368 *
369 * @param key selects message from resource
370 * @param a1 first argument
371 * @param a2 second argument
372 */
373 public void warning(SourcePosition pos, String key, String a1, String a2) {
374 printWarning(pos, getText(key, a1, a2));
375 }
376
377 /**
378 * Print warning message, increment warning count.
379 *
380 * @param key selects message from resource
381 * @param a1 first argument
382 * @param a2 second argument
383 * @param a3 third argument
384 */
385 public void warning(SourcePosition pos, String key, String a1, String a2, String a3) {
386 printWarning(pos, getText(key, a1, a2, a3));
387 }
388
389 /**
390 * Print warning message, increment warning count.
391 *
392 * @param key selects message from resource
393 * @param a1 first argument
394 * @param a2 second argument
395 * @param a3 third argument
396 */
397 public void warning(SourcePosition pos, String key, String a1, String a2, String a3,
398 String a4) {
399 printWarning(pos, getText(key, a1, a2, a3, a4));
400 } 253 }
401 254
402 /** 255 /**
403 * Print a message. 256 * Print a message.
404 * 257 *
405 * @param key selects message from resource 258 * @param key selects message from resource
406 */ 259 */
407 public void notice(String key) { 260 public void notice(String key, Object... args) {
408 printNotice(getText(key)); 261 printNotice(getText(key, args));
409 }
410
411 /**
412 * Print a message.
413 *
414 * @param key selects message from resource
415 * @param a1 first argument
416 */
417 public void notice(String key, String a1) {
418 printNotice(getText(key, a1));
419 }
420
421 /**
422 * Print a message.
423 *
424 * @param key selects message from resource
425 * @param a1 first argument
426 * @param a2 second argument
427 */
428 public void notice(String key, String a1, String a2) {
429 printNotice(getText(key, a1, a2));
430 }
431
432 /**
433 * Print a message.
434 *
435 * @param key selects message from resource
436 * @param a1 first argument
437 * @param a2 second argument
438 * @param a3 third argument
439 */
440 public void notice(String key, String a1, String a2, String a3) {
441 printNotice(getText(key, a1, a2, a3));
442 } 262 }
443 263
444 /** 264 /**
445 * Return total number of errors, including those recorded 265 * Return total number of errors, including those recorded
446 * in the compilation log. 266 * in the compilation log.
473 * TODO: This method does not really belong here. 293 * TODO: This method does not really belong here.
474 */ 294 */
475 public void exit() { 295 public void exit() {
476 throw new ExitJavadoc(); 296 throw new ExitJavadoc();
477 } 297 }
478
479 } 298 }

mercurial