1 package com.ca.commons.cbutil; 2 3 import java.net.URL ; 4 import java.util.*; 5 import java.util.logging.Level ; 6 import java.util.logging.Logger ; 7 8 45 46 public class CBResourceBundle 47 { 48 Hashtable translations = new Hashtable(); 49 50 private static Logger log = Logger.getLogger(CBResourceBundle.class.getName()); 51 52 63 64 public CBResourceBundle(String baseName) 65 { 66 loadBundle(baseName, Locale.getDefault(), ClassLoader.getSystemClassLoader()); 67 } 68 69 82 83 public CBResourceBundle(String baseName, Locale locale) 84 { 85 loadBundle(baseName, locale, ClassLoader.getSystemClassLoader()); 86 } 87 88 89 105 106 public CBResourceBundle(String baseName, Locale locale, ClassLoader loader) 107 { 108 loadBundle(baseName, locale, loader); 109 } 110 111 127 protected void loadBundle(String baseName, Locale locale, ClassLoader loader) 128 { 129 Vector names = getBundleNames(baseName, locale); 130 for (int i = names.size() - 1; i >= 0; i--) 131 { 132 URL url = loader.getResource(names.get(i).toString()); if (loadData(url) == true) 134 return; } 136 137 log.warning("unable to load resource bundle '" + baseName + "'"); 139 } 140 141 149 150 protected static Vector getBundleNames(String baseName, Locale locale) 151 { 152 final Vector result = new Vector(8); 153 final String language = locale.getLanguage(); 154 final int languageLength = language.length(); 155 final String country = locale.getCountry(); 156 final int countryLength = country.length(); 157 final String variant = locale.getVariant(); 158 final int variantLength = variant.length(); 159 160 if (baseName.toLowerCase().endsWith(".properties")) 161 { 162 baseName = baseName.substring(baseName.length() - 11); 163 } 164 165 baseName = baseName.replace('.', '/'); final StringBuffer temp = new StringBuffer (baseName); 167 168 result.addElement(temp.toString() + ".properties"); 169 result.addElement(temp.toString()); 170 171 if (languageLength + countryLength + variantLength == 0) 172 { 173 return result; } 175 176 temp.append('_'); 177 temp.append(language); 178 179 result.addElement(temp.toString() + ".properties"); 180 result.addElement(temp.toString()); 181 182 if (countryLength + variantLength == 0) 183 { 184 return result; 185 } 186 187 temp.append('_'); 188 temp.append(country); 189 190 result.addElement(temp.toString() + ".properties"); 191 result.addElement(temp.toString()); 192 193 if (variantLength == 0) 194 { 195 return result; 196 } 197 198 temp.append('_'); 199 temp.append(variant); 200 201 result.addElement(temp.toString() + ".properties"); 202 result.addElement(temp.toString()); 203 204 return result; 205 } 206 207 208 217 218 protected boolean loadData(URL url) 219 { 220 if (url == null) return false; 222 log.finer("Resource Bundle Reading data from " + ((url == null) ? "null url" : url.toString())); 223 224 try 225 { 226 229 byte[] data = CBUtility.readStream(url.openStream()); 230 231 235 236 String text = CBUtility.readI18NByteArray(data); 237 238 242 243 return parseData(text); 244 } 245 catch (Exception e) 246 { 247 log.log(Level.FINER, "Unable to read data from url: " + ((url == null) ? "(null url)" : url.toString()), e); 248 return false; 249 } 250 } 251 252 259 260 protected boolean parseData(String text) 261 { 262 int startSize = translations.size(); 263 264 int start = 0, end = 0; 265 while ((end = text.indexOf('\n', start)) != -1) 266 { 267 String line = text.substring(start, end); 268 269 270 line = line.trim(); 271 272 if (line.length() != 0 && line.charAt(0) != '#') { 274 try 275 { 276 int equalPos = 0; 277 278 do { 280 equalPos = line.indexOf('=', equalPos + 1); 281 } 282 while (line.charAt(equalPos - 1) == '\\'); 283 284 String key = unescape(line.substring(0, equalPos)).trim(); 285 String trans = line.substring(equalPos + 1).trim(); 286 translations.put(key, trans); 287 288 } 289 catch (Exception e) 290 { 291 log.log(Level.FINER, "Exception parsing data line '" + line, e); 292 } } 294 295 start = end + 1; 296 } 297 298 boolean success = (startSize < translations.size()); 301 if (success == false) 302 log.finer("ParseData unsuccessfull - no new data found"); 303 return success; 304 305 } 306 307 311 312 protected String unescape(String escapeMe) 313 { 314 int pos = 0; 315 while ((pos = escapeMe.indexOf('\\', pos)) >= 0) 316 escapeMe = escapeMe.substring(0, pos) + escapeMe.substring(pos + 1); 317 318 return escapeMe; 319 } 320 321 327 328 public Enumeration keys() 329 { 330 return translations.keys(); 331 } 332 333 340 341 public Enumeration getKeys() 342 { 343 return translations.keys(); 344 } 345 346 352 353 public Object get(Object key) 354 { 355 return translations.get(key); 356 } 357 358 365 366 public Object getObject(Object key) 367 { 368 return translations.get(key); 369 } 370 371 372 382 383 public String getString(String key) 384 { 385 if (key == null) return ""; 386 Object o = translations.get(key); 387 if (o == null) return ""; 388 389 return (o instanceof String ) ? (String ) o : o.toString(); 390 } 391 392 393 } | Popular Tags |