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.sound.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.sound.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: }
064: try {
065: return bundle.getString(msg);
066: } catch (MissingResourceException e) {
067: return "Missing message: " + msg; //$NON-NLS-1$
068: }
069: }
070:
071: /**
072: * Retrieves a message which takes 1 argument.
073: *
074: * @param msg
075: * String the key to look up.
076: * @param arg
077: * Object the object to insert in the formatted output.
078: * @return String the message for that key in the system message bundle.
079: */
080: static public String getString(String msg, Object arg) {
081: return getString(msg, new Object[] { arg });
082: }
083:
084: /**
085: * Retrieves a message which takes 1 integer argument.
086: *
087: * @param msg
088: * String the key to look up.
089: * @param arg
090: * int the integer to insert in the formatted output.
091: * @return String the message for that key in the system message bundle.
092: */
093: static public String getString(String msg, int arg) {
094: return getString(msg, new Object[] { Integer.toString(arg) });
095: }
096:
097: /**
098: * Retrieves a message which takes 1 character argument.
099: *
100: * @param msg
101: * String the key to look up.
102: * @param arg
103: * char the character to insert in the formatted output.
104: * @return String the message for that key in the system message bundle.
105: */
106: static public String getString(String msg, char arg) {
107: return getString(msg, new Object[] { String.valueOf(arg) });
108: }
109:
110: /**
111: * Retrieves a message which takes 2 arguments.
112: *
113: * @param msg
114: * String the key to look up.
115: * @param arg1
116: * Object an object to insert in the formatted output.
117: * @param arg2
118: * Object another object to insert in the formatted output.
119: * @return String the message for that key in the system message bundle.
120: */
121: static public String getString(String msg, Object arg1, Object arg2) {
122: return getString(msg, new Object[] { arg1, arg2 });
123: }
124:
125: /**
126: * Retrieves a message which takes several arguments.
127: *
128: * @param msg
129: * String the key to look up.
130: * @param args
131: * Object[] the objects to insert in the formatted output.
132: * @return String the message for that key in the system message bundle.
133: */
134: static public String getString(String msg, Object[] args) {
135: String format = msg;
136:
137: if (bundle != null) {
138: try {
139: format = bundle.getString(msg);
140: } catch (MissingResourceException e) {
141: }
142: }
143:
144: return format(format, args);
145: }
146:
147: /**
148: * Generates a formatted text string given a source string containing
149: * "argument markers" of the form "{argNum}" where each argNum must be in
150: * the range 0..9. The result is generated by inserting the toString of each
151: * argument into the position indicated in the string.
152: * <p>
153: * To insert the "{" character into the output, use a single backslash
154: * character to escape it (i.e. "\{"). The "}" character does not need to be
155: * escaped.
156: *
157: * @param format
158: * String the format to use when printing.
159: * @param args
160: * Object[] the arguments to use.
161: * @return String the formatted message.
162: */
163: public static String format(String format, Object[] args) {
164: StringBuilder answer = new StringBuilder(format.length()
165: + (args.length * 20));
166: String[] argStrings = new String[args.length];
167: for (int i = 0; i < args.length; ++i) {
168: if (args[i] == null) {
169: argStrings[i] = "<null>"; //$NON-NLS-1$
170: } else {
171: argStrings[i] = args[i].toString();
172: }
173: }
174: int lastI = 0;
175: for (int i = format.indexOf('{', 0); i >= 0; i = format
176: .indexOf('{', lastI)) {
177: if (i != 0 && format.charAt(i - 1) == '\\') {
178: // It's escaped, just print and loop.
179: if (i != 1) {
180: answer.append(format.substring(lastI, i - 1));
181: }
182: answer.append('{');
183: lastI = i + 1;
184: } else {
185: // It's a format character.
186: if (i > format.length() - 3) {
187: // Bad format, just print and loop.
188: answer.append(format.substring(lastI, format
189: .length()));
190: lastI = format.length();
191: } else {
192: int argnum = (byte) Character.digit(format
193: .charAt(i + 1), 10);
194: if (argnum < 0 || format.charAt(i + 2) != '}') {
195: // Bad format, just print and loop.
196: answer.append(format.substring(lastI, i + 1));
197: lastI = i + 1;
198: } else {
199: // Got a good one!
200: answer.append(format.substring(lastI, i));
201: if (argnum >= argStrings.length) {
202: answer.append("<missing argument>"); //$NON-NLS-1$
203: } else {
204: answer.append(argStrings[argnum]);
205: }
206: lastI = i + 3;
207: }
208: }
209: }
210: }
211: if (lastI < format.length()) {
212: answer.append(format.substring(lastI, format.length()));
213: }
214: return answer.toString();
215: }
216:
217: /**
218: * Changes the locale of the messages.
219: *
220: * @param locale
221: * Locale the locale to change to.
222: */
223: static public ResourceBundle setLocale(final Locale locale,
224: final String resource) {
225: try {
226: final ClassLoader loader = VM.bootCallerClassLoader();
227: return (ResourceBundle) AccessController
228: .doPrivileged(new PrivilegedAction<Object>() {
229: public Object run() {
230: return ResourceBundle
231: .getBundle(
232: resource,
233: locale,
234: loader != null ? loader
235: : ClassLoader
236: .getSystemClassLoader());
237: }
238: });
239: } catch (MissingResourceException e) {
240: }
241: return null;
242: }
243:
244: static {
245: // Attempt to load the messages.
246: try {
247: bundle = setLocale(Locale.getDefault(),
248: "org.apache.harmony.sound.internal.nls.messages"); //$NON-NLS-1$
249: } catch (Throwable e) {
250: e.printStackTrace();
251: }
252: }
253: }
|