Class XMLPropertiesConfiguration
- java.lang.Object
-
- org.apache.commons.configuration2.event.BaseEventSource
-
- org.apache.commons.configuration2.AbstractConfiguration
-
- org.apache.commons.configuration2.BaseConfiguration
-
- org.apache.commons.configuration2.XMLPropertiesConfiguration
-
- All Implemented Interfaces:
java.lang.Cloneable
,Configuration
,EventSource
,FileBasedConfiguration
,ImmutableConfiguration
,FileBased
,FileLocatorAware
,SynchronizerSupport
public class XMLPropertiesConfiguration extends BaseConfiguration implements FileBasedConfiguration, FileLocatorAware
This configuration implements the XML properties format introduced in Java, see https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html. An XML properties file looks like this:<?xml version="1.0"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>Description of the property list</comment> <entry key="key1">value1</entry> <entry key="key2">value2</entry> <entry key="key3">value3</entry> </properties>
The Java runtime is not required to use this class. The default encoding for this configuration format is UTF-8. Note that unlikePropertiesConfiguration
,XMLPropertiesConfiguration
does not support includes. Note:Configuration objects of this type can be read concurrently by multiple threads. However if one of these threads modifies the object, synchronization has to be performed manually.- Since:
- 1.1
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
XMLPropertiesConfiguration.XMLPropertiesHandler
SAX Handler to parse a XML properties file.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
DEFAULT_ENCODING
The default encoding (UTF-8 as specified by https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html)private java.lang.String
header
Stores a header comment.private FileLocator
locator
The temporary file locator.private static java.lang.String
MALFORMED_XML_EXCEPTION
Default string used when the XML is malformed
-
Constructor Summary
Constructors Constructor Description XMLPropertiesConfiguration()
Creates an empty XMLPropertyConfiguration object which can be used to synthesize a new Properties file by adding values and then saving().XMLPropertiesConfiguration(org.w3c.dom.Element element)
Creates and loads the XML properties from the specified DOM node.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.lang.String
escapeValue(java.lang.Object value)
Escapes a property value before it is written to disk.java.lang.String
getHeader()
Gets the header comment of this configuration.void
initFileLocator(FileLocator locator)
Initializes this object with aFileLocator
.void
load(org.w3c.dom.Element element)
Parses a DOM element containing the properties.void
read(java.io.Reader in)
Reads the content of this object from the given reader.void
save(org.w3c.dom.Document document, org.w3c.dom.Node parent)
Writes the configuration as child to the given DOM nodevoid
setHeader(java.lang.String header)
Sets the header comment of this configuration.void
write(java.io.Writer out)
Writes the content of this object to the given writer.private void
writeProperty(java.io.PrintWriter out, java.lang.String key, java.lang.Object value)
Write a property.private void
writeProperty(java.io.PrintWriter out, java.lang.String key, java.util.List<?> values)
Write a list property.private void
writeProperty(org.w3c.dom.Document document, org.w3c.dom.Node properties, java.lang.String key, java.lang.Object value)
private void
writeProperty(org.w3c.dom.Document document, org.w3c.dom.Node properties, java.lang.String key, java.util.List<?> values)
-
Methods inherited from class org.apache.commons.configuration2.BaseConfiguration
addPropertyDirect, clearInternal, clearPropertyDirect, clone, containsKeyInternal, containsValueInternal, getKeysInternal, getPropertyInternal, isEmptyInternal, sizeInternal
-
Methods inherited from class org.apache.commons.configuration2.AbstractConfiguration
addErrorLogListener, addProperty, addPropertyInternal, append, beginRead, beginWrite, clear, clearProperty, cloneInterpolator, contains, containsKey, containsValue, copy, endRead, endWrite, get, get, getArray, getArray, getBigDecimal, getBigDecimal, getBigInteger, getBigInteger, getBoolean, getBoolean, getBoolean, getByte, getByte, getByte, getCollection, getCollection, getConfigurationDecoder, getConversionHandler, getDouble, getDouble, getDouble, getDuration, getDuration, getEncodedString, getEncodedString, getFloat, getFloat, getFloat, getInt, getInt, getInteger, getInterpolator, getKeys, getKeys, getKeys, getKeysInternal, getKeysInternal, getList, getList, getList, getList, getListDelimiterHandler, getLogger, getLong, getLong, getLong, getProperties, getProperties, getProperty, getShort, getShort, getShort, getString, getString, getStringArray, getSynchronizer, immutableSubset, initLogger, installInterpolator, interpolate, interpolate, interpolatedConfiguration, isEmpty, isScalarValue, isThrowExceptionOnMissing, lock, setConfigurationDecoder, setConversionHandler, setDefaultLookups, setInterpolator, setListDelimiterHandler, setLogger, setParentInterpolator, setPrefixLookups, setProperty, setPropertyInternal, setSynchronizer, setThrowExceptionOnMissing, size, subset, unlock
-
Methods inherited from class org.apache.commons.configuration2.event.BaseEventSource
addEventListener, clearErrorListeners, clearEventListeners, copyEventListeners, createErrorEvent, createEvent, fireError, fireEvent, getEventListenerRegistrations, getEventListeners, isDetailEvents, removeEventListener, setDetailEvents
-
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.commons.configuration2.Configuration
addProperty, clear, clearProperty, getInterpolator, installInterpolator, setInterpolator, setProperty, subset
-
Methods inherited from interface org.apache.commons.configuration2.ImmutableConfiguration
containsKey, containsValue, get, get, getArray, getArray, getBigDecimal, getBigDecimal, getBigInteger, getBigInteger, getBoolean, getBoolean, getBoolean, getByte, getByte, getByte, getCollection, getCollection, getDouble, getDouble, getDouble, getDuration, getDuration, getEncodedString, getEncodedString, getEnum, getEnum, getFloat, getFloat, getFloat, getInt, getInt, getInteger, getKeys, getKeys, getKeys, getList, getList, getList, getList, getLong, getLong, getLong, getProperties, getProperty, getShort, getShort, getShort, getString, getString, getStringArray, immutableSubset, isEmpty, size
-
Methods inherited from interface org.apache.commons.configuration2.sync.SynchronizerSupport
getSynchronizer, lock, setSynchronizer, unlock
-
-
-
-
Field Detail
-
DEFAULT_ENCODING
public static final java.lang.String DEFAULT_ENCODING
The default encoding (UTF-8 as specified by https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html)
-
MALFORMED_XML_EXCEPTION
private static final java.lang.String MALFORMED_XML_EXCEPTION
Default string used when the XML is malformed- See Also:
- Constant Field Values
-
locator
private FileLocator locator
The temporary file locator.
-
header
private java.lang.String header
Stores a header comment.
-
-
Constructor Detail
-
XMLPropertiesConfiguration
public XMLPropertiesConfiguration()
Creates an empty XMLPropertyConfiguration object which can be used to synthesize a new Properties file by adding values and then saving(). An object constructed by this C'tor can not be tickled into loading included files because it cannot supply a base for relative includes.
-
XMLPropertiesConfiguration
public XMLPropertiesConfiguration(org.w3c.dom.Element element) throws ConfigurationException
Creates and loads the XML properties from the specified DOM node.- Parameters:
element
- The non-null DOM element.- Throws:
ConfigurationException
- Error while loading the Element.- Since:
- 2.0
-
-
Method Detail
-
escapeValue
private java.lang.String escapeValue(java.lang.Object value)
Escapes a property value before it is written to disk.- Parameters:
value
- the value to be escaped- Returns:
- the escaped value
-
getHeader
public java.lang.String getHeader()
Gets the header comment of this configuration.- Returns:
- the header comment
-
initFileLocator
public void initFileLocator(FileLocator locator)
Initializes this object with aFileLocator
. The locator is accessed during load and save operations.- Specified by:
initFileLocator
in interfaceFileLocatorAware
- Parameters:
locator
- the associatedFileLocator
-
load
public void load(org.w3c.dom.Element element) throws ConfigurationException
Parses a DOM element containing the properties. The DOM element has to follow the XML properties format introduced in Java, see https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html- Parameters:
element
- The DOM element- Throws:
ConfigurationException
- Error while interpreting the DOM- Since:
- 2.0
-
read
public void read(java.io.Reader in) throws ConfigurationException
Description copied from interface:FileBased
Reads the content of this object from the given reader. Client code should not call this method directly, but use aFileHandler
for reading data.- Specified by:
read
in interfaceFileBased
- Parameters:
in
- the reader- Throws:
ConfigurationException
- if a non-I/O related problem occurs, e.g. the data read does not have the expected format
-
save
public void save(org.w3c.dom.Document document, org.w3c.dom.Node parent)
Writes the configuration as child to the given DOM node- Parameters:
document
- The DOM document to add the configuration toparent
- The DOM parent node- Since:
- 2.0
-
setHeader
public void setHeader(java.lang.String header)
Sets the header comment of this configuration.- Parameters:
header
- the header comment
-
write
public void write(java.io.Writer out) throws ConfigurationException
Description copied from interface:FileBased
Writes the content of this object to the given writer. Client code should not call this method directly, but use aFileHandler
for writing data.- Specified by:
write
in interfaceFileBased
- Parameters:
out
- the writer- Throws:
ConfigurationException
- if a non-I/O related problem occurs, e.g. the data read does not have the expected format
-
writeProperty
private void writeProperty(org.w3c.dom.Document document, org.w3c.dom.Node properties, java.lang.String key, java.util.List<?> values)
-
writeProperty
private void writeProperty(org.w3c.dom.Document document, org.w3c.dom.Node properties, java.lang.String key, java.lang.Object value)
-
writeProperty
private void writeProperty(java.io.PrintWriter out, java.lang.String key, java.util.List<?> values)
Write a list property.- Parameters:
out
- the output streamkey
- the key of the propertyvalues
- a list with all property values
-
writeProperty
private void writeProperty(java.io.PrintWriter out, java.lang.String key, java.lang.Object value)
Write a property.- Parameters:
out
- the output streamkey
- the key of the propertyvalue
- the value of the property
-
-