Fix resource bundle bug, is now 1.6+ only
authorNiki Roo <niki@nikiroo.be>
Sun, 12 Feb 2017 23:27:27 +0000 (00:27 +0100)
committerNiki Roo <niki@nikiroo.be>
Sun, 12 Feb 2017 23:27:27 +0000 (00:27 +0100)
Makefile.base
VERSION
changelog
src/be/nikiroo/utils/resources/Bundle.java
src/be/nikiroo/utils/resources/FixedResourceBundleControl.java [new file with mode: 0644]

index 939658815593b027f05dd2a522a26fdf186327c1..559b89d9020a53df2b6212600233c68cd88841a2 100644 (file)
@@ -9,7 +9,7 @@
 #SJAR_FLAGS += a list of things to pack, each usually prefixed with "-C src/", for *-sources.jar files
 
 JAVAC = javac
-JAVAC_FLAGS += -encoding UTF-8 -d ./bin/ -cp ./src/ -Xdiags:verbose
+JAVAC_FLAGS += -encoding UTF-8 -d ./bin/ -cp ./src/
 JAVA = java
 JAVA_FLAGS += -cp ./bin/
 JAR = jar
@@ -142,6 +142,6 @@ install:
        mkdir -p "$(PREFIX)/lib" "$(PREFIX)/bin"
        cp $(NAME).jar "$(PREFIX)/lib/"
        echo "#!/bin/sh" > "$(PREFIX)/bin/$(NAME)"
-       echo "$(RJAR) $(RJAR_FLAGS) \"$(PREFIX)/lib/$(NAME).jar\" \"$$@\"" >> "$(PREFIX)/bin/$(NAME)"
+       echo "$(RJAR) $(RJAR_FLAGS) \"$(PREFIX)/lib/$(NAME).jar\" \"\$$@\"" >> "$(PREFIX)/bin/$(NAME)"
        chmod a+rx "$(PREFIX)/bin/$(NAME)"
 
diff --git a/VERSION b/VERSION
index a602fc9e283389306d606c164ce73ac338999373..b0bb878545dc6dc410a02b0df8b7ea9fd5705960 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-0.9.4
+0.9.5
index 9f3dde5ce525da028ddcdec82f507088ea58538b..52991559e4d9884165631e6e0a4f6e5a04008eb5 100644 (file)
--- a/changelog
+++ b/changelog
@@ -1,3 +1,11 @@
+Version 0.9.5
+-------------
+
+Resource bundle bug
+       UTF-8 strings were sometimes wrangled
+       It is fixed by using a Bundle#Control, whih sadly is only available in
+       Java 1.6+
+
 Version 0.9.4
 -------------
 
index 1c63d69f04f84276eeaf19ee3a29e028f52fae54..78d73a8148088619180caa59aa50fffcc2d50d40 100644 (file)
@@ -8,7 +8,6 @@ import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.io.Reader;
-import java.io.UnsupportedEncodingException;
 import java.io.Writer;
 import java.lang.reflect.Field;
 import java.util.ArrayList;
@@ -18,9 +17,6 @@ import java.util.MissingResourceException;
 import java.util.PropertyResourceBundle;
 import java.util.ResourceBundle;
 
-import be.nikiroo.utils.resources.Bundles;
-import be.nikiroo.utils.resources.Meta;
-
 /**
  * This class encapsulate a {@link ResourceBundle} in UTF-8. It only allows to
  * retrieve values associated to an enumeration, and allows some additional
@@ -241,26 +237,17 @@ public class Bundle<E extends Enum<E>> {
        }
 
        /**
-        * Get the value for the given key if it exists in the internal map.
+        * Get the value for the given key if it exists in the internal map, or NULL
+        * if not.
         * 
         * @param key
         *            the key to check for
         * 
-        * @return true if it does
+        * @return the value, or NULL
         */
        protected String getString(String key) {
                if (containsKey(key)) {
-                       try {
-                               // Note: it is also possible to fix the borked charset issue
-                               // with a custom ResourceBundle#Control class, but this one,
-                               // while a workaround, depend less upon the JRE classes, which
-                               // may change
-                               return new String(map.getString(key).getBytes("ISO-8859-1"),
-                                               "UTF-8");
-                       } catch (UnsupportedEncodingException e) {
-                               // Those 2 encodings are always supported
-                               e.printStackTrace();
-                       }
+                       return map.getString(key);
                }
 
                return null;
@@ -432,7 +419,7 @@ public class Bundle<E extends Enum<E>> {
 
                if (map == null) {
                        map = ResourceBundle.getBundle(type.getPackage().getName() + "."
-                                       + name.name(), locale);
+                                       + name.name(), locale, new FixedResourceBundleControl());
                }
        }
 
diff --git a/src/be/nikiroo/utils/resources/FixedResourceBundleControl.java b/src/be/nikiroo/utils/resources/FixedResourceBundleControl.java
new file mode 100644 (file)
index 0000000..30c5889
--- /dev/null
@@ -0,0 +1,60 @@
+package be.nikiroo.utils.resources;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Locale;
+import java.util.PropertyResourceBundle;
+import java.util.ResourceBundle;
+import java.util.ResourceBundle.Control;
+
+/**
+ * Fixed ResourceBundle.Control class. It will use UTF-8 for the files to load.
+ * 
+ * Also support an option to first check into the given path before looking into
+ * the resources.
+ * 
+ * @author niki
+ *
+ */
+class FixedResourceBundleControl extends Control {
+       @Override
+       public ResourceBundle newBundle(String baseName, Locale locale,
+                       String format, ClassLoader loader, boolean reload)
+                       throws IllegalAccessException, InstantiationException, IOException {
+               // The below is a copy of the default implementation.
+               String bundleName = toBundleName(baseName, locale);
+               String resourceName = toResourceName(bundleName, "properties");
+
+               ResourceBundle bundle = null;
+               InputStream stream = null;
+               if (reload) {
+                       URL url = loader.getResource(resourceName);
+                       if (url != null) {
+                               URLConnection connection = url.openConnection();
+                               if (connection != null) {
+                                       connection.setUseCaches(false);
+                                       stream = connection.getInputStream();
+                               }
+                       }
+               } else {
+                       if (stream == null)
+                               stream = loader.getResourceAsStream(resourceName);
+               }
+               if (stream != null) {
+                       try {
+                               // This line is changed to make it to read properties files
+                               // as UTF-8.
+                               // How can someone use an archaic encoding such as ISO 8859-1 by
+                               // *DEFAULT* is beyond me...
+                               bundle = new PropertyResourceBundle(new InputStreamReader(
+                                               stream, "UTF-8"));
+                       } finally {
+                               stream.close();
+                       }
+               }
+               return bundle;
+       }
+}
\ No newline at end of file