C# Class Kajabity.Tools.Java.JavaPropertyReader

This class reads Java style properties from an input stream.
Exibir arquivo Open project: Kajabity/Kajabity-Tools Class Usage Examples

Public Methods

Method Description
JavaPropertyReader ( Hashtable hashtable ) : System

Construct a reader passing a reference to a Hashtable (or JavaProperties) instance where the keys are to be stored.

Parse ( Stream stream ) : void

Load key value pairs (properties) from an input Stream expected to have ISO-8859-1 encoding (code page 28592). The input stream (usually reading from a ".properties" file) consists of a series of lines (terminated by \r, \n or \r\n) each a key value pair, a comment or a blank line.

Leading whitespace (spaces, tabs, formfeeds) are ignored at the start of any line - and a line that is empty or contains only whitespace is blank and ignored.

A line with the first non-whitespace character is a '#' or '!' is a comment line and the rest of the line is ignored.

If the first non-whitespace character is not '#' or '!' then it is the start of a key. A key is all the characters up to the first whitespace or a key/value separator - '=' or ':'.

The separator is optional. Any whitespace after the key or after the separator (if present) is ignored.

The first non-whitespace character after the separator (or after the key if no separator) begins the value. The value may include whitespace, separators, or comment characters.

Any unicode character may be included in either key or value by using escapes preceded by the escape character '\'.

The following special cases are defined:

'\t' - horizontal tab. '\f' - form feed. '\r' - return '\n' - new line '\\' - add escape character. '\ ' - add space in a key or at the start of a value. '\!', '\#' - add comment markers at the start of a key. '\=', '\:' - add a separator in a key.

Any unicode character using the following escape:

'\uXXXX' - where XXXX represents the unicode character code as 4 hexadecimal digits.

Finally, longer lines can be broken by putting an escape at the very end of the line. Any leading space (unless escaped) is skipped at the beginning of the following line.

Examples a-key = a-value a-key : a-value a-key=a-value a-key a-value

All the above will result in the same key/value pair - key "a-key" and value "a-value".

! comment... # another comment...

The above are two examples of comments.

Honk\ Kong = Near China

The above shows how to embed a space in a key - key is "Hong Kong", value is "Near China".

a-longer-key-example = a really long value that is \ split over two lines.

An example of a long line split into two.

Parse ( Stream stream, Encoding encoding ) : void

Load key value pairs (properties) from an input Stream expected to have ISO-8859-1 encoding (code page 28592). The input stream (usually reading from a ".properties" file) consists of a series of lines (terminated by \r, \n or \r\n) each a key value pair, a comment or a blank line.

Leading whitespace (spaces, tabs, formfeeds) are ignored at the start of any line - and a line that is empty or contains only whitespace is blank and ignored.

A line with the first non-whitespace character is a '#' or '!' is a comment line and the rest of the line is ignored.

If the first non-whitespace character is not '#' or '!' then it is the start of a key. A key is all the characters up to the first whitespace or a key/value separator - '=' or ':'.

The separator is optional. Any whitespace after the key or after the separator (if present) is ignored.

The first non-whitespace character after the separator (or after the key if no separator) begins the value. The value may include whitespace, separators, or comment characters.

Any unicode character may be included in either key or value by using escapes preceded by the escape character '\'.

The following special cases are defined:

'\t' - horizontal tab. '\f' - form feed. '\r' - return '\n' - new line '\\' - add escape character. '\ ' - add space in a key or at the start of a value. '\!', '\#' - add comment markers at the start of a key. '\=', '\:' - add a separator in a key.

Any unicode character using the following escape:

'\uXXXX' - where XXXX represents the unicode character code as 4 hexadecimal digits.

Finally, longer lines can be broken by putting an escape at the very end of the line. Any leading space (unless escaped) is skipped at the beginning of the following line.

Examples a-key = a-value a-key : a-value a-key=a-value a-key a-value

All the above will result in the same key/value pair - key "a-key" and value "a-value".

! comment... # another comment...

The above are two examples of comments.

Honk\ Kong = Near China

The above shows how to embed a space in a key - key is "Hong Kong", value is "Near China".

a-longer-key-example = a really long value that is \ split over two lines.

An example of a long line split into two.

Private Methods

Method Description
ReadCharSafe ( ) : int

A method to substitute calls to stream.ReadByte(). The JavaPropertyReader now uses a BinaryReader to read properties. Unlike a plain stream, the BinaryReader will not return -1 when the stream end is reached, instead an IOException is to be thrown.

In this method we perform a check if the stream is already processed to the end, and return -1.

doAction ( int action, int ch ) : void
escapedChar ( int ch ) : char
matches ( int match, int ch ) : bool
nextChar ( ) : int
peekChar ( ) : int

Method Details

JavaPropertyReader() public method

Construct a reader passing a reference to a Hashtable (or JavaProperties) instance where the keys are to be stored.
public JavaPropertyReader ( Hashtable hashtable ) : System
hashtable System.Collections.Hashtable A reference to a hashtable where the key-value pairs can be stored.
return System

Parse() public method

Load key value pairs (properties) from an input Stream expected to have ISO-8859-1 encoding (code page 28592). The input stream (usually reading from a ".properties" file) consists of a series of lines (terminated by \r, \n or \r\n) each a key value pair, a comment or a blank line.

Leading whitespace (spaces, tabs, formfeeds) are ignored at the start of any line - and a line that is empty or contains only whitespace is blank and ignored.

A line with the first non-whitespace character is a '#' or '!' is a comment line and the rest of the line is ignored.

If the first non-whitespace character is not '#' or '!' then it is the start of a key. A key is all the characters up to the first whitespace or a key/value separator - '=' or ':'.

The separator is optional. Any whitespace after the key or after the separator (if present) is ignored.

The first non-whitespace character after the separator (or after the key if no separator) begins the value. The value may include whitespace, separators, or comment characters.

Any unicode character may be included in either key or value by using escapes preceded by the escape character '\'.

The following special cases are defined:

'\t' - horizontal tab. '\f' - form feed. '\r' - return '\n' - new line '\\' - add escape character. '\ ' - add space in a key or at the start of a value. '\!', '\#' - add comment markers at the start of a key. '\=', '\:' - add a separator in a key.

Any unicode character using the following escape:

'\uXXXX' - where XXXX represents the unicode character code as 4 hexadecimal digits.

Finally, longer lines can be broken by putting an escape at the very end of the line. Any leading space (unless escaped) is skipped at the beginning of the following line.

Examples a-key = a-value a-key : a-value a-key=a-value a-key a-value

All the above will result in the same key/value pair - key "a-key" and value "a-value".

! comment... # another comment...

The above are two examples of comments.

Honk\ Kong = Near China

The above shows how to embed a space in a key - key is "Hong Kong", value is "Near China".

a-longer-key-example = a really long value that is \ split over two lines.

An example of a long line split into two.

public Parse ( Stream stream ) : void
stream Stream The input stream that the properties are read from.
return void

Parse() public method

Load key value pairs (properties) from an input Stream expected to have ISO-8859-1 encoding (code page 28592). The input stream (usually reading from a ".properties" file) consists of a series of lines (terminated by \r, \n or \r\n) each a key value pair, a comment or a blank line.

Leading whitespace (spaces, tabs, formfeeds) are ignored at the start of any line - and a line that is empty or contains only whitespace is blank and ignored.

A line with the first non-whitespace character is a '#' or '!' is a comment line and the rest of the line is ignored.

If the first non-whitespace character is not '#' or '!' then it is the start of a key. A key is all the characters up to the first whitespace or a key/value separator - '=' or ':'.

The separator is optional. Any whitespace after the key or after the separator (if present) is ignored.

The first non-whitespace character after the separator (or after the key if no separator) begins the value. The value may include whitespace, separators, or comment characters.

Any unicode character may be included in either key or value by using escapes preceded by the escape character '\'.

The following special cases are defined:

'\t' - horizontal tab. '\f' - form feed. '\r' - return '\n' - new line '\\' - add escape character. '\ ' - add space in a key or at the start of a value. '\!', '\#' - add comment markers at the start of a key. '\=', '\:' - add a separator in a key.

Any unicode character using the following escape:

'\uXXXX' - where XXXX represents the unicode character code as 4 hexadecimal digits.

Finally, longer lines can be broken by putting an escape at the very end of the line. Any leading space (unless escaped) is skipped at the beginning of the following line.

Examples a-key = a-value a-key : a-value a-key=a-value a-key a-value

All the above will result in the same key/value pair - key "a-key" and value "a-value".

! comment... # another comment...

The above are two examples of comments.

Honk\ Kong = Near China

The above shows how to embed a space in a key - key is "Hong Kong", value is "Near China".

a-longer-key-example = a really long value that is \ split over two lines.

An example of a long line split into two.

public Parse ( Stream stream, Encoding encoding ) : void
stream Stream The input stream that the properties are read from.
encoding System.Text.Encoding The encoding that is used to read the properies file stream.
return void