1 17 18 package org.apache.tomcat.util.res; 19 20 import java.text.MessageFormat ; 21 import java.util.Hashtable ; 22 import java.util.Locale ; 23 import java.util.MissingResourceException ; 24 import java.util.ResourceBundle ; 25 26 52 53 public class StringManager { 54 55 58 59 private ResourceBundle bundle; 60 private Locale locale; 61 62 70 71 private StringManager(String packageName) { 72 this( packageName, Locale.getDefault() ); 73 } 74 75 private StringManager(String packageName, Locale loc) { 76 String bundleName = packageName + ".LocalStrings"; 77 bundle = ResourceBundle.getBundle(bundleName, loc); 78 locale = bundle.getLocale(); 80 } 81 82 private StringManager(ResourceBundle bundle ) 83 { 84 this.bundle=bundle; 85 locale = bundle.getLocale(); 86 } 87 88 97 98 public String getString(String key) { 99 if(key == null){ 100 String msg = "key may not have a null value"; 101 102 throw new IllegalArgumentException (msg); 103 } 104 105 String str = null; 106 107 try{ 108 str = bundle.getString(key); 109 }catch(MissingResourceException mre){ 110 str = null; 121 } 122 123 return str; 124 } 125 126 133 134 public String getString(String key, Object [] args) { 135 String iString = null; 136 String value = getString(key); 137 138 142 try { 143 if(args==null){ 145 args = new Object [1]; 146 } 147 148 Object [] nonNullArgs = args; 149 for (int i=0; i<args.length; i++) { 150 if (args[i] == null) { 151 if (nonNullArgs==args){ 152 nonNullArgs=(Object [])args.clone(); 153 } 154 nonNullArgs[i] = "null"; 155 } 156 } 157 if( value==null ) value=key; 158 MessageFormat mf = new MessageFormat (value); 159 mf.setLocale(locale); 160 iString = mf.format(nonNullArgs, new StringBuffer (), null).toString(); 161 } catch (IllegalArgumentException iae) { 162 StringBuffer buf = new StringBuffer (); 163 buf.append(value); 164 for (int i = 0; i < args.length; i++) { 165 buf.append(" arg[" + i + "]=" + args[i]); 166 } 167 iString = buf.toString(); 168 } 169 return iString; 170 } 171 172 180 181 public String getString(String key, Object arg) { 182 Object [] args = new Object [] {arg}; 183 return getString(key, args); 184 } 185 186 195 196 public String getString(String key, Object arg1, Object arg2) { 197 Object [] args = new Object [] {arg1, arg2}; 198 return getString(key, args); 199 } 200 201 211 212 public String getString(String key, Object arg1, Object arg2, 213 Object arg3) { 214 Object [] args = new Object [] {arg1, arg2, arg3}; 215 return getString(key, args); 216 } 217 218 229 230 public String getString(String key, Object arg1, Object arg2, 231 Object arg3, Object arg4) { 232 Object [] args = new Object [] {arg1, arg2, arg3, arg4}; 233 return getString(key, args); 234 } 235 239 private static Hashtable managers = new Hashtable (); 240 241 248 public synchronized static StringManager getManager(String packageName) { 249 StringManager mgr = (StringManager)managers.get(packageName); 250 if (mgr == null) { 251 mgr = new StringManager(packageName); 252 managers.put(packageName, mgr); 253 } 254 return mgr; 255 } 256 257 264 public synchronized static StringManager getManager(ResourceBundle bundle) { 265 return new StringManager( bundle ); 266 } 267 268 276 277 public synchronized static StringManager getManager(String packageName,Locale loc) { 278 StringManager mgr = (StringManager)managers.get(packageName+"_"+loc.toString()); 279 if (mgr == null) { 280 mgr = new StringManager(packageName,loc); 281 managers.put(packageName+"_"+loc.toString(), mgr); 282 } 283 return mgr; 284 } 285 286 } 287 | Popular Tags |