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: package org.apache.harmony.luni.util;
019:
020: import java.security.AccessController;
021: import java.security.PrivilegedAction;
022: import java.util.Locale;
023: import java.util.MissingResourceException;
024: import java.util.ResourceBundle;
025:
026: import org.apache.harmony.kernel.vm.VM;
027:
028: /**
029: * This class contains helper methods for loading resource bundles and
030: * formatting external message strings.
031: */
032:
033: public final class MsgHelp {
034:
035: /**
036: * Generates a formatted text string given a source string containing
037: * "argument markers" of the form "{argNum}" where each argNum must be in
038: * the range 0..9. The result is generated by inserting the toString of each
039: * argument into the position indicated in the string.
040: * <p>
041: * To insert the "{" character into the output, use a single backslash
042: * character to escape it (i.e. "\{"). The "}" character does not need to be
043: * escaped.
044: *
045: * @param format
046: * String the format to use when printing.
047: * @param args
048: * Object[] the arguments to use.
049: * @return String the formatted message.
050: */
051: public static String format(String format, Object[] args) {
052: StringBuilder answer = new StringBuilder(format.length()
053: + (args.length * 20));
054: String[] argStrings = new String[args.length];
055: for (int i = 0; i < args.length; ++i) {
056: if (args[i] == null)
057: argStrings[i] = "<null>";
058: else
059: argStrings[i] = args[i].toString();
060: }
061: int lastI = 0;
062: for (int i = format.indexOf('{', 0); i >= 0; i = format
063: .indexOf('{', lastI)) {
064: if (i != 0 && format.charAt(i - 1) == '\\') {
065: // It's escaped, just print and loop.
066: if (i != 1)
067: answer.append(format.substring(lastI, i - 1));
068: answer.append('{');
069: lastI = i + 1;
070: } else {
071: // It's a format character.
072: if (i > format.length() - 3) {
073: // Bad format, just print and loop.
074: answer.append(format.substring(lastI, format
075: .length()));
076: lastI = format.length();
077: } else {
078: int argnum = (byte) Character.digit(format
079: .charAt(i + 1), 10);
080: if (argnum < 0 || format.charAt(i + 2) != '}') {
081: // Bad format, just print and loop.
082: answer.append(format.substring(lastI, i + 1));
083: lastI = i + 1;
084: } else {
085: // Got a good one!
086: answer.append(format.substring(lastI, i));
087: if (argnum >= argStrings.length)
088: answer.append("<missing argument>");
089: else
090: answer.append(argStrings[argnum]);
091: lastI = i + 3;
092: }
093: }
094: }
095: }
096: if (lastI < format.length())
097: answer.append(format.substring(lastI, format.length()));
098: return answer.toString();
099: }
100:
101: /**
102: * Changes the locale of the messages.
103: *
104: * @param locale
105: * Locale the locale to change to.
106: */
107: static public ResourceBundle setLocale(final Locale locale,
108: final String resource) {
109: try {
110: final ClassLoader loader = VM.bootCallerClassLoader();
111: return (ResourceBundle) AccessController
112: .doPrivileged(new PrivilegedAction<Object>() {
113: public Object run() {
114: return ResourceBundle
115: .getBundle(
116: resource,
117: locale,
118: loader != null ? loader
119: : ClassLoader
120: .getSystemClassLoader());
121: }
122: });
123: } catch (MissingResourceException e) {
124: }
125: return null;
126: }
127: }
|