1 19 20 package org.netbeans.modules.xml.xpath.common; 21 22 import java.text.MessageFormat ; 23 import java.util.Hashtable ; 24 import java.util.Locale ; 25 import java.util.MissingResourceException ; 26 import java.util.ResourceBundle ; 27 28 29 48 public class MessageManager { 49 50 54 55 private static Hashtable managers = new Hashtable (); 56 57 58 private ResourceBundle bundle; 59 60 61 68 private MessageManager(String packageName) { 69 String bundleName = packageName + ".Bundle"; 70 bundle = ResourceBundle.getBundle(bundleName); 71 } 72 73 81 private MessageManager(String baseName, Class clazz) { 82 bundle = ResourceBundle.getBundle(baseName, Locale.getDefault(), clazz.getClassLoader()); 83 } 84 85 88 private static String findName(Class clazz) { 89 String pref = clazz.getName(); 90 int last = pref.lastIndexOf('.'); 91 if (last >= 0) { 92 return pref.substring(0, last); 93 } else { 94 return ""; } 97 } 98 99 107 public static MessageManager getManager(Class clazz) { 108 String packageName = findName(clazz); 109 MessageManager mgr = (MessageManager) managers.get(packageName); 110 if (mgr == null) { 111 String bundleName = (packageName.length() == 0) ? "Bundle" : packageName + ".Bundle"; 112 mgr = new MessageManager(bundleName, clazz); 113 managers.put(packageName, mgr); 114 } 115 return mgr; 116 } 117 118 126 public static synchronized MessageManager getManager(String packageName) { 127 MessageManager mgr = (MessageManager) managers.get(packageName); 128 if (mgr == null) { 129 mgr = new MessageManager(packageName); 130 managers.put(packageName, mgr); 131 } 132 return mgr; 133 } 134 135 136 142 public String getString(String key) { 143 if (key == null) { 144 String msg = "key is null"; 145 throw new NullPointerException (msg); 146 } 147 148 String str = null; 149 150 try { 151 str = bundle.getString(key); 152 } catch (MissingResourceException mre) { 153 str = "Cannot find message associated with key '" + key + "'"; 154 } 155 156 return str; 157 } 158 159 160 168 public String getString(String key, Object [] args) { 169 String iString = null; 170 String value = getString(key); 171 172 try { 173 Object nonNullArgs[] = args; 174 for (int i = 0; i < args.length; i++) { 175 if (args[i] == null) { 176 if (nonNullArgs == args) { 177 nonNullArgs = (Object []) args.clone(); 178 } 179 nonNullArgs[i] = "null"; 180 } 181 } 182 183 iString = MessageFormat.format(value, nonNullArgs); 184 } catch (IllegalArgumentException iae) { 185 StringBuffer buf = new StringBuffer (); 186 buf.append(value); 187 for (int i = 0; i < args.length; i++) { 188 buf.append(" arg[" + i + "]=" + args[i]); 189 } 190 iString = buf.toString(); 191 } 192 193 return iString; 194 } 195 196 197 205 public String getString(String key, Object arg) { 206 Object [] args = new Object []{arg}; 207 return getString(key, args); 208 } 209 210 211 220 public String getString(String key, Object arg1, Object arg2) { 221 Object [] args = new Object []{arg1, arg2}; 222 return getString(key, args); 223 } 224 225 226 236 237 public String getString(String key, Object arg1, Object arg2, 238 Object arg3) { 239 Object [] args = new Object []{arg1, arg2, arg3}; 240 return getString(key, args); 241 } 242 243 244 255 public String getString(String key, Object arg1, Object arg2, Object arg3, 256 Object arg4) { 257 Object [] args = new Object []{arg1, arg2, arg3, arg4}; 258 return getString(key, args); 259 } 260 261 } 262 | Popular Tags |