1 17 package org.columba.core.resourceloader; 18 19 import java.io.File ; 20 import java.io.FileFilter ; 21 import java.net.MalformedURLException ; 22 import java.net.URL ; 23 import java.net.URLClassLoader ; 24 import java.util.Enumeration ; 25 import java.util.HashSet ; 26 import java.util.Hashtable ; 27 import java.util.Locale ; 28 import java.util.MissingResourceException ; 29 import java.util.ResourceBundle ; 30 import java.util.Set ; 31 import java.util.StringTokenizer ; 32 import java.util.logging.Logger ; 33 34 import org.columba.core.config.Config; 35 import org.columba.core.config.DefaultConfigDirectory; 36 import org.columba.core.xml.XmlElement; 37 38 64 public class GlobalResourceLoader { 65 66 private static final Logger LOG = Logger.getLogger("org.columba.core.util"); 67 68 protected static ClassLoader classLoader; 69 70 protected static Hashtable htBundles = new Hashtable (80); 71 72 protected static ResourceBundle globalBundle; 73 74 private static final String GLOBAL_BUNDLE_PATH = "org.columba.core.i18n.global.global"; 75 76 static { 77 initClassLoader(); 78 } 79 80 84 public static void loadLanguage() { 85 XmlElement locale = Config.getInstance().get("options").getElement( 86 "/options/locale"); 87 88 if (locale == null) { 90 locale = new XmlElement("locale"); 92 locale.addAttribute("language", "en"); 93 Config.getInstance().get("options").getElement("/options") 94 .addElement(locale); 95 } 96 97 String language = locale.getAttribute("language"); 98 String country = locale.getAttribute("country", ""); 99 String variant = locale.getAttribute("variant", ""); 100 Locale.setDefault(new Locale (language, country, variant)); 101 initClassLoader(); 102 } 103 104 public static Locale [] getAvailableLocales() { 105 Set locales = new HashSet (); 106 locales.add(new Locale ("en", "")); 107 108 FileFilter langpackFileFilter = new LangPackFileFilter(); 109 File [] langpacks = DefaultConfigDirectory.getInstance().getCurrentPath().listFiles( 110 langpackFileFilter); 111 112 for (int i = 0; i < langpacks.length; i++) { 113 locales.add(extractLocaleFromFilename(langpacks[i].getName())); 114 } 115 116 langpacks = new File (".").listFiles(langpackFileFilter); 117 118 for (int i = 0; i < langpacks.length; i++) { 119 locales.add(extractLocaleFromFilename(langpacks[i].getName())); 120 } 121 122 return (Locale []) locales.toArray(new Locale [0]); 123 } 124 125 private static Locale extractLocaleFromFilename(String name) { 126 String language = ""; 127 String country = ""; 128 String variant = ""; 129 name = name.substring(9, name.length() - 4); 130 131 StringTokenizer tokenizer = new StringTokenizer (name, "_"); 132 133 if (tokenizer.hasMoreElements()) { 134 language = tokenizer.nextToken(); 135 136 if (tokenizer.hasMoreElements()) { 137 country = tokenizer.nextToken(); 138 139 if (tokenizer.hasMoreElements()) { 140 variant = tokenizer.nextToken(); 141 } 142 } 143 } 144 145 return new Locale (language, country, variant); 146 } 147 148 protected static void initClassLoader() { 149 File langpack = null; 150 try { 151 langpack = lookupLanguagePackFile(Locale.getDefault(), DefaultConfigDirectory.getInstance().getCurrentPath()); 152 } catch (RuntimeException ex) { 153 } 155 156 if (langpack == null) { 157 langpack = lookupLanguagePackFile(Locale.getDefault(), 158 new File (".")); 159 } 160 161 if (langpack != null) { 162 LOG 163 .fine("Creating new i18n class loader for " 164 + langpack.getPath()); 165 166 try { 167 classLoader = new URLClassLoader (new URL [] { langpack.toURL() }); 168 } catch (MalformedURLException mue) { 169 } 170 } else { 172 174 182 classLoader = null; 183 } 184 185 try { 186 if (classLoader == null) { 188 globalBundle = ResourceBundle.getBundle(GLOBAL_BUNDLE_PATH, 189 Locale.getDefault()); 190 } else { 191 globalBundle = ResourceBundle.getBundle(GLOBAL_BUNDLE_PATH, 192 Locale.getDefault(), classLoader); 193 } 194 } catch (MissingResourceException mre) { 195 throw new RuntimeException ( 196 "Global resource bundle not found, Columba cannot start."); 197 } 198 } 199 200 204 private static File lookupLanguagePackFile(Locale locale, File directory) { 205 File langpack = new File (directory, "langpack_" + locale.toString() 206 + ".jar"); 207 if (!langpack.exists() || !langpack.isFile()) { 208 langpack = new File (directory, "langpack_" + locale.getLanguage() 209 + ".jar"); 210 } 211 return langpack.exists() && langpack.isFile() ? langpack : null; 212 } 213 214 protected static String generateBundlePath(String sPath, String sName) { 215 return sPath + "." + sName; 216 } 217 218 235 public static String getString(String sPath, String sName, String sID) { 236 if ((sID == null) || sID.equals("")) { 237 return null; 238 } 239 240 ResourceBundle bundle = null; 241 String sBundlePath = null; 242 243 if ((sPath != null) && !sPath.equals("")) { 244 sBundlePath = generateBundlePath(sPath, sName); 247 bundle = (ResourceBundle ) htBundles.get(sBundlePath); 248 } 249 250 if ((bundle == null) && (sBundlePath != null)) { 251 try { 252 if (classLoader == null) { 254 bundle = ResourceBundle.getBundle(sBundlePath, Locale 255 .getDefault()); 256 } else { 257 bundle = ResourceBundle.getBundle(sBundlePath, Locale 258 .getDefault(), classLoader); 259 } 260 261 htBundles.put(sBundlePath, bundle); 262 } catch (MissingResourceException mre) { 263 } 264 } 265 266 if (bundle != null) { 267 try { 268 return bundle.getString(sID); 269 } catch (MissingResourceException mre) { 270 } 271 } 272 273 try { 274 return globalBundle.getString(sID); 275 } catch (MissingResourceException mre) { 276 LOG.severe("'" + sID + "' in '" + sBundlePath 277 + "' could not be found."); 278 279 return sID; 280 } 281 } 282 283 public static void reload() { 284 initClassLoader(); 285 LOG.fine("Reloading cached resource bundles for locale " 286 + Locale.getDefault().toString()); 287 288 try { 289 if (classLoader == null) { 291 globalBundle = ResourceBundle.getBundle(GLOBAL_BUNDLE_PATH, 292 Locale.getDefault()); 293 } else { 294 globalBundle = ResourceBundle.getBundle(GLOBAL_BUNDLE_PATH, 295 Locale.getDefault(), classLoader); 296 } 297 } catch (MissingResourceException mre) { 298 } 299 302 String bundlePath; 303 ResourceBundle bundle; 304 305 for (Enumeration entries = htBundles.keys(); entries.hasMoreElements();) { 306 try { 307 bundlePath = (String ) entries.nextElement(); 308 309 if (classLoader == null) { 312 bundle = ResourceBundle.getBundle(bundlePath, Locale 313 .getDefault()); 314 } else { 315 bundle = ResourceBundle.getBundle(bundlePath, Locale 316 .getDefault(), classLoader); 317 } 318 319 htBundles.put(bundlePath, bundle); 321 } catch (MissingResourceException mre) { 322 } 323 } 326 } 327 328 public static class LangPackFileFilter implements FileFilter { 329 public boolean accept(File file) { 330 String name = file.getName().toLowerCase(); 331 332 return file.isFile() && name.startsWith("langpack_") 333 && name.endsWith(".jar"); 334 } 335 } 336 } | Popular Tags |