001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: * contributor license agreements. See the NOTICE file distributed with
003: * this work for additional information regarding copyright ownership.
004: * The ASF licenses this file to You under the Apache License, Version 2.0
005: * (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: /*
018: * THE FILE HAS BEEN AUTOGENERATED BY MSGTOOL TOOL.
019: * All changes made to this file manually will be overwritten
020: * if this tool runs again. Better make changes in the template file.
021: */
022:
023: package org.apache.harmony.annotation.internal.nls;
024:
025: import java.security.AccessController;
026: import java.security.PrivilegedAction;
027: import java.util.Locale;
028: import java.util.MissingResourceException;
029: import java.util.ResourceBundle;
030:
031: import org.apache.harmony.kernel.vm.VM;
032:
033: /**
034: * This class retrieves strings from a resource bundle and returns them,
035: * formatting them with MessageFormat when required.
036: * <p>
037: * It is used by the system classes to provide national language support, by
038: * looking up messages in the <code>
039: * org.apache.harmony.annotation.internal.nls.messages
040: * </code>
041: * resource bundle. Note that if this file is not available, or an invalid key
042: * is looked up, or resource bundle support is not available, the key itself
043: * will be returned as the associated message. This means that the <em>KEY</em>
044: * should a reasonable human-readable (english) string.
045: *
046: */
047: public class Messages {
048:
049: // ResourceBundle holding the system messages.
050: static private ResourceBundle bundle = null;
051:
052: /**
053: * Retrieves a message which has no arguments.
054: *
055: * @param msg
056: * String the key to look up.
057: * @return String the message for that key in the system message bundle.
058: */
059: static public String getString(String msg) {
060: if (bundle == null)
061: return msg;
062: try {
063: return bundle.getString(msg);
064: } catch (MissingResourceException e) {
065: return "Missing message: " + msg; //$NON-NLS-1$
066: }
067: }
068:
069: /**
070: * Retrieves a message which takes 1 argument.
071: *
072: * @param msg
073: * String the key to look up.
074: * @param arg
075: * Object the object to insert in the formatted output.
076: * @return String the message for that key in the system message bundle.
077: */
078: static public String getString(String msg, Object arg) {
079: return getString(msg, new Object[] { arg });
080: }
081:
082: /**
083: * Retrieves a message which takes 1 integer argument.
084: *
085: * @param msg
086: * String the key to look up.
087: * @param arg
088: * int the integer to insert in the formatted output.
089: * @return String the message for that key in the system message bundle.
090: */
091: static public String getString(String msg, int arg) {
092: return getString(msg, new Object[] { Integer.toString(arg) });
093: }
094:
095: /**
096: * Retrieves a message which takes 1 character argument.
097: *
098: * @param msg
099: * String the key to look up.
100: * @param arg
101: * char the character to insert in the formatted output.
102: * @return String the message for that key in the system message bundle.
103: */
104: static public String getString(String msg, char arg) {
105: return getString(msg, new Object[] { String.valueOf(arg) });
106: }
107:
108: /**
109: * Retrieves a message which takes 2 arguments.
110: *
111: * @param msg
112: * String the key to look up.
113: * @param arg1
114: * Object an object to insert in the formatted output.
115: * @param arg2
116: * Object another object to insert in the formatted output.
117: * @return String the message for that key in the system message bundle.
118: */
119: static public String getString(String msg, Object arg1, Object arg2) {
120: return getString(msg, new Object[] { arg1, arg2 });
121: }
122:
123: /**
124: * Retrieves a message which takes several arguments.
125: *
126: * @param msg
127: * String the key to look up.
128: * @param args
129: * Object[] the objects to insert in the formatted output.
130: * @return String the message for that key in the system message bundle.
131: */
132: static public String getString(String msg, Object[] args) {
133: String format = msg;
134:
135: if (bundle != null) {
136: try {
137: format = bundle.getString(msg);
138: } catch (MissingResourceException e) {
139: }
140: }
141:
142: return format(format, args);
143: }
144:
145: /**
146: * Generates a formatted text string given a source string containing
147: * "argument markers" of the form "{argNum}" where each argNum must be in
148: * the range 0..9. The result is generated by inserting the toString of each
149: * argument into the position indicated in the string.
150: * <p>
151: * To insert the "{" character into the output, use a single backslash
152: * character to escape it (i.e. "\{"). The "}" character does not need to be
153: * escaped.
154: *
155: * @param format
156: * String the format to use when printing.
157: * @param args
158: * Object[] the arguments to use.
159: * @return String the formatted message.
160: */
161: public static String format(String format, Object[] args) {
162: StringBuilder answer = new StringBuilder(format.length()
163: + (args.length * 20));
164: String[] argStrings = new String[args.length];
165: for (int i = 0; i < args.length; ++i) {
166: if (args[i] == null)
167: argStrings[i] = "<null>"; //$NON-NLS-1$
168: else
169: argStrings[i] = args[i].toString();
170: }
171: int lastI = 0;
172: for (int i = format.indexOf('{', 0); i >= 0; i = format
173: .indexOf('{', lastI)) {
174: if (i != 0 && format.charAt(i - 1) == '\\') {
175: // It's escaped, just print and loop.
176: if (i != 1)
177: answer.append(format.substring(lastI, i - 1));
178: answer.append('{');
179: lastI = i + 1;
180: } else {
181: // It's a format character.
182: if (i > format.length() - 3) {
183: // Bad format, just print and loop.
184: answer.append(format.substring(lastI, format
185: .length()));
186: lastI = format.length();
187: } else {
188: int argnum = (byte) Character.digit(format
189: .charAt(i + 1), 10);
190: if (argnum < 0 || format.charAt(i + 2) != '}') {
191: // Bad format, just print and loop.
192: answer.append(format.substring(lastI, i + 1));
193: lastI = i + 1;
194: } else {
195: // Got a good one!
196: answer.append(format.substring(lastI, i));
197: if (argnum >= argStrings.length)
198: answer.append("<missing argument>"); //$NON-NLS-1$
199: else
200: answer.append(argStrings[argnum]);
201: lastI = i + 3;
202: }
203: }
204: }
205: }
206: if (lastI < format.length())
207: answer.append(format.substring(lastI, format.length()));
208: return answer.toString();
209: }
210:
211: /**
212: * Changes the locale of the messages.
213: *
214: * @param locale
215: * Locale the locale to change to.
216: */
217: static public ResourceBundle setLocale(final Locale locale,
218: final String resource) {
219: try {
220: final ClassLoader loader = VM.bootCallerClassLoader();
221: return (ResourceBundle) AccessController
222: .doPrivileged(new PrivilegedAction<Object>() {
223: public Object run() {
224: return ResourceBundle
225: .getBundle(
226: resource,
227: locale,
228: loader != null ? loader
229: : ClassLoader
230: .getSystemClassLoader());
231: }
232: });
233: } catch (MissingResourceException e) {
234: }
235: return null;
236: }
237:
238: static {
239: // Attempt to load the messages.
240: try {
241: bundle = setLocale(Locale.getDefault(),
242: "org.apache.harmony.annotation.internal.nls.messages"); //$NON-NLS-1$
243: } catch (Throwable e) {
244: e.printStackTrace();
245: }
246: }
247: }
|