author | jjg |
Wed, 10 Oct 2012 16:48:21 -0700 | |
changeset 14260 | 727a84636f12 |
parent 14258 | 8d2148961366 |
child 15354 | 52a04c670c05 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
14258 | 2 |
* Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 24 |
*/ |
25 |
package com.sun.tools.doclets.internal.toolkit.util; |
|
26 |
||
14258 | 27 |
import java.text.MessageFormat; |
28 |
import java.util.*; |
|
29 |
||
10 | 30 |
import com.sun.javadoc.*; |
31 |
import com.sun.tools.doclets.internal.toolkit.Configuration; |
|
32 |
||
33 |
||
34 |
/** |
|
35 |
* Retrieve and format messages stored in a resource. |
|
36 |
* |
|
14260
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
37 |
* <p><b>This is NOT part of any supported API. |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
38 |
* If you write code that depends on this, you do so at your own risk. |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
39 |
* This code and its internal interfaces are subject to change or |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
40 |
* deletion without notice.</b> |
10 | 41 |
* |
42 |
* @since 1.2 |
|
43 |
* @author Atul M Dambalkar |
|
44 |
* @author Robert Field |
|
45 |
*/ |
|
46 |
public class MessageRetriever { |
|
47 |
/** |
|
48 |
* The global configuration information for this run. |
|
49 |
*/ |
|
50 |
private final Configuration configuration; |
|
51 |
||
52 |
/** |
|
53 |
* The location from which to lazily fetch the resource.. |
|
54 |
*/ |
|
55 |
private final String resourcelocation; |
|
56 |
||
57 |
/** |
|
58 |
* The lazily fetched resource.. |
|
59 |
*/ |
|
60 |
private ResourceBundle messageRB; |
|
61 |
||
62 |
/** |
|
63 |
* Initilize the ResourceBundle with the given resource. |
|
64 |
* |
|
65 |
* @param rb the esource bundle to read. |
|
66 |
*/ |
|
67 |
public MessageRetriever(ResourceBundle rb) { |
|
68 |
this.configuration = null; |
|
69 |
this.messageRB = rb; |
|
70 |
this.resourcelocation = null; |
|
71 |
} |
|
72 |
||
73 |
/** |
|
74 |
* Initilize the ResourceBundle with the given resource. |
|
75 |
* |
|
76 |
* @param configuration the configuration |
|
77 |
* @param resourcelocation Resource. |
|
78 |
*/ |
|
79 |
public MessageRetriever(Configuration configuration, |
|
80 |
String resourcelocation) { |
|
81 |
this.configuration = configuration; |
|
82 |
this.resourcelocation = resourcelocation; |
|
83 |
} |
|
84 |
||
85 |
/** |
|
86 |
* Get and format message string from resource |
|
87 |
* |
|
88 |
* @param key selects message from resource |
|
1864
c17be9084212
6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents:
10
diff
changeset
|
89 |
* @param args arguments to be replaced in the message. |
10 | 90 |
* @throws MissingResourceException when the key does not |
91 |
* exist in the properties file. |
|
92 |
*/ |
|
1864
c17be9084212
6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents:
10
diff
changeset
|
93 |
public String getText(String key, Object... args) throws MissingResourceException { |
10 | 94 |
if (messageRB == null) { |
95 |
try { |
|
96 |
messageRB = ResourceBundle.getBundle(resourcelocation); |
|
97 |
} catch (MissingResourceException e) { |
|
98 |
throw new Error("Fatal: Resource (" + resourcelocation + |
|
99 |
") for javadoc doclets is missing."); |
|
100 |
} |
|
101 |
} |
|
102 |
String message = messageRB.getString(key); |
|
1864
c17be9084212
6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents:
10
diff
changeset
|
103 |
return MessageFormat.format(message, args); |
10 | 104 |
} |
105 |
||
106 |
/** |
|
107 |
* Print error message, increment error count. |
|
108 |
* |
|
109 |
* @param pos the position of the source |
|
110 |
* @param msg message to print |
|
111 |
*/ |
|
112 |
private void printError(SourcePosition pos, String msg) { |
|
113 |
configuration.root.printError(pos, msg); |
|
114 |
} |
|
115 |
||
116 |
/** |
|
117 |
* Print error message, increment error count. |
|
118 |
* |
|
119 |
* @param msg message to print |
|
120 |
*/ |
|
121 |
private void printError(String msg) { |
|
122 |
configuration.root.printError(msg); |
|
123 |
} |
|
124 |
||
125 |
/** |
|
126 |
* Print warning message, increment warning count. |
|
127 |
* |
|
128 |
* @param pos the position of the source |
|
129 |
* @param msg message to print |
|
130 |
*/ |
|
131 |
private void printWarning(SourcePosition pos, String msg) { |
|
132 |
configuration.root.printWarning(pos, msg); |
|
133 |
} |
|
134 |
||
135 |
/** |
|
136 |
* Print warning message, increment warning count. |
|
137 |
* |
|
138 |
* @param msg message to print |
|
139 |
*/ |
|
140 |
private void printWarning(String msg) { |
|
141 |
configuration.root.printWarning(msg); |
|
142 |
} |
|
143 |
||
144 |
/** |
|
145 |
* Print a message. |
|
146 |
* |
|
147 |
* @param pos the position of the source |
|
148 |
* @param msg message to print |
|
149 |
*/ |
|
150 |
private void printNotice(SourcePosition pos, String msg) { |
|
151 |
configuration.root.printNotice(pos, msg); |
|
152 |
} |
|
153 |
||
154 |
/** |
|
155 |
* Print a message. |
|
156 |
* |
|
157 |
* @param msg message to print |
|
158 |
*/ |
|
159 |
private void printNotice(String msg) { |
|
160 |
configuration.root.printNotice(msg); |
|
161 |
} |
|
162 |
||
163 |
/** |
|
164 |
* Print error message, increment error count. |
|
165 |
* |
|
166 |
* @param pos the position of the source |
|
167 |
* @param key selects message from resource |
|
1864
c17be9084212
6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents:
10
diff
changeset
|
168 |
* @param args arguments to be replaced in the message. |
10 | 169 |
*/ |
1864
c17be9084212
6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents:
10
diff
changeset
|
170 |
public void error(SourcePosition pos, String key, Object... args) { |
c17be9084212
6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents:
10
diff
changeset
|
171 |
printError(pos, getText(key, args)); |
10 | 172 |
} |
173 |
||
174 |
/** |
|
175 |
* Print error message, increment error count. |
|
176 |
* |
|
177 |
* @param key selects message from resource |
|
1864
c17be9084212
6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents:
10
diff
changeset
|
178 |
* @param args arguments to be replaced in the message. |
10 | 179 |
*/ |
1864
c17be9084212
6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents:
10
diff
changeset
|
180 |
public void error(String key, Object... args) { |
c17be9084212
6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents:
10
diff
changeset
|
181 |
printError(getText(key, args)); |
10 | 182 |
} |
183 |
||
184 |
/** |
|
185 |
* Print warning message, increment warning count. |
|
186 |
* |
|
187 |
* @param pos the position of the source |
|
188 |
* @param key selects message from resource |
|
1864
c17be9084212
6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents:
10
diff
changeset
|
189 |
* @param args arguments to be replaced in the message. |
10 | 190 |
*/ |
1864
c17be9084212
6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents:
10
diff
changeset
|
191 |
public void warning(SourcePosition pos, String key, Object... args) { |
c17be9084212
6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents:
10
diff
changeset
|
192 |
printWarning(pos, getText(key, args)); |
10 | 193 |
} |
194 |
||
195 |
/** |
|
196 |
* Print warning message, increment warning count. |
|
197 |
* |
|
198 |
* @param key selects message from resource |
|
1864
c17be9084212
6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents:
10
diff
changeset
|
199 |
* @param args arguments to be replaced in the message. |
10 | 200 |
*/ |
1864
c17be9084212
6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents:
10
diff
changeset
|
201 |
public void warning(String key, Object... args) { |
c17be9084212
6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents:
10
diff
changeset
|
202 |
printWarning(getText(key, args)); |
10 | 203 |
} |
204 |
||
205 |
/** |
|
206 |
* Print a message. |
|
207 |
* |
|
208 |
* @param pos the position of the source |
|
209 |
* @param key selects message from resource |
|
1864
c17be9084212
6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents:
10
diff
changeset
|
210 |
* @param args arguments to be replaced in the message. |
10 | 211 |
*/ |
1864
c17be9084212
6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents:
10
diff
changeset
|
212 |
public void notice(SourcePosition pos, String key, Object... args) { |
c17be9084212
6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents:
10
diff
changeset
|
213 |
printNotice(pos, getText(key, args)); |
10 | 214 |
} |
215 |
||
216 |
/** |
|
217 |
* Print a message. |
|
218 |
* |
|
219 |
* @param key selects message from resource |
|
1864
c17be9084212
6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents:
10
diff
changeset
|
220 |
* @param args arguments to be replaced in the message. |
10 | 221 |
*/ |
1864
c17be9084212
6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents:
10
diff
changeset
|
222 |
public void notice(String key, Object... args) { |
c17be9084212
6794520: MessageRetriever should be upgraded to use varargs Object...
jjg
parents:
10
diff
changeset
|
223 |
printNotice(getText(key, args)); |
10 | 224 |
} |
225 |
} |