From 2cce3dcb72c55aa5cca66e1398f23906e286abc8 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Mon, 13 Feb 2017 00:27:27 +0100 Subject: [PATCH] Fix resource bundle bug, is now 1.6+ only --- Makefile.base | 4 +- VERSION | 2 +- changelog | 8 +++ src/be/nikiroo/utils/resources/Bundle.java | 23 ++----- .../resources/FixedResourceBundleControl.java | 60 +++++++++++++++++++ 5 files changed, 76 insertions(+), 21 deletions(-) create mode 100644 src/be/nikiroo/utils/resources/FixedResourceBundleControl.java diff --git a/Makefile.base b/Makefile.base index 9396588..559b89d 100644 --- a/Makefile.base +++ b/Makefile.base @@ -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 a602fc9..b0bb878 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.9.4 +0.9.5 diff --git a/changelog b/changelog index 9f3dde5..5299155 100644 --- 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 ------------- diff --git a/src/be/nikiroo/utils/resources/Bundle.java b/src/be/nikiroo/utils/resources/Bundle.java index 1c63d69..78d73a8 100644 --- a/src/be/nikiroo/utils/resources/Bundle.java +++ b/src/be/nikiroo/utils/resources/Bundle.java @@ -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> { } /** - * 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> { 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 index 0000000..30c5889 --- /dev/null +++ b/src/be/nikiroo/utils/resources/FixedResourceBundleControl.java @@ -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 -- 2.27.0