2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.harmony.luni.util;
21 * This class contains helper methods for loading resource bundles and
22 * formatting external message strings.
26 public final class MsgHelp {
29 * Generates a formatted text string given a source string containing
30 * "argument markers" of the form "{argNum}" where each argNum must be in
31 * the range 0..9. The result is generated by inserting the toString of each
32 * argument into the position indicated in the string.
34 * To insert the "{" character into the output, use a single backslash
35 * character to escape it (i.e. "\{"). The "}" character does not need to be
39 * String the format to use when printing.
41 * Object[] the arguments to use.
42 * @return String the formatted message.
44 public static String format(String format, Object[] args) {
45 StringBuilder answer = new StringBuilder(format.length()
46 + (args.length * 20));
47 String[] argStrings = new String[args.length];
48 for (int i = 0; i < args.length; ++i) {
50 argStrings[i] = "<null>";
52 argStrings[i] = args[i].toString();
55 for (int i = format.indexOf('{', 0); i >= 0; i = format.indexOf('{',
57 if (i != 0 && format.charAt(i - 1) == '\\') {
58 // It's escaped, just print and loop.
60 answer.append(format.substring(lastI, i - 1));
64 // It's a format character.
65 if (i > format.length() - 3) {
66 // Bad format, just print and loop.
67 answer.append(format.substring(lastI, format.length()));
68 lastI = format.length();
70 // int argnum = (byte) Character.digit(format.charAt(i + 1),
72 int argnum = (byte) (format.charAt(i + 1) - '0');
73 if (argnum < 0 || format.charAt(i + 2) != '}') {
74 // Bad format, just print and loop.
75 answer.append(format.substring(lastI, i + 1));
79 answer.append(format.substring(lastI, i));
80 if (argnum >= argStrings.length)
81 answer.append("<missing argument>");
83 answer.append(argStrings[argnum]);
89 if (lastI < format.length())
90 answer.append(format.substring(lastI, format.length()));
91 return answer.toString();
96 // * Changes the locale of the messages.
99 // * Locale the locale to change to.
101 // static public ResourceBundle setLocale(final Locale locale,
102 // final String resource) {
105 // final ClassLoader loader = VM.bootCallerClassLoader();
106 // return (ResourceBundle) AccessController
107 // .doPrivileged(new PrivilegedAction<Object>() {
108 // public Object run() {
109 // return ResourceBundle.getBundle(resource, locale,
110 // loader != null ? loader : ClassLoader.getSystemClassLoader());
113 // } catch (MissingResourceException e) {
119 // * Class.forName$("java.util.ResourceBundle");
122 // return ResourceBundle.getBundle(resource);