marf.util
Class OptionFileLoader

java.lang.Object
  extended by marf.util.OptionFileLoader
All Implemented Interfaces:
IOptionProvider

public class OptionFileLoader
extends java.lang.Object
implements IOptionProvider

Loads a configuration file.

This class is based upon OptionsFileLoaderSingleton originally implemented by Marc-Andre Laverdiere in the Crytolysis and Ftklipse projects. It was later adapted to be an options provider and brought to MARF by Serguei Mokhov.

The class loads a configuration file with the format:

key=value #comment
WARNING: this is not a great option to keep passwords in memory, because the data is kept as strings.

$Id: OptionFileLoader.java,v 1.2 2007/12/23 06:29:47 mokhov Exp $

Since:
0.3.0.6
Version:
$Revision: 1.2 $
Author:
Marc-Andre Laverdiere, Serguei Mokhov

Field Summary
protected  boolean bIsInitialized
          Indicates that the config was initialized.
protected static char COMMENT_SYMBOL
          Comment symbol.
protected static java.lang.String DEFAULT_CONFIG_FILE_NAME
          Default config file name.
protected  java.util.Hashtable oHashOptionValuePairTracker
          Options tracking in the form (key, value), both kept as strings.
protected static OptionFileLoader oOptionsLoaderInstance
          Singleton Instance.
protected static char SEPARATOR_SYMBOL
          Separator symbol.
 
Constructor Summary
protected OptionFileLoader()
          Constructs an uninitialized options loader.
 
Method Summary
 void addActiveOption(int piOptionCode, java.lang.String pstrOptionString)
           
 void addActiveOption(java.lang.String pstrOptionString)
           
 void addActiveOption(java.lang.String pstrOptionString, java.lang.String pstrOptionValue)
           
 void addValidOption(int piOptionCode, java.lang.String pstrOptionString)
           
 void addValidOption(int piOptionCode, java.lang.String pstrOptionString, boolean pbRequiresArgument)
           
 void addValidOption(java.lang.String pstrOptionString)
           
 java.util.Hashtable getActiveOptions()
           
 java.lang.String getArgumentString()
           
 java.lang.String[] getArgumentVector()
           
 java.lang.String getConfigurationSetting(java.lang.String pstrKey)
          Allows retrieving setting by a key.
static OptionFileLoader getInstance()
          Obtains singleton instance.
 java.util.Vector getInvalidOptions()
           
protected  java.lang.String getKey(java.lang.String pstrLine)
          Obtains the key of the line
 java.lang.String getOption(int piOption)
           
 java.lang.String getOption(int piOption, boolean pbReturnOnError)
           
 int getOption(java.lang.String pstrOption)
           
 int getOption(java.lang.String pstrOption, boolean pbReturnOnError)
           
 java.lang.String getOptionArgument(int piOption)
           
 java.lang.String getOptionArgument(int piOption, boolean pbReturnOnError)
           
 java.lang.String getOptionArgument(java.lang.String pstrOption)
           
 java.lang.String getOptionArgument(java.lang.String pstrOption, boolean pbReturnOnError)
           
 java.util.Hashtable getValidOptions()
           
protected  java.lang.String getValue(java.lang.String pstrLine)
          Obtains the value of the line.
 boolean isActiveOption(int piOption)
           
 boolean isActiveOption(java.lang.String pstrOption)
           
 boolean isInitialized()
           
 boolean isInvalidOption(java.lang.String pstrOption)
           
 boolean isValidOption(int piOption)
           
 boolean isValidOption(java.lang.String pstrOption)
           
 void loadConfiguration()
          Loads default configuration file.
 void loadConfiguration(java.lang.String pstrFileName)
          Loads specified configuration file.
protected  java.lang.String removeComment(java.lang.String pstrLine)
          Removes the comment from the line
 int size()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

oOptionsLoaderInstance

protected static OptionFileLoader oOptionsLoaderInstance
Singleton Instance.


oHashOptionValuePairTracker

protected java.util.Hashtable oHashOptionValuePairTracker
Options tracking in the form (key, value), both kept as strings.


bIsInitialized

protected boolean bIsInitialized
Indicates that the config was initialized.


DEFAULT_CONFIG_FILE_NAME

protected static final java.lang.String DEFAULT_CONFIG_FILE_NAME
Default config file name.

See Also:
Constant Field Values

COMMENT_SYMBOL

protected static final char COMMENT_SYMBOL
Comment symbol.

See Also:
Constant Field Values

SEPARATOR_SYMBOL

protected static final char SEPARATOR_SYMBOL
Separator symbol.

See Also:
Constant Field Values
Constructor Detail

OptionFileLoader

protected OptionFileLoader()
Constructs an uninitialized options loader.

Method Detail

getInstance

public static OptionFileLoader getInstance()
Obtains singleton instance.

Returns:
a unique instance of the options loader

loadConfiguration

public void loadConfiguration()
                       throws java.io.IOException
Loads default configuration file.

Throws:
java.io.IOException - on error reading file

loadConfiguration

public void loadConfiguration(java.lang.String pstrFileName)
                       throws java.io.IOException
Loads specified configuration file.

Parameters:
pstrFileName - name of the configuration file
Throws:
java.io.IOException - on error reading file

getConfigurationSetting

public java.lang.String getConfigurationSetting(java.lang.String pstrKey)
Allows retrieving setting by a key.

Parameters:
pstrKey - the key
Returns:
the setting

removeComment

protected java.lang.String removeComment(java.lang.String pstrLine)
Removes the comment from the line

Parameters:
pstrLine - line of data
Returns:
null if only a comment, a string otherwise

getKey

protected java.lang.String getKey(java.lang.String pstrLine)
Obtains the key of the line

Parameters:
pstrLine - line of data
Returns:
the string to the left of the separator

getValue

protected java.lang.String getValue(java.lang.String pstrLine)
Obtains the value of the line.

Parameters:
pstrLine - line of data
Returns:
the string to the right of the separator

isInitialized

public boolean isInitialized()
Returns:
true if a configuration file was loaded, false otherwise.

addValidOption

public void addValidOption(java.lang.String pstrOptionString)
Specified by:
addValidOption in interface IOptionProvider

addValidOption

public void addValidOption(int piOptionCode,
                           java.lang.String pstrOptionString)
Specified by:
addValidOption in interface IOptionProvider

addValidOption

public void addValidOption(int piOptionCode,
                           java.lang.String pstrOptionString,
                           boolean pbRequiresArgument)
Specified by:
addValidOption in interface IOptionProvider

addActiveOption

public void addActiveOption(java.lang.String pstrOptionString)
Specified by:
addActiveOption in interface IOptionProvider

addActiveOption

public void addActiveOption(int piOptionCode,
                            java.lang.String pstrOptionString)
Specified by:
addActiveOption in interface IOptionProvider

addActiveOption

public void addActiveOption(java.lang.String pstrOptionString,
                            java.lang.String pstrOptionValue)
Specified by:
addActiveOption in interface IOptionProvider

size

public int size()
Specified by:
size in interface IOptionProvider
See Also:
IOptionProvider.size()

getArgumentVector

public java.lang.String[] getArgumentVector()
Specified by:
getArgumentVector in interface IOptionProvider

getArgumentString

public java.lang.String getArgumentString()
Specified by:
getArgumentString in interface IOptionProvider

getOption

public int getOption(java.lang.String pstrOption)
Specified by:
getOption in interface IOptionProvider

getOption

public int getOption(java.lang.String pstrOption,
                     boolean pbReturnOnError)
Specified by:
getOption in interface IOptionProvider

getOptionArgument

public java.lang.String getOptionArgument(java.lang.String pstrOption,
                                          boolean pbReturnOnError)
Specified by:
getOptionArgument in interface IOptionProvider

getOption

public java.lang.String getOption(int piOption)
Specified by:
getOption in interface IOptionProvider

getOption

public java.lang.String getOption(int piOption,
                                  boolean pbReturnOnError)
Specified by:
getOption in interface IOptionProvider

getOptionArgument

public java.lang.String getOptionArgument(java.lang.String pstrOption)
Specified by:
getOptionArgument in interface IOptionProvider

getOptionArgument

public java.lang.String getOptionArgument(int piOption)
Specified by:
getOptionArgument in interface IOptionProvider

getOptionArgument

public java.lang.String getOptionArgument(int piOption,
                                          boolean pbReturnOnError)
Specified by:
getOptionArgument in interface IOptionProvider

getInvalidOptions

public java.util.Vector getInvalidOptions()
Specified by:
getInvalidOptions in interface IOptionProvider

getActiveOptions

public java.util.Hashtable getActiveOptions()
Specified by:
getActiveOptions in interface IOptionProvider

getValidOptions

public java.util.Hashtable getValidOptions()
Specified by:
getValidOptions in interface IOptionProvider

isActiveOption

public boolean isActiveOption(java.lang.String pstrOption)
Specified by:
isActiveOption in interface IOptionProvider

isActiveOption

public boolean isActiveOption(int piOption)
Specified by:
isActiveOption in interface IOptionProvider

isValidOption

public boolean isValidOption(java.lang.String pstrOption)
Specified by:
isValidOption in interface IOptionProvider

isValidOption

public boolean isValidOption(int piOption)
Specified by:
isValidOption in interface IOptionProvider

isInvalidOption

public boolean isInvalidOption(java.lang.String pstrOption)
Specified by:
isInvalidOption in interface IOptionProvider


SourceForge Logo