ilog.views.util
Class IlvResourceUtil

java.lang.Object
  extended by ilog.views.util.IlvResourceUtil

public class IlvResourceUtil
extends Object

Utilities for resource bundles.

Use of this class, instead of ResourceBundle.getBundle, allows applets loaded over http to perform only a minimal number of http requests (assuming the applet's main code uses the setAvailableResourceSuffixes(java.lang.String...) method to specify which locale is supported by the applet's resources).

The second idea here is not to hard code resource bundle paths. Instead, we pass the corresponding class. The advantage: if classes are moved, the resource bundle path is also up to date.

For example:

     String s = IlvResourceUtil.getString(key, Myclass.class);
 
It is not recommended to use this utility with an implicit class reference such as
     String s = IlvResourceUtil.getString(key, getClass());
 
because this variant works only if all subclasses of the current class are in the same package.

Since:
JViews 8.1

Method Summary
static ResourceBundle getBundle(String baseName)
          Returns a resource bundle, examining the available resource suffixes.
static ResourceBundle getBundle(String name, Class clazz)
          Returns the resource bundle in the package of the input class.
static ResourceBundle getBundle(String name, Class clazz, Locale locale)
          Returns the resource bundle in the package of the input class.
static ResourceBundle getBundle(String baseName, Locale locale)
          Returns a resource bundle, examining the available resource suffixes.
static ResourceBundle getBundle(String baseName, Locale locale, ClassLoader loader)
          Returns a resource bundle, examining the available resource suffixes.
static String getSafeString(String key, Class clazz)
          Returns a string for the given key from the resource bundle named "messages" in the package of the input class.
static String getSafeString(String key, Class clazz, Locale locale)
          Returns a string for the given key from the resource bundle named "messages" in the package of the input class.
static String getSafeString(String key, String bundle, Class clazz)
          Returns a string for the given key from the resource bundle in the package of the input class.
static String getSafeString(String key, String bundle, Class clazz, Locale locale)
          Returns a string for the given key from the resource bundle in the package of the input class.
static String getString(String key, Class clazz)
          Returns a string for the given key from the resource bundle named "messages" in the package of the input class.
static String getString(String key, Class clazz, Locale locale)
          Returns a string for the given key from the resource bundle named "messages" in the package of the input class.
static String getString(String key, Class clazz, Locale locale, String defString)
          Returns a string for the given key from the resource bundle named "messages" in the package of the input class.
static String getString(String key, Class clazz, String defString)
          Returns a string for the given key from the resource bundle named "messages" in the package of the input class.
static String getString(String key, String bundle, Class clazz)
          Returns a string for the given key from the resource bundle in the package of the input class.
static String getString(String key, String bundle, Class clazz, Locale locale)
          Returns a string for the given key from the resource bundle in the package of the input class.
static String getString(String key, String bundle, Class clazz, Locale locale, String defString)
          Returns a string for the given key from the resource bundle in the package of the input class.
static String getString(String key, String bundle, Class clazz, String defString)
          Returns a string for the given key from the resource bundle in the package of the input class.
static void setAvailableResourceSuffixes(String... suffixes)
          Sets the available resource suffixes.
static void setDebug(int debug)
          Sets the debugging mode.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getString

public static String getString(String key,
                               Class clazz)
Returns a string for the given key from the resource bundle named "messages" in the package of the input class. The fully qualified name is determined by concatenating the path of the class and the string "messages". The class loader is determined from the input class. The default locale is used.

Parameters:
key - The key of the resource string.
clazz - The input class, to determine the package name and the class loader.
Returns:
the string for the given key
Throws:
MissingResourceException - if no string for the given key can be found

getString

public static String getString(String key,
                               Class clazz,
                               Locale locale)
Returns a string for the given key from the resource bundle named "messages" in the package of the input class. The fully qualified name is determined by concatenating the path of the class and the string "messages". The class loader is determined from the input class.

Parameters:
key - The key of the resource string.
clazz - The input class, to determine the package name and the class loader.
locale - The locale.
Returns:
the string for the given key
Throws:
MissingResourceException - if no string for the given key can be found

getString

public static String getString(String key,
                               String bundle,
                               Class clazz)
Returns a string for the given key from the resource bundle in the package of the input class. The fully qualified name is determined by concatenating the path of the class and the bundle name. The class loader is determined from the input class. The default locale is used.

Parameters:
key - The key of the resource string.
bundle - The base name of the resource bundle excluding the package name.
clazz - The input class, to determine the package name and the class loader.
Returns:
the string for the given key
Throws:
MissingResourceException - if no string for the given key can be found

getString

public static String getString(String key,
                               String bundle,
                               Class clazz,
                               Locale locale)
Returns a string for the given key from the resource bundle in the package of the input class. The fully qualified name is determined by concatenating the path of the class and the bundle name. The class loader is determined from the input class.

Parameters:
key - The key of the resource string.
bundle - The base name of the resource bundle excluding the package name.
clazz - The input class, to determine the package name and the class loader.
locale - The locale.
Returns:
the string for the given key
Throws:
MissingResourceException - if no string for the given key can be found

getString

public static String getString(String key,
                               Class clazz,
                               String defString)
Returns a string for the given key from the resource bundle named "messages" in the package of the input class. The fully qualified name is determined by concatenating the path of the class and the string "messages". The class loader is determined from the input class. The default locale is used.

This variant does not throw missing resource exceptions. If the resource is missing, the default string is returned instead.

Parameters:
key - The key of the resource string.
clazz - The input class, to determine the package name and the class loader.
defString - If the resource bundle is not found or no string for the given key can be found, this default string is returned.
Returns:
the string for the given key

getString

public static String getString(String key,
                               Class clazz,
                               Locale locale,
                               String defString)
Returns a string for the given key from the resource bundle named "messages" in the package of the input class. The fully qualified name is determined by concatenating the path of the class and the string "messages". The class loader is determined from the input class.

This variant does not throw missing resource exceptions. If the resource is missing, the default string is returned instead.

Parameters:
key - The key of the resource string.
clazz - The input class, to determine the package name and the class loader.
locale - The locale.
defString - If the resource bundle is not found or no string for the given key can be found, this default string is returned.
Returns:
the string for the given key

getString

public static String getString(String key,
                               String bundle,
                               Class clazz,
                               String defString)
Returns a string for the given key from the resource bundle in the package of the input class. The fully qualified name is determined by concatenating the path of the class and the bundle name. The class loader is determined from the input class. The default locale is used.

This variant does not throw missing resource exceptions. If the resource is missing, the default string is returned instead.

Parameters:
key - The key of the resource string.
bundle - The base name of the resource bundle excluding the package name.
clazz - The input class, to determine the package name and the class loader.
defString - If the resource bundle is not found or no string for the given key can be found, this default string is returned.
Returns:
the string for the given key

getString

public static String getString(String key,
                               String bundle,
                               Class clazz,
                               Locale locale,
                               String defString)
Returns a string for the given key from the resource bundle in the package of the input class. The fully qualified name is determined by concatenating the path of the class and the bundle name. The class loader is determined from the input class.

This variant does not throw missing resource exceptions. If the resource is missing, the default string is returned instead.

Parameters:
key - The key of the resource string.
bundle - The base name of the resource bundle excluding the package name.
clazz - The input class, to determine the package name and the class loader.
locale - The locale.
defString - If the resource bundle is not found or no string for the given key can be found, this default string is returned.
Returns:
the string for the given key

getSafeString

public static String getSafeString(String key,
                                   Class clazz)
Returns a string for the given key from the resource bundle named "messages" in the package of the input class. The fully qualified name is determined by concatenating the path of the class and the string "messages". The class loader is determined from the input class. The default locale is used.

This variant does not throw missing resource exceptions. If the resource is missing, the key is returned instead.

Parameters:
key - The key of the resource string.
clazz - The input class, to determine the package name and the class loader.
Returns:
the string for the given key

getSafeString

public static String getSafeString(String key,
                                   Class clazz,
                                   Locale locale)
Returns a string for the given key from the resource bundle named "messages" in the package of the input class. The fully qualified name is determined by concatenating the path of the class and the string "messages". The class loader is determined from the input class.

This variant does not throw missing resource exceptions. If the resource is missing, the key is returned instead.

Parameters:
key - The key of the resource string.
clazz - The input class, to determine the package name and the class loader.
locale - The locale.
Returns:
the string for the given key

getSafeString

public static String getSafeString(String key,
                                   String bundle,
                                   Class clazz)
Returns a string for the given key from the resource bundle in the package of the input class. The fully qualified name is determined by concatenating the path of the class and the bundle name. The class loader is determined from the input class. The default locale is used.

This variant does not throw missing resource exceptions. If the resource is missing, the key is returned instead.

Parameters:
key - The key of the resource string.
bundle - The base name of the resource bundle excluding the package name.
clazz - The input class, to determine the package name and the class loader.
Returns:
the string for the given key

getSafeString

public static String getSafeString(String key,
                                   String bundle,
                                   Class clazz,
                                   Locale locale)
Returns a string for the given key from the resource bundle in the package of the input class. The fully qualified name is determined by concatenating the path of the class and the bundle name. The class loader is determined from the input class.

This variant does not throw missing resource exceptions. If the resource is missing, the key is returned instead.

Parameters:
key - The key of the resource string.
bundle - The base name of the resource bundle excluding the package name.
clazz - The input class, to determine the package name and the class loader.
locale - The locale.
Returns:
the string for the given key

getBundle

public static ResourceBundle getBundle(String name,
                                       Class clazz)
Returns the resource bundle in the package of the input class. The fully qualified name is determined by concatenating the path of the class and the input name. The class loader is determined from the input class. The default locale is used.

Parameters:
name - The base name of the resource bundle excluding the package name.
clazz - The input class, to determine the package name and the class loader.

getBundle

public static ResourceBundle getBundle(String name,
                                       Class clazz,
                                       Locale locale)
Returns the resource bundle in the package of the input class. The fully qualified name is determined by concatenating the path of the class and the input name. The class loader is determined from the input class.

Parameters:
name - The base name of the resource bundle excluding the package name.
clazz - The input class, to determine the package name and the class loader.
locale - The locale.

setAvailableResourceSuffixes

public static void setAvailableResourceSuffixes(String... suffixes)
Sets the available resource suffixes. This is useful for applets that don't provide resources for all possible locales. Normally, if a resource is requested for a locale that is not available, several unsuccessful requests are made to the server to load the resource. This slows down the applet. When setting the available resource suffixes, the requests are avoided for those locales that don't match the available suffixes, and instead the resource bundle for the best matching default locale is loaded. This speeds up the applet.

The following sample shows how to use the API:

 IlvResourceUtil.setAvailableResourceSuffixes("", "_fr", "_de" );
 
This statement specifies that the default bundle (""), the generic French bundle ("_fr") and the generic German bundle ("_de") are available. If the locale is currently Canadian French, it will not try to load the bundle "_fr_CA" but immediately use the bundle "fr" instead.

In order to set back to the default behavior, call:

 IlvResourceUtil.setAvailableResourceSuffixes(null);
 

Suffixes have the following formats:

The language part is a valid ISO Language Code. These codes are the lower-case, two-letter codes as defined by ISO-639. The country part is a valid ISO Country Code. These codes are the upper-case, two-letter codes as defined by ISO-3166. The variant argument is a vendor or browser-specific code. For example, use WIN for Windows, MAC for Macintosh, and POSIX for POSIX.


setDebug

public static void setDebug(int debug)
Sets the debugging mode. If the debugging mode is enabled, and available resource suffixes are set, it reports all failures of resource bundle loadings. The mode options are:


getBundle

public static ResourceBundle getBundle(String baseName,
                                       Locale locale,
                                       ClassLoader loader)
                                throws MissingResourceException
Returns a resource bundle, examining the available resource suffixes.

Parameters:
baseName - The base name of the resource bundle including the package name.
locale - The locale.
loader - The class loader to load the bundle.
Throws:
MissingResourceException

getBundle

public static ResourceBundle getBundle(String baseName,
                                       Locale locale)
                                throws MissingResourceException
Returns a resource bundle, examining the available resource suffixes.

Parameters:
baseName - The base name of the resource bundle including the package name.
locale - The locale.
Throws:
MissingResourceException

getBundle

public static ResourceBundle getBundle(String baseName)
Returns a resource bundle, examining the available resource suffixes. It returns the bundle for the default locale.

Parameters:
baseName - The base name of the resource bundle including the package name.


Copyright © 1996-2007 ILOG S.A. All rights reserved.   Documentation homepage.   . All Rights Reserved.