1 17 18 19 package org.apache.naming; 20 21 import java.text.MessageFormat ; 22 import java.util.Hashtable ; 23 import java.util.MissingResourceException ; 24 import java.util.ResourceBundle ; 25 26 48 49 public class StringManager { 50 51 54 55 private ResourceBundle bundle; 56 57 65 66 private StringManager(String packageName) { 67 String bundleName = packageName + ".LocalStrings"; 68 bundle = ResourceBundle.getBundle(bundleName); 69 } 70 71 76 77 public String getString(String key) { 78 if (key == null) { 79 String msg = "key is null"; 80 81 throw new NullPointerException (msg); 82 } 83 84 String str = null; 85 86 try { 87 str = bundle.getString(key); 88 } catch (MissingResourceException mre) { 89 str = "Cannot find message associated with key '" + key + "'"; 90 } 91 92 return str; 93 } 94 95 102 103 public String getString(String key, Object [] args) { 104 String iString = null; 105 String value = getString(key); 106 107 111 try { 112 Object nonNullArgs[] = args; 114 for (int i=0; i<args.length; i++) { 115 if (args[i] == null) { 116 if (nonNullArgs==args) nonNullArgs=(Object [])args.clone(); 117 nonNullArgs[i] = "null"; 118 } 119 } 120 121 iString = MessageFormat.format(value, nonNullArgs); 122 } catch (IllegalArgumentException iae) { 123 StringBuffer buf = new StringBuffer (); 124 buf.append(value); 125 for (int i = 0; i < args.length; i++) { 126 buf.append(" arg[" + i + "]=" + args[i]); 127 } 128 iString = buf.toString(); 129 } 130 return iString; 131 } 132 133 141 142 public String getString(String key, Object arg) { 143 Object [] args = new Object [] {arg}; 144 return getString(key, args); 145 } 146 147 156 157 public String getString(String key, Object arg1, Object arg2) { 158 Object [] args = new Object [] {arg1, arg2}; 159 return getString(key, args); 160 } 161 162 172 173 public String getString(String key, Object arg1, Object arg2, 174 Object arg3) { 175 Object [] args = new Object [] {arg1, arg2, arg3}; 176 return getString(key, args); 177 } 178 179 190 191 public String getString(String key, Object arg1, Object arg2, 192 Object arg3, Object arg4) { 193 Object [] args = new Object [] {arg1, arg2, arg3, arg4}; 194 return getString(key, args); 195 } 196 200 private static Hashtable managers = new Hashtable (); 201 202 209 210 public synchronized static StringManager getManager(String packageName) { 211 StringManager mgr = (StringManager)managers.get(packageName); 212 if (mgr == null) { 213 mgr = new StringManager(packageName); 214 managers.put(packageName, mgr); 215 } 216 return mgr; 217 } 218 } 219 | Popular Tags |