-
Notifications
You must be signed in to change notification settings - Fork 152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
buxfix(i18n): read utf8 resource if needed. #190 #212
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package org.mamute.i18n; | ||
/* https://gist.github.com/Hasacz89/d93955ec91afc73a06e3 */ | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
import java.security.AccessController; | ||
import java.security.PrivilegedActionException; | ||
import java.security.PrivilegedExceptionAction; | ||
import java.util.Locale; | ||
import java.util.PropertyResourceBundle; | ||
import java.util.ResourceBundle; | ||
|
||
public class UTF8Control extends ResourceBundle.Control { | ||
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); | ||
ResourceBundle bundle = null; | ||
switch (format) { | ||
case "java.class": | ||
try { | ||
@SuppressWarnings("unchecked") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code is quite complex also, maybe we should extract two classes here, something like: JavaClassResourceLoader and JavaPropertiesResourceLoader, what do you think? |
||
Class<? extends ResourceBundle> bundleClass | ||
= (Class<? extends ResourceBundle>) loader.loadClass(bundleName); | ||
|
||
// If the class isn't a ResourceBundle subclass, throw a | ||
// ClassCastException. | ||
if (ResourceBundle.class.isAssignableFrom(bundleClass)) { | ||
bundle = bundleClass.newInstance(); | ||
} else { | ||
throw new ClassCastException(bundleClass.getName() | ||
+ " cannot be cast to ResourceBundle"); | ||
} | ||
} catch (ClassNotFoundException e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't we at least log this exception? |
||
} | ||
|
||
break; | ||
case "java.properties": | ||
final String resourceName = toResourceName(bundleName, "properties"); | ||
final ClassLoader classLoader = loader; | ||
final boolean reloadFlag = reload; | ||
InputStream stream; | ||
try { | ||
stream = AccessController.doPrivileged( | ||
new PrivilegedExceptionAction<InputStream>() { | ||
public InputStream run() throws IOException { | ||
InputStream is = null; | ||
if (reloadFlag) { | ||
URL url = classLoader.getResource(resourceName); | ||
if (url != null) { | ||
URLConnection connection = url.openConnection(); | ||
if (connection != null) { | ||
// Disable caches to get fresh data for | ||
// reloading. | ||
connection.setUseCaches(false); | ||
is = connection.getInputStream(); | ||
} | ||
} | ||
} else { | ||
is = classLoader.getResourceAsStream(resourceName); | ||
} | ||
return is; | ||
} | ||
}); | ||
} catch (PrivilegedActionException e) { | ||
throw (IOException) e.getException(); | ||
} | ||
if (stream != null) { | ||
try { | ||
bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8")); | ||
} finally { | ||
stream.close(); | ||
} | ||
} | ||
|
||
break; | ||
default: | ||
throw new IllegalArgumentException("unknown format: " + format); | ||
} | ||
return bundle; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this code is quite big and complex, I think we should extract it to a separate class to be able to unit test it easier, what do you think @khajavi?