All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class sdsu.util.ProgramProperties

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

public class ProgramProperties
extends 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 Index

 o ProgramProperties(String)
Create a new ProgramProperties from data in command line arguments.
 o ProgramProperties(String[])
Create a new ProgramProperties from data in command line arguments.
 o ProgramProperties(String[], String)
Create a new ProgramProperties from data in command line arguments.

Method Index

 o containsKey(String)
Returns true if ProgramProperties contains the given key.
 o fileOptional(String[], String)
Create a new ProgramProperties, where the configuration file is optional.
 o getFloat(String)
Returns the float value associated with the given key.
 o getFloat(String, float)
Returns the float value associated with the given key.
 o getInt(String)
Returns the int value associated with the given key.
 o getInt(String, int)
Returns the int value associated with the given key.
 o getString(String)
Returns the string value associated with the given key.
 o getString(String, String)
Returns the string value associated with the given key.

Constructors

 o ProgramProperties
 public ProgramProperties(String mainArguments[]) throws IOException
Create a new ProgramProperties from data in command line arguments.

Parameters:
mainArguments - arguments passed to main.
Throws: IOException
if there is a problem reading or parsing the configuration file for program, which can be specified in the command line.
 o ProgramProperties
 public ProgramProperties(String configurationFileName) throws 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: IOException
if there is a problem reading or parsing the configuration file for program.
 o ProgramProperties
 public ProgramProperties(String mainArguments[],
                          String configurationFileName) throws 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: IOException
if there is a problem reading or parsing the configuration file for program.

Methods

 o fileOptional
 public static ProgramProperties fileOptional(String mainArguments[],
                                              String configurationFileName) throws 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: IOException
if there is a problem reading the configuration file for program.
 o getString
 public String getString(String key)
Returns the string value associated with the given key. Returns null if key does not exist.

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

 o getInt
 public int getInt(String key) throws NumberFormatException
Returns the int value associated with the given key.

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

Throws: NumberFormatException
if the value at the key can not be converted to an int.
 o getFloat
 public float getFloat(String key) throws NumberFormatException
Returns the float value associated with the given key.

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

Throws: NumberFormatException
if the value at the key can not be converted to an float.
 o containsKey
 public boolean containsKey(String key)
Returns true if ProgramProperties contains the given key. Returns false otherwise.


All Packages  Class Hierarchy  This Package  Previous  Next  Index