1 6 package javax.jdo; 7 8 import java.security.AccessController ; 9 import java.security.PrivilegedAction ; 10 import java.text.MessageFormat ; 11 import java.util.*; 12 13 16 17 public class I18NHelper { 18 19 private I18NHelper() { 20 bundle = null; 21 } 22 23 private I18NHelper(String bundleName, ClassLoader loader) { 24 bundle = null; 25 bundle = loadBundle(bundleName, loader); 26 } 27 28 public static I18NHelper getInstance(String bundleName) { 29 return getInstance(bundleName, (javax.jdo.I18NHelper.class) 30 .getClassLoader()); 31 } 32 33 public static I18NHelper getInstance(final Class cls) { 34 ClassLoader classLoader = (ClassLoader ) AccessController 35 .doPrivileged(new PrivilegedAction () { 36 37 public Object run() { 38 return cls.getClassLoader(); 39 } 40 41 }); 42 String bundle = getPackageName(cls.getName()) + ".Bundle"; 43 return getInstance(bundle, classLoader); 44 } 45 46 public static I18NHelper getInstance(String bundleName, ClassLoader loader) { 47 I18NHelper helper = (I18NHelper) helpers.get(bundleName); 48 if (helper != null) { 49 return helper; 50 } else { 51 helper = new I18NHelper(bundleName, loader); 52 helpers.put(bundleName, helper); 53 return (I18NHelper) helpers.get(bundleName); 54 } 55 } 56 57 public String msg(String messageKey) { 58 return getMessage(bundle, messageKey); 59 } 60 61 public String msg(String messageKey, Object arg1) { 62 return getMessage(bundle, messageKey, arg1); 63 } 64 65 public String msg(String messageKey, Object arg1, Object arg2) { 66 return getMessage(bundle, messageKey, arg1, arg2); 67 } 68 69 public String msg(String messageKey, Object arg1, Object arg2, Object arg3) { 70 return getMessage(bundle, messageKey, arg1, arg2, arg3); 71 } 72 73 public String msg(String messageKey, Object args[]) { 74 return getMessage(bundle, messageKey, args); 75 } 76 77 public String msg(String messageKey, int arg) { 78 return getMessage(bundle, messageKey, arg); 79 } 80 81 public String msg(String messageKey, boolean arg) { 82 return getMessage(bundle, messageKey, arg); 83 } 84 85 private static final ResourceBundle loadBundle(String bundleName, 86 ClassLoader loader) { 87 ResourceBundle messages = (ResourceBundle) bundles.get(bundleName); 88 if (messages == null) { 89 messages = ResourceBundle.getBundle(bundleName, locale, loader); 90 bundles.put(bundleName, messages); 91 } 92 return messages; 93 } 94 95 private static final String getMessage(ResourceBundle messages, 96 String messageKey) { 97 return messages.getString(messageKey); 98 } 99 100 private static final String getMessage(ResourceBundle messages, 101 String messageKey, Object msgArgs[]) { 102 for (int i = 0; i < msgArgs.length; i++) 103 if (msgArgs[i] == null) 104 msgArgs[i] = ""; 105 106 MessageFormat formatter = new MessageFormat (messages 107 .getString(messageKey)); 108 return formatter.format(((Object ) (msgArgs))); 109 } 110 111 private static final String getMessage(ResourceBundle messages, 112 String messageKey, Object arg) { 113 Object args[] = { arg }; 114 return getMessage(messages, messageKey, args); 115 } 116 117 private static final String getMessage(ResourceBundle messages, 118 String messageKey, Object arg1, Object arg2) { 119 Object args[] = { arg1, arg2 }; 120 return getMessage(messages, messageKey, args); 121 } 122 123 private static final String getMessage(ResourceBundle messages, 124 String messageKey, Object arg1, Object arg2, Object arg3) { 125 Object args[] = { arg1, arg2, arg3 }; 126 return getMessage(messages, messageKey, args); 127 } 128 129 private static final String getMessage(ResourceBundle messages, 130 String messageKey, int arg) { 131 Object args[] = { new Integer (arg) }; 132 return getMessage(messages, messageKey, args); 133 } 134 135 private static final String getMessage(ResourceBundle messages, 136 String messageKey, boolean arg) { 137 Object args[] = { String.valueOf(arg) }; 138 return getMessage(messages, messageKey, args); 139 } 140 141 private static final String getPackageName(String className) { 142 int index = className.lastIndexOf('.'); 143 return index == -1 ? "" : className.substring(0, index); 144 } 145 146 private static Hashtable bundles = new Hashtable(); 147 148 private static Hashtable helpers = new Hashtable(); 149 150 private static Locale locale = Locale.getDefault(); 151 152 private ResourceBundle bundle; 153 154 private static final String bundleSuffix = ".Bundle"; 155 156 } | Popular Tags |