Tue, 09 Oct 2012 19:10:00 -0700
8000663: clean up langtools imports
Reviewed-by: darcy
1 /*
2 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package com.sun.tools.doclets.internal.toolkit.util;
27 import java.text.MessageFormat;
28 import java.util.*;
30 import com.sun.javadoc.*;
31 import com.sun.tools.doclets.internal.toolkit.Configuration;
34 /**
35 * Retrieve and format messages stored in a resource.
36 *
37 * This code is not part of an API.
38 * It is implementation that is subject to change.
39 * Do not use it as an API
40 *
41 * @since 1.2
42 * @author Atul M Dambalkar
43 * @author Robert Field
44 */
45 public class MessageRetriever {
46 /**
47 * The global configuration information for this run.
48 */
49 private final Configuration configuration;
51 /**
52 * The location from which to lazily fetch the resource..
53 */
54 private final String resourcelocation;
56 /**
57 * The lazily fetched resource..
58 */
59 private ResourceBundle messageRB;
61 /**
62 * Initilize the ResourceBundle with the given resource.
63 *
64 * @param rb the esource bundle to read.
65 */
66 public MessageRetriever(ResourceBundle rb) {
67 this.configuration = null;
68 this.messageRB = rb;
69 this.resourcelocation = null;
70 }
72 /**
73 * Initilize the ResourceBundle with the given resource.
74 *
75 * @param configuration the configuration
76 * @param resourcelocation Resource.
77 */
78 public MessageRetriever(Configuration configuration,
79 String resourcelocation) {
80 this.configuration = configuration;
81 this.resourcelocation = resourcelocation;
82 }
84 /**
85 * Get and format message string from resource
86 *
87 * @param key selects message from resource
88 * @param args arguments to be replaced in the message.
89 * @throws MissingResourceException when the key does not
90 * exist in the properties file.
91 */
92 public String getText(String key, Object... args) throws MissingResourceException {
93 if (messageRB == null) {
94 try {
95 messageRB = ResourceBundle.getBundle(resourcelocation);
96 } catch (MissingResourceException e) {
97 throw new Error("Fatal: Resource (" + resourcelocation +
98 ") for javadoc doclets is missing.");
99 }
100 }
101 String message = messageRB.getString(key);
102 return MessageFormat.format(message, args);
103 }
105 /**
106 * Print error message, increment error count.
107 *
108 * @param pos the position of the source
109 * @param msg message to print
110 */
111 private void printError(SourcePosition pos, String msg) {
112 configuration.root.printError(pos, msg);
113 }
115 /**
116 * Print error message, increment error count.
117 *
118 * @param msg message to print
119 */
120 private void printError(String msg) {
121 configuration.root.printError(msg);
122 }
124 /**
125 * Print warning message, increment warning count.
126 *
127 * @param pos the position of the source
128 * @param msg message to print
129 */
130 private void printWarning(SourcePosition pos, String msg) {
131 configuration.root.printWarning(pos, msg);
132 }
134 /**
135 * Print warning message, increment warning count.
136 *
137 * @param msg message to print
138 */
139 private void printWarning(String msg) {
140 configuration.root.printWarning(msg);
141 }
143 /**
144 * Print a message.
145 *
146 * @param pos the position of the source
147 * @param msg message to print
148 */
149 private void printNotice(SourcePosition pos, String msg) {
150 configuration.root.printNotice(pos, msg);
151 }
153 /**
154 * Print a message.
155 *
156 * @param msg message to print
157 */
158 private void printNotice(String msg) {
159 configuration.root.printNotice(msg);
160 }
162 /**
163 * Print error message, increment error count.
164 *
165 * @param pos the position of the source
166 * @param key selects message from resource
167 * @param args arguments to be replaced in the message.
168 */
169 public void error(SourcePosition pos, String key, Object... args) {
170 printError(pos, getText(key, args));
171 }
173 /**
174 * Print error message, increment error count.
175 *
176 * @param key selects message from resource
177 * @param args arguments to be replaced in the message.
178 */
179 public void error(String key, Object... args) {
180 printError(getText(key, args));
181 }
183 /**
184 * Print warning message, increment warning count.
185 *
186 * @param pos the position of the source
187 * @param key selects message from resource
188 * @param args arguments to be replaced in the message.
189 */
190 public void warning(SourcePosition pos, String key, Object... args) {
191 printWarning(pos, getText(key, args));
192 }
194 /**
195 * Print warning message, increment warning count.
196 *
197 * @param key selects message from resource
198 * @param args arguments to be replaced in the message.
199 */
200 public void warning(String key, Object... args) {
201 printWarning(getText(key, args));
202 }
204 /**
205 * Print a message.
206 *
207 * @param pos the position of the source
208 * @param key selects message from resource
209 * @param args arguments to be replaced in the message.
210 */
211 public void notice(SourcePosition pos, String key, Object... args) {
212 printNotice(pos, getText(key, args));
213 }
215 /**
216 * Print a message.
217 *
218 * @param key selects message from resource
219 * @param args arguments to be replaced in the message.
220 */
221 public void notice(String key, Object... args) {
222 printNotice(getText(key, args));
223 }
224 }