net.sourceforge.groboutils.util.io.v1
Class PropertyDatabase

java.lang.Object
  extended bynet.sourceforge.groboutils.util.io.v1.PropertyDatabase

public class PropertyDatabase
extends java.lang.Object

A database of property files. Internally, it uses a ResourceBundle, so that locale specific properties can be used. The format is for the application defined property files to be defined as "read-only", and for a user defined property file to be the readable/writeable properties (i.e. modifications or additions or removals of the default properties). User settings override the read-only settings. The user property file is not localized, and is stored at $home/.app-name/user.properties (or the filename may be specified).

By default, the properties are loaded from the Resource streams, although this can be changed.

Before using this class, you must initialize the user property file by either setApplicationName( String ) or setUserPropertyFile( String ).

The stored data is only of type String, and multiple identical key entries are not possible - only the first one is allowed.

Since:
January 7, 2001
Version:
$Date: 2003/02/10 22:52:45 $
Author:
Matt Albrecht groboclown@users.sourceforge.net

Constructor Summary
PropertyDatabase()
          Default Constructor
PropertyDatabase(java.util.Locale l)
          Specify the Locale to load the properties from.
 
Method Summary
 void addResourceBundle(java.lang.String resourceName)
          Adds a resource bundle of the given name to the database, from the specified locale.
protected  void autoSave()
           
 java.io.File getApplicationDirectory()
          Returns the directory where the user properties are stored for the current application, or null if there is no application set.
 boolean getBooleanValue(java.lang.String key)
          Convenience function to convert a property to a boolean value.
 byte getByteValue(java.lang.String key)
          Convenience function to convert a property to a byte value.
 char getCharValue(java.lang.String key)
          Convenience function to convert a property to a char value.
 double getDoubleValue(java.lang.String key)
          Convenience function to convert a property to a double value.
 float getFloatValue(java.lang.String key)
          Convenience function to convert a property to a float value.
 int getIntValue(java.lang.String key)
          Convenience function to convert a property to an int value.
 long getLongValue(java.lang.String key)
          Convenience function to convert a property to a long value.
 short getShortValue(java.lang.String key)
          Convenience function to convert a property to a short value.
 java.lang.String getValue(java.lang.String key)
          Retrieves the value associated with the given key.
 boolean isAutosaveOn()
          Retrieves the auto-save setting.
protected  void loadUserProperties()
           
 java.lang.String removeValue(java.lang.String key)
          Removes a value from the properties.
 void saveUserProperties()
          Saves the current user properties.
 void setApplicationName(java.lang.String name)
          Set the application name and thus the corresponding directory that will store the user properties.
 void setAutosaveOn(boolean yes)
          Sets the current autosave setting.
 void setBooleanValue(java.lang.String key, boolean value)
          Convenience function to convert a boolean value to a String property.
 void setByteValue(java.lang.String key, byte value)
          Convenience function to convert a byte value to a String property.
 void setCharValue(java.lang.String key, char value)
          Convenience function to convert a char value to a String property.
 void setDoubleValue(java.lang.String key, double value)
          Convenience function to convert a byte value to a String property.
 void setFloatValue(java.lang.String key, float value)
          Convenience function to convert a float value to a String property.
 void setIntValue(java.lang.String key, int value)
          Convenience function to convert an int value to a String property.
 void setLongValue(java.lang.String key, long value)
          Convenience function to convert a long value to a String property.
 void setShortValue(java.lang.String key, short value)
          Convenience function to convert a short value to a String property.
 void setTrace(java.io.PrintWriter tracer)
          Sets the trace stream.
 void setUserPropertyFile(java.lang.String name)
          Set the user property file name exactly.
 void setValue(java.lang.String key, java.lang.String value)
          Sets the given value to the user properties.
 java.lang.String setValueToDefault(java.lang.String key)
          Resets the user property to the resource bundle's default value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PropertyDatabase

public PropertyDatabase()
Default Constructor


PropertyDatabase

public PropertyDatabase(java.util.Locale l)
Specify the Locale to load the properties from.

Method Detail

setApplicationName

public void setApplicationName(java.lang.String name)
                        throws java.io.IOException
Set the application name and thus the corresponding directory that will store the user properties. The user properties are located at $home/.app-name/user.properties. If the user property file is already set, then an IllegalStateException is thrown.

Parameters:
name - name of the application.
Throws:
java.io.IOException

getApplicationDirectory

public java.io.File getApplicationDirectory()
Returns the directory where the user properties are stored for the current application, or null if there is no application set.


setUserPropertyFile

public void setUserPropertyFile(java.lang.String name)
                         throws java.io.IOException
Set the user property file name exactly. If the user property file is already set, then an IllegalStateException is thrown.

Throws:
java.io.IOException

saveUserProperties

public void saveUserProperties()
                        throws java.io.IOException
Saves the current user properties.

Throws:
java.io.IOException

addResourceBundle

public void addResourceBundle(java.lang.String resourceName)
Adds a resource bundle of the given name to the database, from the specified locale. Note that if a user property already exists with a given key, then the user property overrides the resource property.


isAutosaveOn

public boolean isAutosaveOn()
Retrieves the auto-save setting.


setAutosaveOn

public void setAutosaveOn(boolean yes)
Sets the current autosave setting.


removeValue

public java.lang.String removeValue(java.lang.String key)
Removes a value from the properties. If the property is defined by a resource, then the user list must specify that it is removed.

Parameters:
key - the key to remove
Returns:
the value the key was assigned to, or null if nothing was removed.

setValue

public void setValue(java.lang.String key,
                     java.lang.String value)
Sets the given value to the user properties.

Parameters:
key - the key to assign the value to
value - the value to be assigned to the key

getValue

public java.lang.String getValue(java.lang.String key)
Retrieves the value associated with the given key.

Parameters:
key - the key to pull the value out of

setValueToDefault

public java.lang.String setValueToDefault(java.lang.String key)
Resets the user property to the resource bundle's default value.

Parameters:
key - the key to be reset.
Returns:
the default value for the key.

getIntValue

public int getIntValue(java.lang.String key)
Convenience function to convert a property to an int value.

Returns:
the given key converted to an integer, or Integer.MIN_VALUE if there was a parse error.

setIntValue

public void setIntValue(java.lang.String key,
                        int value)
Convenience function to convert an int value to a String property.


getBooleanValue

public boolean getBooleanValue(java.lang.String key)
Convenience function to convert a property to a boolean value.

Returns:
the given key converted to a boolean, or false if there was a parse error.

setBooleanValue

public void setBooleanValue(java.lang.String key,
                            boolean value)
Convenience function to convert a boolean value to a String property.


getByteValue

public byte getByteValue(java.lang.String key)
Convenience function to convert a property to a byte value.

Returns:
the given key converted to a byte, or Byte.MIN_VALUE if there was a parse error.

setByteValue

public void setByteValue(java.lang.String key,
                         byte value)
Convenience function to convert a byte value to a String property.


getCharValue

public char getCharValue(java.lang.String key)
Convenience function to convert a property to a char value.

Returns:
the given key converted to a char, or Character.MIN_VALUE if there was a parse error.

setCharValue

public void setCharValue(java.lang.String key,
                         char value)
Convenience function to convert a char value to a String property.


getDoubleValue

public double getDoubleValue(java.lang.String key)
Convenience function to convert a property to a double value.

Returns:
the given key converted to a double, or Double.MIN_VALUE if there was a parse error.

setDoubleValue

public void setDoubleValue(java.lang.String key,
                           double value)
Convenience function to convert a byte value to a String property.


getFloatValue

public float getFloatValue(java.lang.String key)
Convenience function to convert a property to a float value.

Returns:
the given key converted to a float, or Float.MIN_VALUE if there was a parse error.

setFloatValue

public void setFloatValue(java.lang.String key,
                          float value)
Convenience function to convert a float value to a String property.


getLongValue

public long getLongValue(java.lang.String key)
Convenience function to convert a property to a long value.

Returns:
the given key converted to a long, or Long.MIN_VALUE if there was a parse error.

setLongValue

public void setLongValue(java.lang.String key,
                         long value)
Convenience function to convert a long value to a String property.


getShortValue

public short getShortValue(java.lang.String key)
Convenience function to convert a property to a short value.

Returns:
the given key converted to a short, or Short.MIN_VALUE if there was a parse error.

setShortValue

public void setShortValue(java.lang.String key,
                          short value)
Convenience function to convert a short value to a String property.


setTrace

public void setTrace(java.io.PrintWriter tracer)
Sets the trace stream. If you set this to non-null, then warnings, such as ResourceBundles containing duplicate keys, will be reported to the stream. Errors will still be thrown as exceptions. Autosave will send any exceptions to this trace.


loadUserProperties

protected void loadUserProperties()
                           throws java.io.IOException
Throws:
java.io.IOException

autoSave

protected void autoSave()


Copyright © 2001-2003 by The GroboUtils Project