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