ResourceBundle rb = ResourceBundle.getBundle(“Messages“,
new ResourceBundle.Control() {
public List<String> getFormats(String baseName) {
if (baseName == null)
throw new NullPointerException();
return Arrays.asList(“xml”);
}
public ResourceBundle newBundle(String baseName,
Locale locale,
String format,
ClassLoader loader,
boolean reload)
throws IllegalAccessException,
InstantiationException,
IOException {
if (baseName null || locale null
|| format null || loader null)
throw new NullPointerException();
ResourceBundle bundle = null;
if (format.equals(“xml”)) {
String bundleName = toBundleName(baseName, locale);
String resourceName = toResourceName(bundleName, format);
InputStream stream = null;
if (reload) {
URL url = loader.getResource(resourceName);
if (url != null) {
URLConnection connection = url.openConnection();
if (connection != null) {
// Disable caches to get fresh data for
// reloading.
connection.setUseCaches(false);
stream = connection.getInputStream();
}
}
} else {
stream = loader.getResourceAsStream(resourceName);
}
if (stream != null) {
BufferedInputStream bis = new BufferedInputStream(stream);
bundle = new XMLResourceBundle(bis);
bis.close();
}
}
return bundle;
}
});
ResourceBundle.Controldefines a set of callback methods that are invoked by the ResourceBundle.getBundle factory methods during the bundle loading process. In other words, aResourceBundle.Controlcollaborates with the factory methods for loading resource bundles. The default implementation of the callback methods provides the information necessary for the factory methods to perform the default behavior.In addition to the callback methods, the toBundleName and toResourceName methods are defined primarily for convenience in implementing the callback methods. However, the
toBundleNamemethod could be overridden to provide different conventions in the organization and packaging of localized resources. ThetoResourceNamemethod isfinalto avoid use of wrong resource and class name separators.Two factory methods, getControl(List) and getNoFallbackControl(List) , provide
ResourceBundle.Controlinstances that implement common variations of the default bundle loading process.The formats returned by the getFormats method and candidate locales returned by the getCandidateLocales method must be consistent in all
ResourceBundle.getBundleinvocations for the same base bundle. Otherwise, theResourceBundle.getBundlemethods may return unintended bundles. For example, if only"java.class"is returned by thegetFormatsmethod for the first call toResourceBundle.getBundleand only"java.properties"for the second call, then the second call will return the class-based one that has been cached during the first call.A
ResourceBundle.Controlinstance must be thread-safe if it's simultaneously used by multiple threads.ResourceBundle.getBundledoes not synchronize to call theResourceBundle.Controlmethods. The default implementations of the methods are thread-safe.Applications can specify
ResourceBundle.Controlinstances returned by thegetControlfactory methods or created from a subclass ofResourceBundle.Controlto customize the bundle loading process. The following are examples of changing the default bundle loading process.Example 1
The following code lets
ResourceBundle.getBundlelook up only properties-based resources.import java.util.*; import static java.util.ResourceBundle.Control.*; ... ResourceBundle bundle = ResourceBundle.getBundle("MyResources", new Locale("fr", "CH"), ResourceBundle.Control.getControl(FORMAT_PROPERTIES));Given the resource bundles in the example in theResourceBundle.getBundledescription, thisResourceBundle.getBundlecall loadsMyResources_fr_CH.propertieswhose parent isMyResources_fr.propertieswhose parent isMyResources.properties. (MyResources_fr_CH.propertiesis not hidden, butMyResources_fr_CH.classis.)Example 2
The following is an example of loading XML-based bundles using Properties.loadFromXML .
ResourceBundle rb = ResourceBundle.getBundle("Messages", new ResourceBundle.Control() { public List<String> getFormats(String baseName) { if (baseName == null) throw new NullPointerException(); return Arrays.asList("xml"); } public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException, IOException { if (baseName == null || locale == null || format == null || loader == null) throw new NullPointerException(); ResourceBundle bundle = null; if (format.equals("xml")) { String bundleName = toBundleName(baseName, locale); String resourceName = toResourceName(bundleName, format); InputStream stream = null; if (reload) { URL url = loader.getResource(resourceName); if (url != null) { URLConnection connection = url.openConnection(); if (connection != null) { // Disable caches to get fresh data for // reloading. connection.setUseCaches(false); stream = connection.getInputStream(); } } } else { stream = loader.getResourceAsStream(resourceName); } if (stream != null) { BufferedInputStream bis = new BufferedInputStream(stream); bundle = new XMLResourceBundle(bis); bis.close(); } } return bundle; } }); ... private static class XMLResourceBundle extends ResourceBundle { private Properties props; XMLResourceBundle(InputStream stream) throws IOException { props = new Properties(); props.loadFromXML(stream); } protected Object handleGetObject(String key) { return props.getProperty(key); } public Enumeration<String> getKeys() { ... } }