1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 /*
19 * THE FILE HAS BEEN AUTOGENERATED BY MSGTOOL TOOL.
20 * All changes made to this file manually will be overwritten
21 * if this tool runs again. Better make changes in the template file.
22 * - Shamelessly taken from Apache Harmony.
23 */
24
25 package org.apache.river.impl;
26
27
28 import java.security.AccessController;
29 import java.security.PrivilegedAction;
30 import java.util.Locale;
31 import java.util.MissingResourceException;
32 import java.util.ResourceBundle;
33
34
35
36 /**
37 * This class retrieves strings from a resource bundle and returns them,
38 * formatting them with MessageFormat when required.
39 * <p>
40 * It is used by the system classes to provide national language support, by
41 * looking up messages in the <code>
42 * org.apache.harmony.security.internal.nls.messages
43 * </code>
44 * resource bundle. Note that if this file is not available, or an invalid key
45 * is looked up, or resource bundle support is not available, the key itself
46 * will be returned as the associated message. This means that the <em>KEY</em>
47 * should a reasonable human-readable (english) string.
48 * @since 3.0.0
49 */
50 public class Messages {
51
52 // ResourceBundle holding the system messages.
53 static final private ResourceBundle bundle ;
54
55 static {
56 // Attempt to load the messages.
57 ResourceBundle rb = null;
58 try {
59 rb = setLocale(Locale.getDefault(),
60 "org.apache.river.impl.messages"); //$NON-NLS-1$
61 } catch (Throwable e) {
62 System.err.println(e);
63 }
64 bundle = rb;
65 }
66
67 /**
68 * Retrieves a message which has no arguments.
69 *
70 * @param msg
71 * String the key to look up.
72 * @return String the message for that key in the system message bundle.
73 */
74 static public String getString(String msg) {
75 if (bundle == null) {
76 return msg;
77 }
78 try {
79 return bundle.getString(msg);
80 } catch (MissingResourceException e) {
81 return "Missing message: " + msg; //$NON-NLS-1$
82 }
83 }
84
85 /**
86 * Retrieves a message which takes 1 argument.
87 *
88 * @param msg
89 * String the key to look up.
90 * @param arg
91 * Object the object to insert in the formatted output.
92 * @return String the message for that key in the system message bundle.
93 */
94 static public String getString(String msg, Object arg) {
95 return getString(msg, new Object[] { arg });
96 }
97
98 /**
99 * Retrieves a message which takes 1 integer argument.
100 *
101 * @param msg
102 * String the key to look up.
103 * @param arg
104 * int the integer to insert in the formatted output.
105 * @return String the message for that key in the system message bundle.
106 */
107 static public String getString(String msg, int arg) {
108 return getString(msg, new Object[] { Integer.toString(arg) });
109 }
110
111 /**
112 * Retrieves a message which takes 1 character argument.
113 *
114 * @param msg
115 * String the key to look up.
116 * @param arg
117 * char the character to insert in the formatted output.
118 * @return String the message for that key in the system message bundle.
119 */
120 static public String getString(String msg, char arg) {
121 return getString(msg, new Object[] { String.valueOf(arg) });
122 }
123
124 /**
125 * Retrieves a message which takes 2 arguments.
126 *
127 * @param msg
128 * String the key to look up.
129 * @param arg1
130 * Object an object to insert in the formatted output.
131 * @param arg2
132 * Object another object to insert in the formatted output.
133 * @return String the message for that key in the system message bundle.
134 */
135 static public String getString(String msg, Object arg1, Object arg2) {
136 return getString(msg, new Object[] { arg1, arg2 });
137 }
138
139 /**
140 * Retrieves a message which takes several arguments.
141 *
142 * @param msg
143 * String the key to look up.
144 * @param args
145 * Object[] the objects to insert in the formatted output.
146 * @return String the message for that key in the system message bundle.
147 */
148 static public String getString(String msg, Object[] args) {
149 String format = msg;
150
151 if (bundle != null) {
152 try {
153 format = bundle.getString(msg);
154 } catch (MissingResourceException e) {
155 System.err.println(e);
156 }
157 }
158
159 return format(format, args);
160 }
161
162 /**
163 * Generates a formatted text string given a source string containing
164 * "argument markers" of the form "{argNum}" where each argNum must be in
165 * the range 0..9. The result is generated by inserting the toString of each
166 * argument into the position indicated in the string.
167 * <p>
168 * To insert the "{" character into the output, use a single backslash
169 * character to escape it (i.e. "\{"). The "}" character does not need to be
170 * escaped.
171 *
172 * @param format
173 * String the format to use when printing.
174 * @param args
175 * Object[] the arguments to use.
176 * @return String the formatted message.
177 */
178 public static String format(String format, Object[] args) {
179 StringBuilder answer = new StringBuilder(format.length()
180 + (args.length * 20));
181 String[] argStrings = new String[args.length];
182 for (int i = 0; i < args.length; ++i) {
183 if (args[i] == null) {
184 argStrings[i] = "<null>";
185 } //$NON-NLS-1$
186 else {
187 argStrings[i] = args[i].toString();
188 }
189 }
190 int lastI = 0;
191 for (int i = format.indexOf('{', 0); i >= 0; i = format.indexOf('{',
192 lastI)) {
193 if (i != 0 && format.charAt(i - 1) == '\\') {
194 // It's escaped, just print and loop.
195 if (i != 1) {
196 answer.append(format.substring(lastI, i - 1));
197 }
198 answer.append('{');
199 lastI = i + 1;
200 } else {
201 // It's a format character.
202 if (i > format.length() - 3) {
203 // Bad format, just print and loop.
204 answer.append(format.substring(lastI, format.length()));
205 lastI = format.length();
206 } else {
207 int argnum = (byte) Character.digit(format.charAt(i + 1),
208 10);
209 if (argnum < 0 || format.charAt(i + 2) != '}') {
210 // Bad format, just print and loop.
211 answer.append(format.substring(lastI, i + 1));
212 lastI = i + 1;
213 } else {
214 // Got a good one!
215 answer.append(format.substring(lastI, i));
216 if (argnum >= argStrings.length) {
217 answer.append("<missing argument>");
218 } //$NON-NLS-1$
219 else {
220 answer.append(argStrings[argnum]);
221 }
222 lastI = i + 3;
223 }
224 }
225 }
226 }
227 if (lastI < format.length()) {
228 answer.append(format.substring(lastI, format.length()));
229 }
230 return answer.toString();
231 }
232
233 /**
234 * Changes the locale of the messages.
235 *
236 * @param locale
237 * Locale the locale to change to.
238 */
239 static public ResourceBundle setLocale(final Locale locale,
240 final String resource) {
241 try {
242 final ClassLoader loader = null;
243 return (ResourceBundle) AccessController
244 .doPrivileged(new PrivilegedAction<Object>() {
245 public Object run() {
246 return ResourceBundle.getBundle(resource, locale,
247 loader != null ? loader : ClassLoader.getSystemClassLoader());
248 }
249 });
250 } catch (MissingResourceException e) {
251 System.err.println(e);
252 }
253 return null;
254 }
255
256
257 }