001: /*
002: * Copyright 1998-2006 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: package com.sun.tools.doclets.internal.toolkit.util;
026:
027: import com.sun.javadoc.*;
028: import com.sun.tools.doclets.internal.toolkit.Configuration;
029: import java.util.*;
030: import java.text.MessageFormat;
031:
032: /**
033: * Retrieve and format messages stored in a resource.
034: *
035: * This code is not part of an API.
036: * It is implementation that is subject to change.
037: * Do not use it as an API
038: *
039: * @since 1.2
040: * @author Atul M Dambalkar
041: * @author Robert Field
042: */
043: public class MessageRetriever {
044: /**
045: * The global configuration information for this run.
046: */
047: private final Configuration configuration;
048:
049: /**
050: * The location from which to lazily fetch the resource..
051: */
052: private final String resourcelocation;
053:
054: /**
055: * The lazily fetched resource..
056: */
057: private ResourceBundle messageRB;
058:
059: /**
060: * Initilize the ResourceBundle with the given resource.
061: *
062: * @param rb the esource bundle to read.
063: */
064: public MessageRetriever(ResourceBundle rb) {
065: this .configuration = null;
066: this .messageRB = rb;
067: this .resourcelocation = null;
068: }
069:
070: /**
071: * Initilize the ResourceBundle with the given resource.
072: *
073: * @param configuration the configuration
074: * @param resourcelocation Resource.
075: */
076: public MessageRetriever(Configuration configuration,
077: String resourcelocation) {
078: this .configuration = configuration;
079: this .resourcelocation = resourcelocation;
080: }
081:
082: /**
083: * get and format message string from resource
084: *
085: * @param key selects message from resource
086: */
087: public String getText(String key) {
088: return getText(key, (String) null);
089: }
090:
091: /**
092: * Get and format message string from resource
093: *
094: * @param key selects message from resource
095: * @param a1 Argument, to be repalced in the message.
096: */
097: public String getText(String key, String a1) {
098: return getText(key, a1, null);
099: }
100:
101: /**
102: * Get and format message string from resource
103: *
104: * @param key selects message from resource
105: * @param a1 first argument to be replaced in the message.
106: * @param a2 second argument to be replaced in the message.
107: */
108: public String getText(String key, String a1, String a2) {
109: return getText(key, a1, a2, null);
110: }
111:
112: /**
113: * Get and format message string from resource
114: *
115: * @param key selects message from resource
116: * @param a1 first argument to be replaced in the message.
117: * @param a2 second argument to be replaced in the message.
118: * @param a3 third argument to be replaced in the message.
119: * @throws MissingResourceException when the key does not
120: * exist in the properties file.
121: */
122: public String getText(String key, String a1, String a2, String a3)
123: throws MissingResourceException {
124: if (messageRB == null) {
125: try {
126: messageRB = ResourceBundle.getBundle(resourcelocation);
127: } catch (MissingResourceException e) {
128: throw new Error("Fatal: Resource (" + resourcelocation
129: + ") for javadoc doclets is missing.");
130: }
131: }
132: String message = messageRB.getString(key);
133: return MessageFormat.format(message, a1, a2, a3);
134: }
135:
136: /**
137: * Print error message, increment error count.
138: *
139: * @param pos the position of the source
140: * @param msg message to print
141: */
142: private void printError(SourcePosition pos, String msg) {
143: configuration.root.printError(pos, msg);
144: }
145:
146: /**
147: * Print error message, increment error count.
148: *
149: * @param msg message to print
150: */
151: private void printError(String msg) {
152: configuration.root.printError(msg);
153: }
154:
155: /**
156: * Print warning message, increment warning count.
157: *
158: * @param pos the position of the source
159: * @param msg message to print
160: */
161: private void printWarning(SourcePosition pos, String msg) {
162: configuration.root.printWarning(pos, msg);
163: }
164:
165: /**
166: * Print warning message, increment warning count.
167: *
168: * @param msg message to print
169: */
170: private void printWarning(String msg) {
171: configuration.root.printWarning(msg);
172: }
173:
174: /**
175: * Print a message.
176: *
177: * @param pos the position of the source
178: * @param msg message to print
179: */
180: private void printNotice(SourcePosition pos, String msg) {
181: configuration.root.printNotice(pos, msg);
182: }
183:
184: /**
185: * Print a message.
186: *
187: * @param msg message to print
188: */
189: private void printNotice(String msg) {
190: configuration.root.printNotice(msg);
191: }
192:
193: /**
194: * Print error message, increment error count.
195: *
196: * @param pos the position of the source
197: * @param key selects message from resource
198: */
199: public void error(SourcePosition pos, String key) {
200: printError(pos, getText(key));
201: }
202:
203: /**
204: * Print error message, increment error count.
205: *
206: * @param key selects message from resource
207: */
208: public void error(String key) {
209: printError(getText(key));
210: }
211:
212: /**
213: * Print error message, increment error count.
214: *
215: * @param pos the position of the source
216: * @param key selects message from resource
217: * @param a1 first argument to be replaced in the message.
218: */
219: public void error(SourcePosition pos, String key, String a1) {
220: printError(pos, getText(key, a1));
221: }
222:
223: /**
224: * Print error message, increment error count.
225: *
226: * @param key selects message from resource
227: * @param a1 first argument to be replaced in the message.
228: */
229: public void error(String key, String a1) {
230: printError(getText(key, a1));
231: }
232:
233: /**
234: * Print error message, increment error count.
235: *
236: * @param pos the position of the source
237: * @param key selects message from resource
238: * @param a1 first argument to be replaced in the message.
239: * @param a2 second argument to be replaced in the message.
240: */
241: public void error(SourcePosition pos, String key, String a1,
242: String a2) {
243: printError(pos, getText(key, a1, a2));
244: }
245:
246: /**
247: * Print error message, increment error count.
248: *
249: * @param key selects message from resource
250: * @param a1 first argument to be replaced in the message.
251: * @param a2 second argument to be replaced in the message.
252: */
253: public void error(String key, String a1, String a2) {
254: printError(getText(key, a1, a2));
255: }
256:
257: /**
258: * Print error message, increment error count.
259: *
260: * @param pos the position of the source
261: * @param key selects message from resource
262: * @param a1 first argument to be replaced in the message.
263: * @param a2 second argument to be replaced in the message.
264: * @param a3 third argument to be replaced in the message.
265: */
266: public void error(SourcePosition pos, String key, String a1,
267: String a2, String a3) {
268: printError(pos, getText(key, a1, a2, a3));
269: }
270:
271: /**
272: * Print error message, increment error count.
273: *
274: * @param key selects message from resource
275: * @param a1 first argument to be replaced in the message.
276: * @param a2 second argument to be replaced in the message.
277: * @param a3 third argument to be replaced in the message.
278: */
279: public void error(String key, String a1, String a2, String a3) {
280: printError(getText(key, a1, a2, a3));
281: }
282:
283: /**
284: * Print warning message, increment warning count.
285: *
286: * @param pos the position of the source
287: * @param key selects message from resource
288: */
289: public void warning(SourcePosition pos, String key) {
290: printWarning(pos, getText(key));
291: }
292:
293: /**
294: * Print warning message, increment warning count.
295: *
296: * @param key selects message from resource
297: */
298: public void warning(String key) {
299: printWarning(getText(key));
300: }
301:
302: /**
303: * Print warning message, increment warning count.
304: *
305: * @param pos the position of the source
306: * @param key selects message from resource
307: * @param a1 first argument to be replaced in the message.
308: */
309: public void warning(SourcePosition pos, String key, String a1) {
310: printWarning(pos, getText(key, a1));
311: }
312:
313: /**
314: * Print warning message, increment warning count.
315: *
316: * @param key selects message from resource
317: * @param a1 first argument to be replaced in the message.
318: */
319: public void warning(String key, String a1) {
320: printWarning(getText(key, a1));
321: }
322:
323: /**
324: * Print warning message, increment warning count.
325: *
326: * @param pos the position of the source
327: * @param key selects message from resource
328: * @param a1 first argument to be replaced in the message.
329: * @param a2 second argument to be replaced in the message.
330: */
331: public void warning(SourcePosition pos, String key, String a1,
332: String a2) {
333: printWarning(pos, getText(key, a1, a2));
334: }
335:
336: /**
337: * Print warning message, increment warning count.
338: *
339: * @param key selects message from resource
340: * @param a1 first argument to be replaced in the message.
341: * @param a2 second argument to be replaced in the message.
342: */
343: public void warning(String key, String a1, String a2) {
344: printWarning(getText(key, a1, a2));
345: }
346:
347: /**
348: * Print warning message, increment warning count.
349: *
350: * @param pos the position of the source
351: * @param key selects message from resource
352: * @param a1 first argument to be replaced in the message.
353: * @param a2 second argument to be replaced in the message.
354: * @param a3 third argument to be replaced in the message.
355: */
356: public void warning(SourcePosition pos, String key, String a1,
357: String a2, String a3) {
358: printWarning(pos, getText(key, a1, a2, a3));
359: }
360:
361: /**
362: * Print warning message, increment warning count.
363: *
364: * @param key selects message from resource
365: * @param a1 first argument to be replaced in the message.
366: * @param a2 second argument to be replaced in the message.
367: * @param a3 third argument to be replaced in the message.
368: */
369: public void warning(String key, String a1, String a2, String a3) {
370: printWarning(getText(key, a1, a2, a3));
371: }
372:
373: /**
374: * Print a message.
375: *
376: * @param pos the position of the source
377: * @param key selects message from resource
378: */
379: public void notice(SourcePosition pos, String key) {
380: printNotice(pos, getText(key));
381: }
382:
383: /**
384: * Print a message.
385: *
386: * @param key selects message from resource
387: */
388: public void notice(String key) {
389: printNotice(getText(key));
390: }
391:
392: /**
393: * Print a message.
394: * @param pos the position of the source
395: * @param key selects message from resource
396: * @param a1 first argument to be replaced in the message.
397: */
398: public void notice(SourcePosition pos, String key, String a1) {
399: printNotice(pos, getText(key, a1));
400: }
401:
402: /**
403: * Print a message.
404: *
405: * @param key selects message from resource
406: * @param a1 first argument to be replaced in the message.
407: */
408: public void notice(String key, String a1) {
409: printNotice(getText(key, a1));
410: }
411:
412: /**
413: * Print a message.
414: *
415: * @param pos the position of the source
416: * @param key selects message from resource
417: * @param a1 first argument to be replaced in the message.
418: * @param a2 second argument to be replaced in the message.
419: */
420: public void notice(SourcePosition pos, String key, String a1,
421: String a2) {
422: printNotice(pos, getText(key, a1, a2));
423: }
424:
425: /**
426: * Print a message.
427: *
428: * @param key selects message from resource
429: * @param a1 first argument to be replaced in the message.
430: * @param a2 second argument to be replaced in the message.
431: */
432: public void notice(String key, String a1, String a2) {
433: printNotice(getText(key, a1, a2));
434: }
435:
436: /**
437: * Print a message.
438: *
439: * @param pos the position of the source
440: * @param key selects message from resource
441: * @param a1 first argument to be replaced in the message.
442: * @param a2 second argument to be replaced in the message.
443: * @param a3 third argument to be replaced in the message.
444: */
445: public void notice(SourcePosition pos, String key, String a1,
446: String a2, String a3) {
447: printNotice(pos, getText(key, a1, a2, a3));
448: }
449:
450: /**
451: * Print a message.
452: *
453: * @param key selects message from resource
454: * @param a1 first argument to be replaced in the message.
455: * @param a2 second argument to be replaced in the message.
456: * @param a3 third argument to be replaced in the message.
457: */
458: public void notice(String key, String a1, String a2, String a3) {
459: printNotice(getText(key, a1, a2, a3));
460: }
461: }
|