1 package com.tonbeller.tbutils.res; 2 3 import java.io.File ; 4 import java.text.MessageFormat ; 5 import java.util.Collection ; 6 import java.util.List ; 7 import java.util.Locale ; 8 import java.util.MissingResourceException ; 9 10 15 public class Resources { 16 private ReplacingResourceProvider provider; 17 private Locale locale; 18 private File home; 19 private PersistentResourceProvider persistentProvider; 20 private CompositeResourceProvider compositeProvider; 21 22 static final String PERSISTENT_PROPERTIES = "persistent.properties"; 23 24 28 public static Resources instance() { 29 return ResourcesFactory.instance().getResources(null, null); 30 } 31 32 36 public static Resources instance(Locale browserLocale) { 37 return ResourcesFactory.instance().getResources(browserLocale, null); 38 } 39 40 49 public static Resources instance(Locale browserLocale, Class clazz) { 50 return ResourcesFactory.instance().getResources(browserLocale, clazz.getPackage().getName() + ".resources"); 51 } 52 53 61 public static Resources instance(Class clazz) { 62 return ResourcesFactory.instance().getResources(Locale.getDefault(), clazz.getPackage().getName() + ".resources"); 63 } 64 65 71 public static Resources instance(Locale browserLocale, String bundleName) { 72 return ResourcesFactory.instance().getResources(browserLocale, bundleName); 73 } 74 75 Resources(CompositeResourceProvider compositeProvider, Locale locale, File home) { 76 this.locale = locale; 77 this.home = home; 78 this.compositeProvider = compositeProvider; 79 File persistentProperties = new File (home, PERSISTENT_PROPERTIES); 80 this.persistentProvider = new FilePersistentResourceProvider(persistentProperties); 81 compositeProvider.add(0, persistentProvider); 83 this.provider = new ReplacingResourceProvider(compositeProvider); 84 } 85 86 92 public String getOptionalString(String key, String defaultValue) { 93 String s = provider.getString(key); 94 if (s == null) 95 return defaultValue; 96 return s.trim(); 97 } 98 99 103 public String getString(String key) throws MissingResourceException { 104 String s = provider.getString(key); 105 if (s == null) 106 throw new MissingResourceException ("missing resource for " + key, this.getClass().getName(), key); 107 return s.trim(); 108 } 109 110 113 public String getString(String key, Object arg) throws MissingResourceException { 114 String fmt = getString(key); 115 return MessageFormat.format(fmt, new Object []{arg}); 116 } 117 118 121 public String getString(String key, Object arg0, Object arg1) throws MissingResourceException { 122 String fmt = getString(key); 123 return MessageFormat.format(fmt, new Object []{arg0, arg1}); 124 } 125 126 129 public String getString(String key, Object [] args) throws MissingResourceException { 130 String fmt = getString(key); 131 return MessageFormat.format(fmt, args); 132 } 133 134 137 public boolean getBoolean(String key) throws MissingResourceException { 138 String s = getString(key); 139 return "true".equals(s) || "on".equals(s) || "yes".equals(s); 140 } 141 142 public boolean getOptionalBoolean(String key, boolean defaultValue) { 143 try { 144 return getBoolean(key); 145 } catch (MissingResourceException e) { 146 return defaultValue; 147 } 148 } 149 150 153 public int getInteger(String key) throws MissingResourceException { 154 String s = getString(key); 155 return Integer.parseInt(s); 156 } 157 158 public int getOptionalInteger(String key, int defaultValue) { 159 try { 160 return getInteger(key); 161 } catch (MissingResourceException e) { 162 return defaultValue; 163 } 164 } 165 166 169 public long getLong(String key) throws MissingResourceException { 170 String s = getString(key); 171 return Long.parseLong(s); 172 } 173 174 public long getOptionalLong(String key, long defaultValue) { 175 try { 176 return getLong(key); 177 } catch (MissingResourceException e) { 178 return defaultValue; 179 } 180 } 181 182 185 public double getDouble(String key) throws MissingResourceException { 186 String s = getString(key); 187 return Double.parseDouble(s); 188 } 189 190 public double getOptionalDouble(String key, double defaultValue) { 191 try { 192 return getDouble(key); 193 } catch (MissingResourceException e) { 194 return defaultValue; 195 } 196 } 197 198 202 public Collection keySet() { 203 return provider.keySet(); 204 } 205 206 209 public Locale getLocale() { 210 return locale; 211 } 212 213 217 public File getHomeDir() { 218 return home; 219 } 220 221 225 public void setPersistentString(String key, String value) { 226 persistentProvider.store(key, value); 227 } 228 229 233 public void setPersistentBoolean(String key, boolean value) { 234 persistentProvider.store(key, value ? "true" : "false"); 235 } 236 237 241 public void setPersistentInteger(String key, int value) { 242 persistentProvider.store(key, Integer.toString(value)); 243 } 244 245 249 public void setPersistentLong(String key, long value) { 250 persistentProvider.store(key, Long.toString(value)); 251 } 252 253 257 public void setPersistentDouble(String key, double value) { 258 persistentProvider.store(key, Double.toString(value)); 259 } 260 261 public void removePersistent(String key) { 262 persistentProvider.remove(key); 263 } 264 265 268 public void flush() { 269 persistentProvider.flush(); 270 } 271 272 276 public void dump(Dumper d) { 277 provider.dump(d); 278 } 279 280 305 public String replace(String s) { 306 return provider.replace(s); 307 } 308 309 314 public List getProviders() { 315 return compositeProvider.getProviders(); 316 } 317 } | Popular Tags |