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