1 11 12 package org.eclipse.osgi.framework.msg; 13 14 import java.security.AccessController ; 15 import java.security.PrivilegedAction ; 16 import java.util.*; 17 18 public class MessageFormat { 19 20 protected ResourceBundle bundle; 22 protected Locale locale; 23 24 public MessageFormat(String bundleName) { 25 init(bundleName, Locale.getDefault(), this.getClass()); 26 } 27 28 public MessageFormat(String bundleName, Locale locale) { 29 init(bundleName, locale, this.getClass()); 30 } 31 32 public MessageFormat(String bundleName, Locale locale, Class clazz) { 33 init(bundleName, locale, clazz); 34 } 35 36 protected void init(final String bundleName, final Locale locale, final Class clazz) { 37 bundle = (ResourceBundle) AccessController.doPrivileged(new PrivilegedAction () { 38 public Object run() { 39 ClassLoader loader = clazz.getClassLoader(); 40 41 if (loader == null) { 42 loader = ClassLoader.getSystemClassLoader(); 43 } 44 45 try { 46 return ResourceBundle.getBundle(bundleName, locale, loader); 47 } catch (MissingResourceException e) { 48 return null; 49 } 50 } 51 }); 52 53 this.locale = locale; 54 } 55 56 60 public Locale getLocale() { 61 return locale; 62 } 63 64 72 public String getString(String msg) { 73 if (bundle == null) { 74 return msg; 75 } 76 77 try { 78 return bundle.getString(msg); 79 } catch (MissingResourceException e) { 80 return msg; 81 } 82 } 83 84 94 public String getString(String msg, Object arg) { 95 return getString(msg, new Object [] {arg}); 96 } 97 98 108 public String getString(String msg, int arg) { 109 return getString(msg, new Object [] {Integer.toString(arg)}); 110 } 111 112 122 public String getString(String msg, char arg) { 123 return getString(msg, new Object [] {String.valueOf(arg)}); 124 } 125 126 138 public String getString(String msg, Object arg1, Object arg2) { 139 return getString(msg, new Object [] {arg1, arg2}); 140 } 141 142 152 public String getString(String msg, Object [] args) { 153 String format = msg; 154 155 if (bundle != null) { 156 try { 157 format = bundle.getString(msg); 158 } catch (MissingResourceException e) { 159 } 160 } 161 162 return format(format, args); 163 } 164 165 182 public static String format(String format, Object [] args) { 183 StringBuffer answer = new StringBuffer (); 184 String [] argStrings = new String [args.length]; 185 186 for (int i = 0; i < args.length; ++i) { 187 argStrings[i] = args[i] == null ? "<null>" : args[i].toString(); } 189 190 int lastI = 0; 191 192 for (int i = format.indexOf('{', 0); i >= 0; i = format.indexOf('{', lastI)) { 193 if (i != 0 && format.charAt(i - 1) == '\\') { 194 if (i != 1) { 196 answer.append(format.substring(lastI, i - 1)); 197 } 198 answer.append('{'); 199 lastI = i + 1; 200 } else { 201 if (i > format.length() - 3) { 203 answer.append(format.substring(lastI, format.length())); 205 lastI = format.length(); 206 } else { 207 int argnum = (byte) Character.digit(format.charAt(i + 1), 10); 208 if (argnum < 0 || format.charAt(i + 2) != '}') { 209 answer.append(format.substring(lastI, i + 1)); 211 lastI = i + 1; 212 } else { 213 answer.append(format.substring(lastI, i)); 215 if (argnum >= argStrings.length) { 216 answer.append("<missing argument>"); } else { 218 answer.append(argStrings[argnum]); 219 } 220 lastI = i + 3; 221 } 222 } 223 } 224 } 225 226 if (lastI < format.length()) { 227 answer.append(format.substring(lastI, format.length())); 228 } 229 230 return answer.toString(); 231 } 232 } | Popular Tags |