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.archive.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.archive.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: // ignore
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: int lastI = 0;
174: for (int i = format.indexOf('{', 0); i >= 0; i = format
175: .indexOf('{', lastI)) {
176: if (i != 0 && format.charAt(i - 1) == '\\') {
177: // It's escaped, just print and loop.
178: if (i != 1)
179: answer.append(format.substring(lastI, i - 1));
180: answer.append('{');
181: lastI = i + 1;
182: } else {
183: // It's a format character.
184: if (i > format.length() - 3) {
185: // Bad format, just print and loop.
186: answer.append(format.substring(lastI, format
187: .length()));
188: lastI = format.length();
189: } else {
190: int argnum = (byte) Character.digit(format
191: .charAt(i + 1), 10);
192: if (argnum < 0 || format.charAt(i + 2) != '}') {
193: // Bad format, just print and loop.
194: answer.append(format.substring(lastI, i + 1));
195: lastI = i + 1;
196: } else {
197: // Got a good one!
198: answer.append(format.substring(lastI, i));
199: if (argnum >= argStrings.length)
200: answer.append("<missing argument>"); //$NON-NLS-1$
201: else
202: answer.append(argStrings[argnum]);
203: lastI = i + 3;
204: }
205: }
206: }
207: }
208: if (lastI < format.length())
209: answer.append(format.substring(lastI, format.length()));
210: return answer.toString();
211: }
212:
213: /**
214: * Changes the locale of the messages.
215: *
216: * @param locale
217: * Locale the locale to change to.
218: */
219: static public ResourceBundle setLocale(final Locale locale,
220: final String resource) {
221: try {
222: final ClassLoader loader = VM.bootCallerClassLoader();
223: return (ResourceBundle) AccessController
224: .doPrivileged(new PrivilegedAction<Object>() {
225: public Object run() {
226: return ResourceBundle
227: .getBundle(
228: resource,
229: locale,
230: loader != null ? loader
231: : ClassLoader
232: .getSystemClassLoader());
233: }
234: });
235: } catch (MissingResourceException e) {
236: // ignore
237: }
238: return null;
239: }
240:
241: static {
242: // Attempt to load the messages.
243: try {
244: bundle = setLocale(Locale.getDefault(),
245: "org.apache.harmony.archive.internal.nls.messages"); //$NON-NLS-1$
246: } catch (Throwable e) {
247: e.printStackTrace();
248: }
249: }
250: }
|