C# Class Commons.Collections.ExtendedProperties

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:

  • Each property has the syntax key = value
  • The key may use any character but the equal sign '='.
  • value may be separated on different lines if a backslash is placed at the end of the line that continues below.
  • If value is a list of strings, each token is separated by a comma ','.
  • Commas in each token are escaped placing a backslash right before the comma.
  • If a key is used more than once, the values are appended like if they were on the same line separated with commas.
  • Blank lines and lines starting with character '#' are skipped.
  • If a property is named "include" (or whatever is defined by setInclude() and getInclude() and the value of that property is the full path to a file on disk, that file will be included into the ConfigurationsRepository. You can also pull in files relative to the parent configuration file. So if you have something like the following: include = additional.properties Then "additional.properties" is expected to be in the same directory as the parent configuration file. Duplicate name values will be replaced, so be careful.

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!

Inheritance: System.Collections.Hashtable
Datei anzeigen Open project: rasmus-toftdahl-olesen/NVelocity Class Usage Examples

Protected Properties

Property Type Description
basePath String
file String
fileSeparator String
include String
isInitialized bool
keysAsListed System.Collections.ArrayList

Public Methods

Method Description
AddProperty ( String key, Object token ) : void

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"] *

ClearProperty ( String key ) : void

Clear a property in the configuration. *

Combine ( ExtendedProperties c ) : void

Combines an existing Hashtable with this Hashtable. * Warning: It will overwrite previous entries without warning. *

ConvertProperties ( ExtendedProperties p ) : ExtendedProperties

Convert a standard properties class into a configuration class.

ExtendedProperties ( ) : System

Creates an empty extended properties object.

ExtendedProperties ( String file ) : System

Creates and loads the extended properties from the specified file.

ExtendedProperties ( String file, String defaultFile ) : System

Creates and loads the extended properties from the specified file.

GetBoolean ( String key, System.Boolean defaultValue ) : System.Boolean

Get a boolean associated with the given configuration key. *

GetBoolean ( String key ) : bool

Get a boolean associated with the given configuration key. *

GetByte ( String key, Byte defaultValue ) : Byte

Get a byte associated with the given configuration key. *

GetByte ( String key ) : sbyte

Get a byte associated with the given configuration key. *

GetByte ( String key, sbyte defaultValue ) : sbyte

Get a byte associated with the given configuration key. *

GetDouble ( String key ) : Double

Get a double associated with the given configuration key. *

GetDouble ( String key, Double defaultValue ) : Double

Get a double associated with the given configuration key. *

GetFloat ( String key, System.Single defaultValue ) : System.Single

Get a float associated with the given configuration key. *

GetFloat ( String key ) : float

Get a float associated with the given configuration key. *

GetInt ( String name ) : Int32

The purpose of this method is to get the configuration resource with the given name as an integer. *

GetInt ( String name, int def ) : Int32

The purpose of this method is to get the configuration resource with the given name as an integer, or a default value. *

GetInteger ( String key ) : Int32

Get a int associated with the given configuration key. *

GetInteger ( String key, Int32 defaultValue ) : Int32

Get a int associated with the given configuration key. *

GetKeys ( String prefix ) : IEnumerable

Get the list of the keys contained in the configuration repository. *

Get the list of the keys contained in the configuration repository that match the specified prefix. *

GetLong ( String key ) : System.Int64

Get a long associated with the given configuration key. *

GetLong ( String key, System.Int64 defaultValue ) : System.Int64

Get a long associated with the given configuration key. *

GetProperties ( String key ) : Hashtable

Get a list of properties associated with the given configuration key. *

GetProperties ( String key, Hashtable defaultProps ) : Hashtable

Get a list of properties associated with the given configuration key. *

GetProperty ( String key ) : Object

Gets a property from the configuration. *

GetString ( String key ) : String

Get a string associated with the given configuration key. *

GetString ( String key, String defaultValue ) : String

Get a string associated with the given configuration key. *

GetStringArray ( String key ) : String[]

Get an array of strings associated with the given configuration key. *

GetStringList ( String key ) : List

Gets the string list.

GetVector ( String key ) : ArrayList

Get a Vector of strings associated with the given configuration key. *

GetVector ( String key, ArrayList defaultValue ) : ArrayList

Get a Vector of strings associated with the given configuration key. *

IsInitialized ( ) : bool

Private initializer method that sets up the generic resources.

Indicate to client code whether property resources have been initialized or not.

Load ( Stream input ) : void
Load ( Stream input, String encoding ) : void

Load the properties from the given input stream and using the specified encoding.

Save ( TextWriter output, String Header ) : void

Save the properties to the given outputStream.

SetProperty ( String key, Object value ) : void

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

Subset ( String prefix ) : ExtendedProperties

Create an ExtendedProperties object that is a subset of this one. Take into account duplicate keys by using the setProperty() in ExtendedProperties. *

TestBoolean ( String value ) : String

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. *

ToString ( ) : String

Display the configuration for debugging purposes.

Private Methods

Method Description
AddPropertyDirect ( String key, Object obj ) : void

Adds a key/value pair to the map. This routine does no magic morphing. It ensures the keyList is maintained *

AddStringProperty ( String key, String token ) : void

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 inadvertent vectorization. Thanks to Leon Messerschmidt for this one.

ValueToString ( Object value ) : String
WriteKeyOutput ( TextWriter textWriter, String key, String value ) : void

Method Details

AddProperty() public method

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"] *
public AddProperty ( String key, Object token ) : void
key String
token Object
return void

ClearProperty() public method

Clear a property in the configuration. *
public ClearProperty ( String key ) : void
key String key to remove along with corresponding value. /// ///
return void

Combine() public method

Combines an existing Hashtable with this Hashtable. * Warning: It will overwrite previous entries without warning. *
public Combine ( ExtendedProperties c ) : void
c ExtendedProperties ExtendedProperties /// ///
return void

ConvertProperties() public static method

Convert a standard properties class into a configuration class.
public static ConvertProperties ( ExtendedProperties p ) : ExtendedProperties
p ExtendedProperties properties object to convert into a ExtendedProperties object.
return ExtendedProperties

ExtendedProperties() public method

Creates an empty extended properties object.
public ExtendedProperties ( ) : System
return System

ExtendedProperties() public method

Creates and loads the extended properties from the specified file.
public ExtendedProperties ( String file ) : System
file String A String.
return System

ExtendedProperties() public method

Creates and loads the extended properties from the specified file.
public ExtendedProperties ( String file, String defaultFile ) : System
file String A String.
defaultFile String File to load defaults from.
return System

GetBoolean() public method

Get a boolean associated with the given configuration key. *
is thrown if the key maps to an /// object that is not a Boolean. /// ///
public GetBoolean ( String key, System.Boolean defaultValue ) : System.Boolean
key String The configuration key. ///
defaultValue System.Boolean The default value. ///
return System.Boolean

GetBoolean() public method

Get a boolean associated with the given configuration key. *
is thrown if the key doesn't /// map to an existing object. /// is thrown if the key maps to an /// object that is not a Boolean. /// ///
public GetBoolean ( String key ) : bool
key String The configuration key. ///
return bool

GetByte() public method

Get a byte associated with the given configuration key. *
is thrown if the key maps to an /// object that is not a Byte. ///
public GetByte ( String key, Byte defaultValue ) : Byte
key String The configuration key. ///
defaultValue Byte The default value. ///
return Byte

GetByte() public method

Get a byte associated with the given configuration key. *
is thrown if the key doesn't /// map to an existing object. /// is thrown if the key maps to an /// object that is not a Byte. ///
public GetByte ( String key ) : sbyte
key String The configuration key. ///
return sbyte

GetByte() public method

Get a byte associated with the given configuration key. *
is thrown if the key maps to an /// object that is not a Byte. ///
public GetByte ( String key, sbyte defaultValue ) : sbyte
key String The configuration key. ///
defaultValue sbyte The default value. ///
return sbyte

GetDouble() public method

Get a double associated with the given configuration key. *
is thrown if the key doesn't /// map to an existing object. /// is thrown if the key maps to an /// object that is not a Double. ///
public GetDouble ( String key ) : Double
key String The configuration key. ///
return Double

GetDouble() public method

Get a double associated with the given configuration key. *
is thrown if the key maps to an /// object that is not a Double. ///
public GetDouble ( String key, Double defaultValue ) : Double
key String The configuration key. ///
defaultValue Double The default value. ///
return Double

GetFloat() public method

Get a float associated with the given configuration key. *
is thrown if the key maps to an /// object that is not a Float. ///
public GetFloat ( String key, System.Single defaultValue ) : System.Single
key String The configuration key. ///
defaultValue System.Single The default value. ///
return System.Single

GetFloat() public method

Get a float associated with the given configuration key. *
is thrown if the key doesn't /// map to an existing object. /// is thrown if the key maps to an /// object that is not a Float. ///
public GetFloat ( String key ) : float
key String The configuration key. ///
return float

GetInt() public method

The purpose of this method is to get the configuration resource with the given name as an integer. *
public GetInt ( String name ) : Int32
name String The resource name. ///
return System.Int32

GetInt() public method

The purpose of this method is to get the configuration resource with the given name as an integer, or a default value. *
public GetInt ( String name, int def ) : Int32
name String The resource name ///
def int The default value of the resource. ///
return System.Int32

GetInteger() public method

Get a int associated with the given configuration key. *
is thrown if the key doesn't /// map to an existing object. /// is thrown if the key maps to an /// object that is not a Integer. ///
public GetInteger ( String key ) : Int32
key String The configuration key. ///
return System.Int32

GetInteger() public method

Get a int associated with the given configuration key. *
is thrown if the key maps to an /// object that is not a Integer. ///
public GetInteger ( String key, Int32 defaultValue ) : Int32
key String The configuration key. ///
defaultValue System.Int32 The default value. ///
return System.Int32

GetKeys() public method

Get the list of the keys contained in the configuration repository. * Get the list of the keys contained in the configuration repository that match the specified prefix. *
public GetKeys ( String prefix ) : IEnumerable
prefix String The prefix to test against. ///
return IEnumerable

GetLong() public method

Get a long associated with the given configuration key. *
is thrown if the key doesn't /// map to an existing object. /// is thrown if the key maps to an /// object that is not a Long. ///
public GetLong ( String key ) : System.Int64
key String The configuration key. ///
return System.Int64

GetLong() public method

Get a long associated with the given configuration key. *
is thrown if the key maps to an /// object that is not a Long. ///
public GetLong ( String key, System.Int64 defaultValue ) : System.Int64
key String The configuration key. ///
defaultValue System.Int64 The default value. ///
return System.Int64

GetProperties() public method

Get a list of properties associated with the given configuration key. *
is thrown if the key maps to an /// object that is not a String/Vector. /// if one of the tokens is /// malformed (does not contain an equals sign). /// ///
public GetProperties ( String key ) : Hashtable
key String The configuration key. ///
return System.Collections.Hashtable

GetProperties() public method

Get a list of properties associated with the given configuration key. *
is thrown if the key maps to an /// object that is not a String/Vector. /// if one of the tokens is /// malformed (does not contain an equals sign). /// ///
public GetProperties ( String key, Hashtable defaultProps ) : Hashtable
key String The configuration key. ///
defaultProps System.Collections.Hashtable Default property values. ///
return System.Collections.Hashtable

GetProperty() public method

Gets a property from the configuration. *
public GetProperty ( String key ) : Object
key String property to retrieve ///
return Object

GetString() public method

Get a string associated with the given configuration key. *
is thrown if the key maps to an /// object that is not a String. /// ///
public GetString ( String key ) : String
key String The configuration key. ///
return String

GetString() public method

Get a string associated with the given configuration key. *
is thrown if the key maps to an /// object that is not a String. /// ///
public GetString ( String key, String defaultValue ) : String
key String The configuration key. ///
defaultValue String The default value. ///
return String

GetStringArray() public method

Get an array of strings associated with the given configuration key. *
is thrown if the key maps to an /// object that is not a String/Vector. /// ///
public GetStringArray ( String key ) : String[]
key String The configuration key. ///
return String[]

GetStringList() public method

Gets the string list.
public GetStringList ( String key ) : List
key String The key.
return List

GetVector() public method

Get a Vector of strings associated with the given configuration key. *
is thrown if the key maps to an /// object that is not a Vector. /// ///
public GetVector ( String key ) : ArrayList
key String The configuration key. ///
return System.Collections.ArrayList

GetVector() public method

Get a Vector of strings associated with the given configuration key. *
is thrown if the key maps to an /// object that is not a Vector. /// ///
public GetVector ( String key, ArrayList defaultValue ) : ArrayList
key String The configuration key. ///
defaultValue System.Collections.ArrayList The default value. ///
return System.Collections.ArrayList

IsInitialized() public method

Private initializer method that sets up the generic resources. Indicate to client code whether property resources have been initialized or not.
if there was an I/O problem.
public IsInitialized ( ) : bool
return bool

Load() public method

public Load ( Stream input ) : void
input Stream
return void

Load() public method

Load the properties from the given input stream and using the specified encoding.
public Load ( Stream input, String encoding ) : void
input Stream An InputStream. ///
encoding String An encoding. ///
return void

Save() public method

Save the properties to the given outputStream.
///
public Save ( TextWriter output, String Header ) : void
output System.IO.TextWriter An OutputStream. ///
Header String A String. ///
return void

SetProperty() public method

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

Subset() public method

Create an ExtendedProperties object that is a subset of this one. Take into account duplicate keys by using the setProperty() in ExtendedProperties. *
public Subset ( String prefix ) : ExtendedProperties
prefix String prefix /// ///
return ExtendedProperties

TestBoolean() public method

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. *
public TestBoolean ( String value ) : String
value String The value to test for boolean state. ///
return String

ToString() public method

Display the configuration for debugging purposes.
public ToString ( ) : String
return String

Property Details

basePath protected_oe property

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

file protected_oe property

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

fileSeparator protected_oe property

File separator.
protected String fileSeparator
return String

include protected_oe static_oe property

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

isInitialized protected_oe property

Has this configuration been initialized.
protected bool isInitialized
return bool

keysAsListed protected_oe property

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.
protected ArrayList,System.Collections keysAsListed
return System.Collections.ArrayList