1 16 package com.blandware.atleap.common.util; 17 18 import java.io.Serializable ; 19 import java.text.MessageFormat ; 20 import java.util.HashMap ; 21 import java.util.Locale ; 22 import java.util.Set ; 23 import java.util.HashSet ; 24 25 33 public class ApplicationResourceStorage implements Serializable { 34 35 38 protected BundleStorage bundles = new BundleStorage(); 39 40 47 public synchronized void putMessage(Locale locale, String key, String value) { 48 if ( value == null ) { 49 throw new NullPointerException ("Value cannot be null"); 50 } 51 52 Bundle bundle = bundles.getBundle(locale); 53 if ( bundle == null ) { 54 bundle = new Bundle(locale); 55 bundles.addBundle(bundle); 56 } 57 58 bundles.getBundle(locale).putMessage(key, value); 59 } 60 61 67 public Set getKeys(Locale locale) { 68 Bundle bundle = bundles.getBundle(locale); 69 Set keys = new HashSet (); 70 if ( bundle != null ) { 71 keys = bundle.getKeys(); 72 } 73 return keys; 74 } 75 76 81 public void mergeBundle(Bundle bundle) { 82 bundles.addBundle(bundle); 83 } 84 85 90 public void dropBundle(Locale locale) { 91 bundles.removeBundle(locale); 92 } 93 94 102 public String getMessage(Locale locale, String key, Object [] args) { 103 Bundle bundle = bundles.getBundle(locale); 104 return bundle != null ? bundle.getMessage(key, args) : null; 105 } 106 107 114 public boolean isMessagePresent(Locale locale, String key) { 115 return getMessage(locale, key, null) != null; 116 } 117 118 121 public static class Bundle implements Serializable { 122 123 126 protected HashMap messages = new HashMap (); 127 128 131 protected HashMap formats = new HashMap (); 132 133 136 protected Locale locale; 137 138 143 public Bundle(Locale locale) { 144 if ( locale == null ) { 145 throw new IllegalArgumentException ("Locale cannot be null"); 146 } 147 this.locale = locale; 148 } 149 150 155 public Locale getLocale() { 156 return locale; 157 } 158 159 166 public String getMessage(String key) { 167 return (String ) messages.get(key); 168 } 169 170 177 public String getMessage(String key, Object [] args) { 178 179 if ( args == null || args.length == 0 ) { 180 return getMessage(key); 181 } 182 183 MessageFormat format = null; 184 synchronized ( formats ) { 185 format = (MessageFormat ) formats.get(key); 186 if ( format == null ) { 187 String msg = getMessage(key); 188 189 if ( msg == null ) { 190 return null; 191 } 192 193 format = new MessageFormat (escape(msg)); 194 format.setLocale(locale); 195 196 formats.put(key, format); 197 } 198 } 199 200 return format.format(args); 201 } 202 203 208 public Set getKeys() { 209 return messages.keySet(); 210 } 211 212 218 public void putMessage(String key, String value) { 219 messages.put(key, value); 220 221 formats.remove(key); 223 } 224 225 230 public int getSize() { 231 return messages.size(); 232 } 233 234 240 protected String escape(String string) { 241 242 if ( (string == null) || (string.indexOf('\'') < 0) ) { 243 return string; 244 } 245 246 int n = string.length(); 247 StringBuffer sb = new StringBuffer (n); 248 249 for ( int i = 0; i < n; i++ ) { 250 char ch = string.charAt(i); 251 252 if ( ch == '\'' ) { 253 sb.append('\''); 254 } 255 256 sb.append(ch); 257 } 258 259 return sb.toString(); 260 261 } 262 } 263 264 267 protected static class BundleStorage implements Serializable { 268 269 272 protected HashMap bundles = new HashMap (); 273 274 279 public void addBundle(Bundle bundle) { 280 Locale locale = bundle.getLocale(); 281 String localeKey = getLocaleKey(locale); 282 bundles.put(localeKey, bundle); 283 } 284 285 290 public void removeBundle(Locale locale) { 291 bundles.remove(getLocaleKey(locale)); 292 } 293 294 300 public Bundle getBundle(Locale locale) { 301 return (Bundle) bundles.get(getLocaleKey(locale)); 302 } 303 304 310 protected String getLocaleKey(Locale locale) { 311 return locale.toString(); 312 } 313 } 314 315 } 316 | Popular Tags |