C# Класс 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!

Наследование: System.Collections.Hashtable
Показать файл Открыть проект Примеры использования класса

Защищенные свойства (Protected)

Свойство Тип Описание
basePath String
file String
fileSeparator String
include String
isInitialized bool
keysAsListed System.Collections.ArrayList

Открытые методы

Метод Описание
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.

Приватные методы

Метод Описание
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

Описание методов

AddProperty() публичный Метод

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
Результат void

ClearProperty() публичный Метод

Clear a property in the configuration. *
public ClearProperty ( String key ) : void
key String key to remove along with corresponding value. /// ///
Результат void

Combine() публичный Метод

Combines an existing Hashtable with this Hashtable. * Warning: It will overwrite previous entries without warning. *
public Combine ( ExtendedProperties c ) : void
c ExtendedProperties ExtendedProperties /// ///
Результат void

ConvertProperties() публичный статический Метод

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.
Результат ExtendedProperties

ExtendedProperties() публичный Метод

Creates an empty extended properties object.
public ExtendedProperties ( ) : System
Результат System

ExtendedProperties() публичный Метод

Creates and loads the extended properties from the specified file.
public ExtendedProperties ( String file ) : System
file String A String.
Результат System

ExtendedProperties() публичный Метод

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.
Результат System

GetBoolean() публичный Метод

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. ///
Результат System.Boolean

GetBoolean() публичный Метод

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. ///
Результат bool

GetByte() публичный Метод

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. ///
Результат Byte

GetByte() публичный Метод

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. ///
Результат sbyte

GetByte() публичный Метод

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. ///
Результат sbyte

GetDouble() публичный Метод

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. ///
Результат Double

GetDouble() публичный Метод

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. ///
Результат Double

GetFloat() публичный Метод

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. ///
Результат System.Single

GetFloat() публичный Метод

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. ///
Результат float

GetInt() публичный Метод

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. ///
Результат System.Int32

GetInt() публичный Метод

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. ///
Результат System.Int32

GetInteger() публичный Метод

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. ///
Результат System.Int32

GetInteger() публичный Метод

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. ///
Результат System.Int32

GetKeys() публичный Метод

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. ///
Результат IEnumerable

GetLong() публичный Метод

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. ///
Результат System.Int64

GetLong() публичный Метод

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. ///
Результат System.Int64

GetProperties() публичный Метод

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. ///
Результат System.Collections.Hashtable

GetProperties() публичный Метод

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. ///
Результат System.Collections.Hashtable

GetProperty() публичный Метод

Gets a property from the configuration. *
public GetProperty ( String key ) : Object
key String property to retrieve ///
Результат Object

GetString() публичный Метод

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. ///
Результат String

GetString() публичный Метод

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. ///
Результат String

GetStringArray() публичный Метод

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. ///
Результат String[]

GetStringList() публичный Метод

Gets the string list.
public GetStringList ( String key ) : List
key String The key.
Результат List

GetVector() публичный Метод

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. ///
Результат System.Collections.ArrayList

GetVector() публичный Метод

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. ///
Результат System.Collections.ArrayList

IsInitialized() публичный Метод

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
Результат bool

Load() публичный Метод

public Load ( Stream input ) : void
input Stream
Результат void

Load() публичный Метод

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. ///
Результат void

Save() публичный Метод

Save the properties to the given outputStream.
///
public Save ( TextWriter output, String Header ) : void
output System.IO.TextWriter An OutputStream. ///
Header String A String. ///
Результат void

SetProperty() публичный Метод

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
Результат void

Subset() публичный Метод

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 /// ///
Результат ExtendedProperties

TestBoolean() публичный Метод

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. ///
Результат String

ToString() публичный Метод

Display the configuration for debugging purposes.
public ToString ( ) : String
Результат String

Описание свойств

basePath защищенное свойство

Base path of the configuration file used to create this ExtendedProperties object.
protected String basePath
Результат String

file защищенное свойство

The file connected to this repository (holding comments and such).
protected String file
Результат String

fileSeparator защищенное свойство

File separator.
protected String fileSeparator
Результат String

include защищенное статическое свойство

This is the name of the property that can point to other properties file for including other properties files.
protected static String include
Результат String

isInitialized защищенное свойство

Has this configuration been initialized.
protected bool isInitialized
Результат bool

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.
protected ArrayList,System.Collections keysAsListed
Результат System.Collections.ArrayList