1 16 17 package javax.jdo.spi; 18 19 import java.util.*; 20 import java.text.MessageFormat ; 21 import java.security.AccessController ; 22 import java.security.PrivilegedAction ; 23 24 import javax.jdo.JDOFatalInternalException; 25 26 46 public class I18NHelper { 47 48 50 private static Hashtable bundles = new Hashtable(); 51 52 54 private static Hashtable helpers = new Hashtable(); 55 56 58 private static Locale locale = Locale.getDefault(); 59 60 62 private final String bundleName; 63 64 66 private ResourceBundle bundle = null; 67 68 70 private Throwable failure = null; 71 72 73 private static final String bundleSuffix = ".Bundle"; 75 76 private I18NHelper() { 77 this.bundleName = null; 78 } 79 80 85 private I18NHelper (String bundleName, ClassLoader loader) { 86 this.bundleName = bundleName; 87 try { 88 bundle = loadBundle (bundleName, loader); 89 } 90 catch (Throwable e) { 91 failure = e; 92 } 93 } 94 95 100 public static I18NHelper getInstance (String bundleName) { 101 return getInstance (bundleName, I18NHelper.class.getClassLoader()); 102 } 103 104 111 public static I18NHelper getInstance (final Class cls) { 112 ClassLoader classLoader = (ClassLoader ) AccessController.doPrivileged ( 113 new PrivilegedAction () { 114 public Object run () { 115 return cls.getClassLoader(); 116 } 117 } 118 ); 119 String bundle = getPackageName (cls.getName()) + bundleSuffix; 120 return getInstance (bundle, classLoader); 121 } 122 123 131 public static I18NHelper getInstance (String bundleName, 132 ClassLoader loader) { 133 I18NHelper helper = (I18NHelper) helpers.get (bundleName); 134 if (helper != null) { 135 return helper; 136 } 137 helper = new I18NHelper(bundleName, loader); 138 helpers.put (bundleName, helper); 139 return (I18NHelper) helpers.get (bundleName); 142 } 143 144 148 public String msg (String messageKey) { 149 assertBundle (messageKey); 150 return getMessage (bundle, messageKey); 151 } 152 153 158 public String msg (String messageKey, Object arg1) { 159 assertBundle (messageKey); 160 return getMessage (bundle, messageKey, arg1); 161 } 162 163 169 public String msg (String messageKey, Object arg1, Object arg2) { 170 assertBundle (messageKey); 171 return getMessage (bundle, messageKey, arg1, arg2); 172 } 173 174 181 public String msg (String messageKey, Object arg1, Object arg2, Object arg3) { 182 assertBundle (messageKey); 183 return getMessage (bundle, messageKey, arg1, arg2, arg3); 184 } 185 186 191 public String msg (String messageKey, Object [] args) { 192 assertBundle (messageKey); 193 return getMessage (bundle, messageKey, args); 194 } 195 196 201 public String msg (String messageKey, int arg) { 202 assertBundle (messageKey); 203 return getMessage(bundle, messageKey, arg); 204 } 205 206 211 public String msg (String messageKey, boolean arg) { 212 assertBundle (messageKey); 213 return getMessage(bundle, messageKey, arg); 214 } 215 216 220 public ResourceBundle getResourceBundle () { 221 assertBundle (); 222 return bundle; 223 } 224 225 227 233 final private static ResourceBundle loadBundle( 234 String bundleName, ClassLoader loader) { 235 ResourceBundle messages = (ResourceBundle)bundles.get(bundleName); 236 237 if (messages == null) { 239 messages = ResourceBundle.getBundle(bundleName, locale, loader); 240 bundles.put(bundleName, messages); 241 } 242 return messages; 243 } 244 245 250 private void assertBundle () { 251 if (failure != null) 252 throw new JDOFatalInternalException ( 253 "No resources could be found for bundle:\"" + 254 bundle + "\" ", failure); 255 } 256 257 263 private void assertBundle (String key) { 264 if (failure != null) 265 throw new JDOFatalInternalException ( 266 "No resources could be found to annotate error message key:\"" + 267 key + "\"", failure); 268 } 269 270 276 final private static String getMessage(ResourceBundle messages, String messageKey) 277 { 278 return messages.getString(messageKey); 279 } 280 281 288 final private static String getMessage(ResourceBundle messages, String messageKey, Object msgArgs[]) 289 { 290 for (int i=0; i<msgArgs.length; i++) { 291 if (msgArgs[i] == null) msgArgs[i] = ""; } 293 MessageFormat formatter = new MessageFormat (messages.getString(messageKey)); 294 return formatter.format(msgArgs); 295 } 296 297 304 final private static String getMessage(ResourceBundle messages, String messageKey, Object arg) 305 { 306 Object []args = {arg}; 307 return getMessage(messages, messageKey, args); 308 } 309 310 318 final private static String getMessage(ResourceBundle messages, String messageKey, Object arg1, 319 Object arg2) 320 { 321 Object []args = {arg1, arg2}; 322 return getMessage(messages, messageKey, args); 323 } 324 325 334 final private static String getMessage(ResourceBundle messages, String messageKey, Object arg1, 335 Object arg2, Object arg3) 336 { 337 Object []args = {arg1, arg2, arg3}; 338 return getMessage(messages, messageKey, args); 339 } 340 341 348 final private static String getMessage(ResourceBundle messages, String messageKey, int arg) 349 { 350 Object []args = {new Integer (arg)}; 351 return getMessage(messages, messageKey, args); 352 } 353 354 361 final private static String getMessage(ResourceBundle messages, String messageKey, boolean arg) 362 { 363 Object []args = {String.valueOf(arg)}; 364 return getMessage(messages, messageKey, args); 365 } 366 367 373 final private static String getPackageName(final String className) 374 { 375 final int index = className.lastIndexOf('.'); 376 return ((index != -1) ? className.substring(0, index) : ""); } 378 } 379 | Popular Tags |