|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--sdsu.util.ProgramProperties
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 commentThe 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 commentThe 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.
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 |
public ProgramProperties(java.lang.String[] mainArguments) throws java.io.IOException
mainArguments
- arguments passed to main.public ProgramProperties(java.lang.String configurationFileName) throws java.io.IOException
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.public ProgramProperties(java.lang.String[] mainArguments, java.lang.String configurationFileName) throws java.io.IOException
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.Method Detail |
public static ProgramProperties fileOptional(java.lang.String[] mainArguments, java.lang.String configurationFileName) throws java.io.IOException
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.public java.lang.String getString(java.lang.String key)
public java.lang.String getString(java.lang.String key, java.lang.String defaultValue)
public int getInt(java.lang.String key) throws java.lang.NumberFormatException
public int getInt(java.lang.String key, int defaultValue) throws java.lang.NumberFormatException
public float getFloat(java.lang.String key) throws java.lang.NumberFormatException
public float getFloat(java.lang.String key, float defaultValue) throws java.lang.NumberFormatException
public boolean containsKey(java.lang.String key)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |