public class ExtProperties extends DeprecationAwareExtProperties
Please consider using the PropertiesConfiguration
class in
Commons-Configuration as soon as it is released.
The Extended Properties syntax is explained here:
key = value
Here is an example of a valid extended properties file:
# lines starting with # are comments
# This is the simplest property
key = value
# A long property may be separated on multiple lines
longvalue = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
# This is a property with many tokens
tokens_on_a_line = first token, second token
# This sequence generates exactly the same result
tokens_on_multiple_lines = first token
tokens_on_multiple_lines = second token
# commas may be escaped in tokens
commas.escaped = Hi\, what'up?
NOTE: this class has not been written for performance nor low memory usage. In fact, it's way slower than it could be and generates too much memory garbage. But since performance is not an issue during intialization (and there is not much time to improve it), I wrote it this way. If you don't like it, go ahead and tune it up! This class is a clone of org.apache.commons.collections.ExtendedProperties (which has been removed from commons-collections-4.x)
Modifier and Type | Field and Description |
---|---|
protected String |
basePath
Base path of the configuration file used to create
this ExtProperties object.
|
protected static String |
END_TOKEN |
protected String |
file
The file connected to this repository (holding comments and
such).
|
protected String |
fileSeparator
File separator.
|
protected static String |
include
This is the name of the property that can point to other
properties file for including other properties files.
|
protected boolean |
isInitialized
Has this configuration been initialized.
|
protected ArrayList<String> |
keysAsListed
These are the keys in the order they listed
in the configuration file.
|
protected static String |
START_TOKEN |
logger
Constructor and Description |
---|
ExtProperties()
Creates an empty extended properties object.
|
ExtProperties(String file)
Creates and loads the extended properties from the specified file.
|
ExtProperties(String file,
String defaultFile)
Creates and loads the extended properties from the specified file.
|
Modifier and Type | Method and Description |
---|---|
void |
addProperty(String key,
Object value)
Add a property to the configuration.
|
void |
clearProperty(String key)
Clear a property in the configuration.
|
void |
combine(ExtProperties props)
Combines an existing Hashtable with this Hashtable.
|
static ExtProperties |
convertProperties(Map props)
Convert a Map into a configuration class.
|
static ExtProperties |
convertProperties(Properties props)
Convert a standard properties class into a configuration class.
|
void |
display()
Display the configuration for debugging purposes to System.out.
|
boolean |
getBoolean(String key)
Get a boolean associated with the given configuration key.
|
boolean |
getBoolean(String key,
boolean defaultValue)
Get a boolean associated with the given configuration key.
|
Boolean |
getBoolean(String key,
Boolean defaultValue)
Get a boolean associated with the given configuration key.
|
byte |
getByte(String key)
Get a byte associated with the given configuration key.
|
byte |
getByte(String key,
byte defaultValue)
Get a byte associated with the given configuration key.
|
Byte |
getByte(String key,
Byte defaultValue)
Get a byte associated with the given configuration key.
|
double |
getDouble(String key)
Get a double associated with the given configuration key.
|
double |
getDouble(String key,
double defaultValue)
Get a double associated with the given configuration key.
|
Double |
getDouble(String key,
Double defaultValue)
Get a double associated with the given configuration key.
|
float |
getFloat(String key)
Get a float associated with the given configuration key.
|
float |
getFloat(String key,
float defaultValue)
Get a float associated with the given configuration key.
|
Float |
getFloat(String key,
Float defaultValue)
Get a float associated with the given configuration key.
|
String |
getInclude()
Gets the property value for including other properties files.
|
int |
getInt(String name)
The purpose of this method is to get the configuration resource
with the given name as an integer.
|
int |
getInt(String name,
int def)
The purpose of this method is to get the configuration resource
with the given name as an integer, or a default value.
|
int |
getInteger(String key)
Get a int associated with the given configuration key.
|
int |
getInteger(String key,
int defaultValue)
Get a int associated with the given configuration key.
|
Integer |
getInteger(String key,
Integer defaultValue)
Get a int associated with the given configuration key.
|
Iterator<String> |
getKeys()
Get the list of the keys contained in the configuration
repository.
|
Iterator<String> |
getKeys(String prefix)
Get the list of the keys contained in the configuration
repository that match the specified prefix.
|
List |
getList(String key)
Get a List of strings associated with the given configuration key.
|
List |
getList(String key,
List defaultValue)
Get a List of strings associated with the given configuration key.
|
long |
getLong(String key)
Get a long associated with the given configuration key.
|
long |
getLong(String key,
long defaultValue)
Get a long associated with the given configuration key.
|
Long |
getLong(String key,
Long defaultValue)
Get a long associated with the given configuration key.
|
Properties |
getProperties(String key)
Get a list of properties associated with the given
configuration key.
|
Properties |
getProperties(String key,
Properties defaults)
Get a list of properties associated with the given
configuration key.
|
Object |
getProperty(String key)
Gets a property from the configuration.
|
short |
getShort(String key)
Get a short associated with the given configuration key.
|
short |
getShort(String key,
short defaultValue)
Get a short associated with the given configuration key.
|
Short |
getShort(String key,
Short defaultValue)
Get a short associated with the given configuration key.
|
String |
getString(String key)
Get a string associated with the given configuration key.
|
String |
getString(String key,
String defaultValue)
Get a string associated with the given configuration key.
|
String[] |
getStringArray(String key)
Get an array of strings associated with the given configuration
key.
|
Vector |
getVector(String key)
Get a Vector of strings associated with the given configuration
key.
|
Vector |
getVector(String key,
Vector defaultValue)
Get a Vector of strings associated with the given configuration key.
|
protected String |
interpolate(String base)
Interpolate key names to handle ${key} stuff
|
protected String |
interpolateHelper(String base,
List priorVariables)
Recursive handler for multiple levels of interpolation.
|
boolean |
isInitialized()
Indicate to client code whether property
resources have been initialized or not.
|
void |
load(InputStream input)
Load the properties from the given input stream.
|
void |
load(InputStream input,
String enc)
Load the properties from the given input stream
and using the specified encoding.
|
void |
save(OutputStream output,
String header)
Save the properties to the given output stream.
|
void |
setInclude(String inc)
Sets the property value for including other properties files.
|
void |
setProperty(String key,
Object value)
Set a property, this will replace any previously
set values.
|
ExtProperties |
subset(String prefix)
Create an ExtProperties object that is a subset
of this one.
|
String |
testBoolean(String value)
Test whether the string represent by value maps to a boolean
value or not.
|
containsKey, get, put, translateKey, warnDeprecated
clear, clone, compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keys, keySet, merge, putAll, putIfAbsent, rehash, remove, remove, replace, replace, replaceAll, size, toString, values
protected String file
protected String basePath
protected String fileSeparator
protected boolean isInitialized
protected static String include
protected ArrayList<String> keysAsListed
protected static final String START_TOKEN
protected static final String END_TOKEN
public ExtProperties()
public ExtProperties(String file) throws IOException
file
- the filename to loadIOException
- if a file error occurspublic ExtProperties(String file, String defaultFile) throws IOException
file
- the filename to loaddefaultFile
- a second filename to load default values fromIOException
- if a file error occursprotected String interpolate(String base)
base
- string to interpolateprotected String interpolateHelper(String base, List priorVariables)
base
- string with the ${key} variablespriorVariables
- serves two purposes: to allow checking for
loops, and creating a meaningful exception message should a loop
occur. It's 0'th element will be set to the value of base from
the first call. All subsequent interpolated variables are added
afterward.public boolean isInitialized()
public String getInclude()
public void setInclude(String inc)
inc
- A String.public void load(InputStream input) throws IOException
input
- the InputStream to load fromIOException
- if an IO error occurspublic void load(InputStream input, String enc) throws IOException
input
- the InputStream to load fromenc
- the encoding to useIOException
- if an IO error occurspublic Object getProperty(String key)
key
- property to retrievepublic void addProperty(String key, Object value)
resource.loaders = file
is already present in the configuration and you
addProperty("resource.loaders", "classpath")
Then you will end up with a Vector like the
following:
["file", "classpath"]
key
- the key to addvalue
- the value to addpublic void setProperty(String key, Object value)
key
- the key to setvalue
- the value to setpublic void save(OutputStream output, String header) throws IOException
The stream is not closed, but it is flushed.
output
- an OutputStream, may be nullheader
- a textual comment to act as a file headerIOException
- if an IO error occurspublic void combine(ExtProperties props)
Warning: It will overwrite previous entries without warning.
props
- the properties to combinepublic void clearProperty(String key)
key
- the property key to remove along with corresponding valuepublic Iterator<String> getKeys()
public Iterator<String> getKeys(String prefix)
prefix
- the prefix to matchpublic ExtProperties subset(String prefix)
prefix
- the prefix to get a subset forpublic void display()
public String getString(String key)
key
- The configuration key.ClassCastException
- is thrown if the key maps to an
object that is not a String.public String getString(String key, String defaultValue)
key
- The configuration key.defaultValue
- The default value.ClassCastException
- is thrown if the key maps to an
object that is not a String.public Properties getProperties(String key)
key
- The configuration key.ClassCastException
- is thrown if the key maps to an
object that is not a String/List.IllegalArgumentException
- if one of the tokens is
malformed (does not contain an equals sign).public Properties getProperties(String key, Properties defaults)
key
- The configuration key.defaults
- default valuesClassCastException
- is thrown if the key maps to an
object that is not a String/List.IllegalArgumentException
- if one of the tokens is
malformed (does not contain an equals sign).public String[] getStringArray(String key)
key
- The configuration key.ClassCastException
- is thrown if the key maps to an
object that is not a String/List.public Vector getVector(String key)
key
- The configuration key.ClassCastException
- is thrown if the key maps to an
object that is not a Vector.public Vector getVector(String key, Vector defaultValue)
The list is a copy of the internal data of this object, and as such you may alter it freely.
key
- The configuration key.defaultValue
- The default value.ClassCastException
- is thrown if the key maps to an
object that is not a Vector.public List getList(String key)
The list is a copy of the internal data of this object, and as such you may alter it freely.
key
- The configuration key.ClassCastException
- is thrown if the key maps to an
object that is not a List.public List getList(String key, List defaultValue)
The list is a copy of the internal data of this object, and as such you may alter it freely.
key
- The configuration key.defaultValue
- The default value.ClassCastException
- is thrown if the key maps to an
object that is not a List.public boolean getBoolean(String key)
key
- The configuration key.NoSuchElementException
- is thrown if the key doesn't
map to an existing object.ClassCastException
- is thrown if the key maps to an
object that is not a Boolean.public boolean getBoolean(String key, boolean defaultValue)
key
- The configuration key.defaultValue
- The default value.ClassCastException
- is thrown if the key maps to an
object that is not a Boolean.public Boolean getBoolean(String key, Boolean defaultValue)
key
- The configuration key.defaultValue
- The default value.ClassCastException
- is thrown if the key maps to an
object that is not a Boolean.public String testBoolean(String value)
true
, on
,
and yes
for a true
boolean value, and
false
, off
, and no
for
false
boolean values. Case of value to test for
boolean status is ignored.value
- the value to test for boolean statetrue
or false
if the supplied
text maps to a boolean value, or null
otherwise.public byte getByte(String key)
key
- The configuration key.NoSuchElementException
- is thrown if the key doesn't
map to an existing object.ClassCastException
- is thrown if the key maps to an
object that is not a Byte.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public byte getByte(String key, byte defaultValue)
key
- The configuration key.defaultValue
- The default value.ClassCastException
- is thrown if the key maps to an
object that is not a Byte.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public Byte getByte(String key, Byte defaultValue)
key
- The configuration key.defaultValue
- The default value.ClassCastException
- is thrown if the key maps to an
object that is not a Byte.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public short getShort(String key)
key
- The configuration key.NoSuchElementException
- is thrown if the key doesn't
map to an existing object.ClassCastException
- is thrown if the key maps to an
object that is not a Short.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public short getShort(String key, short defaultValue)
key
- The configuration key.defaultValue
- The default value.ClassCastException
- is thrown if the key maps to an
object that is not a Short.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public Short getShort(String key, Short defaultValue)
key
- The configuration key.defaultValue
- The default value.ClassCastException
- is thrown if the key maps to an
object that is not a Short.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public int getInt(String name)
name
- The resource name.public int getInt(String name, int def)
name
- The resource namedef
- The default value of the resource.public int getInteger(String key)
key
- The configuration key.NoSuchElementException
- is thrown if the key doesn't
map to an existing object.ClassCastException
- is thrown if the key maps to an
object that is not a Integer.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public int getInteger(String key, int defaultValue)
key
- The configuration key.defaultValue
- The default value.ClassCastException
- is thrown if the key maps to an
object that is not a Integer.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public Integer getInteger(String key, Integer defaultValue)
key
- The configuration key.defaultValue
- The default value.ClassCastException
- is thrown if the key maps to an
object that is not a Integer.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public long getLong(String key)
key
- The configuration key.NoSuchElementException
- is thrown if the key doesn't
map to an existing object.ClassCastException
- is thrown if the key maps to an
object that is not a Long.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public long getLong(String key, long defaultValue)
key
- The configuration key.defaultValue
- The default value.ClassCastException
- is thrown if the key maps to an
object that is not a Long.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public Long getLong(String key, Long defaultValue)
key
- The configuration key.defaultValue
- The default value.ClassCastException
- is thrown if the key maps to an
object that is not a Long.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public float getFloat(String key)
key
- The configuration key.NoSuchElementException
- is thrown if the key doesn't
map to an existing object.ClassCastException
- is thrown if the key maps to an
object that is not a Float.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public float getFloat(String key, float defaultValue)
key
- The configuration key.defaultValue
- The default value.ClassCastException
- is thrown if the key maps to an
object that is not a Float.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public Float getFloat(String key, Float defaultValue)
key
- The configuration key.defaultValue
- The default value.ClassCastException
- is thrown if the key maps to an
object that is not a Float.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public double getDouble(String key)
key
- The configuration key.NoSuchElementException
- is thrown if the key doesn't
map to an existing object.ClassCastException
- is thrown if the key maps to an
object that is not a Double.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public double getDouble(String key, double defaultValue)
key
- The configuration key.defaultValue
- The default value.ClassCastException
- is thrown if the key maps to an
object that is not a Double.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public Double getDouble(String key, Double defaultValue)
key
- The configuration key.defaultValue
- The default value.ClassCastException
- is thrown if the key maps to an
object that is not a Double.NumberFormatException
- is thrown if the value mapped
by the key has not a valid number format.public static ExtProperties convertProperties(Properties props)
NOTE: From Commons Collections 3.2 this method will pick up any default parent Properties of the specified input object.
props
- the properties object to convertpublic static ExtProperties convertProperties(Map props)
NOTE: From Commons Collections 3.2 this method will pick up any default parent Properties of the specified input object.
props
- the Map object to convertCopyright © 2000–2020 The Apache Software Foundation. All rights reserved.