org.apache.commons.resources.impl
Class CollectionResourcesBase

java.lang.Object
  extended by org.apache.commons.resources.impl.ResourcesBase
      extended by org.apache.commons.resources.impl.CollectionResourcesBase
All Implemented Interfaces:
Serializable, Resources
Direct Known Subclasses:
JDBCResources, PropertyResources, WebappPropertyResources, WebappXMLResources, XMLResources

public abstract class CollectionResourcesBase
extends ResourcesBase

Abstract base classes for Resources implementations that store their name-value mappings for each supported Locale in a URL-accessible resource file with a common base URL. Subclasses need only override loadLocale() to manage the details of loading the name-value mappings for a particular Locale.

See Also:
Serialized Form

Constructor Summary
CollectionResourcesBase(String name, String base)
          Create a new Resources instance with the specified logical name and base URL.
 
Method Summary
 void destroy()
          This method must be called when the manager of this resource decides that it's no longer needed.
 Locale getDefaultLocale()
          Return the default locale.
 Iterator getKeys()
          Return an Iterator over the defined keys in this Resources instance.
protected  List getLocaleList(Locale locale)
          Return a List of Locales that should be searched when locating resources for the specified Locale.
protected  Map getLocaleMap(Locale locale)
          Return the Map to be used to resolve name-value mappings for the specified Locale.
protected abstract  Map getLocaleMap(String baseUrl, Locale locale)
          Return a Map containing the name-value mappings for the specified base URL and requested Locale, if there are any.
protected  String getLocaleSuffix(Locale locale)
          Return the Locale-specific suffix for the specified Locale.
 Object getObject(String key, Locale locale)
          Return the content for the specified key as an Object, localized based on the specified locale.
 void setDefaultLocale(Locale defaultLocale)
          Set the default locale.
 
Methods inherited from class org.apache.commons.resources.impl.ResourcesBase
getBufferSize, getBytes, getInputStream, getName, getReader, getString, init, isReturnNull, setBufferSize, setReturnNull
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CollectionResourcesBase

public CollectionResourcesBase(String name,
                               String base)

Create a new Resources instance with the specified logical name and base URL.

Parameters:
name - Logical name of the new instance
base - Base URL of the resource files that contain the per-Locale name-value mappings for this Resources instance
Method Detail

setDefaultLocale

public void setDefaultLocale(Locale defaultLocale)
Set the default locale.

Parameters:
defaultLocale - The default Locale.

getDefaultLocale

public Locale getDefaultLocale()
Return the default locale.

Returns:
The default Locale.

getKeys

public Iterator getKeys()
Description copied from class: ResourcesBase

Return an Iterator over the defined keys in this Resources instance.

Specified by:
getKeys in interface Resources
Specified by:
getKeys in class ResourcesBase
Returns:
The keys contained in this resources instance.
See Also:
ResourcesBase.getKeys()

getObject

public Object getObject(String key,
                        Locale locale)

Return the content for the specified key as an Object, localized based on the specified locale.

Specified by:
getObject in interface Resources
Specified by:
getObject in class ResourcesBase
Parameters:
key - Identifier for the requested content
locale - Locale with which to localize retrieval, or null for the default Locale
Returns:
The content for the specified key.
Throws:
ResourcesException - if an error occurs retrieving or returning the requested content
ResourcesKeyException - if the no value for the specified key was found, and isReturnNull() returns false

destroy

public void destroy()

This method must be called when the manager of this resource decides that it's no longer needed. After this method is called, no further calls to any of the getXxx() methods are allowed.

Specified by:
destroy in interface Resources
Overrides:
destroy in class ResourcesBase
Throws:
ResourcesException - if an error occurs during finalization

getLocaleList

protected List getLocaleList(Locale locale)

Return a List of Locales that should be searched when locating resources for the specified Locale. The returned list will start with the specified Locale itself, followed by Locales that do not specify variant, country, or language modifiers. For example, if you pass in a Locale for the en_US_POSIX combination, the returned list will have Locale instances for the following country/language/variant combinations:

The search order calculated by this method makes it easy for Resources implementations to implement hierarchical search strategies similar to that employed by the standard Java class java.util.ResourceBundle.

Parameters:
locale - Locale on which to base the list calculation
Returns:
A List of locales.

getLocaleMap

protected Map getLocaleMap(Locale locale)

Return the Map to be used to resolve name-value mappings for the specified Locale. Caching is utilized to ensure that a call to getLocaleMap(base,locale) occurs only once per Locale.

Parameters:
locale - Locale for which to return a name-value mappings Map
Returns:
A name-value Map for the specified locale.

getLocaleMap

protected abstract Map getLocaleMap(String baseUrl,
                                    Locale locale)

Return a Map containing the name-value mappings for the specified base URL and requested Locale, if there are any. If there are no defined mappings for the specified Locale, return an empty Map instead.

Concrete subclasses must override this method to perform the appropriate lookup. A typical implementation will construct an absolute URL based on the specified base URL and Locale, retrieve the specified resource file (if any), and parse it into a Map structure.

Caching of previously retrieved Maps (if any) should be performed by callers of this method. Therefore, this method should always attempt to retrieve the specified resource and load it appropriately.

Parameters:
baseUrl - Base URL of the resource files for this Resources instance
locale - Locale for which name-value mappings are requested
Returns:
A name-value Map for the specified URL and locale.

getLocaleSuffix

protected String getLocaleSuffix(Locale locale)

Return the Locale-specific suffix for the specified Locale. If the specified Locale has zero-length language and country components, the returned suffix will also have zero length. Otherwise, it will contain an underscore character followed by the non-zero-length language, country, and variant properties (separated by underscore characters).

Parameters:
locale - Locale for which a suffix string is requested
Returns:
The locale specific suffix.


Copyright © 2001-2010 The Apache Software Foundation. All Rights Reserved.