XML resource bundle : ResourceBundle « I18N « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » I18N » ResourceBundle 
13.4.11.XML resource bundlePrevious/Next

The Strings.xml Resource Bundle

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="Key">Value</entry>
</properties> 


</code>    

        <code><![CDATA

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
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 reloadthrows 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) {
      connection.setUseCaches(false);
    }
    InputStream stream = connection.getInputStream();
    if (stream == null) {
      return null;
    }
    BufferedInputStream bis = new BufferedInputStream(stream);
    bundle = new XMLResourceBundle(bis);
    bis.close();

    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 streamthrows IOException {
    props = new Properties();
    props.loadFromXML(stream);
  }

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

  public Enumeration<String> getKeys() {
    Set<String> handleKeys = props.stringPropertyNames();
    return Collections.enumeration(handleKeys);
  }
}
13.4.ResourceBundle
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.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.