PageRenderTime 59ms CodeModel.GetById 43ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mpv5/i18n/ResourceBundleUtf8.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 138 lines | 96 code | 21 blank | 21 comment | 12 complexity | c324c1f90b1536b4212c0a316519e2ad MD5 | raw file
  1package mpv5.i18n;
  2
  3import java.io.UnsupportedEncodingException;
  4import java.util.Collections;
  5import java.util.Enumeration;
  6import java.util.HashMap;
  7import java.util.Locale;
  8import java.util.Map;
  9import java.util.PropertyResourceBundle;
 10import java.util.ResourceBundle;
 11import mpv5.logging.Log;
 12
 13/**
 14 * Same as {@link ResourceBundle} but is target for UTF- property file resources.
 15 * May not be used with -bit ASCII - only with UTF-.
 16 *
 17 * @author Marc Neumann
 18 */
 19public class ResourceBundleUtf8 {
 20
 21    private static class PropertyResourceBundleUtf extends ResourceBundle {
 22
 23        private final Map<String, String> valueByKey = new HashMap<String, String>();
 24        private final PropertyResourceBundle bundle;
 25
 26        private PropertyResourceBundleUtf(PropertyResourceBundle pBundle) {
 27            this.bundle = pBundle;
 28            loadEntries(pBundle, valueByKey);
 29        }
 30
 31        /**
 32         * @see java.util.ResourceBundle#getKeys()
 33         */
 34        @Override
 35        public Enumeration<String> getKeys() {
 36            return Collections.enumeration(valueByKey.keySet());
 37        }
 38
 39        private void loadEntries(PropertyResourceBundle pBundle, Map<String, String> pValueByKey) {
 40            for (Enumeration<String> keys = pBundle.getKeys(); keys.hasMoreElements();) {
 41                String key = keys.nextElement();
 42                String valueRaw = pBundle.getString(key);
 43                String value = null;
 44
 45                try {
 46                    value = new String(valueRaw.getBytes("ISO-8859-1"), "UTF-8");
 47
 48                } catch (UnsupportedEncodingException e) {
 49                    Log.Debug(this, "could not load UTF- property resource bundle [" + pBundle + "]");
 50                }
 51
 52                if (pValueByKey.put(key, value) != null) {
 53                    Log.Debug(this, "duplicate key [" + key + "] in UTF- property resource bundle [" + pBundle + "]");
 54                }
 55            }
 56        }
 57
 58        /**
 59         * @see java.util.ResourceBundle#handleGetObject(java.lang.String)
 60         */
 61        @Override
 62        protected Object handleGetObject(String pKey) {
 63            if (valueByKey.containsKey(pKey)) {
 64                return valueByKey.get(pKey);
 65            } else {
 66                try {
 67                    Log.Debug(this, "Key missing in " + bundle + " : " + pKey);
 68                    return getBundle(LanguageManager.defLanguageBundleName).getObject(pKey);
 69                } catch (Exception e) {
 70                    return pKey;
 71                }
 72            }
 73        }
 74    }
 75    private static Map<ClassLoader, Map<String, Map<Locale, ResourceBundle>>> bundleByClassLoaderByBaseNameByLocale =
 76            new HashMap<ClassLoader, Map<String, Map<Locale, ResourceBundle>>>();
 77
 78    /**
 79     * @see ResourceBundle#getBundle(String)
 80     */
 81    public static ResourceBundle getBundle(String pBaseName) {
 82        ResourceBundle bundle = ResourceBundle.getBundle(pBaseName);
 83        return createUtfPropertyResourceBundle(bundle);
 84    }
 85
 86    /**
 87     * @see ResourceBundle#getBundle(String, Locale)
 88     */
 89    public static final ResourceBundle getBundle(String pBaseName, Locale pLocale) {
 90        ResourceBundle bundle = ResourceBundle.getBundle(pBaseName, pLocale);
 91        return createUtfPropertyResourceBundle(bundle);
 92    }
 93
 94    /**
 95     * @see ResourceBundle#getBundle(String, Locale, ClassLoader)
 96     */
 97    public static ResourceBundle getBundle(String pBaseName, Locale pLocale, ClassLoader pLoader) {
 98
 99        Map<String, Map<Locale, ResourceBundle>> bundleByBaseNameByLocale;
100        Map<Locale, ResourceBundle> bundleByLocale = null;
101        ResourceBundle bundle = null;
102
103        synchronized (bundleByClassLoaderByBaseNameByLocale) {
104            bundleByBaseNameByLocale = bundleByClassLoaderByBaseNameByLocale.get(pLoader);
105            if (bundleByBaseNameByLocale == null) {
106                bundleByBaseNameByLocale = new HashMap<String, Map<Locale, ResourceBundle>>();
107                bundleByClassLoaderByBaseNameByLocale.put(pLoader, bundleByBaseNameByLocale);
108            }
109        }
110
111        synchronized (bundleByBaseNameByLocale) {
112            bundleByLocale = bundleByBaseNameByLocale.get(pBaseName);
113            if (bundleByLocale == null) {
114                bundleByLocale = new HashMap<Locale, ResourceBundle>();
115                bundleByBaseNameByLocale.put(pBaseName, bundleByLocale);
116            }
117        }
118
119        synchronized (bundleByLocale) {
120            bundle = bundleByLocale.get(pLocale);
121            if (bundle == null) {
122                bundle = ResourceBundle.getBundle(pBaseName, pLocale);
123                bundle = createUtfPropertyResourceBundle(bundle);
124                bundleByLocale.put(pLocale, bundle);
125            }
126        }
127
128        return bundle;
129    }
130
131    private static ResourceBundle createUtfPropertyResourceBundle(ResourceBundle pBundle) {
132        if (!(pBundle instanceof PropertyResourceBundle)) {
133            Log.Debug(ResourceBundleUtf8.class, "only UTF- property files are supported");
134        }
135
136        return new PropertyResourceBundleUtf((PropertyResourceBundle) pBundle);
137    }
138}