Sky View Café - Java

org.shetline.util
Class BundleResourceProvider

java.lang.Object
  extended by org.shetline.util.BundleResourceProvider
All Implemented Interfaces:
ResourceProvider

public class BundleResourceProvider
extends Object
implements ResourceProvider

A basic implementation of ResourceProvider which relies on one or more .properties files to provide string resources.


Field Summary
protected  Vector<ResourceBundle> bundles
           
protected static String defaultEncoding
           
protected static PropertiesControl.LocaleMode defaultLocaleMode
           
protected static Locale defaultRootLocale
           
 
Constructor Summary
BundleResourceProvider(Class classInPackage, String baseName)
          Constructor specifying a class and a base name for a .properties file located in the package directory of the specified class.
BundleResourceProvider(Class classInPackage, String baseName, Locale locale)
          Constructor specifying a class and a base name for a .properties file located in the package directory of the specified class matching the specified locale.
BundleResourceProvider(String baseName)
          Constructor specifying one base name, or a comma-delimited list of base names, for one or more .properties resource files.
BundleResourceProvider(String[] baseNames)
          Constructor specifying an array of base names for one or more .properties resource files.
BundleResourceProvider(String[] baseNames, Locale locale)
          Constructor specifying an array of base names for one or more .properties resource files matching the specified locale.
BundleResourceProvider(String[] baseNames, Locale locale, String encoding, PropertiesControl.LocaleMode localeMode, Locale rootLocale)
          Constructor specifying an array of base names for one or more .properties resource files matching the specified locale.
BundleResourceProvider(String baseName, Locale locale)
          Constructor specifying one base name, or a comma-delimited list of base names, for one or more .properties resource files matching the specified locale.
 
Method Summary
 void addChoicesFromResource(JComboBox comboBox, String key)
          Fill in selections choices for a JComboBox using a delimited (as per ResourceProvider.getStrings(String) resource string.
 String format(String patternKey, Object... args)
          Return a formatted string, filled in using a specified list of arguments, as per ExtendedMessageFormat.format(String, Object...), where the pattern argument is a resource string matching the specified patternKey.
 ResourceBundle[] getBundles()
           
 int getInt(String key)
          Return an integer value for the given key.
 int getInt(String key, int errorValue)
          Return an integer value for the given key.
 String getString(String key)
          Return a resource string for the given key.
 String getString(String key, String defaultValue)
          Return a resource string for the given key.
 String getString(String key, String defaultValue, boolean failQuietly)
          Return a resource string for the given key.
 String[] getStrings(String key)
          Return an array of strings parsed from the resource string for the given key.
 String[] getStrings(String key, char delimiter)
          Return an array of strings parsed from the resource string for the given key.
 String[] getStrings(String key, char delimiter, char escapeChar)
          Return an array of strings parsed from the resource string for the given key.
static void setPropertiesDefaults(String encoding, PropertiesControl.LocaleMode localeMode, Locale rootLocale)
          Sets the default characteristics for resource bundles used by BundleResourceProvider instances.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

bundles

protected Vector<ResourceBundle> bundles

defaultEncoding

protected static String defaultEncoding

defaultLocaleMode

protected static PropertiesControl.LocaleMode defaultLocaleMode

defaultRootLocale

protected static Locale defaultRootLocale
Constructor Detail

BundleResourceProvider

public BundleResourceProvider(String baseName)
Constructor specifying one base name, or a comma-delimited list of base names, for one or more .properties resource files. When multiple base names are provided, the corresponding resources are searched in the order provided, with the first set of resources having the highest priority.

Parameters:
baseName - Base name of a .properties resource file, or comma-delimited list of base names.

BundleResourceProvider

public BundleResourceProvider(String baseName,
                              Locale locale)
Constructor specifying one base name, or a comma-delimited list of base names, for one or more .properties resource files matching the specified locale. When multiple base names are provided, the corresponding resources are searched in the order provided, with the first set of resources having the highest priority.

Parameters:
baseName - Base name of a .properties resource file, or comma-delimited list of base names.
locale - Locale for .properties files search.

BundleResourceProvider

public BundleResourceProvider(Class classInPackage,
                              String baseName)
Constructor specifying a class and a base name for a .properties file located in the package directory of the specified class.

Parameters:
classInPackage - Any class in the same package where the .properties file is located.
baseName - Base name of a .properties resource file.

BundleResourceProvider

public BundleResourceProvider(Class classInPackage,
                              String baseName,
                              Locale locale)
Constructor specifying a class and a base name for a .properties file located in the package directory of the specified class matching the specified locale.

Parameters:
classInPackage - Any class in the same package where the .properties file is located.
baseName - Base name of a .properties resource file.
locale - Locale for .properties file search.

BundleResourceProvider

public BundleResourceProvider(String[] baseNames)
Constructor specifying an array of base names for one or more .properties resource files. When multiple base names are provided, the corresponding resources are searched in the order provided, with the first set of resources having the highest priority.

Parameters:
baseNames - Base name of a .properties resource file.

BundleResourceProvider

public BundleResourceProvider(String[] baseNames,
                              Locale locale)
Constructor specifying an array of base names for one or more .properties resource files matching the specified locale. When multiple base names are provided, the corresponding resources are searched in the order provided, with the first set of resources having the highest priority.

Parameters:
baseNames - Base names of .properties resource files.
locale - Locale for .properties file search.

BundleResourceProvider

public BundleResourceProvider(String[] baseNames,
                              Locale locale,
                              String encoding,
                              PropertiesControl.LocaleMode localeMode,
                              Locale rootLocale)
Constructor specifying an array of base names for one or more .properties resource files matching the specified locale. When multiple base names are provided, the corresponding resources are searched in the order provided, with the first set of resources having the highest priority.

Parameters:
baseNames - Base names of .properties resource files.
locale - Locale for .properties file search.
encoding - Encoding of .properties file.
localeMode - Determines if country and/or variant of locale matter.
rootLocale - Locale of root .properties file.
Method Detail

setPropertiesDefaults

public static void setPropertiesDefaults(String encoding,
                                         PropertiesControl.LocaleMode localeMode,
                                         Locale rootLocale)
Sets the default characteristics for resource bundles used by BundleResourceProvider instances.

Parameters:
encoding - Encoding of .properties file.
localeMode - Determines if country and/or variant of locale matter.
rootLocale - Locale of root .properties file.

getString

public String getString(String key)
Description copied from interface: ResourceProvider
Return a resource string for the given key.

Specified by:
getString in interface ResourceProvider
Parameters:
key - Resource key.
Returns:
Matching string resource, or "<<key>>" if no match is found.

getString

public String getString(String key,
                        String defaultValue)
Description copied from interface: ResourceProvider
Return a resource string for the given key.

Specified by:
getString in interface ResourceProvider
Parameters:
key - Resource key.
defaultValue - Default return value if resource not found.
Returns:
Matching string resource, or defaultValue if no match is found.

getString

public String getString(String key,
                        String defaultValue,
                        boolean failQuietly)
Description copied from interface: ResourceProvider
Return a resource string for the given key.

Specified by:
getString in interface ResourceProvider
Parameters:
key - Resource key.
defaultValue - Default return value if resource not found.
failQuietly - If true, log no error for unmatched resource key.
Returns:
Matching string resource, or defaultValue if no match is found.

getStrings

public String[] getStrings(String key)
Description copied from interface: ResourceProvider

Return an array of strings parsed from the resource string for the given key.

The delimiters used to separate multiple string items will be automatically determined as follows, in this order of evaluation:

  1. If the resource contains any new line characters (\n), use new line as the item separator.
  2. If the resource contains any tab characters (\t), use tab as the item separator.
  3. If the resource contains any semicolons (;), use semicolon as the item separator, use backslash (\) as an escape character for literally-intended semicolons, and double backslash for single literally-intended backslashes
  4. If the resource contains any commas (,), use comma as the item separator, use backslash (\) as an escape character for literally-intended commas, and double backslash for single literally-intended backslashes

If none of the above delimiters are found, the resource will be treated as a single string.

Specified by:
getStrings in interface ResourceProvider
Parameters:
key - Resource key.
Returns:
Matching string resource parsed into an array of strings, or a single-item array containing "<<key>>" if no matching resource is found.

getStrings

public String[] getStrings(String key,
                           char delimiter)
Description copied from interface: ResourceProvider
Return an array of strings parsed from the resource string for the given key.

Specified by:
getStrings in interface ResourceProvider
Parameters:
key - Resource key.
delimiter - The delimiter character used to separate individual string items. If the delimiter is either semicolon (;) or comma (,) backslash (\) is used as an escape character.
Returns:
Matching string resource parsed into an array of strings, or a single-item array containing "<<key>>" if no matching resource is found.

getStrings

public String[] getStrings(String key,
                           char delimiter,
                           char escapeChar)
Description copied from interface: ResourceProvider
Return an array of strings parsed from the resource string for the given key.

Specified by:
getStrings in interface ResourceProvider
Parameters:
key - Resource key.
delimiter - The delimiter character used to separate individual string items.
escapeChar - An escape character. If the null character (\0) is specified, no escaping is performed.
Returns:
Matching string resource parsed into an array of strings, or a single-item array containing "<<key>>" if no matching resource is found.

getInt

public int getInt(String key)
Description copied from interface: ResourceProvider
Return an integer value for the given key.

Specified by:
getInt in interface ResourceProvider
Parameters:
key - Resource key.
Returns:
Integer value of matching string resource, 0 if no match is found or if the matching resource does not parse as a proper integer.

getInt

public int getInt(String key,
                  int errorValue)
Description copied from interface: ResourceProvider
Return an integer value for the given key.

Specified by:
getInt in interface ResourceProvider
Parameters:
key - Resource key.
errorValue - Value to return for missing or invalid resources.
Returns:
Integer value of matching string resource, errorValue if no match is found or if the matching resource does not parse as a proper integer.

format

public String format(String patternKey,
                     Object... args)
Description copied from interface: ResourceProvider

Return a formatted string, filled in using a specified list of arguments, as per ExtendedMessageFormat.format(String, Object...), where the pattern argument is a resource string matching the specified patternKey.

Specified by:
format in interface ResourceProvider
Parameters:
patternKey - Resource key for a formatting pattern.
args - Values to be applied to the formatting pattern.
Returns:
A formatted string based on the supplied pattern and values.
See Also:
MessageFormat.format(String, Object...)

addChoicesFromResource

public void addChoicesFromResource(JComboBox comboBox,
                                   String key)
Description copied from interface: ResourceProvider
Fill in selections choices for a JComboBox using a delimited (as per ResourceProvider.getStrings(String) resource string.

Specified by:
addChoicesFromResource in interface ResourceProvider
Parameters:
comboBox - Target JComboBox.
key - Resource key of delimited list of choices.

getBundles

public ResourceBundle[] getBundles()

Sky View Café - Java