public class PropertiesConfiguration extends AbstractFileConfiguration
In this class, empty PropertyConfigurations can be built, properties added and later saved. include statements are (obviously) not supported if you don't construct a PropertyConfiguration from a file.
The properties file syntax is explained here, basically it follows
the syntax of the stream parsed by Properties.load(java.io.Reader)
and
adds several useful extensions:
key <separator> value
. The
separators accepted are '='
, ':'
and any white
space character. Examples:
key1 = value1 key2 : value2 key3 value3
key\:foo = bar
key = This property, has multiple, valueswill result in a property with three values. You can change the value delimiter using the
AbstractConfiguration.setListDelimiter(char)
method. Setting the delimiter to 0 will disable value splitting completely.
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? # properties can reference other properties base.prop = /base first.prop = ${base.prop}/first second.prop = ${first.prop}/second
A PropertiesConfiguration
object is associated with an
instance of the
class,
which is responsible for storing the layout of the parsed properties file
(i.e. empty lines, comments, and such things). The PropertiesConfigurationLayout
getLayout()
method can be used to obtain this layout object. With setLayout()
a new layout object can be set. This should be done before a properties file
was loaded.
Properties.load(java.io.Reader)
Modifier and Type | Class and Description |
---|---|
static class |
PropertiesConfiguration.PropertiesReader
This class is used to read properties lines.
|
static class |
PropertiesConfiguration.PropertiesWriter
This class is used to write properties lines.
|
Modifier and Type | Field and Description |
---|---|
(package private) static java.lang.String |
COMMENT_CHARS
Constant for the supported comment characters.
|
private static java.lang.String |
DEFAULT_ENCODING
The default encoding (ISO-8859-1 as specified by
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html)
|
private static int |
HEX_RADIX
Constant for the radix of hex numbers.
|
private static java.lang.String |
include
This is the name of the property that can point to other
properties file for including other properties files.
|
private boolean |
includesAllowed
Allow file inclusion or not
|
private PropertiesConfigurationLayout |
layout
Stores the layout object.
|
private static java.lang.String |
LINE_SEPARATOR
Constant for the platform specific line separator.
|
private static char[] |
SEPARATORS
The list of possible key/value separators
|
private static int |
UNICODE_LEN
Constant for the length of a unicode literal.
|
private static char[] |
WHITE_SPACE
The white space characters used as key/value separators.
|
autoSave, basePath, EVENT_RELOAD, fileName, strategy
END_TOKEN, EVENT_ADD_PROPERTY, EVENT_CLEAR, EVENT_CLEAR_PROPERTY, EVENT_READ_PROPERTY, EVENT_SET_PROPERTY, START_TOKEN
Constructor and Description |
---|
PropertiesConfiguration()
Creates an empty PropertyConfiguration object which can be
used to synthesize a new Properties file by adding values and
then saving().
|
PropertiesConfiguration(java.io.File file)
Creates and loads the extended properties from the specified file.
|
PropertiesConfiguration(java.lang.String fileName)
Creates and loads the extended properties from the specified file.
|
PropertiesConfiguration(java.net.URL url)
Creates and loads the extended properties from the specified URL.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
clone()
Creates a copy of this object.
|
protected PropertiesConfigurationLayout |
createLayout()
Creates the associated layout object.
|
java.lang.String |
getHeader()
Return the comment header.
|
static java.lang.String |
getInclude()
Gets the property value for including other properties files.
|
boolean |
getIncludesAllowed()
Reports the status of file inclusion.
|
PropertiesConfigurationLayout |
getLayout()
Returns the associated layout object.
|
(package private) static boolean |
isCommentLine(java.lang.String line)
Tests whether a line is a comment, i.e.
|
void |
load(java.io.Reader in)
Load the properties from the given reader.
|
private void |
loadIncludeFile(java.lang.String fileName)
Helper method for loading an included properties file.
|
(package private) boolean |
propertyLoaded(java.lang.String key,
java.lang.String value)
This method is invoked by the associated
object for each
property definition detected in the parsed properties file. |
void |
save(java.io.Writer writer)
Save the configuration to the specified stream.
|
void |
setBasePath(java.lang.String basePath)
Extend the setBasePath method to turn includes
on and off based on the existence of a base path.
|
void |
setHeader(java.lang.String header)
Set the comment header.
|
static void |
setInclude(java.lang.String inc)
Sets the property value for including other properties files.
|
protected void |
setIncludesAllowed(boolean includesAllowed)
Controls whether additional files can be loaded by the include =
|
void |
setLayout(PropertiesConfigurationLayout layout)
Sets the associated layout object.
|
protected static java.lang.String |
unescapeJava(java.lang.String str,
char delimiter)
Unescapes any Java literals found in the
String to a
Writer . |
addProperty, clearProperty, containsKey, enterNoReload, exitNoReload, fireEvent, getBasePath, getEncoding, getFile, getFileName, getKeys, getPath, getProperty, getReloadingStrategy, getURL, isAutoSave, isEmpty, load, load, load, load, load, load, possiblySave, reload, save, save, save, save, save, save, setAutoSave, setEncoding, setFile, setFileName, setPath, setProperty, setReloadingStrategy, setURL
addPropertyDirect, clear, clearPropertyDirect
addErrorLogListener, createInterpolator, getBigDecimal, getBigDecimal, getBigInteger, getBigInteger, getBoolean, getBoolean, getBoolean, getByte, getByte, getByte, getDefaultListDelimiter, getDelimiter, getDouble, getDouble, getDouble, getFloat, getFloat, getFloat, getInt, getInt, getInteger, getInterpolator, getKeys, getList, getList, getListDelimiter, getLogger, getLong, getLong, getLong, getProperties, getProperties, getShort, getShort, getShort, getString, getString, getStringArray, getSubstitutor, interpolate, interpolate, interpolateHelper, isDelimiterParsingDisabled, isThrowExceptionOnMissing, resolveContainerStore, setDefaultListDelimiter, setDelimiter, setDelimiterParsingDisabled, setListDelimiter, setLogger, setThrowExceptionOnMissing, subset
addConfigurationListener, addErrorListener, clearConfigurationListeners, clearErrorListeners, createErrorEvent, createEvent, fireError, getConfigurationListeners, getErrorListeners, isDetailEvents, removeConfigurationListener, removeErrorListener, setDetailEvents
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
clear, getBigDecimal, getBigDecimal, getBigInteger, getBigInteger, getBoolean, getBoolean, getBoolean, getByte, getByte, getByte, getDouble, getDouble, getDouble, getFloat, getFloat, getFloat, getInt, getInt, getInteger, getKeys, getList, getList, getLong, getLong, getLong, getProperties, getShort, getShort, getShort, getString, getString, getStringArray, subset
static final java.lang.String COMMENT_CHARS
private static java.lang.String include
private static final char[] SEPARATORS
private static final char[] WHITE_SPACE
private static final java.lang.String DEFAULT_ENCODING
private static final java.lang.String LINE_SEPARATOR
private static final int HEX_RADIX
private static final int UNICODE_LEN
private PropertiesConfigurationLayout layout
private boolean includesAllowed
public PropertiesConfiguration()
public PropertiesConfiguration(java.lang.String fileName) throws ConfigurationException
fileName
- The name of the properties file to load.ConfigurationException
- Error while loading the properties filepublic PropertiesConfiguration(java.io.File file) throws ConfigurationException
file
- The properties file to load.ConfigurationException
- Error while loading the properties filepublic PropertiesConfiguration(java.net.URL url) throws ConfigurationException
url
- The location of the properties file to load.ConfigurationException
- Error while loading the properties filepublic static java.lang.String getInclude()
public static void setInclude(java.lang.String inc)
inc
- A String.protected void setIncludesAllowed(boolean includesAllowed)
includesAllowed
- includesAllowed True if Includes are allowed.public boolean getIncludesAllowed()
public java.lang.String getHeader()
public void setHeader(java.lang.String header)
header
- the header to usepublic PropertiesConfigurationLayout getLayout()
public void setLayout(PropertiesConfigurationLayout layout)
layout
- the new layout object; can be null, then a new
layout object will be createdprotected PropertiesConfigurationLayout createLayout()
public void load(java.io.Reader in) throws ConfigurationException
clear()
method is not called, so
the properties contained in the loaded file will be added to the
actual set of properties.in
- An InputStream.ConfigurationException
- if an error occurspublic void save(java.io.Writer writer) throws ConfigurationException
writer
- the output stream used to save the configurationConfigurationException
- if an error occurspublic void setBasePath(java.lang.String basePath)
setBasePath
in interface FileConfiguration
setBasePath
in class AbstractFileConfiguration
basePath
- The new basePath to set.public java.lang.Object clone()
clone
in class AbstractFileConfiguration
boolean propertyLoaded(java.lang.String key, java.lang.String value) throws ConfigurationException
PropertiesConfigurationLayout
object for each
property definition detected in the parsed properties file. Its task is
to check whether this is a special property definition (e.g. the
include
property). If not, the property must be added to
this configuration. The return value indicates whether the property
should be treated as a normal property. If it is false, the
layout object will ignore this property.key
- the property keyvalue
- the property valueConfigurationException
- if an error occursstatic boolean isCommentLine(java.lang.String line)
line
- the lineprotected static java.lang.String unescapeJava(java.lang.String str, char delimiter)
Unescapes any Java literals found in the String
to a
Writer
.
str
- the String
to unescape, may be nulldelimiter
- the delimiter for multi-valued propertiesjava.lang.IllegalArgumentException
- if the Writer is null
private void loadIncludeFile(java.lang.String fileName) throws ConfigurationException
load()
when an include
property
is encountered. It tries to resolve relative file names based on the
current base path. If this fails, a resolution based on the location of
this properties file is tried.fileName
- the name of the file to loadConfigurationException
- if loading fails