Customizing Resource Bundle Loading : ResourceBundle « I18N « Java Tutorial

import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;

public class XMLResourceBundleControl extends ResourceBundle.Control {
  private static String XML = "xml";

  public List<String> getFormats(String baseName) {
    return Collections.singletonList(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)) {
      return null;

    String bundleName = toBundleName(baseName, locale);
    String resourceName = toResourceName(bundleName, format);
    URL url = loader.getResource(resourceName);
    if (url == null) {
      return null;
    URLConnection connection = url.openConnection();
    if (connection == null) {
      return null;
    if (reload) {
    InputStream stream = connection.getInputStream();
    if (stream == null) {
      return null;
    BufferedInputStream bis = new BufferedInputStream(stream);
    bundle = new XMLResourceBundle(bis);

    return bundle;

  public static void main(String args[]) {
    ResourceBundle bundle = ResourceBundle.getBundle("Strings", new XMLResourceBundleControl());
    String string = bundle.getString("Key");
    System.out.println("Key: " + string);

class XMLResourceBundle extends ResourceBundle {
  private Properties props;

  XMLResourceBundle(InputStream stream) throws IOException {
    props = new Properties();

  protected Object handleGetObject(String key) {
    return props.getProperty(key);

  public Enumeration<String> getKeys() {
    Set<String> handleKeys = props.stringPropertyNames();
    return Collections.enumeration(handleKeys);

13.4.1.File name for java.util.ResourceBundle
13.4.2.Load resources via a resources file
13.4.3.Reading Properties Files using ResourceBundle
13.4.4.Convert ResourceBundle to Properties
13.4.5.Convert ResourceBundle to Map
13.4.6.An Internationalized Swing Application
13.4.7.JOptionPane Resources
13.4.8.Using the JDK 6 ResourceBundle class
13.4.9.Displaying Calendar Names
13.4.10.Customizing Resource Bundle Loading
13.4.11.XML resource bundle
13.4.12.Get resource bundle for a certain locale
13.4.13.Java file based resource bundle
13.4.14.ResourceBundle: avoid a performance penalty by superfluous resource (and classes loaded by Class.forName) lookups on web server in applets.