DbtuTools project
Java Library

org.edelbyte.util
Class UnixCfgParser

java.lang.Object
  |
  +--org.edelbyte.util.UnixCfgParser

public class UnixCfgParser
extends java.lang.Object

A basic Unix configuration file parser. The parser reads from a java.io.BufferedReader (or one of its subclasses) and returns a java.util.Properties object with the found key and value pairs. It is an alternative to the java.lang.Properties.load() method (which has some strange behavior).

More information can be found in the detailed description of the parse(java.io.BufferedReader) method.

Author:
Daniel Scheibli <daniel.scheibli at edelbyte.org>

Field Summary
protected  boolean bBetweenCheckEnabled
          Field holding the activation status of the Between Check mechanism (default is false).
protected static java.lang.String COMMENT_TOKEN
          Constant holding the token used for the comment detection within the stream.
protected static java.lang.String CONTINUATION_TOKEN
          Constant holding the token used for the line continuation detection within the stream.
protected static java.lang.String EQUALS_TOKEN
          Constant holding the token used for the key and value pair separation within the stream.
protected  java.lang.String oBetweenBeginToken
          Field holding the token, which indicates the beginning of the key and value pairs part of the stream.
protected  java.lang.String oBetweenEndToken
          Field holding the token, which indicates the end of the key and value pairs part of the stream.
 
Constructor Summary
UnixCfgParser()
          Creates a new instance of this class.
 
Method Summary
 java.lang.String getBetweenBeginToken()
          Returns the token, which indicates the beginning of the key and value pairs part (from the oBetweenBeginToken field).
 java.lang.String getBetweenEndToken()
          Returns the token, which indicates the end of the key and value pairs part (from the oBetweenEndToken field).
 boolean isBetweenCheckEnabled()
          Returns true for the case, that the Between Check mechanism is active.

The Between Check mechanism ensures, that there are key and value pairs between the so called BetweenBegin and BetweenEnd tokens (see getBetweenBeginToken() and getBetweenEndToken()) only.
 java.util.Properties parse(java.io.BufferedReader oReader)
          Reads key and value pairs from a java.io.BufferedReader (or one of its subclasses).

The parser reads the stream line wise using the readLine() method of the object given by the oReader parameter.
 void setBetweenBeginToken(java.lang.String oBetweenBeginToken)
          Set a alternative value for the BetweenBegin token, which is used by the Between Check mechanism.
 void setBetweenCheckEnabled(boolean bStatus)
          Enables or disables the Between Check mechanism while parsing the stream.

The Between Check mechanism ensures, that there are key and value pairs between the so called BetweenBegin and BetweenEnd tokens (see getBetweenBeginToken() and getBetweenEndToken()) only.
 void setBetweenEndToken(java.lang.String oBetweenEndToken)
          Set a alternative value for the BetweenEnd token, which is used by the Between Check mechanism.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COMMENT_TOKEN

protected static final java.lang.String COMMENT_TOKEN
Constant holding the token used for the comment detection within the stream.

EQUALS_TOKEN

protected static final java.lang.String EQUALS_TOKEN
Constant holding the token used for the key and value pair separation within the stream.

CONTINUATION_TOKEN

protected static final java.lang.String CONTINUATION_TOKEN
Constant holding the token used for the line continuation detection within the stream.

oBetweenBeginToken

protected java.lang.String oBetweenBeginToken
Field holding the token, which indicates the beginning of the key and value pairs part of the stream. It is used for the Between Check mechanism.

oBetweenEndToken

protected java.lang.String oBetweenEndToken
Field holding the token, which indicates the end of the key and value pairs part of the stream. It is used for the Between Check mechanism.

bBetweenCheckEnabled

protected boolean bBetweenCheckEnabled
Field holding the activation status of the Between Check mechanism (default is false).
Constructor Detail

UnixCfgParser

public UnixCfgParser()
Creates a new instance of this class.
Method Detail

getBetweenBeginToken

public java.lang.String getBetweenBeginToken()
Returns the token, which indicates the beginning of the key and value pairs part (from the oBetweenBeginToken field).
Returns:
A new String, containing the indicator token

getBetweenEndToken

public java.lang.String getBetweenEndToken()
Returns the token, which indicates the end of the key and value pairs part (from the oBetweenEndToken field).
Returns:
A new String, containing the indicator token

isBetweenCheckEnabled

public boolean isBetweenCheckEnabled()
Returns true for the case, that the Between Check mechanism is active.

The Between Check mechanism ensures, that there are key and value pairs between the so called BetweenBegin and BetweenEnd tokens (see getBetweenBeginToken() and getBetweenEndToken()) only.
Returns:
true if the Between Check mechanism is active.

parse

public java.util.Properties parse(java.io.BufferedReader oReader)
                           throws java.io.IOException,
                                  java.text.ParseException
Reads key and value pairs from a java.io.BufferedReader (or one of its subclasses).

The parser reads the stream line wise using the readLine() method of the object given by the oReader parameter. A line can hold up to one key and value pair. Everything after the first occurrence of "#" is interpreted as comment and therefore skipped. Lines containing tabs and blanks only are skipped as well. The first occurrence of "=" is interpreted as a delimiter separating key and value. After the separation of key and value, the key is converted to upper case. Lines ending with " \" (please notice the blank leading the backslash!) indicates a line continuation.

An sample configuration file is listed below:

##### Database configuration file for SAMPLE database

#BEGIN
Backup.Enabled = YES             ##### Backup allowed [YES|NO]
Backup.Rule.1  = (A > B) &&  \   ##### Backup rule number one
                 (C != D)
#END

For the given example, the returning java.util.Properties object contains the following key and value pairs:
Parameters:
oReader - The name of the DB2 Instance to be addressed.
Returns:
The java.util.Properties object containing the key and value pairs found while parsing.
Throws:
java.io.IOException - For the case, that there was an error while reading the stream.
java.text.ParseException - For the case, that there was an error while parsing the stream.

setBetweenBeginToken

public void setBetweenBeginToken(java.lang.String oBetweenBeginToken)
Set a alternative value for the BetweenBegin token, which is used by the Between Check mechanism.
Parameters:
oBetweeenBeginToken - The String containing the new value (for the oBetweenBeginToken field).

setBetweenCheckEnabled

public void setBetweenCheckEnabled(boolean bStatus)
Enables or disables the Between Check mechanism while parsing the stream.

The Between Check mechanism ensures, that there are key and value pairs between the so called BetweenBegin and BetweenEnd tokens (see getBetweenBeginToken() and getBetweenEndToken()) only.
Parameters:
bStatus - The status for the Between Check mechanism to be set.

setBetweenEndToken

public void setBetweenEndToken(java.lang.String oBetweenEndToken)
Set a alternative value for the BetweenEnd token, which is used by the Between Check mechanism.
Parameters:
oBetweeenEndToken - The String containing the new value (for the oBetweenEndToken field).

DbtuTools project
Java Library