sdsu.util
Class ProgramProperties

java.lang.Object
  |
  +--sdsu.util.ProgramProperties

public class ProgramProperties
extends java.lang.Object

ProgramProperties obtains program options from command line arguments and configuration files. Program options are given in key-value (or name-value) pairs as in a hastable. Given a key (in a get method), the ProgramProperties first checks the command line flags for a pair with that key. If the key is found in the command line the corresponding value is returned. If the key is not found the configuration file is checked. One can give a default value in the program for cases where the key is not in the command line or the configuration file.

Flags are indicated in command line via "-" before the flag. Supported syntax:
-flag=value
-flag value The string "flag" (without the '-' and without the "'s) is stored as key. The string "value" is the associated value in the hashtable. If -flag is the last argument or the argument after -flag starts with a '-', the string "NO_VALUE" is stored as value for the flag.
--flagChars Individual characters after the -- are treated as separate flags. The string "NO_VALUE" is stored as value for each flag.
-- ignore rest of the command line arguments.

Configuration files can either contain ASCII versions of java.util.Properties or sdsu.util.LabeledData objects. The basic format for a Properties file is:

		#A comment to the end of the line, they go between lines of data
		key1=value1
		key2=value2	with spaces
		key3 with spaces=value3	#part of the value, not a comment
 
The basic format for a LabeledData file is:
		#A comment to the end of the line, they go between lines of data
		key1 = value1;
		key2='value2	with spaces';
		'key3 with spaces'=value3;		# a comment
 
The actual name of a Properties file must end in ".properties". The actual name of a LabeledData file must end in ".labeledData". The file extension comparison used is not case sensitive. The name of the configuration file is given in the constructor. The name does not have to include the file extension ".properties" or ".labeledData", the extensions will be added. This means you should not have files with the same name different extensions in the same directory. The command line flag "-conf=fileName" overrides the file name given in the constructor.

If you need many configuration files for a program you may wish to use a Respository instead of this class.

Version:
1.0 13 January 1998
Author:
Roger Whitney (whitney@cs.sdsu.edu)
See Also:
Properties, LabeledData, LocalRepository

Constructor Summary
ProgramProperties(java.lang.String configurationFileName)
          Create a new ProgramProperties from data in command line arguments.
ProgramProperties(java.lang.String[] mainArguments)
          Create a new ProgramProperties from data in command line arguments.
ProgramProperties(java.lang.String[] mainArguments, java.lang.String configurationFileName)
          Create a new ProgramProperties from data in command line arguments.
 
Method Summary
 boolean containsKey(java.lang.String key)
          Returns true if ProgramProperties contains the given key.
static ProgramProperties fileOptional(java.lang.String[] mainArguments, java.lang.String configurationFileName)
          Create a new ProgramProperties, where the configuration file is optional.
 float getFloat(java.lang.String key)
          Returns the float value associated with the given key.
 float getFloat(java.lang.String key, float defaultValue)
          Returns the float value associated with the given key.
 int getInt(java.lang.String key)
          Returns the int value associated with the given key.
 int getInt(java.lang.String key, int defaultValue)
          Returns the int value associated with the given key.
 java.lang.String getString(java.lang.String key)
          Returns the string value associated with the given key.
 java.lang.String getString(java.lang.String key, java.lang.String defaultValue)
          Returns the string value associated with the given key.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ProgramProperties

public ProgramProperties(java.lang.String[] mainArguments)
                  throws java.io.IOException
Create a new ProgramProperties from data in command line arguments.
Parameters:
mainArguments - arguments passed to main.
Throws:
java.io.IOException - if there is a problem reading or parsing the configuration file for program, which can be specified in the command line.

ProgramProperties

public ProgramProperties(java.lang.String configurationFileName)
                  throws java.io.IOException
Create a new ProgramProperties from data in command line arguments.
Parameters:
configurationFileName - name of configuration file for program. File must contain either a java.util.Properties or sdsu.util.LabeledData. Actual file name must end in either ".properties" or ".labeledData". The suffex can be omitted from the file name given here.
Throws:
java.io.IOException - if there is a problem reading or parsing the configuration file for program.

ProgramProperties

public ProgramProperties(java.lang.String[] mainArguments,
                         java.lang.String configurationFileName)
                  throws java.io.IOException
Create a new ProgramProperties from data in command line arguments.
Parameters:
mainArguments - arguments passed to main.
configurationFileName - name of configuration file for program. File must contain either a java.util.Properties or sdsu.util.LabeledData. Actual file name must end in either ".properties" or ".labeledData". The suffex can be omitted from the file name given here.
Throws:
java.io.IOException - if there is a problem reading or parsing the configuration file for program.
Method Detail

fileOptional

public static ProgramProperties fileOptional(java.lang.String[] mainArguments,
                                             java.lang.String configurationFileName)
                                      throws java.io.IOException
Create a new ProgramProperties, where the configuration file is optional. That is if the listed file can not be found no exception is thrown. Only those key-value pairs given in the command line or those given default values will exist.
Parameters:
mainArguments - arguments passed to main.
configurationFileName - name of configuration file for program. File must contain either a java.util.Properties or sdsu.util.LabeledData. Actual file name must end in either ".properties" or ".labeledData". The suffex can be omitted from the file name given here.
Throws:
java.io.IOException - if there is a problem reading the configuration file for program.

getString

public java.lang.String getString(java.lang.String key)
Returns the string value associated with the given key. Returns null if key does not exist.

getString

public java.lang.String getString(java.lang.String key,
                                  java.lang.String defaultValue)
Returns the string value associated with the given key. Returns defaultValue if key does not exist.

getInt

public int getInt(java.lang.String key)
           throws java.lang.NumberFormatException
Returns the int value associated with the given key.
Throws:
java.lang.NumberFormatException - if key does not exist. or the value at the key can not be converted to an int.

getInt

public int getInt(java.lang.String key,
                  int defaultValue)
           throws java.lang.NumberFormatException
Returns the int value associated with the given key. Returns defaultValue if the key does not exist.
Throws:
java.lang.NumberFormatException - if the value at the key can not be converted to an int.

getFloat

public float getFloat(java.lang.String key)
               throws java.lang.NumberFormatException
Returns the float value associated with the given key.
Throws:
java.lang.NumberFormatException - if key does not exist. or the value at the key can not be converted to an float.

getFloat

public float getFloat(java.lang.String key,
                      float defaultValue)
               throws java.lang.NumberFormatException
Returns the float value associated with the given key. Returns defaultValue if the key does not exist.
Throws:
java.lang.NumberFormatException - if the value at the key can not be converted to an float.

containsKey

public boolean containsKey(java.lang.String key)
Returns true if ProgramProperties contains the given key. Returns false otherwise.