Class ExtProperties
- All Implemented Interfaces:
Serializable
,Cloneable
,Map<String,
Object>
Please consider using the PropertiesConfiguration
class in
Commons-Configuration as soon as it is released.
The Extended Properties syntax is explained here:
-
Each property has the syntax
key = value
- The key may use any character but the equal sign '='.
- value may be separated on different lines if a backslash is placed at the end of the line that continues below.
- If value is a list of strings, each token is separated by a comma ','.
- Commas in each token are escaped placing a backslash right before the comma.
- Backslashes are escaped by using two consecutive backslashes i.e. \\
- If a key is used more than once, the values are appended as if they were on the same line separated with commas.
- Blank lines and lines starting with character '#' are skipped.
- If a property is named "include" (or whatever is defined by setInclude() and getInclude() and the value of that property is the full path to a file on disk, that file will be included into the ConfigurationsRepository. You can also pull in files relative to the parent configuration file. So if you have something like the following: include = additional.properties Then "additional.properties" is expected to be in the same directory as the parent configuration file. Duplicate name values will be replaced, so be careful.
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)
- Since:
- 2.0
- Version:
- $Revision: $, $Id: ExtProperties.java$
- Author:
- Stefano Mazzocchi, Jon S. Stevens, Dave Bryson, Jason van Zyl, Geir Magnusson Jr., Leon Messerschmidt, Kent Johnson, Daniel Rall, Ilkka Priha, Janek Bogucki, Mohan Kishore, Stephen Colebourne, Shinobu Kawai, Henning P. Schmiedehausen, Claude Brisson
- See Also:
-
Nested Class Summary
-
Field Summary
Modifier and TypeFieldDescriptionprotected String
Base path of the configuration file used to create this ExtProperties object.protected static final String
protected String
The file connected to this repository (holding comments and such).protected String
File separator.protected static String
This is the name of the property that can point to other properties file for including other properties files.protected boolean
Has this configuration been initialized.These are the keys in the order they listed in the configuration file.protected static final String
Fields inherited from class org.apache.velocity.util.DeprecationAwareExtProperties
logger
-
Constructor Summary
ConstructorDescriptionCreates 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. -
Method Summary
Modifier and TypeMethodDescriptionvoid
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.getBoolean
(String key, Boolean defaultValue) Get a boolean associated with the given configuration key.byte
Get a byte associated with the given configuration key.byte
Get a byte associated with the given configuration key.Get a byte associated with the given configuration key.double
Get a double associated with the given configuration key.double
Get a double associated with the given configuration key.Get a double associated with the given configuration key.float
Get a float associated with the given configuration key.float
Get a float associated with the given configuration key.Get a float associated with the given configuration key.Gets the property value for including other properties files.int
The purpose of this method is to get the configuration resource with the given name as an integer.int
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.getInteger
(String key, Integer defaultValue) Get a int associated with the given configuration key.getKeys()
Get the list of the keys contained in the configuration repository.Get the list of the keys contained in the configuration repository that match the specified prefix.Get a List of strings associated with the given configuration key.Get a List of strings associated with the given configuration key.long
Get a long associated with the given configuration key.long
Get a long associated with the given configuration key.Get a long associated with the given configuration key.getProperties
(String key) Get a list of properties associated with the given configuration key.getProperties
(String key, Properties defaults) Get a list of properties associated with the given configuration key.getProperty
(String key) Gets a property from the configuration.short
Get a short associated with the given configuration key.short
Get a short associated with the given configuration key.Get a short associated with the given configuration key.Get a string associated with the given configuration key.Get a string associated with the given configuration key.String[]
getStringArray
(String key) Get an array of strings associated with the given configuration key.Get a Vector of strings associated with the given configuration key.Get a Vector of strings associated with the given configuration key.protected String
interpolate
(String base) Interpolate key names to handle ${key} stuffprotected String
interpolateHelper
(String base, List<String> priorVariables) Recursive handler for multiple levels of interpolation.boolean
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.Create an ExtProperties object that is a subset of this one.testBoolean
(String value) Test whether the string represent by value maps to a boolean value or not.Methods inherited from class org.apache.velocity.util.DeprecationAwareExtProperties
containsKey, get, put, translateKey, warnDeprecated
Methods inherited from class java.util.Hashtable
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
-
Field Details
-
file
The file connected to this repository (holding comments and such). -
basePath
Base path of the configuration file used to create this ExtProperties object. -
fileSeparator
File separator. -
isInitialized
protected boolean isInitializedHas this configuration been initialized. -
include
This is the name of the property that can point to other properties file for including other properties files. -
keysAsListed
These are the keys in the order they listed in the configuration file. This is useful when you wish to perform operations with configuration information in a particular order. -
START_TOKEN
- See Also:
-
END_TOKEN
- See Also:
-
-
Constructor Details
-
ExtProperties
public ExtProperties()Creates an empty extended properties object. -
ExtProperties
Creates and loads the extended properties from the specified file.- Parameters:
file
- the filename to load- Throws:
IOException
- if a file error occurs
-
ExtProperties
Creates and loads the extended properties from the specified file.- Parameters:
file
- the filename to loaddefaultFile
- a second filename to load default values from- Throws:
IOException
- if a file error occurs
-
-
Method Details
-
interpolate
Interpolate key names to handle ${key} stuff- Parameters:
base
- string to interpolate- Returns:
- returns the key name with the ${key} substituted
-
interpolateHelper
Recursive handler for multiple levels of interpolation. When called the first time, priorVariables should be null.- Parameters:
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.- Returns:
- the string with the interpolation taken care of
-
isInitialized
public boolean isInitialized()Indicate to client code whether property resources have been initialized or not.- Returns:
- initialization status
-
getInclude
Gets the property value for including other properties files. By default it is "include".- Returns:
- A String.
-
setInclude
Sets the property value for including other properties files. By default it is "include".- Parameters:
inc
- A String.
-
load
Load the properties from the given input stream.- Parameters:
input
- the InputStream to load from- Throws:
IOException
- if an IO error occurs
-
load
Load the properties from the given input stream and using the specified encoding.- Parameters:
input
- the InputStream to load fromenc
- the encoding to use- Throws:
IOException
- if an IO error occurs
-
getProperty
Gets a property from the configuration.- Parameters:
key
- property to retrieve- Returns:
- value as object. Will return user value if exists, if not then default value if exists, otherwise null
-
addProperty
Add a property to the configuration. If it already exists then the value stated here will be added to the configuration entry. For example, ifresource.loaders = file
is already present in the configuration and youaddProperty("resource.loaders", "classpath")
Then you will end up with a Vector like the following:["file", "classpath"]
- Parameters:
key
- the key to addvalue
- the value to add
-
setProperty
Set a property, this will replace any previously set values. Set values is implicitly a call to clearProperty(key), addProperty(key,value).- Parameters:
key
- the key to setvalue
- the value to set
-
save
Save the properties to the given output stream.The stream is not closed, but it is flushed.
- Parameters:
output
- an OutputStream, may be nullheader
- a textual comment to act as a file header- Throws:
IOException
- if an IO error occurs
-
combine
Combines an existing Hashtable with this Hashtable.Warning: It will overwrite previous entries without warning.
- Parameters:
props
- the properties to combine
-
clearProperty
Clear a property in the configuration.- Parameters:
key
- the property key to remove along with corresponding value
-
getKeys
Get the list of the keys contained in the configuration repository.- Returns:
- an Iterator over the keys
-
getKeys
Get the list of the keys contained in the configuration repository that match the specified prefix.- Parameters:
prefix
- the prefix to match- Returns:
- an Iterator of keys that match the prefix
-
subset
Create an ExtProperties object that is a subset of this one. Take into account duplicate keys by using the setProperty() in ExtProperties.- Parameters:
prefix
- the prefix to get a subset for- Returns:
- a new independent ExtProperties
-
display
public void display()Display the configuration for debugging purposes to System.out. -
getString
Get a string associated with the given configuration key.- Parameters:
key
- The configuration key.- Returns:
- The associated string.
- Throws:
ClassCastException
- is thrown if the key maps to an object that is not a String.
-
getString
Get a string associated with the given configuration key.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated string if key is found, default value otherwise.
- Throws:
ClassCastException
- is thrown if the key maps to an object that is not a String.
-
getProperties
Get a list of properties associated with the given configuration key.- Parameters:
key
- The configuration key.- Returns:
- The associated properties if key is found.
- Throws:
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).
-
getProperties
Get a list of properties associated with the given configuration key.- Parameters:
key
- The configuration key.defaults
- default values- Returns:
- The associated properties if key is found.
- Throws:
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).
-
getStringArray
Get an array of strings associated with the given configuration key.- Parameters:
key
- The configuration key.- Returns:
- The associated string array if key is found.
- Throws:
ClassCastException
- is thrown if the key maps to an object that is not a String/List.
-
getVector
Get a Vector of strings associated with the given configuration key.- Parameters:
key
- The configuration key.- Returns:
- The associated Vector.
- Throws:
ClassCastException
- is thrown if the key maps to an object that is not a Vector.
-
getVector
Get a Vector of strings associated with the given configuration key.The list is a copy of the internal data of this object, and as such you may alter it freely.
- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated Vector.
- Throws:
ClassCastException
- is thrown if the key maps to an object that is not a Vector.
-
getList
Get a List of strings associated with the given configuration key.The list is a copy of the internal data of this object, and as such you may alter it freely.
- Parameters:
key
- The configuration key.- Returns:
- The associated List object.
- Throws:
ClassCastException
- is thrown if the key maps to an object that is not a List.- Since:
- Commons Collections 3.2
-
getList
Get a List of strings associated with the given configuration key.The list is a copy of the internal data of this object, and as such you may alter it freely.
- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated List.
- Throws:
ClassCastException
- is thrown if the key maps to an object that is not a List.- Since:
- Commons Collections 3.2
-
getBoolean
Get a boolean associated with the given configuration key.- Parameters:
key
- The configuration key.- Returns:
- The associated boolean.
- Throws:
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.
-
getBoolean
Get a boolean associated with the given configuration key.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated boolean.
- Throws:
ClassCastException
- is thrown if the key maps to an object that is not a Boolean.
-
getBoolean
Get a boolean associated with the given configuration key.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated boolean if key is found and has valid format, default value otherwise.
- Throws:
ClassCastException
- is thrown if the key maps to an object that is not a Boolean.
-
testBoolean
Test whether the string represent by value maps to a boolean value or not. We will allowtrue
,on
, andyes
for atrue
boolean value, andfalse
,off
, andno
forfalse
boolean values. Case of value to test for boolean status is ignored.- Parameters:
value
- the value to test for boolean state- Returns:
true
orfalse
if the supplied text maps to a boolean value, ornull
otherwise.
-
getByte
Get a byte associated with the given configuration key.- Parameters:
key
- The configuration key.- Returns:
- The associated byte.
- Throws:
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.
-
getByte
Get a byte associated with the given configuration key.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated byte.
- Throws:
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.
-
getByte
Get a byte associated with the given configuration key.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated byte if key is found and has valid format, default value otherwise.
- Throws:
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.
-
getShort
Get a short associated with the given configuration key.- Parameters:
key
- The configuration key.- Returns:
- The associated short.
- Throws:
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.
-
getShort
Get a short associated with the given configuration key.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated short.
- Throws:
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.
-
getShort
Get a short associated with the given configuration key.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated short if key is found and has valid format, default value otherwise.
- Throws:
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.
-
getInt
The purpose of this method is to get the configuration resource with the given name as an integer.- Parameters:
name
- The resource name.- Returns:
- The value of the resource as an integer.
-
getInt
The purpose of this method is to get the configuration resource with the given name as an integer, or a default value.- Parameters:
name
- The resource namedef
- The default value of the resource.- Returns:
- The value of the resource as an integer.
-
getInteger
Get a int associated with the given configuration key.- Parameters:
key
- The configuration key.- Returns:
- The associated int.
- Throws:
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.
-
getInteger
Get a int associated with the given configuration key.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated int.
- Throws:
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.
-
getInteger
Get a int associated with the given configuration key.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated int if key is found and has valid format, default value otherwise.
- Throws:
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.
-
getLong
Get a long associated with the given configuration key.- Parameters:
key
- The configuration key.- Returns:
- The associated long.
- Throws:
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.
-
getLong
Get a long associated with the given configuration key.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated long.
- Throws:
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.
-
getLong
Get a long associated with the given configuration key.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated long if key is found and has valid format, default value otherwise.
- Throws:
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.
-
getFloat
Get a float associated with the given configuration key.- Parameters:
key
- The configuration key.- Returns:
- The associated float.
- Throws:
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.
-
getFloat
Get a float associated with the given configuration key.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated float.
- Throws:
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.
-
getFloat
Get a float associated with the given configuration key.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated float if key is found and has valid format, default value otherwise.
- Throws:
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.
-
getDouble
Get a double associated with the given configuration key.- Parameters:
key
- The configuration key.- Returns:
- The associated double.
- Throws:
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.
-
getDouble
Get a double associated with the given configuration key.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated double.
- Throws:
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.
-
getDouble
Get a double associated with the given configuration key.- Parameters:
key
- The configuration key.defaultValue
- The default value.- Returns:
- The associated double if key is found and has valid format, default value otherwise.
- Throws:
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.
-
convertProperties
Convert a standard properties class into a configuration class.NOTE: From Commons Collections 3.2 this method will pick up any default parent Properties of the specified input object.
- Parameters:
props
- the properties object to convert- Returns:
- new ExtProperties created from props
-
convertProperties
Convert a Map into a configuration class.NOTE: From Commons Collections 3.2 this method will pick up any default parent Properties of the specified input object.
- Parameters:
props
- the Map object to convert- Returns:
- new ExtProperties created from props
-