com.taco.util
Class ResourceBundleFactory

java.lang.Object
  extended by com.taco.util.AbstractResourceBundleFactory
      extended by com.taco.util.ResourceBundleFactory
All Implemented Interfaces:
IResourceBundleFactory, java.io.Serializable, java.lang.Cloneable

public class ResourceBundleFactory
extends AbstractResourceBundleFactory
implements java.lang.Cloneable, java.io.Serializable

A concrete implementation of IResourceBundleFactory. This implementation supports resource bundles in the form of .class files for subclasses of AccessibleResourceBundle and .properties files which are interpreted by instances of MultiLineProperties.

See Also:
Serialized Form

Field Summary
protected static java.util.regex.Pattern _BUNDLE_NAME_PATTERN
          The regex pattern compiled from _BUNDLE_NAME_REGEX_STRING.
protected static java.lang.String _BUNDLE_NAME_REGEX_STRING
          A regex string for a bundle name.
static ResourceBundleFactory DEFAULT_INSTANCE
          A well-known instance of this class.
 
Constructor Summary
ResourceBundleFactory()
           
 
Method Summary
protected  java.util.List _computeBundleNameCandidates(java.lang.String baseName, java.util.Locale locale)
          Build a list of candidate bundle names for the given locale, in the following order: baseName + "_" + language baseName + "_" + language + "_" + country baseName + "_" + language + "_" + country + "_" + variant Candidate bundle names where the final component is an empty string are omitted.
protected  java.util.Locale _computeLocaleForBundleName(java.lang.String bundleName)
          Parse the name of a bundle to get the locale it corresponds to.
protected  AccessibleResourceBundle _loadOrphanBundle(java.lang.String bundleName, java.util.Locale defaultLocale, java.lang.ClassLoader loader)
          This method is called when it has been determined that a resource bundle is not in the cache, and we want to load the bundle from the system.
protected  AccessibleResourceBundle _loadOrphanBundleFromClass(java.lang.String bundleName, java.util.Locale defaultLocale, java.lang.ClassLoader loader)
          Load a bundle from a .class file.
protected  AccessibleResourceBundle _loadOrphanBundleFromProperties(java.lang.String bundleName, java.lang.ClassLoader loader)
          Load an instance of MultiLinePropertyResourceBundle based on a resource with the bundle name as the main part of its filename, and ".properties" as its extension.
protected  AccessibleResourceBundle _loadOrphanBundleFromUIManager(java.lang.String bundleName)
          Load a bundle based on the UIManager.
protected  AccessibleResourceBundle _lookupBundleInCache(java.lang.String bundleName, java.util.Locale defaultLocale, java.lang.ClassLoader loader)
          Look in the bundle cache to see if a bundle for the arguments has been cached.
 void cleanBundleCache()
          To save space, remove entries from the bundle cache whose keys have cleared references to class loaders or whose values are cleared references to resource bundles.
 java.lang.Object clone()
          Make a clone of this factory.
 java.util.ResourceBundle getBundle(java.lang.String baseName, java.util.Locale locale, java.lang.ClassLoader loader, java.util.Locale defaultLocale)
          Get the bundle using the argument basename, locale, and class loader.
 void invalidateBundles(java.lang.String baseName, java.util.Locale locale, java.lang.ClassLoader loader, java.util.Locale defaultLocale)
          If resource bundles are cached, ensure that calls to getBundle() will reload the bundles corresponding to the argument basename, locale, and class loader.
static void main(java.lang.String[] args)
          This simple test program prints all mappings in a resource bundle.
static void usage()
          Print usage information for main().
 
Methods inherited from class com.taco.util.AbstractResourceBundleFactory
getBundle, getBundle, getBundle, getBundle, invalidateBundles, invalidateBundles, invalidateBundles
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_BUNDLE_NAME_REGEX_STRING

protected static final java.lang.String _BUNDLE_NAME_REGEX_STRING
A regex string for a bundle name. The following groups are captured:
  1. The base name.
  2. The language code (may be null).
  3. The country code (may be null).
  4. The variant (may be null).

See Also:
Constant Field Values

_BUNDLE_NAME_PATTERN

protected static final java.util.regex.Pattern _BUNDLE_NAME_PATTERN
The regex pattern compiled from _BUNDLE_NAME_REGEX_STRING.


DEFAULT_INSTANCE

public static final ResourceBundleFactory DEFAULT_INSTANCE
A well-known instance of this class.

Constructor Detail

ResourceBundleFactory

public ResourceBundleFactory()
Method Detail

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Make a clone of this factory. The clone has an empty cache.

Overrides:
clone in class java.lang.Object
Throws:
java.lang.CloneNotSupportedException

getBundle

public final java.util.ResourceBundle getBundle(java.lang.String baseName,
                                                java.util.Locale locale,
                                                java.lang.ClassLoader loader,
                                                java.util.Locale defaultLocale)
                                         throws java.util.MissingResourceException
Description copied from interface: IResourceBundleFactory
Get the bundle using the argument basename, locale, and class loader. Note: the class loader may be null, to indicate the system class loader is to be used, or the caller may pass getClass().getClassLoader() to use the class loader of the caller's class.

Specified by:
getBundle in interface IResourceBundleFactory
Throws:
java.util.MissingResourceException

invalidateBundles

public void invalidateBundles(java.lang.String baseName,
                              java.util.Locale locale,
                              java.lang.ClassLoader loader,
                              java.util.Locale defaultLocale)
Description copied from interface: IResourceBundleFactory
If resource bundles are cached, ensure that calls to getBundle() will reload the bundles corresponding to the argument basename, locale, and class loader.

Specified by:
invalidateBundles in interface IResourceBundleFactory

cleanBundleCache

public void cleanBundleCache()
To save space, remove entries from the bundle cache whose keys have cleared references to class loaders or whose values are cleared references to resource bundles.


usage

public static void usage()
Print usage information for main().


main

public static void main(java.lang.String[] args)
This simple test program prints all mappings in a resource bundle.


_loadOrphanBundle

protected AccessibleResourceBundle _loadOrphanBundle(java.lang.String bundleName,
                                                     java.util.Locale defaultLocale,
                                                     java.lang.ClassLoader loader)
                                              throws java.util.MissingResourceException

This method is called when it has been determined that a resource bundle is not in the cache, and we want to load the bundle from the system. Return an instance of AccessibleResourceBundle whose parent can be set by this factory.

If a bundle cannot be loaded, throw a MissingResourceException.

This method may use several strategies for loading resource bundles and is intended to be overloaded in the case when a different implementation of resource bundle (particularly those that requiring run-time parsing) is added.

This base implementation tries to load resources bundles using the the following methods, in the order listed:

Throws:
java.util.MissingResourceException

_loadOrphanBundleFromClass

protected AccessibleResourceBundle _loadOrphanBundleFromClass(java.lang.String bundleName,
                                                              java.util.Locale defaultLocale,
                                                              java.lang.ClassLoader loader)
                                                       throws java.util.MissingResourceException
Load a bundle from a .class file.

Throws:
java.util.MissingResourceException

_loadOrphanBundleFromProperties

protected AccessibleResourceBundle _loadOrphanBundleFromProperties(java.lang.String bundleName,
                                                                   java.lang.ClassLoader loader)
                                                            throws java.util.MissingResourceException
Load an instance of MultiLinePropertyResourceBundle based on a resource with the bundle name as the main part of its filename, and ".properties" as its extension.

Throws:
java.util.MissingResourceException

_loadOrphanBundleFromUIManager

protected AccessibleResourceBundle _loadOrphanBundleFromUIManager(java.lang.String bundleName)
                                                           throws java.util.MissingResourceException
Load a bundle based on the UIManager.

Throws:
java.util.MissingResourceException

_computeLocaleForBundleName

protected java.util.Locale _computeLocaleForBundleName(java.lang.String bundleName)
Parse the name of a bundle to get the locale it corresponds to. If the name doesn't have any locale information or doesn't have a not a well-formed locale, return a locale with an empty language and country string.


_lookupBundleInCache

protected AccessibleResourceBundle _lookupBundleInCache(java.lang.String bundleName,
                                                        java.util.Locale defaultLocale,
                                                        java.lang.ClassLoader loader)
                                                 throws java.util.MissingResourceException
Look in the bundle cache to see if a bundle for the arguments has been cached. If so, it return it. Otherwise, throw a MissingResourceException. This method is suitable to be called by subclasses because it synchronizes with the bundle cache, which is a private member. It needs to be called whenever there is a possibility that a recursive call to getBundle() put a bundle in the cache that is in the process of being built. For example, a call to _loadOrphanBundleFromClass() uses reflection to instantiate a resource bundle. Since we have no idea what could be happening in the constructor of the bundle class, it's possible that it calls getBundle(), and a bundle was put into the cache. But we must be sure that every bundle returned by _loadOrphanBundle() must either be in the cache already or is not duplicated in the cache.

Throws:
java.util.MissingResourceException

_computeBundleNameCandidates

protected java.util.List _computeBundleNameCandidates(java.lang.String baseName,
                                                      java.util.Locale locale)
Build a list of candidate bundle names for the given locale, in the following order: Candidate bundle names where the final component is an empty string are omitted. Note that if the language or country is an empty string, strange names like "MyBundle_es__Mac OS X" may be in the returned list.