public class NonsharedParameters extends Object implements Cloneable
parameterName = value ...It may also contain comments or section headers (i.e. anything that does not match the above pattern). Parameter names are not case sensitive. Initial and terminal spaces are trimmed for both parameter names and values. Boolean parameters accept multiple ways of expressing "true" (namely "on", "true", "yes" and "active"). You may also use ${param} constructs to refer to parameters set earlier, these references are replaced at the time the file is read by the current value of 'param' (null if it does not have a value!) Alternatively you can use $[param] references, they will be replaced by the current value of 'param' at runtime. Thus, param could be set/overriden after the actual reference appears, i.e. always reflects current settings. This class does function as an object. Example:
// Read data from my.ini
NonsharedParameters params = new NonsharedParameters("my.ini");
// Abort with error message if the following parameters are not specified
params.ensureParameters(
"firstPar - some help text for the first parameter",
"secondPar - some help text for the secondparameter"
);
// Retrieve the value of a parameter
String p=params.get("firstPar");
You can load parameters from multiple files. These will overlay.
You can reference a .ini file in another .ini file using the 'include' parameter,
included files will be loaded at the point of the 'include' statements,
parameter settings following the include statement can
overwrite parameter settings of this included ini file.
Example:
Content of main.ini:
//load database configuration
include = db_myserver.ini
// overwrite or add parameters
databasePort = 5555
myOtherParameter = 4
Content of db_myserver.ini:
databaseSystem = postgres
databaseDatabase = example
databaseUser = albert
databaseHost = localhost
databasePort = 5432
If main.ini is loaded, the 'databasePort' parameter will have value '5555' in the resulting NonsharedParameters instance.
Include is recursive, make sure you do not generate a cycle!| Modifier and Type | Class and Description |
|---|---|
static class |
NonsharedParameters.ParameterFileException
Reading or writing the parameters ini file failed for some reason
In some cases the enclosing program might resolve the issue by providing another file
|
static class |
NonsharedParameters.UndefinedParameterException
Thrown for an undefined Parameter
|
| Modifier and Type | Field and Description |
|---|---|
String |
basePath
Holds the path that should be assumed to be the base path to the current directory for all local path values
|
File |
iniFile
Holds the filename of the ini-file
|
static Pattern |
INIPATTERN
Holds the pattern used for ini-file-entries
|
static FinalSet<String> |
no
Holds words that count as "no" for boolean parameters
|
static Pattern |
READTIMEVARIABLEPATTERN |
static Pattern |
RUNTIMVARIABLEPATTERN |
Map<String,String> |
values
Contains the values for the parameters
|
| Constructor and Description |
|---|
NonsharedParameters()
Constructors
|
NonsharedParameters(File iniFile) |
NonsharedParameters(File iniFile,
String localPath) |
NonsharedParameters(NonsharedParameters other) |
NonsharedParameters(String iniFile) |
NonsharedParameters(String iniFile,
String localPath) |
| Modifier and Type | Method and Description |
|---|---|
void |
add(String key,
String value)
Adds a value to the map and to the ini file, if not yet there
|
NonsharedParameters |
clone()
Cloning implementation
|
void |
ensureParameters(String... p)
Reports an error message and aborts if the parameters are undefined.
|
String |
get(String s)
Returns a value for a parameter
|
String |
get(String s,
String defaultValue)
Returns a value for a parameter, returning a default value by default
|
boolean |
getBoolean(String s)
Returns a value for a boolean parameter
|
boolean |
getBoolean(String s,
boolean defaultValue)
Returns a value for a boolean parameter, returning a default value by default
|
boolean |
getBooleanArgument(String[] args,
String... argnames)
Parses the arguments of the main method and tells whether a parameter is on or off
|
Boolean |
getBooleanObject(String s)
Returns a value for a boolean parameter
|
Boolean |
getBooleanObject(String s,
Boolean defaultValue)
Returns a value for a boolean parameter, returning a default value by default
|
Database |
getDatabase()
Returns the database defined in this ini-file
|
double |
getDouble(String s)
Returns a value for an integer parameter
|
Double |
getDouble(String s,
Double defaultValue)
Returns a value for an integer parameter returning the default value if undefined
|
File |
getFile(String s)
Returns a value for a file or folder parameter; same as getPath but returns an actual File instance
|
File |
getFile(String s,
File defaultValue)
Returns a value for a file or folder parameter, returning the default value if undefined; same as getPath but returns an actual File instance
|
float |
getFloat(String s)
Returns a value for an integer parameter
|
float |
getFloat(String s,
float defaultValue)
Returns a value for an integer parameter returning the default value if undefined
|
Float |
getFloatObject(String s)
Returns a value for an integer parameter
|
Float |
getFloatObject(String s,
Float defaultValue)
Returns a value for an integer parameter returning the default value if undefined
|
int |
getInt(String s)
Returns a value for an integer parameter
|
int |
getInt(String s,
int defaultValue)
Returns a value for an integer parameter returning the default value if undefined
|
Integer |
getInteger(String s)
Returns a value for an integer parameter as Integer object
|
Integer |
getInteger(String s,
Integer defaultValue)
Returns a value for an integer parameter returning the default value if undefined
|
List<String> |
getList(String s)
Returns a value for a list parameter
|
Map<String,String> |
getMap(String s)
Returns a value for a map parameter
|
String |
getMapEntry(String s,
String key)
Returns a value for a map parameter
|
String |
getOrRequest(String s,
String description)
Returns a value for a parameter.
|
String |
getOrRequestAndAdd(String s,
String description)
Returns a value for a parameter.
|
boolean |
getOrRequestAndAddBoolean(String s,
String description)
Returns a value for a parameter.
|
boolean |
getOrRequestBoolean(String s,
String description)
Returns a value for a parameter.
|
File |
getOrRequestFileParameter(String s,
String description)
Returns a value for a parameter.
|
int |
getOrRequestInteger(String s,
String description)
Returns a value for a parameter.
|
String |
getPath(String s)
Returns a value for a file or folder parameter; same as getFile but returns the path as String
also adjusts local paths such that a global path is returned (if a base path is set)
|
String |
getPath(String s,
String defaultValue)
Returns a value for a file or folder parameter, returning the default value if undefined; same as getFile but returns the path as String
|
Timestamp |
getTimestamp(String s)
Returns a value for a date parameter;
|
Timestamp |
getTimestamp(String s,
Timestamp defaultValue)
Returns a value for a date parameter, returning the default value if undefined;
|
URI |
getURI(String s)
Returns a URI built from a given parameter's value
|
URI |
getURI(String s,
URI defaultValue)
Returns a URI built from a given parameter's value, but returns a given default value if the parameter is undefined
|
URL |
getURL(String s)
Returns a URL built from a given parameter's value
|
URL |
getURL(String s,
URL defaultValue)
Returns a URL built from a given parameter's value, but returns a given default value if the parameter is undefined
|
void |
init(File f)
Initializes the parameters from a file
|
void |
init(File f,
boolean mainIni) |
void |
init(String filename)
Initializes the parameters from a file
|
void |
init(String file,
boolean mainIni) |
void |
init(String filename,
File... folders)
Seeks the file in all given folders
|
void |
initiateAllClassAttributes(Object object)
Checks for all full-upper-case class attributes whether there is a matching parameter
and sets its value to the parameter value.
|
void |
initiateClassAttributes(Object object)
Checks for all full-upper-case class attributes whether there is a matching parameter
and sets its value to the parameter value.
|
void |
initiateClassAttributes(String className,
Object object)
Checks for all full-upper-case class attributes whether there is a matching parameter
and sets its value to the parameter value.
|
boolean |
isDefined(String s)
Tells whether a parameter is defined
|
static void |
main(String[] args)
Test routine
|
Object |
matchObjectAttribut(Field field)
Deprecated.
|
Set<String> |
parameters()
Returns all defined parameters
|
String |
remove(String parameter)
Removes a value from the mapping (NOT: from the file)
|
void |
reset()
Deletes all current values
|
void |
saveAs(String file)
Stores the parameters in a given file
|
void |
set(String param,
String value)
sets a value for a parameter
|
public File iniFile
public String basePath
public static Pattern INIPATTERN
public static Pattern READTIMEVARIABLEPATTERN
public static Pattern RUNTIMVARIABLEPATTERN
public NonsharedParameters()
public NonsharedParameters(File iniFile) throws NonsharedParameters.ParameterFileException
public NonsharedParameters(String iniFile) throws NonsharedParameters.ParameterFileException
public NonsharedParameters(File iniFile, String localPath) throws NonsharedParameters.ParameterFileException
public NonsharedParameters(String iniFile, String localPath) throws NonsharedParameters.ParameterFileException
public NonsharedParameters(NonsharedParameters other)
public NonsharedParameters clone()
public Timestamp getTimestamp(String s) throws NonsharedParameters.UndefinedParameterException
public Timestamp getTimestamp(String s, Timestamp defaultValue) throws NonsharedParameters.UndefinedParameterException
public String getPath(String s) throws NonsharedParameters.UndefinedParameterException
public String getPath(String s, String defaultValue) throws NonsharedParameters.UndefinedParameterException
public File getFile(String s) throws NonsharedParameters.UndefinedParameterException
public File getFile(String s, File defaultValue) throws NonsharedParameters.UndefinedParameterException
public URI getURI(String s) throws NonsharedParameters.UndefinedParameterException, URISyntaxException
public URI getURI(String s, URI defaultValue) throws NonsharedParameters.UndefinedParameterException, URISyntaxException
public URL getURL(String s) throws NonsharedParameters.UndefinedParameterException, MalformedURLException
public URL getURL(String s, URL defaultValue) throws NonsharedParameters.UndefinedParameterException, MalformedURLException
public Integer getInteger(String s) throws NonsharedParameters.UndefinedParameterException
public Integer getInteger(String s, Integer defaultValue) throws NonsharedParameters.UndefinedParameterException
public int getInt(String s) throws NonsharedParameters.UndefinedParameterException
public int getInt(String s, int defaultValue) throws NonsharedParameters.UndefinedParameterException
public Float getFloatObject(String s) throws NonsharedParameters.UndefinedParameterException
public Float getFloatObject(String s, Float defaultValue) throws NonsharedParameters.UndefinedParameterException
public float getFloat(String s) throws NonsharedParameters.UndefinedParameterException
public float getFloat(String s, float defaultValue) throws NonsharedParameters.UndefinedParameterException
public double getDouble(String s) throws NonsharedParameters.UndefinedParameterException
public Double getDouble(String s, Double defaultValue) throws NonsharedParameters.UndefinedParameterException
public Boolean getBooleanObject(String s) throws NonsharedParameters.UndefinedParameterException
public Boolean getBooleanObject(String s, Boolean defaultValue)
public boolean getBoolean(String s) throws NonsharedParameters.UndefinedParameterException
public boolean getBoolean(String s, boolean defaultValue)
public List<String> getList(String s) throws NonsharedParameters.UndefinedParameterException
public Map<String,String> getMap(String s) throws NonsharedParameters.UndefinedParameterException
public String getMapEntry(String s, String key) throws NonsharedParameters.UndefinedParameterException
public String get(String s) throws NonsharedParameters.UndefinedParameterException
public String get(String s, String defaultValue)
public String getOrRequest(String s, String description)
public String getOrRequestAndAdd(String s, String description) throws IOException
IOExceptionpublic boolean getOrRequestAndAddBoolean(String s, String description) throws IOException
IOExceptionpublic File getOrRequestFileParameter(String s, String description)
public boolean getOrRequestBoolean(String s, String description)
public int getOrRequestInteger(String s, String description)
public void add(String key, String value) throws IOException
IOExceptionpublic void init(File f) throws NonsharedParameters.ParameterFileException
public void init(File f, boolean mainIni) throws NonsharedParameters.ParameterFileException
public void init(String filename, File... folders) throws NonsharedParameters.ParameterFileException
public void init(String filename) throws NonsharedParameters.ParameterFileException
public void init(String file, boolean mainIni) throws NonsharedParameters.ParameterFileException
public boolean isDefined(String s)
public void ensureParameters(String... p)
public boolean getBooleanArgument(String[] args, String... argnames)
public void reset()
public Database getDatabase() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException
@Deprecated public Object matchObjectAttribut(Field field) throws IllegalAccessException
field - the attribute against which to match the parametersIllegalAccessExceptionpublic void initiateClassAttributes(String className, Object object)
className - name of the class for which to checkobject - object, which needs to be an instance of the given classpublic void initiateAllClassAttributes(Object object)
object - object, which needs to be an instance of the given classpublic void initiateClassAttributes(Object object)
object - object, which needs to be an instance of the given classpublic void saveAs(String file) throws NonsharedParameters.ParameterFileException
Copyright © 2018. All rights reserved.