xv.text
Class NLS

java.lang.Object
  extended by xv.text.NLS

public class NLS
extends Object

This class is based on the Eclipse Native Language Support system. It does not use ResourceBundle, instead using PropertiesReader to parse the appropriate properties. The great thing about this class versus ResourceBundle is that it allows you to specify the character encoding, so you can use property files written in UTF-8 and not ISO 8859-1.

This reduces the amount of memory required.

The one side-effect is that the first property set takes precedence. So if you set a property multiple times in a .properties file, only the first value takes effect while with a ResourceBundle only the last value will take effect.

Of course, there's little reason to set the same property multiple times in a .properties file, so this shouldn't come up too much, but be aware. Do note that certain keys are considered equivalent. For example, "foo.bar" is treated the same as "foo_bar".

Author:
xv

Constructor Summary
NLS()
           
 
Method Summary
static String format(String format, Object... arguments)
          Formats a string.
static void initialize(Class<?> cls, String resource)
          Initializes the given class, setting all public static String fields that are non-final to the properties loaded from a set of properties files, using the default locale.
static void initialize(Class<?> cls, String resource, Locale locale)
          Initializes the given class, setting all public static String fields that are non-final to the properties loaded from a set of properties files.
static void initialize(Class<?> cls, String resource, Locale locale, String encoding)
          Initializes the given class, setting all public static String fields that are non-final to the properties loaded from a set of properties files.
static HashMap<String,String> loadLocaleProperties(URL base)
          Loads a map of strings to strings using a PropertiesReader, handling overrides in the same manner as initialize(Class, String, Locale).
static HashMap<String,String> loadLocaleProperties(URL base, Locale locale, String encoding)
          Loads a map of strings to strings using a PropertiesReader, handling overrides in the same manner as initialize(Class, String, Locale).
static HashMap<String,String> loadLocaleProperties(URL base, String encoding)
          Loads a map of strings to strings using a PropertiesReader, handling overrides in the same manner as initialize(Class, String, Locale).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NLS

public NLS()
Method Detail

initialize

public static void initialize(Class<?> cls,
                              String resource)
Initializes the given class, setting all public static String fields that are non-final to the properties loaded from a set of properties files, using the default locale.

This class follows the same load order as ResourceBundle.getBundle(String, Locale, ClassLoader).

There are some important differences. The first is that this implementation only sets the first value found in a given Properties file, while ResourceBundle will set the last value found in a given Properties file.

The second is that various keys are treated the same. '.' are replaced with '_' in keys found. So assuming the given .properties file:

foo.bar = one
foo_bar = two
foo.bar = three
And the following class:
public class Example {
    public static String foo_bar;
}
Then the field foo_bar will be set to "one" once this method completes.

Parameters:
cls - the class to set properties in
resource - the name of the resource, as a classname similar to what ResourceBundle.getBundle(String) takes

initialize

public static void initialize(Class<?> cls,
                              String resource,
                              Locale locale)
Initializes the given class, setting all public static String fields that are non-final to the properties loaded from a set of properties files.

This class follows the same load order as ResourceBundle.getBundle(String, Locale, ClassLoader).

There are some important differences. The first is that this implementation only sets the first value found in a given Properties file, while ResourceBundle will set the last value found in a given Properties file.

The second is that various keys are treated the same. '.' are replaced with '_' in keys found. So assuming the given .properties file:

foo.bar = one
foo_bar = two
foo.bar = three
And the following class:
public class Example {
    public static String foo_bar;
}
Then the field foo_bar will be set to "one" once this method completes.

Parameters:
cls - the class to set properties in
resource - the name of the resource, as a classname similar to what ResourceBundle.getBundle(String) takes
locale - the locale to load from

initialize

public static void initialize(Class<?> cls,
                              String resource,
                              Locale locale,
                              String encoding)
Initializes the given class, setting all public static String fields that are non-final to the properties loaded from a set of properties files.

This class follows the same load order as ResourceBundle.getBundle(String, Locale, ClassLoader).

There are some important differences. The first is that this implementation only sets the first value found in a given Properties file, while ResourceBundle will set the last value found in a given Properties file.

The second is that various keys are treated the same. '.' are replaced with '_' in keys found. So assuming the given .properties file:

foo.bar = one
foo_bar = two
foo.bar = three
And the following class:
public class Example {
    public static String foo_bar;
}
Then the field foo_bar will be set to "one" once this method completes.

Parameters:
cls - the class to set properties in
resource - the name of the resource, as a classname similar to what ResourceBundle.getBundle(String) takes
locale - the locale to load from
encoding - the encoding to use - if null, defaults to ISO 8859-1 if null

format

public static String format(String format,
                            Object... arguments)
Formats a string. This will substitute items similar to the way that MessageFormat does, minus supporting any other formats. Essentially this is a simple substitution. Each instance of "{n}", where n is any number greater than or equal to 0, is replaced with the Object.toString() result of the given argument. If there is no corresponding argument, an empty string is substituted.

Parameters:
format - the string containing the format
arguments - the objects that specify the format
Returns:
the formatted string

loadLocaleProperties

public static HashMap<String,String> loadLocaleProperties(URL base)
                                                   throws IOException
Loads a map of strings to strings using a PropertiesReader, handling overrides in the same manner as initialize(Class, String, Locale). Unlike initialize(Class, String, Locale), this method does not "munge" keys (foo_bar and foo.bar are unique), but it does follow the same "first value found sets the property."

Sometimes it turns out to be easier to just deal with properties than fields... but the cost is that you have to deal with exceptions on your own.

Parameters:
base - the base URL to the given resource
Returns:
Throws:
IOException - if an I/O error occurs

loadLocaleProperties

public static HashMap<String,String> loadLocaleProperties(URL base,
                                                          String encoding)
                                                   throws IOException
Loads a map of strings to strings using a PropertiesReader, handling overrides in the same manner as initialize(Class, String, Locale). Unlike initialize(Class, String, Locale), this method does not "munge" keys (foo_bar and foo.bar are unique), but it does follow the same "first value found sets the property."

Sometimes it turns out to be easier to just deal with properties than fields... but the cost is that you have to deal with exceptions on your own.

Parameters:
base - the base URL to the given resource
encoding - the character encoding to use, or null to use whatever the resource says to use or ISO 8859-1 if the resource doesn't specify
Returns:
Throws:
IOException - if an I/O error occurs

loadLocaleProperties

public static HashMap<String,String> loadLocaleProperties(URL base,
                                                          Locale locale,
                                                          String encoding)
                                                   throws IOException
Loads a map of strings to strings using a PropertiesReader, handling overrides in the same manner as initialize(Class, String, Locale). Unlike initialize(Class, String, Locale), this method does not "munge" keys (foo_bar and foo.bar are unique), but it does follow the same "first value found sets the property."

Sometimes it turns out to be easier to just deal with properties than fields... but the cost is that you have to deal with exceptions on your own.

Parameters:
base - the base URL to the given resource
locale -
encoding - the character encoding to use, or null to use whatever the resource says to use or ISO 8859-1 if the resource doesn't specify
Returns:
Throws:
IOException - if an I/O error occurs


Copyright © 2008-2011. All Rights Reserved.