1 17 18 19 package org.apache.catalina.util; 20 21 import java.text.MessageFormat ; 22 import java.util.Hashtable ; 23 import java.util.Locale ; 24 import java.util.MissingResourceException ; 25 import java.util.ResourceBundle ; 26 import java.net.URLClassLoader ; 27 28 50 51 public class StringManager { 52 53 56 57 private ResourceBundle bundle; 58 59 private static org.apache.commons.logging.Log log= 60 org.apache.commons.logging.LogFactory.getLog( StringManager.class ); 61 62 70 71 private StringManager(String packageName) { 72 String bundleName = packageName + ".LocalStrings"; 73 try { 74 bundle = ResourceBundle.getBundle(bundleName); 75 return; 76 } catch( MissingResourceException ex ) { 77 ClassLoader cl=Thread.currentThread().getContextClassLoader(); 79 if( cl != null ) { 80 try { 81 bundle=ResourceBundle.getBundle(bundleName, Locale.getDefault(), cl); 82 return; 83 } catch(MissingResourceException ex2) { 84 } 85 } 86 if( cl==null ) 87 cl=this.getClass().getClassLoader(); 88 89 if (log.isDebugEnabled()) 90 log.debug("Can't find resource " + bundleName + 91 " " + cl); 92 if( cl instanceof URLClassLoader ) { 93 if (log.isDebugEnabled()) 94 log.debug( ((URLClassLoader )cl).getURLs()); 95 } 96 } 97 } 98 99 104 public String getString(String key) { 105 return MessageFormat.format(getStringInternal(key), (Object [])null); 106 } 107 108 109 protected String getStringInternal(String key) { 110 if (key == null) { 111 String msg = "key is null"; 112 113 throw new NullPointerException (msg); 114 } 115 116 String str = null; 117 118 if( bundle==null ) 119 return key; 120 try { 121 str = bundle.getString(key); 122 } catch (MissingResourceException mre) { 123 str = "Cannot find message associated with key '" + key + "'"; 124 } 125 126 return str; 127 } 128 129 136 137 public String getString(String key, Object [] args) { 138 String iString = null; 139 String value = getStringInternal(key); 140 141 145 try { 146 Object nonNullArgs[] = args; 148 for (int i=0; i<args.length; i++) { 149 if (args[i] == null) { 150 if (nonNullArgs==args) nonNullArgs=(Object [])args.clone(); 151 nonNullArgs[i] = "null"; 152 } 153 } 154 155 iString = MessageFormat.format(value, nonNullArgs); 156 } catch (IllegalArgumentException iae) { 157 StringBuffer buf = new StringBuffer (); 158 buf.append(value); 159 for (int i = 0; i < args.length; i++) { 160 buf.append(" arg[" + i + "]=" + args[i]); 161 } 162 iString = buf.toString(); 163 } 164 return iString; 165 } 166 167 175 176 public String getString(String key, Object arg) { 177 Object [] args = new Object [] {arg}; 178 return getString(key, args); 179 } 180 181 190 191 public String getString(String key, Object arg1, Object arg2) { 192 Object [] args = new Object [] {arg1, arg2}; 193 return getString(key, args); 194 } 195 196 206 207 public String getString(String key, Object arg1, Object arg2, 208 Object arg3) { 209 Object [] args = new Object [] {arg1, arg2, arg3}; 210 return getString(key, args); 211 } 212 213 224 225 public String getString(String key, Object arg1, Object arg2, 226 Object arg3, Object arg4) { 227 Object [] args = new Object [] {arg1, arg2, arg3, arg4}; 228 return getString(key, args); 229 } 230 234 private static Hashtable managers = new Hashtable (); 235 236 243 244 public synchronized static StringManager getManager(String packageName) { 245 StringManager mgr = (StringManager)managers.get(packageName); 246 247 if (mgr == null) { 248 mgr = new StringManager(packageName); 249 managers.put(packageName, mgr); 250 } 251 return mgr; 252 } 253 } 254 | Popular Tags |