org.apache.velocity.runtime.configuration

Class Configuration

public class Configuration extends Hashtable

Deprecated: As of version 1.1, please use ExtendedProperties from the Jakarta Commons Collections component.

This class extends normal Java properties by adding the possibility to use the same key many times concatenating the value strings instead of overwriting them.

The Extended Properties syntax is explained here:

Here is an example of a valid extended properties file:

      # lines starting with # are comments

      # This is the simplest property
      key = value

      # A long property may be separated on multiple lines
      longvalue = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \
                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

      # This is a property with many tokens
      tokens_on_a_line = first token, second token

      # This sequence generates exactly the same result
      tokens_on_multiple_lines = first token
      tokens_on_multiple_lines = second token

      # commas may be escaped in tokens
      commas.excaped = Hi\, what'up?
 

NOTE: this class has not been written for performance nor low memory usage. In fact, it's way slower than it could be and generates too much memory garbage. But since performance is not an issue during intialization (and there is not much time to improve it), I wrote it this way. If you don't like it, go ahead and tune it up!

Version: $Id: Configuration.java,v 1.34 2003/05/04 17:14:37 geirm Exp $

Author: Stefano Mazzocchi Jon S. Stevens Dave Bryson Jason van Zyl Geir Magnusson Jr. Leon Messerschmidt Nested Class Summary classConfiguration.PropertiesReader

This class is used to read properties lines.
classConfiguration.PropertiesTokenizer
This class divides into tokens a property value.
Field Summary
protected StringbasePath
Base path of the configuration file used to create this Configuration object.
Configurationdefaults
Default configurations repository.
ExtendedPropertiesdeprecationCrutch
protected Stringfile
The file connected to this repository (holding comments and such).
protected StringfileSeparator
File separator.
protected static Stringinclude
This is the name of the property that can point to other properties file for including other properties files.
protected booleanisInitialized
Has this configuration been intialized.
protected ArrayListkeysAsListed
These are the keys in the order they listed in the configuration file.
Constructor Summary
Configuration()
Creates an empty extended properties object.
Configuration(String file)
Creates and loads the extended properties from the specified file.
Configuration(String file, String defaultFile)
Creates and loads the extended properties from the specified file.
Method Summary
voidaddProperty(String key, Object token)
Add a property to the configuration.
voidaddStringProperty(String key, String token)
Sets a string property w/o checking for commas - used internally when a property has been broken up into strings that could contain escaped commas to prevent the inadvertant vectorization.
voidclearProperty(String key)
Clear a property in the configuration.
voidcombine(Configuration c)
Combines an existing Hashtable with this Hashtable.
static ConfigurationconvertProperties(Properties p)
Convert a standard properties class into a configuration class.
voiddisplay()
Display the configuration for debugging purposes.
booleangetBoolean(String key)
Get a boolean associated with the given configuration key.
booleangetBoolean(String key, boolean defaultValue)
Get a boolean associated with the given configuration key.
BooleangetBoolean(String key, Boolean defaultValue)
Get a boolean associated with the given configuration key.
bytegetByte(String key)
Get a byte associated with the given configuration key.
bytegetByte(String key, byte defaultValue)
Get a byte associated with the given configuration key.
BytegetByte(String key, Byte defaultValue)
Get a byte associated with the given configuration key.
doublegetDouble(String key)
Get a double associated with the given configuration key.
doublegetDouble(String key, double defaultValue)
Get a double associated with the given configuration key.
DoublegetDouble(String key, Double defaultValue)
Get a double associated with the given configuration key.
ExtendedPropertiesgetExtendedProperties()

Routine intended for deprecation period only as we switch from using the Configuration class in Velocity to the Jakarta Commons ExtendedProperties

Do not use this for general use.

floatgetFloat(String key)
Get a float associated with the given configuration key.
floatgetFloat(String key, float defaultValue)
Get a float associated with the given configuration key.
FloatgetFloat(String key, Float defaultValue)
Get a float associated with the given configuration key.
StringgetInclude()
Gets the property value for including other properties files.
intgetInt(String name)
The purpose of this method is to get the configuration resource with the given name as an integer.
intgetInt(String name, int def)
The purpose of this method is to get the configuration resource with the given name as an integer, or a default value.
intgetInteger(String key)
Get a int associated with the given configuration key.
intgetInteger(String key, int defaultValue)
Get a int associated with the given configuration key.
IntegergetInteger(String key, Integer defaultValue)
Get a int associated with the given configuration key.
IteratorgetKeys()
Get the list of the keys contained in the configuration repository.
IteratorgetKeys(String prefix)
Get the list of the keys contained in the configuration repository that match the specified prefix.
longgetLong(String key)
Get a long associated with the given configuration key.
longgetLong(String key, long defaultValue)
Get a long associated with the given configuration key.
LonggetLong(String key, Long defaultValue)
Get a long associated with the given configuration key.
PropertiesgetProperties(String key)
Get a list of properties associated with the given configuration key.
PropertiesgetProperties(String key, Properties defaults)
Get a list of properties associated with the given configuration key.
ObjectgetProperty(String key)
Gets a property from the configuration.
shortgetShort(String key)
Get a short associated with the given configuration key.
shortgetShort(String key, short defaultValue)
Get a short associated with the given configuration key.
ShortgetShort(String key, Short defaultValue)
Get a short associated with the given configuration key.
StringgetString(String key)
Get a string associated with the given configuration key.
StringgetString(String key, String defaultValue)
Get a string associated with the given configuration key.
String[]getStringArray(String key)
Get an array of strings associated with the given configuration key.
VectorgetVector(String key)
Get a Vector of strings associated with the given configuration key.
VectorgetVector(String key, Vector defaultValue)
Get a Vector of strings associated with the given configuration key.
voidinit(Configuration exp)
Private initializer method that sets up the generic resources.
booleanisInitialized()
Indicate to client code whether property resources have been initialized or not.
voidload(InputStream input)
Load the properties from the given input stream.
voidsave(OutputStream output, String Header)
Save the properties to the given outputstream.
voidsetInclude(String inc)
Sets the property value for including other properties files.
voidsetProperty(String key, Object value)
Set a property, this will replace any previously set values.
Configurationsubset(String prefix)
Create a Configurations object that is a subset of this one.
StringtestBoolean(String value)
Test whether the string represent by value maps to a boolean value or not.

Field Detail

basePath

protected String basePath
Base path of the configuration file used to create this Configuration object.

defaults

private Configuration defaults
Default configurations repository.

deprecationCrutch

private ExtendedProperties deprecationCrutch

file

protected String file
The file connected to this repository (holding comments and such).

Serial:

fileSeparator

protected String fileSeparator
File separator.

include

protected static String include
This is the name of the property that can point to other properties file for including other properties files.

isInitialized

protected boolean isInitialized
Has this configuration been intialized.

keysAsListed

protected ArrayList keysAsListed
These are the keys in the order they listed in the configuration file. This is useful when you wish to perform operations with configuration information in a particular order.

Constructor Detail

Configuration

public Configuration()
Creates an empty extended properties object.

Configuration

public Configuration(String file)
Creates and loads the extended properties from the specified file.

Parameters: file A String.

Throws: IOException.

Configuration

public Configuration(String file, String defaultFile)
Creates and loads the extended properties from the specified file.

Parameters: file A String.

Throws: IOException.

Method Detail

addProperty

public void addProperty(String key, Object token)
Add a property to the configuration. If it already exists then the value stated here will be added to the configuration entry. For example, if resource.loader = file is already present in the configuration and you addProperty("resource.loader", "classpath") Then you will end up with a Vector like the following: ["file", "classpath"]

Parameters: String key String value

addStringProperty

private void addStringProperty(String key, String token)
Sets a string property w/o checking for commas - used internally when a property has been broken up into strings that could contain escaped commas to prevent the inadvertant vectorization. Thanks to Leon Messerschmidt for this one.

clearProperty

public void clearProperty(String key)
Clear a property in the configuration.

Parameters: String key to remove along with corresponding value.

combine

public void combine(Configuration c)
Combines an existing Hashtable with this Hashtable. Warning: It will overwrite previous entries without warning.

Parameters: Configuration

convertProperties

public static Configuration convertProperties(Properties p)
Convert a standard properties class into a configuration class.

Parameters: Properties properties object to convert into a Configuration object.

Returns: Configuration configuration created from the properties object.

display

public void display()
Display the configuration for debugging purposes.

getBoolean

public boolean getBoolean(String key)
Get a boolean associated with the given configuration key.

Parameters: key The configuration key.

Returns: The associated boolean.

Throws: NoSuchElementException is thrown if the key doesn't map to an existing object. ClassCastException is thrown if the key maps to an object that is not a Boolean.

getBoolean

public boolean getBoolean(String key, boolean defaultValue)
Get a boolean associated with the given configuration key.

Parameters: key The configuration key. defaultValue The default value.

Returns: The associated boolean.

Throws: ClassCastException is thrown if the key maps to an object that is not a Boolean.

getBoolean

public Boolean getBoolean(String key, Boolean defaultValue)
Get a boolean associated with the given configuration key.

Parameters: key The configuration key. defaultValue The default value.

Returns: The associated boolean if key is found and has valid format, default value otherwise.

Throws: ClassCastException is thrown if the key maps to an object that is not a Boolean.

getByte

public byte getByte(String key)
Get a byte associated with the given configuration key.

Parameters: key The configuration key.

Returns: The associated byte.

Throws: NoSuchElementException is thrown if the key doesn't map to an existing object. ClassCastException is thrown if the key maps to an object that is not a Byte. NumberFormatException is thrown if the value mapped by the key has not a valid number format.

getByte

public byte getByte(String key, byte defaultValue)
Get a byte associated with the given configuration key.

Parameters: key The configuration key. defaultValue The default value.

Returns: The associated byte.

Throws: ClassCastException is thrown if the key maps to an object that is not a Byte. NumberFormatException is thrown if the value mapped by the key has not a valid number format.

getByte

public Byte getByte(String key, Byte defaultValue)
Get a byte associated with the given configuration key.

Parameters: key The configuration key. defaultValue The default value.

Returns: The associated byte if key is found and has valid format, default value otherwise.

Throws: ClassCastException is thrown if the key maps to an object that is not a Byte. NumberFormatException is thrown if the value mapped by the key has not a valid number format.

getDouble

public double getDouble(String key)
Get a double associated with the given configuration key.

Parameters: key The configuration key.

Returns: The associated double.

Throws: NoSuchElementException is thrown if the key doesn't map to an existing object. ClassCastException is thrown if the key maps to an object that is not a Double. NumberFormatException is thrown if the value mapped by the key has not a valid number format.

getDouble

public double getDouble(String key, double defaultValue)
Get a double associated with the given configuration key.

Parameters: key The configuration key. defaultValue The default value.

Returns: The associated double.

Throws: ClassCastException is thrown if the key maps to an object that is not a Double. NumberFormatException is thrown if the value mapped by the key has not a valid number format.

getDouble

public Double getDouble(String key, Double defaultValue)
Get a double associated with the given configuration key.

Parameters: key The configuration key. defaultValue The default value.

Returns: The associated double if key is found and has valid format, default value otherwise.

Throws: ClassCastException is thrown if the key maps to an object that is not a Double. NumberFormatException is thrown if the value mapped by the key has not a valid number format.

getExtendedProperties

public ExtendedProperties getExtendedProperties()

Deprecated: Do not use. For deprecation assistance only.

Routine intended for deprecation period only as we switch from using the Configuration class in Velocity to the Jakarta Commons ExtendedProperties

Do not use this for general use. It will disappear

Returns: ExtendedProperties containing data of Configuration

getFloat

public float getFloat(String key)
Get a float associated with the given configuration key.

Parameters: key The configuration key.

Returns: The associated float.

Throws: NoSuchElementException is thrown if the key doesn't map to an existing object. ClassCastException is thrown if the key maps to an object that is not a Float. NumberFormatException is thrown if the value mapped by the key has not a valid number format.

getFloat

public float getFloat(String key, float defaultValue)
Get a float associated with the given configuration key.

Parameters: key The configuration key. defaultValue The default value.

Returns: The associated float.

Throws: ClassCastException is thrown if the key maps to an object that is not a Float. NumberFormatException is thrown if the value mapped by the key has not a valid number format.

getFloat

public Float getFloat(String key, Float defaultValue)
Get a float associated with the given configuration key.

Parameters: key The configuration key. defaultValue The default value.

Returns: The associated float if key is found and has valid format, default value otherwise.

Throws: ClassCastException is thrown if the key maps to an object that is not a Float. NumberFormatException is thrown if the value mapped by the key has not a valid number format.

getInclude

public String getInclude()
Gets the property value for including other properties files. By default it is "include".

Returns: A String.

getInt

public int getInt(String name)
The purpose of this method is to get the configuration resource with the given name as an integer.

Parameters: name The resource name.

Returns: The value of the resource as an integer.

getInt

public int getInt(String name, int def)
The purpose of this method is to get the configuration resource with the given name as an integer, or a default value.

Parameters: name The resource name def The default value of the resource.

Returns: The value of the resource as an integer.

getInteger

public int getInteger(String key)
Get a int associated with the given configuration key.

Parameters: key The configuration key.

Returns: The associated int.

Throws: NoSuchElementException is thrown if the key doesn't map to an existing object. ClassCastException is thrown if the key maps to an object that is not a Integer. NumberFormatException is thrown if the value mapped by the key has not a valid number format.

getInteger

public int getInteger(String key, int defaultValue)
Get a int associated with the given configuration key.

Parameters: key The configuration key. defaultValue The default value.

Returns: The associated int.

Throws: ClassCastException is thrown if the key maps to an object that is not a Integer. NumberFormatException is thrown if the value mapped by the key has not a valid number format.

getInteger

public Integer getInteger(String key, Integer defaultValue)
Get a int associated with the given configuration key.

Parameters: key The configuration key. defaultValue The default value.

Returns: The associated int if key is found and has valid format, default value otherwise.

Throws: ClassCastException is thrown if the key maps to an object that is not a Integer. NumberFormatException is thrown if the value mapped by the key has not a valid number format.

getKeys

public Iterator getKeys()
Get the list of the keys contained in the configuration repository.

Returns: An Iterator.

getKeys

public Iterator getKeys(String prefix)
Get the list of the keys contained in the configuration repository that match the specified prefix.

Parameters: prefix The prefix to test against.

Returns: An Iterator of keys that match the prefix.

getLong

public long getLong(String key)
Get a long associated with the given configuration key.

Parameters: key The configuration key.

Returns: The associated long.

Throws: NoSuchElementException is thrown if the key doesn't map to an existing object. ClassCastException is thrown if the key maps to an object that is not a Long. NumberFormatException is thrown if the value mapped by the key has not a valid number format.

getLong

public long getLong(String key, long defaultValue)
Get a long associated with the given configuration key.

Parameters: key The configuration key. defaultValue The default value.

Returns: The associated long.

Throws: ClassCastException is thrown if the key maps to an object that is not a Long. NumberFormatException is thrown if the value mapped by the key has not a valid number format.

getLong

public Long getLong(String key, Long defaultValue)
Get a long associated with the given configuration key.

Parameters: key The configuration key. defaultValue The default value.

Returns: The associated long if key is found and has valid format, default value otherwise.

Throws: ClassCastException is thrown if the key maps to an object that is not a Long. NumberFormatException is thrown if the value mapped by the key has not a valid number format.

getProperties

public Properties getProperties(String key)
Get a list of properties associated with the given configuration key.

Parameters: key The configuration key.

Returns: The associated properties if key is found.

Throws: ClassCastException is thrown if the key maps to an object that is not a String/Vector. IllegalArgumentException if one of the tokens is malformed (does not contain an equals sign).

getProperties

public Properties getProperties(String key, Properties defaults)
Get a list of properties associated with the given configuration key.

Parameters: key The configuration key.

Returns: The associated properties if key is found.

Throws: ClassCastException is thrown if the key maps to an object that is not a String/Vector. IllegalArgumentException if one of the tokens is malformed (does not contain an equals sign).

getProperty

public Object getProperty(String key)
Gets a property from the configuration.

Parameters: key property to retrieve

Returns: value as object. Will return user value if exists, if not then default value if exists, otherwise null

getShort

public short getShort(String key)
Get a short associated with the given configuration key.

Parameters: key The configuration key.

Returns: The associated short.

Throws: NoSuchElementException is thrown if the key doesn't map to an existing object. ClassCastException is thrown if the key maps to an object that is not a Short. NumberFormatException is thrown if the value mapped by the key has not a valid number format.

getShort

public short getShort(String key, short defaultValue)
Get a short associated with the given configuration key.

Parameters: key The configuration key. defaultValue The default value.

Returns: The associated short.

Throws: ClassCastException is thrown if the key maps to an object that is not a Short. NumberFormatException is thrown if the value mapped by the key has not a valid number format.

getShort

public Short getShort(String key, Short defaultValue)
Get a short associated with the given configuration key.

Parameters: key The configuration key. defaultValue The default value.

Returns: The associated short if key is found and has valid format, default value otherwise.

Throws: ClassCastException is thrown if the key maps to an object that is not a Short. NumberFormatException is thrown if the value mapped by the key has not a valid number format.

getString

public String getString(String key)
Get a string associated with the given configuration key.

Parameters: key The configuration key.

Returns: The associated string.

Throws: ClassCastException is thrown if the key maps to an object that is not a String.

getString

public String getString(String key, String defaultValue)
Get a string associated with the given configuration key.

Parameters: key The configuration key. defaultValue The default value.

Returns: The associated string if key is found, default value otherwise.

Throws: ClassCastException is thrown if the key maps to an object that is not a String.

getStringArray

public String[] getStringArray(String key)
Get an array of strings associated with the given configuration key.

Parameters: key The configuration key.

Returns: The associated string array if key is found.

Throws: ClassCastException is thrown if the key maps to an object that is not a String/Vector.

getVector

public Vector getVector(String key)
Get a Vector of strings associated with the given configuration key.

Parameters: key The configuration key.

Returns: The associated Vector.

Throws: ClassCastException is thrown if the key maps to an object that is not a Vector.

getVector

public Vector getVector(String key, Vector defaultValue)
Get a Vector of strings associated with the given configuration key.

Parameters: key The configuration key. defaultValue The default value.

Returns: The associated Vector.

Throws: ClassCastException is thrown if the key maps to an object that is not a Vector.

init

private void init(Configuration exp)
Private initializer method that sets up the generic resources.

Throws: IOException, if there was an I/O problem.

isInitialized

public boolean isInitialized()
Indicate to client code whether property resources have been initialized or not.

load

public void load(InputStream input)
Load the properties from the given input stream.

Parameters: input An InputStream.

Throws: IOException.

save

public void save(OutputStream output, String Header)
Save the properties to the given outputstream.

Parameters: output An OutputStream. header A String.

Throws: IOException.

setInclude

public void setInclude(String inc)
Sets the property value for including other properties files. By default it is "include".

Parameters: inc A String.

setProperty

public void setProperty(String key, Object value)
Set a property, this will replace any previously set values. Set values is implicitly a call to clearProperty(key), addProperty(key,value).

Parameters: String key String value

subset

public Configuration subset(String prefix)
Create a Configurations object that is a subset of this one. Take into account duplicate keys by using the setProperty() in Configuration.

Parameters: String prefix

testBoolean

public String testBoolean(String value)
Test whether the string represent by value maps to a boolean value or not. We will allow true, on, and yes for a true boolean value, and false, off, and no for false boolean values. Case of value to test for boolean status is ignored.

Parameters: String The value to test for boolean state.

Returns: true or false if the supplied text maps to a boolean value, or null otherwise.

Copyright B) 2002 Apache Software Foundation. All Rights Reserved.