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 /**
034 * Retrieve and format messages stored in a resource.
035 *
036 * This code is not part of an API.
037 * It is implementation that is subject to change.
038 * Do not use it as an API
039 *
040 * @since 1.2
041 * @author Atul M Dambalkar
042 * @author Robert Field
043 */
044 public class MessageRetriever {
045 /**
046 * The global configuration information for this run.
047 */
048 private final Configuration configuration;
049
050 /**
051 * The location from which to lazily fetch the resource..
052 */
053 private final String resourcelocation;
054
055 /**
056 * The lazily fetched resource..
057 */
058 private ResourceBundle messageRB;
059
060 /**
061 * Initilize the ResourceBundle with the given resource.
062 *
063 * @param rb the esource bundle to read.
064 */
065 public MessageRetriever(ResourceBundle rb) {
066 this.configuration = null;
067 this.messageRB = rb;
068 this.resourcelocation = null;
069 }
070
071 /**
072 * Initilize the ResourceBundle with the given resource.
073 *
074 * @param configuration the configuration
075 * @param resourcelocation Resource.
076 */
077 public MessageRetriever(Configuration configuration,
078 String resourcelocation) {
079 this.configuration = configuration;
080 this.resourcelocation = resourcelocation;
081 }
082
083 /**
084 * Get and format message string from resource
085 *
086 * @param key selects message from resource
087 * @param args arguments to be replaced in the message.
088 * @throws MissingResourceException when the key does not
089 * exist in the properties file.
090 */
091 public String getText(String key, Object... args) throws MissingResourceException {
092 if (messageRB == null) {
093 try {
094 messageRB = ResourceBundle.getBundle(resourcelocation);
095 } catch (MissingResourceException e) {
096 throw new Error("Fatal: Resource (" + resourcelocation +
097 ") for javadoc doclets is missing.");
098 }
099 }
100 String message = messageRB.getString(key);
101 return MessageFormat.format(message, args);
102 }
103
104 /**
105 * Print error message, increment error count.
106 *
107 * @param pos the position of the source
108 * @param msg message to print
109 */
110 private void printError(SourcePosition pos, String msg) {
111 configuration.root.printError(pos, msg);
112 }
113
114 /**
115 * Print error message, increment error count.
116 *
117 * @param msg message to print
118 */
119 private void printError(String msg) {
120 configuration.root.printError(msg);
121 }
122
123 /**
124 * Print warning message, increment warning count.
125 *
126 * @param pos the position of the source
127 * @param msg message to print
128 */
129 private void printWarning(SourcePosition pos, String msg) {
130 configuration.root.printWarning(pos, msg);
131 }
132
133 /**
134 * Print warning message, increment warning count.
135 *
136 * @param msg message to print
137 */
138 private void printWarning(String msg) {
139 configuration.root.printWarning(msg);
140 }
141
142 /**
143 * Print a message.
144 *
145 * @param pos the position of the source
146 * @param msg message to print
147 */
148 private void printNotice(SourcePosition pos, String msg) {
149 configuration.root.printNotice(pos, msg);
150 }
151
152 /**
153 * Print a message.
154 *
155 * @param msg message to print
156 */
157 private void printNotice(String msg) {
158 configuration.root.printNotice(msg);
159 }
160
161 /**
162 * Print error message, increment error count.
163 *
164 * @param pos the position of the source
165 * @param key selects message from resource
166 * @param args arguments to be replaced in the message.
167 */
168 public void error(SourcePosition pos, String key, Object... args) {
169 printError(pos, getText(key, args));
170 }
171
172 /**
173 * Print error message, increment error count.
174 *
175 * @param key selects message from resource
176 * @param args arguments to be replaced in the message.
177 */
178 public void error(String key, Object... args) {
179 printError(getText(key, args));
180 }
181
182 /**
183 * Print warning message, increment warning count.
184 *
185 * @param pos the position of the source
186 * @param key selects message from resource
187 * @param args arguments to be replaced in the message.
188 */
189 public void warning(SourcePosition pos, String key, Object... args) {
190 printWarning(pos, getText(key, args));
191 }
192
193 /**
194 * Print warning message, increment warning count.
195 *
196 * @param key selects message from resource
197 * @param args arguments to be replaced in the message.
198 */
199 public void warning(String key, Object... args) {
200 printWarning(getText(key, args));
201 }
202
203 /**
204 * Print a message.
205 *
206 * @param pos the position of the source
207 * @param key selects message from resource
208 * @param args arguments to be replaced in the message.
209 */
210 public void notice(SourcePosition pos, String key, Object... args) {
211 printNotice(pos, getText(key, args));
212 }
213
214 /**
215 * Print a message.
216 *
217 * @param key selects message from resource
218 * @param args arguments to be replaced in the message.
219 */
220 public void notice(String key, Object... args) {
221 printNotice(getText(key, args));
222 }
223 }