Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.dinopolis.util.resource
Class FileResources  view FileResources download FileResources.java

java.lang.Object
  extended byorg.dinopolis.util.resource.AbstractResources
      extended byorg.dinopolis.util.resource.FileResources
All Implemented Interfaces:
org.dinopolis.util.Resources

public class FileResources
extends AbstractResources

This class provides the functionality to read resources from a system or user resource file. The way that this is done is: It looks for a resource file within the users home directory and if a proper file is found, it looks for the specific proporty. If no file or the specific property was not found, the system resource file is being asked for the property.

Stored resources can not only be requested as Strings, but also in many different other types, like integers, StringArrays, booleans, Colors, Icons and others.

Resource files contain key/value pairs. The keys uniquely identify a locale-specific object in the bundle. Here's an example resource file that contains two key/value pairs:

 my_app.dimension.width=300
 my_app.dimension.height=200
 
Keys are always Strings. In this example, the keys are my_app.dimension.width and my_app.dimension.height. In the above example, the values are also Strings--200 and 300--but they may also be interpreted as integer values using the method AbstractResources.getInt(java.lang.String) 55 .

The single argumented getter methods all require the key as an argument and return the object if found. If the object is not found, the getter method throws a MissingResourceException. To avoid this behavior, it is also possible to use the corresponding methods that take two arguments, the key and the default value. If no property was bound under the given key, the default value will be returned instead of throwing a MissingResourceException.

Version:
$Revision: 1.7 $

Nested Class Summary
(package private)  class FileResources.FileResourcesEnumeration
          The Enumeration for this FileResources.
 
Field Summary
protected static java.lang.String DESCRIPTION_SUFFIX
          the description suffix
protected static java.lang.String KEY_VALUE_SEPARATORS
          key - value seperators
protected static java.util.WeakHashMap mapping_
          the mapping holding all requested FileResources
protected static java.lang.String POSSIBLE_VALUES_SUFFIX
          the possible values suffix
protected static java.lang.String PROPERTY_EXTENSION
          the property filename extension
protected  java.util.ResourceBundle system_bundle_
          the resource bundle
protected  java.lang.String system_resource_base_name_
          the user resource base dir
protected static java.lang.String TITLE_SUFFIX
          the title suffix
protected static java.lang.String TYPE_SUFFIX
          the type suffix
protected  java.util.Properties user_properties_
          the users private resource bundle
protected  java.lang.String user_resource_base_name_
          the system resource base dir
protected  java.io.File user_resource_file_
          the file that holds the users resources
protected static java.lang.String WHITE_SPACE_CHARS
          whitespace charakters
 
Fields inherited from class org.dinopolis.util.resource.AbstractResources
attached_resources_, NULL_KEY, UNSET_KEY
 
Fields inherited from interface org.dinopolis.util.Resources
GROUPS, KEY_DELIMITER, RESOURCE_STRING_ARRAY_DELIMITER, SWALLOW_IDENTIFIER, SWALLOW_LENGTH, VAR_IDENTIFIER, VAR_LENGTH
 
Constructor Summary
protected FileResources(java.io.File user_resource_file, java.util.Properties user_bundle, java.lang.String user_resource_base_name, java.util.ResourceBundle system_bundle, java.lang.String system_resource_base_name)
          Creates a new FileResources class.
 
Method Summary
protected  java.util.Enumeration doGetKeys()
          Returns an Enumeration containing all keys of all resources.
protected  void doStore()
          Call this method to make all changes performed by unset and setter methods persistent.
 java.lang.String getDescription(java.lang.String key)
          Returns a string that describes the key and its possible values.
 java.lang.String[] getPossibleValues(java.lang.String key)
          Returns an array of all values that are valid.
static FileResources getResources(java.lang.Class caller, java.lang.String base_name)
          Get the appropriate FileResources for the given base_name and searches the system bundle in the callers package name.
static FileResources getResources(java.lang.Class caller, java.lang.String base_name, java.util.Locale locale)
          Get the appropriate FileResources for the given base_name and locale and searches the system bundle in the callers package name.
static FileResources getResources(java.lang.Class caller, java.lang.String base_name, java.lang.String dir_name)
          Get the appropriate FileResources for the given base_name and the given dir_name and searches the system bundle in the callers package name.
static FileResources getResources(java.lang.Class caller, java.lang.String base_name, java.lang.String dir_name, java.util.Locale locale)
          Get the appropriate ResourceBundle.
static FileResources getResources(java.lang.Class caller, java.lang.String base_name, java.lang.String dir_name, java.util.Locale locale, java.lang.ClassLoader loader)
          Get the appropriate ResourceBundle with the use of the given classloader.
static FileResources getResources(java.lang.String base_name)
          Get the appropriate FileResources for the given base_name.
static FileResources getResources(java.lang.String base_name, java.util.Locale locale)
          Get the appropriate FileResources for the given base_name and the given locale.
static FileResources getResources(java.lang.String base_name, java.lang.String dir_name)
          Get the appropriate FileResources for the given base_name and the given dir_name.
static FileResources getResources(java.lang.String base_name, java.lang.String dir_name, java.util.Locale locale)
          Get the appropriate FileResources for the given base_name, the given dir_name and the given locale.
protected static java.lang.String getSystemResourceBaseName(java.lang.Class caller)
          Returs the base name of the system resource bundle.
protected static java.util.ResourceBundle getSystemResourceBundle(java.lang.String system_resource_base_dir, java.lang.String base_name, java.util.Locale locale, java.lang.ClassLoader loader)
          Returns the system resource bundle.
 java.lang.String getTitle(java.lang.String key)
          Returns the title for the given key.
 java.lang.Class getType(java.lang.String key)
          Returns the type of the value bound under the given key.
 java.net.URL getURL(java.lang.String key)
          Returns the Url loaded from the resource bundle.
protected static java.lang.String getUserResourceBaseName(java.lang.String dir_name)
          Returns the path of the users resource bundle.
protected static java.util.Properties getUsersResourceBundle(java.io.File user_resource_file)
          Returns the user resource bundle.
protected static java.io.File getUsersResourceFile(java.lang.String base_name, java.lang.String user_resource_dir)
          Returns the user resource file.
protected  java.lang.String getValue(java.lang.String key)
          Gets the bound value for the given key.
 boolean isModificationSupported()
          Returns true, if this resources is capable of storing and deleting resources, false otherwise.
protected  void resetValue(java.lang.String key)
          Resets the bound value for the given key to its default value.
 void setDescription(java.lang.String key, java.lang.String description)
          Sets the description for the given key.
 void setPossibleValues(java.lang.String key, java.lang.String[] possible_values)
          Sets the possible Values for the given key.
 void setTitle(java.lang.String key, java.lang.String title)
          Sets the title for the given key.
 void setType(java.lang.String key, java.lang.Class type)
          Sets the type for the given key.
protected  void setValue(java.lang.String key, java.lang.String value)
          Registers the given value under the given key.
protected  void unsetValue(java.lang.String key)
          Removes the bound value for the given key, if no value was bound under the given key, this method does nothing.
 
Methods inherited from class org.dinopolis.util.resource.AbstractResources
addPropertyChangeListener, addPropertyChangeListener, attachResources, detachResources, expand, findResourcesForKey, get, get, getBoolean, getBoolean, getClassForType, getColor, getColor, getDouble, getDouble, getDoubleArray, getDoubleArray, getFile, getFile, getIcon, getIcon, getInt, getInt, getIntArray, getIntArray, getKeys, getString, getString, getStringArray, getStringArray, getStringArray, getTypeForClass, getURL, getValueAllAttached, removePropertyChangeListener, removePropertyChangeListener, replace, reset, resetValueAllAttached, setBoolean, setColor, setDouble, setDoubleArray, setInt, setIntArray, setString, setStringArray, setStringArray, setValueAllAttached, store, unset, unsetValueAllAttached
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROPERTY_EXTENSION

protected static final java.lang.String PROPERTY_EXTENSION
the property filename extension

See Also:
Constant Field Values

TITLE_SUFFIX

protected static final java.lang.String TITLE_SUFFIX
the title suffix

See Also:
Constant Field Values

DESCRIPTION_SUFFIX

protected static final java.lang.String DESCRIPTION_SUFFIX
the description suffix

See Also:
Constant Field Values

POSSIBLE_VALUES_SUFFIX

protected static final java.lang.String POSSIBLE_VALUES_SUFFIX
the possible values suffix

See Also:
Constant Field Values

TYPE_SUFFIX

protected static final java.lang.String TYPE_SUFFIX
the type suffix

See Also:
Constant Field Values

KEY_VALUE_SEPARATORS

protected static final java.lang.String KEY_VALUE_SEPARATORS
key - value seperators

See Also:
Constant Field Values

WHITE_SPACE_CHARS

protected static final java.lang.String WHITE_SPACE_CHARS
whitespace charakters

See Also:
Constant Field Values

system_bundle_

protected java.util.ResourceBundle system_bundle_
the resource bundle


user_properties_

protected java.util.Properties user_properties_
the users private resource bundle


user_resource_file_

protected java.io.File user_resource_file_
the file that holds the users resources


user_resource_base_name_

protected java.lang.String user_resource_base_name_
the system resource base dir


system_resource_base_name_

protected java.lang.String system_resource_base_name_
the user resource base dir


mapping_

protected static java.util.WeakHashMap mapping_
the mapping holding all requested FileResources

Constructor Detail

FileResources

protected FileResources(java.io.File user_resource_file,
                        java.util.Properties user_bundle,
                        java.lang.String user_resource_base_name,
                        java.util.ResourceBundle system_bundle,
                        java.lang.String system_resource_base_name)
Creates a new FileResources class.

Method Detail

getResources

public static FileResources getResources(java.lang.String base_name)
                                  throws java.util.MissingResourceException
Get the appropriate FileResources for the given base_name.


getResources

public static FileResources getResources(java.lang.Class caller,
                                         java.lang.String base_name)
                                  throws java.util.MissingResourceException
Get the appropriate FileResources for the given base_name and searches the system bundle in the callers package name. See also getResources(java.lang.Class,java.lang.String,java.lang.String,java.util.Locale) 55


getResources

public static FileResources getResources(java.lang.String base_name,
                                         java.util.Locale locale)
                                  throws java.util.MissingResourceException
Get the appropriate FileResources for the given base_name and the given locale.


getResources

public static FileResources getResources(java.lang.Class caller,
                                         java.lang.String base_name,
                                         java.util.Locale locale)
                                  throws java.util.MissingResourceException
Get the appropriate FileResources for the given base_name and locale and searches the system bundle in the callers package name. See also getResources(java.lang.Class,java.lang.String,java.lang.String,java.util.Locale) 55


getResources

public static FileResources getResources(java.lang.String base_name,
                                         java.lang.String dir_name)
                                  throws java.util.MissingResourceException
Get the appropriate FileResources for the given base_name and the given dir_name. The dir_name specifies the directory name of the users resource file, relative to the users home directory.


getResources

public static FileResources getResources(java.lang.Class caller,
                                         java.lang.String base_name,
                                         java.lang.String dir_name)
                                  throws java.util.MissingResourceException
Get the appropriate FileResources for the given base_name and the given dir_name and searches the system bundle in the callers package name. The dir_name specifies the directory name of the users resource file, relative to the users home directory.


getResources

public static FileResources getResources(java.lang.String base_name,
                                         java.lang.String dir_name,
                                         java.util.Locale locale)
                                  throws java.util.MissingResourceException
Get the appropriate FileResources for the given base_name, the given dir_name and the given locale. The dir_name specifies the directory name of the users resource file, relative to the users home directory.


getResources

public static FileResources getResources(java.lang.Class caller,
                                         java.lang.String base_name,
                                         java.lang.String dir_name,
                                         java.util.Locale locale)
                                  throws java.util.MissingResourceException
Get the appropriate ResourceBundle. The ResourceBundle consists of two parts, the system resources (for default settings) and user specific settings. Any given properties in the users resources will overwrite the system resources! The users resources are searched in the following order: $user_home/dir_name/base_name.properties, $user_home/.dir_name/base_name.properties, $user_home/dir_name.toLowerCase()/base_name.properties, $user_home/.dir_name.toLowerCase()/base_name.properties

The system resources are expected to be in the same package than the caller!

Example:
caller: org.dinopolis.util.FileResources
base_name: TestFileResources
dir_name: test_resources
locale: Locale.getDefault()
users home dir: ~dfreis

then, the users bundle is suspected to be:
"~dfreis/test_resources/TestFileResources.properties", or if not found:
"~dfreis/.test_resources/TestFileResources.properties" the system resources are expected to be in:
"org.dinopolis.util.FileResources.TestFileResources.properties" within your classpath!

The sample code of the caller for the given example will look like this:

FileResources resources = FileResources.getResources(getClass(), "TestFileResources", "test_resources");


getResources

public static FileResources getResources(java.lang.Class caller,
                                         java.lang.String base_name,
                                         java.lang.String dir_name,
                                         java.util.Locale locale,
                                         java.lang.ClassLoader loader)
                                  throws java.util.MissingResourceException
Get the appropriate ResourceBundle with the use of the given classloader.


getSystemResourceBaseName

protected static java.lang.String getSystemResourceBaseName(java.lang.Class caller)
Returs the base name of the system resource bundle. The base name corresponds to the callers package name.


getSystemResourceBundle

protected static java.util.ResourceBundle getSystemResourceBundle(java.lang.String system_resource_base_dir,
                                                                  java.lang.String base_name,
                                                                  java.util.Locale locale,
                                                                  java.lang.ClassLoader loader)
                                                           throws java.util.MissingResourceException
Returns the system resource bundle.


getUserResourceBaseName

protected static java.lang.String getUserResourceBaseName(java.lang.String dir_name)
Returns the path of the users resource bundle. The Resource bundle is searched in the following order: $user_home/dir_name, $user_home/.dir_name, $user_home/dir_name.toLowerCase(), $user_home/.dir_name.toLowerCase().


getUsersResourceFile

protected static java.io.File getUsersResourceFile(java.lang.String base_name,
                                                   java.lang.String user_resource_dir)
Returns the user resource file. located in user_resource_dir with the base name base_name.


getUsersResourceBundle

protected static java.util.Properties getUsersResourceBundle(java.io.File user_resource_file)
Returns the user resource bundle.


isModificationSupported

public boolean isModificationSupported()
Returns true, if this resources is capable of storing and deleting resources, false otherwise.

Specified by:
isModificationSupported in interface org.dinopolis.util.Resources
Overrides:
isModificationSupported in class AbstractResources

getValue

protected java.lang.String getValue(java.lang.String key)
                             throws java.util.MissingResourceException
Gets the bound value for the given key.

Specified by:
getValue in class AbstractResources

doGetKeys

protected java.util.Enumeration doGetKeys()
Returns an Enumeration containing all keys of all resources.

Specified by:
doGetKeys in class AbstractResources

setValue

protected void setValue(java.lang.String key,
                        java.lang.String value)
Registers the given value under the given key. Key and value are garanteed to be non-null! Overwrite this method in classes extending AbstractResources if set methods are supported.

Overrides:
setValue in class AbstractResources

unsetValue

protected void unsetValue(java.lang.String key)
Removes the bound value for the given key, if no value was bound under the given key, this method does nothing. Key is garanteed to be non-null! Overwrite this method in classes extending AbstractResources, if remove is supported.

Overrides:
unsetValue in class AbstractResources

resetValue

protected void resetValue(java.lang.String key)
Resets the bound value for the given key to its default value. If no value was bound under the given key, this method does nothing. Key is garanteed to be non-null! Removes the value from the user properties.

Overrides:
resetValue in class AbstractResources

doStore

protected void doStore()
                throws java.io.IOException
Call this method to make all changes performed by unset and setter methods persistent. Overwrite this method in classes extending AbstractResources, if modifications are supported.

Overrides:
doStore in class AbstractResources

getTitle

public java.lang.String getTitle(java.lang.String key)
Returns the title for the given key. If no title for this key is available, null is returned. Overwrite this method in classes extending AbstractResources, if titles are supported. by default, this method returns null.

Specified by:
getTitle in interface org.dinopolis.util.Resources
Overrides:
getTitle in class AbstractResources

setTitle

public void setTitle(java.lang.String key,
                     java.lang.String title)
Sets the title for the given key. Deleted the title if title is 'null'.

Specified by:
setTitle in interface org.dinopolis.util.Resources
Overrides:
setTitle in class AbstractResources

getDescription

public java.lang.String getDescription(java.lang.String key)
Returns a string that describes the key and its possible values. If no description for this key is available, null is returned. Overwrite this method in classes extending AbstractResources, if descriptions are supported. by default, this method returns null.

Specified by:
getDescription in interface org.dinopolis.util.Resources
Overrides:
getDescription in class AbstractResources

setDescription

public void setDescription(java.lang.String key,
                           java.lang.String description)
Sets the description for the given key. Deleted the description if description is 'null'.

Specified by:
setDescription in interface org.dinopolis.util.Resources
Overrides:
setDescription in class AbstractResources

getType

public java.lang.Class getType(java.lang.String key)
Returns the type of the value bound under the given key. Note that the returned "Class" object may describe a built-in Java type such as "int" (Integer.TYPE). For arrays, this may be e.g.: int[].class. If no type for this key is available, null is returned. Overwrite this method in classes extending AbstractResources, if types are supported. by default, this method returns null.

Specified by:
getType in interface org.dinopolis.util.Resources
Overrides:
getType in class AbstractResources

setType

public void setType(java.lang.String key,
                    java.lang.Class type)
Sets the type for the given key. Deleted the kype if type is 'null'.

Specified by:
setType in interface org.dinopolis.util.Resources
Overrides:
setType in class AbstractResources

getPossibleValues

public java.lang.String[] getPossibleValues(java.lang.String key)
Returns an array of all values that are valid. This is usefull if a value may be choosen out of a predifined set of possible values. If no set of valid values exists null is returned. Overwrite this method in classes extending AbstractResources, if types is a set of chooseable values. by default, this method returns null.

Specified by:
getPossibleValues in interface org.dinopolis.util.Resources
Overrides:
getPossibleValues in class AbstractResources

setPossibleValues

public void setPossibleValues(java.lang.String key,
                              java.lang.String[] possible_values)
Sets the possible Values for the given key. Deleted the possible Values if possible_values are 'null'.

Specified by:
setPossibleValues in interface org.dinopolis.util.Resources
Overrides:
setPossibleValues in class AbstractResources

getURL

public java.net.URL getURL(java.lang.String key)
                    throws java.util.MissingResourceException
Returns the Url loaded from the resource bundle. The key in the resource file may be relative to the resource file. Any variables found within the properties value will be replaced.

Specified by:
getURL in interface org.dinopolis.util.Resources
Overrides:
getURL in class AbstractResources