|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.commons.configuration.AbstractConfiguration
org.apache.commons.configuration.HierarchicalConfiguration
org.apache.commons.configuration.XMLConfiguration
A specialized hierarchical configuration class that is able to parse XML documents.
The parsed document will be stored keeping its structure. The class also
tries to preserve as much information from the loaded XML document as
possible, including comments and processing instructions. These will be
contained in documents created by the save()
methods, too.
Nested Class Summary |
Nested classes inherited from class org.apache.commons.configuration.HierarchicalConfiguration |
HierarchicalConfiguration.BuilderVisitor, HierarchicalConfiguration.Node, HierarchicalConfiguration.NodeVisitor |
Field Summary |
Fields inherited from class org.apache.commons.configuration.AbstractConfiguration |
END_TOKEN, START_TOKEN |
Constructor Summary | |
XMLConfiguration()
Creates a new instance of XMLConfiguration . |
|
XMLConfiguration(File file)
Creates a new instance of XMLConfiguration .
|
|
XMLConfiguration(String fileName)
Creates a new instance of XMLConfiguration .
|
|
XMLConfiguration(URL url)
Creates a new instance of XMLConfiguration .
|
Method Summary | |
protected void |
addPropertyDirect(String key,
Object obj)
Adds the property with the specified key. |
void |
clearProperty(String key)
Remove a property from the configuration. |
void |
clearTree(String key)
Removes all values of the property with the given name and of keys that start with this name. |
protected Document |
createDocument()
Creates a DOM document from the internal tree of configuration nodes. |
protected HierarchicalConfiguration.Node |
createNode(String name)
Creates a new node object. |
String |
getBasePath()
Return the base path. |
Document |
getDocument()
Returns the XML document this configuration was loaded from. |
String |
getEncoding()
Return the encoding used to store the configuration file. |
File |
getFile()
Return the file where the configuration is stored. |
String |
getFileName()
Return the name of the file. |
ReloadingStrategy |
getReloadingStrategy()
Return the reloading strategy. |
String |
getRootElementName()
Returns the name of the root element. |
URL |
getURL()
Return the URL where the configuration is stored. |
void |
initProperties(Document document,
boolean elemRefs)
Initializes this configuration from an XML document. |
boolean |
isAutoSave()
Tells if properties are automatically saved to the disk. |
void |
load()
Load the configuration from the underlying URL. |
void |
load(File file)
Load the configuration from the specified file. |
void |
load(InputStream in)
Load the configuration from the specified stream, using the encoding returned by getEncoding() . |
void |
load(InputStream in,
String encoding)
Load the configuration from the specified stream, using the specified encoding. |
void |
load(Reader in)
Load the properties from the given reader. |
void |
load(String fileName)
Locate the specified file and load the configuration. |
void |
load(URL url)
Load the configuration from the specified URL. |
void |
reload()
Reload the configuration. |
void |
save()
Save the configuration. |
void |
save(File file)
Save the configuration to the specified file. |
void |
save(OutputStream out)
Save the configuration to the specified stream, using the encoding returned by getEncoding() . |
void |
save(OutputStream out,
String encoding)
Save the configuration to the specified stream, using the specified encoding. |
void |
save(String fileName)
Save the configuration to the specified file. |
void |
save(URL url)
Save the configuration to the specified URL if it's a file URL. |
void |
save(Writer writer)
Saves the configuration to the specified writer. |
void |
setAutoSave(boolean autoSave)
Enable of disable the automatical saving of modified properties to the disk. |
void |
setBasePath(String basePath)
Set the base path. |
void |
setEncoding(String encoding)
Set the encoding used to store the configuration file. |
void |
setFile(File file)
Set the file where the configuration is stored. |
void |
setFileName(String fileName)
Set the name of the file. |
void |
setProperty(String key,
Object value)
Set a property, this will replace any previously set values. |
void |
setReloadingStrategy(ReloadingStrategy strategy)
Set the reloading strategy. |
void |
setRootElementName(String name)
Sets the name of the root element. |
void |
setURL(URL url)
The URL where the configuration is stored. |
Methods inherited from class org.apache.commons.configuration.HierarchicalConfiguration |
addNodes, clearNode, containsKey, createAddPath, fetchAddNode, fetchNodeList, findLastPathNode, findPropertyNodes, getKeys, getKeys, getMaxIndex, getProperty, getRoot, isEmpty, nodeDefined, removeNode, setRoot, subset |
Methods inherited from class org.apache.commons.configuration.AbstractConfiguration |
addProperty, clear, getBigDecimal, getBigDecimal, getBigInteger, getBigInteger, getBoolean, getBoolean, getBoolean, getByte, getByte, getByte, getDelimiter, getDouble, getDouble, getDouble, getFloat, getFloat, getFloat, getInt, getInt, getInteger, getList, getList, getLong, getLong, getLong, getProperties, getProperties, getShort, getShort, getShort, getString, getString, getStringArray, interpolate, interpolateHelper, isThrowExceptionOnMissing, resolveContainerStore, setDelimiter, setThrowExceptionOnMissing |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface org.apache.commons.configuration.Configuration |
addProperty, clear, containsKey, getBigDecimal, getBigDecimal, getBigInteger, getBigInteger, getBoolean, getBoolean, getBoolean, getByte, getByte, getByte, getDouble, getDouble, getDouble, getFloat, getFloat, getFloat, getInt, getInt, getInteger, getKeys, getKeys, getList, getList, getLong, getLong, getLong, getProperties, getProperty, getShort, getShort, getShort, getString, getString, getStringArray, isEmpty, subset |
Constructor Detail |
public XMLConfiguration()
XMLConfiguration
.
public XMLConfiguration(String fileName) throws ConfigurationException
XMLConfiguration
.
The configuration is loaded from the specified file
fileName
- the name of the file to load
ConfigurationException
- if the file cannot be loadedpublic XMLConfiguration(File file) throws ConfigurationException
XMLConfiguration
.
The configuration is loaded from the specified file.
file
- the file
ConfigurationException
- if an error occurs while loading the filepublic XMLConfiguration(URL url) throws ConfigurationException
XMLConfiguration
.
The configuration is loaded from the specified URL.
url
- the URL
ConfigurationException
- if loading causes an errorMethod Detail |
public String getRootElementName()
public void setRootElementName(String name)
UnsupportedOperationException
exception is thrown. Whether this configuration has been loaded from an
XML document or not can be found out using the getDocument()
method.
name
- the name of the root elementpublic Document getDocument()
protected void addPropertyDirect(String key, Object obj)
HierarchicalConfiguration
Adds the property with the specified key.
To be able to deal with the structure supported by this configuration implementation the passed in key is of importance, especially the indices it might contain. The following example should clearify this: Suppose the actual configuration contains the following elements:
tables +-- table +-- name = user +-- fields +-- field +-- name = uid +-- field +-- name = firstName ... +-- table +-- name = documents +-- fields ...
In this example a database structure is defined, e.g. all fields
of the first table could be accessed using the key
tables.table(0).fields.field.name
. If now properties are
to be added, it must be exactly specified at which position in the
hierarchy the new property is to be inserted. So to add a new field name
to a table it is not enough to say just
config.addProperty("tables.table.fields.field.name", "newField");
The statement given above contains some ambiguity. For instance
it is not clear, to which table the new field should be added. If this
method finds such an ambiguity, it is resolved by following the last
valid path. Here this would be the last table. The same is true for the
field
; because there are multiple fields and no explicit
index is provided, a new name
property would be added to
the last field - which is propably not what was desired.
To make
things clear explicit indices should be provided whenever possible. In
the example above the exact table could be specified by providing an
index for the table
element as in
tables.table(1).fields
. By specifying an index it can
also be expressed that at a given position in the configuration tree a
new branch should be added. In the example above we did not want to add
an additional name
element to the last field of the table,
but we want a complete new field
element. This can be
achieved by specifying an invalid index (like -1) after the element where
a new branch should be created. Given this our example would run:
config.addProperty("tables.table(1).fields.field(-1).name", "newField");
With this notation it is possible to add new branches
everywhere. We could for instance create a new table
element by specifying
config.addProperty("tables.table(-1).fields.field.name", "newField2");
(Note that because after the table
element a new
branch is created indices in following elements are not relevant; the
branch is new so there cannot be any ambiguities.)
addPropertyDirect
in class HierarchicalConfiguration
key
- the key of the new propertyobj
- the value of the new propertypublic void clearProperty(String key)
Configuration
clearProperty
in interface Configuration
clearProperty
in class HierarchicalConfiguration
key
- the key of the property to be removedpublic void clearTree(String key)
HierarchicalConfiguration
clearTree("foo")
would remove both properties.
clearTree
in class HierarchicalConfiguration
key
- the key of the property to be removedpublic void setProperty(String key, Object value)
Configuration
setProperty
in interface Configuration
setProperty
in class HierarchicalConfiguration
public void initProperties(Document document, boolean elemRefs)
document
- the document to be parsedelemRefs
- a flag whether references to the XML elements should be setprotected Document createDocument() throws ConfigurationException
ConfigurationException
- if an error occursprotected HierarchicalConfiguration.Node createNode(String name)
XMLNode
class.
createNode
in class HierarchicalConfiguration
name
- the node's name
public void load() throws ConfigurationException
FileConfiguration
load
in interface FileConfiguration
ConfigurationException
public void load(String fileName) throws ConfigurationException
FileConfiguration
load
in interface FileConfiguration
fileName
- the name of the file loaded
ConfigurationException
public void load(File file) throws ConfigurationException
FileConfiguration
load
in interface FileConfiguration
file
- the loaded file
ConfigurationException
public void load(URL url) throws ConfigurationException
FileConfiguration
load
in interface FileConfiguration
url
- the URL of the file loaded
ConfigurationException
public void load(InputStream in) throws ConfigurationException
FileConfiguration
FileConfiguration.getEncoding()
.
load
in interface FileConfiguration
in
- the input stream
ConfigurationException
public void load(InputStream in, String encoding) throws ConfigurationException
FileConfiguration
load
in interface FileConfiguration
in
- the input streamencoding
- the encoding used. null
to use the default encoding
ConfigurationException
public void load(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.
load
in interface FileConfiguration
in
- An InputStream.
ConfigurationException
public void save() throws ConfigurationException
FileConfiguration
save
in interface FileConfiguration
ConfigurationException
public void save(String fileName) throws ConfigurationException
FileConfiguration
save
in interface FileConfiguration
fileName
-
ConfigurationException
public void save(File file) throws ConfigurationException
FileConfiguration
save
in interface FileConfiguration
file
-
ConfigurationException
public void save(URL url) throws ConfigurationException
FileConfiguration
save
in interface FileConfiguration
url
-
ConfigurationException
public void save(OutputStream out) throws ConfigurationException
FileConfiguration
FileConfiguration.getEncoding()
.
save
in interface FileConfiguration
out
-
ConfigurationException
public void save(OutputStream out, String encoding) throws ConfigurationException
FileConfiguration
save
in interface FileConfiguration
out
- encoding
-
ConfigurationException
public void save(Writer writer) throws ConfigurationException
save
in interface FileConfiguration
writer
- the writer used to save the configuration
ConfigurationException
- if an error occurspublic String getFileName()
FileConfiguration
getFileName
in interface FileConfiguration
public void setFileName(String fileName)
FileConfiguration
setFileName
in interface FileConfiguration
fileName
- the name of the filepublic String getBasePath()
FileConfiguration
getBasePath
in interface FileConfiguration
public void setBasePath(String basePath)
FileConfiguration
setBasePath
in interface FileConfiguration
basePath
- the base path.public File getFile()
FileConfiguration
getFile
in interface FileConfiguration
public void setFile(File file)
FileConfiguration
setFile
in interface FileConfiguration
file
- public URL getURL()
FileConfiguration
getURL
in interface FileConfiguration
public void setURL(URL url)
FileConfiguration
setURL
in interface FileConfiguration
url
- public void setAutoSave(boolean autoSave)
FileConfiguration
setAutoSave
in interface FileConfiguration
autoSave
- true
to enable, false
to disablepublic boolean isAutoSave()
FileConfiguration
isAutoSave
in interface FileConfiguration
true
if auto-saving is enabled, false
otherwisepublic ReloadingStrategy getReloadingStrategy()
FileConfiguration
getReloadingStrategy
in interface FileConfiguration
public void setReloadingStrategy(ReloadingStrategy strategy)
FileConfiguration
setReloadingStrategy
in interface FileConfiguration
public void reload()
FileConfiguration
reload
in interface FileConfiguration
public String getEncoding()
FileConfiguration
getEncoding
in interface FileConfiguration
public void setEncoding(String encoding)
FileConfiguration
setEncoding
in interface FileConfiguration
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |