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
Afficher le fichier Open project: rasmus-toftdahl-olesen/NVelocity Class Usage Examples

Protected Properties

Свойство Type Description
basePath String
file String
fileSeparator String
include String
isInitialized bool
keysAsListed System.Collections.ArrayList

Méthodes publiques

Méthode 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

Méthode 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 méthode

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
Résultat void

ClearProperty() public méthode

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

Combine() public méthode

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

ConvertProperties() public static méthode

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.
Résultat ExtendedProperties

ExtendedProperties() public méthode

Creates an empty extended properties object.
public ExtendedProperties ( ) : System
Résultat System

ExtendedProperties() public méthode

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

ExtendedProperties() public méthode

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.
Résultat System

GetBoolean() public méthode

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. ///
Résultat System.Boolean

GetBoolean() public méthode

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. ///
Résultat bool

GetByte() public méthode

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. ///
Résultat Byte

GetByte() public méthode

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. ///
Résultat sbyte

GetByte() public méthode

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. ///
Résultat sbyte

GetDouble() public méthode

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. ///
Résultat Double

GetDouble() public méthode

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. ///
Résultat Double

GetFloat() public méthode

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. ///
Résultat System.Single

GetFloat() public méthode

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. ///
Résultat float

GetInt() public méthode

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. ///
Résultat System.Int32

GetInt() public méthode

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. ///
Résultat System.Int32

GetInteger() public méthode

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. ///
Résultat System.Int32

GetInteger() public méthode

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. ///
Résultat System.Int32

GetKeys() public méthode

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. ///
Résultat IEnumerable

GetLong() public méthode

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. ///
Résultat System.Int64

GetLong() public méthode

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. ///
Résultat System.Int64

GetProperties() public méthode

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. ///
Résultat System.Collections.Hashtable

GetProperties() public méthode

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. ///
Résultat System.Collections.Hashtable

GetProperty() public méthode

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

GetString() public méthode

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. ///
Résultat String

GetString() public méthode

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. ///
Résultat String

GetStringArray() public méthode

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. ///
Résultat String[]

GetStringList() public méthode

Gets the string list.
public GetStringList ( String key ) : List
key String The key.
Résultat List

GetVector() public méthode

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. ///
Résultat System.Collections.ArrayList

GetVector() public méthode

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. ///
Résultat System.Collections.ArrayList

IsInitialized() public méthode

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
Résultat bool

Load() public méthode

public Load ( Stream input ) : void
input Stream
Résultat void

Load() public méthode

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. ///
Résultat void

Save() public méthode

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

SetProperty() public méthode

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
Résultat void

Subset() public méthode

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 /// ///
Résultat ExtendedProperties

TestBoolean() public méthode

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. ///
Résultat String

ToString() public méthode

Display the configuration for debugging purposes.
public ToString ( ) : String
Résultat String

Property Details

basePath protected_oe property

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

file protected_oe property

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

fileSeparator protected_oe property

File separator.
protected String fileSeparator
Résultat 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
Résultat String

isInitialized protected_oe property

Has this configuration been initialized.
protected bool isInitialized
Résultat 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
Résultat System.Collections.ArrayList