com.calitha.resource
Class XMLResourceBundle

java.lang.Object
  extended by java.util.ResourceBundle
      extended by com.calitha.resource.XMLResourceBundle
All Implemented Interfaces:
IXMLResourceBundle

public class XMLResourceBundle
extends ResourceBundle
implements IXMLResourceBundle

The XMLResourceBundle is used to support resources specified in XML format. The XMLResourceBundle can be instantiated in two ways: (1) By using the classic ResourceBundle class with the static getBundle method and pass the XMLResourceBundleControl to it. (2) By using the static getResourceBundle method in this class. This second way is preferable, because you will get an object of the XMLResourceBundle class directly and it's easier to write anyway.

The resources that you get by calling the getObject method are always IResource objects. Therefore never use the getString methods! Unfortunately those methods could not be overridden, because they are final. It is advisable to use the methods declared in this class to retrieve resources. They can use namespaces, can return IResource objects, let you get the resource content as text directly, and you can specify a default text value.

Author:
Robert van Loenhout

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.ResourceBundle
ResourceBundle.Control
 
Field Summary
protected  Map<String,IResource> map
           
 
Fields inherited from class java.util.ResourceBundle
parent
 
Method Summary
 Enumeration<String> getKeys()
           
 IResource getResource(String id)
          Requests a resource from the bundle.
 IResource getResource(String namespace, String id)
          Requests a resource from the bundle.
static XMLResourceBundle getResourceBundle(String baseName)
          Gets a resourcebundle which is parsed from an XML file.
static XMLResourceBundle getResourceBundle(String baseName, Locale locale)
          Gets a resourcebundle which is parsed from an XML file.
static XMLResourceBundle getResourceBundle(String baseName, Locale locale, ClassLoader loader)
          Gets a resourcebundle which is parsed from an XML file.
 String getText(String id)
          Requests the text content of a resource from the bundle.
 String getText(String namespace, String id)
          Requests the text content of a resource from the bundle.
 String getTextDefault(String id, String fallback)
          Requests the text content of a resource from the bundle.
 String getTextDefault(String namespace, String id, String fallback)
          Requests the text content of a resource from the bundle.
protected  Object handleGetObject(String key)
           
 
Methods inherited from class java.util.ResourceBundle
clearCache, clearCache, containsKey, getBundle, getBundle, getBundle, getBundle, getBundle, getBundle, getLocale, getObject, getString, getStringArray, handleKeySet, keySet, setParent
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

map

protected Map<String,IResource> map
Method Detail

getKeys

public Enumeration<String> getKeys()
Specified by:
getKeys in class ResourceBundle

handleGetObject

protected Object handleGetObject(String key)
Specified by:
handleGetObject in class ResourceBundle

getResource

public IResource getResource(String namespace,
                             String id)
Description copied from interface: IXMLResourceBundle
Requests a resource from the bundle.

This method works similar as the getObject method, but casts the object to an IResource.

Specified by:
getResource in interface IXMLResourceBundle
Parameters:
namespace - namespace
id - id
Returns:
resource object
See Also:
ResourceBundle.getObject(java.lang.String)

getResource

public IResource getResource(String id)
Description copied from interface: IXMLResourceBundle
Requests a resource from the bundle.

This method always uses an empty namespace. This method works similar as the getObject method, but casts the object to an IResource.

Specified by:
getResource in interface IXMLResourceBundle
Parameters:
id - id
Returns:
resource object
See Also:
ResourceBundle.getObject(java.lang.String)

getText

public String getText(String namespace,
                      String id)
Description copied from interface: IXMLResourceBundle
Requests the text content of a resource from the bundle.

Specified by:
getText in interface IXMLResourceBundle
Parameters:
namespace - namespace
id - id
Returns:
resource object
See Also:
getResource(java.lang.String, java.lang.String)

getText

public String getText(String id)
Description copied from interface: IXMLResourceBundle
Requests the text content of a resource from the bundle.

This method always uses an empty namespace.

Specified by:
getText in interface IXMLResourceBundle
Parameters:
id - id
Returns:
resource object
See Also:
getResource(java.lang.String, java.lang.String)

getTextDefault

public String getTextDefault(String namespace,
                             String id,
                             String fallback)
Description copied from interface: IXMLResourceBundle
Requests the text content of a resource from the bundle.

The default fallback string is returned if the resource cannot be found.

Specified by:
getTextDefault in interface IXMLResourceBundle
Parameters:
namespace - namespace
id - id
fallback - fallback text
Returns:
resource object
See Also:
getResource(java.lang.String, java.lang.String)

getTextDefault

public String getTextDefault(String id,
                             String fallback)
Description copied from interface: IXMLResourceBundle
Requests the text content of a resource from the bundle.

Always uses an empty namespace. The default fallback string is returned if the resource cannot be found.

Specified by:
getTextDefault in interface IXMLResourceBundle
Parameters:
id - id
fallback - fallback text
Returns:
resource object
See Also:
getResource(java.lang.String, java.lang.String)

getResourceBundle

public static XMLResourceBundle getResourceBundle(String baseName)
Gets a resourcebundle which is parsed from an XML file.

Parameters:
baseName - the base name of the resource bundle, a fully qualified class name
Returns:
a resource bundle for the given base name and the default locale
Throws:
IllegalArgumentException - if an argument is null
See Also:
ResourceBundle.getBundle(String)

getResourceBundle

public static XMLResourceBundle getResourceBundle(String baseName,
                                                  Locale locale)
Gets a resourcebundle which is parsed from an XML file.

Parameters:
baseName - the base name of the resource bundle, a fully qualified class name
locale - the locale for which a resource bundle is desired
Returns:
a resource bundle for the given base name and the default locale
Throws:
IllegalArgumentException - if an argument is null
See Also:
ResourceBundle.getBundle(String, Locale)

getResourceBundle

public static XMLResourceBundle getResourceBundle(String baseName,
                                                  Locale locale,
                                                  ClassLoader loader)
Gets a resourcebundle which is parsed from an XML file.

Parameters:
baseName - the base name of the resource bundle, a fully qualified class name
locale - the locale for which a resource bundle is desired
loader - the class loader from which to load the resource bundle
Returns:
a resource bundle for the given base name and the default locale
Throws:
IllegalArgumentException - if an argument is null
See Also:
ResourceBundle.getBundle(String, Locale, ClassLoader)