Adds _j2sclasslist.txt to SwingJS distribution; fixes missing
[jalview.git] / src / org / apache / harmony / luni / util / MsgHelp.java
1 /*
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.apache.harmony.luni.util;
19
20 /**
21  * This class contains helper methods for loading resource bundles and
22  * formatting external message strings.
23  * 
24  */
25
26 public final class MsgHelp {
27
28         /**
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.
33          * <p>
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
36          * escaped.
37          * 
38          * @param format
39          *            String the format to use when printing.
40          * @param args
41          *            Object[] the arguments to use.
42          * @return String the formatted message.
43          */
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) {
49                         if (args[i] == null)
50                                 argStrings[i] = "<null>";
51                         else
52                                 argStrings[i] = args[i].toString();
53                 }
54                 int lastI = 0;
55                 for (int i = format.indexOf('{', 0); i >= 0; i = format.indexOf('{',
56                                 lastI)) {
57                         if (i != 0 && format.charAt(i - 1) == '\\') {
58                                 // It's escaped, just print and loop.
59                                 if (i != 1)
60                                         answer.append(format.substring(lastI, i - 1));
61                                 answer.append('{');
62                                 lastI = i + 1;
63                         } else {
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();
69                                 } else {
70 //                                      int argnum = (byte) Character.digit(format.charAt(i + 1),
71 //                                                      10);
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));
76                                                 lastI = i + 1;
77                                         } else {
78                                                 // Got a good one!
79                                                 answer.append(format.substring(lastI, i));
80                                                 if (argnum >= argStrings.length)
81                                                         answer.append("<missing argument>");
82                                                 else
83                                                         answer.append(argStrings[argnum]);
84                                                 lastI = i + 3;
85                                         }
86                                 }
87                         }
88                 }
89                 if (lastI < format.length())
90                         answer.append(format.substring(lastI, format.length()));
91                 return answer.toString();
92         }
93
94 //
95 //      /**
96 //       * Changes the locale of the messages.
97 //       * 
98 //       * @param locale
99 //       *            Locale the locale to change to.
100 //       */
101 //      static public ResourceBundle setLocale(final Locale locale,
102 //                      final String resource) {
103 //              /*
104 //              try {
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());
111 //                                              }
112 //                                      });
113 //              } catch (MissingResourceException e) {
114 //              }
115 //              */
116 //              /**
117 //               * @j2sNative
118 //               * 
119 //               * Class.forName$("java.util.ResourceBundle");
120 //               */
121 //              {}
122 //              return ResourceBundle.getBundle(resource);
123 //              //return null;
124 //      }
125 }