public class INIConfiguration extends BaseHierarchicalConfiguration implements FileBasedConfiguration
A specialized hierarchical configuration implementation for parsing ini files.
An initialization or ini file is a configuration file typically found on Microsoft's Windows operating system and contains data for Windows based applications.
Although popularized by Windows, ini files can be used on any system or platform due to the fact that they are merely text files that can easily be parsed and modified by both humans and computers.
A typical ini file could look something like:
[section1] ; this is a comment! var1 = foo var2 = bar [section2] var1 = doo
The format of ini files is fairly straight forward and is composed of three components:
key = value
format.There are various implementations of the ini file format by various vendors which has caused a number of differences to appear. As far as possible this configuration tries to be lenient and support most of the differences.
Some of the differences supported are as follows:
var1 : foo
.Global parameters are also allowed; any parameters declared before a section is declared are added to a global section. It is important to note that this global section does not have a name.
In all instances, a parameter's key is prepended with its section name and a '.' (period). Thus a parameter named
"var1" in "section1" will have the key section1.var1
in this configuration. (This is the default behavior.
Because this is a hierarchical configuration you can change this by setting a different
ExpressionEngine
.)
default = ok [section1] var1 = foo var2 = doodle [section2] ; a comment var1 = baz var2 = shoodle bad = = worse [section3] # another comment var1 : foo var2 : bar var5 : test1 [section3] var3 = foo var4 = bar var5 = test2 [sectionSeparators] passwd : abc=def a:b = "value"
This ini file will be parsed without error. Note:
getProperty("default")
.getProperty("section1.var1")
.getProperty("section2. ")
, notice the period '.' and
the space following the section name.passwd
and the value
abc=def
. This default behavior can be changed by using quotes. If there is a separator character before the
first quote character (ignoring whitespace), this character is used as separator. Thus the second property definition
in the section has the key a:b
and the value value
.Internally, this configuration maps the content of the represented ini file to its node structure in the following way:
This explains how the keys for the properties can be constructed. You can also use other methods of
HierarchicalConfiguration
for querying or manipulating the hierarchy of configuration nodes, for instance the
configurationAt()
method for obtaining the data of a specific section. However, be careful that the storage
scheme described above is not violated (e.g. by adding multiple levels of nodes or inserting duplicate section
nodes). Otherwise, the special methods for ini configurations may not work correctly!
The set of sections in this configuration can be retrieved using the getSections()
method. For obtaining a
SubnodeConfiguration
with the content of a specific section the getSection()
method can be used.
Like other Configuration
implementations, this class uses a Synchronizer
object to control concurrent
access. By choosing a suitable implementation of the Synchronizer
interface, an instance can be made
thread-safe or not. Note that access to most of the properties typically set through a builder is not protected by
the Synchronizer
. The intended usage is that these properties are set once at construction time through the
builder and after that remain constant. If you wish to change such properties during life time of an instance, you
have to use the lock()
and unlock()
methods manually to ensure that other threads see your changes.
As this class extends AbstractConfiguration
, all basic features like variable interpolation, list handling,
or data type conversions are available as well. This is described in the chapter
Basic features
and AbstractConfiguration of the user's guide.
Note that this configuration does not support properties with null values. Such properties are considered to be section nodes.
BaseHierarchicalConfiguration.BuilderVisitor
Modifier and Type | Field and Description |
---|---|
protected static String |
COMMENT_CHARS
The default characters that signal the start of a comment line.
|
protected static String |
SEPARATOR_CHARS
The default characters used to separate keys from values.
|
Constructor and Description |
---|
INIConfiguration()
Create a new empty INI Configuration.
|
INIConfiguration(HierarchicalConfiguration<ImmutableNode> c)
Creates a new instance of
INIConfiguration with the content of the specified
HierarchicalConfiguration . |
Modifier and Type | Method and Description |
---|---|
String |
getCommentLeadingCharsUsedInInput()
Get comment leading separator used in INI reading.
|
SubnodeConfiguration |
getSection(String name)
Returns a configuration with the content of the specified section.
|
Set<String> |
getSections()
Return a set containing the sections in this ini configuration.
|
String |
getSeparatorUsedInInput()
Get separator used in INI reading.
|
String |
getSeparatorUsedInOutput()
Get separator used in INI output.
|
protected boolean |
isCommentLine(String line)
Determine if the given line is a comment line.
|
protected boolean |
isSectionLine(String line)
Determine if the given line is a section.
|
void |
read(Reader in)
Load the configuration from the given reader.
|
void |
setCommentLeadingCharsUsedInInput(String separator)
Allows setting the leading comment separator which is used in reading an INI file
|
void |
setSeparatorUsedInInput(String separator)
Allows setting the key and value separator which is used in reading an INI file
|
void |
setSeparatorUsedInOutput(String separator)
Allows setting the key and value separator which is used for the creation of the resulting INI output
|
void |
write(Writer writer)
Save the configuration to the specified writer.
|
childConfigurationsAt, childConfigurationsAt, cloneNodeModel, configurationAt, configurationAt, configurationsAt, configurationsAt, createSubConfigurationForTrackedNode, getNodeModel, getSubConfigurationNodeSelector, getSubConfigurationParentModel, immutableChildConfigurationsAt, immutableConfigurationAt, immutableConfigurationAt, immutableConfigurationsAt, initSubConfigurationForThisParent, interpolatedConfiguration, subnodeConfigurationChanged, subset
addNodes, addNodesInternal, addPropertyDirect, addPropertyInternal, clearInternal, clearPropertyDirect, clearTree, clearTreeInternal, clone, containsKeyInternal, fetchNodeList, getExpressionEngine, getKeysInternal, getKeysInternal, getMaxIndex, getMaxIndexInternal, getModel, getPropertyInternal, getRootElementName, getRootElementNameInternal, isEmptyInternal, nodeDefined, nodeKey, resolveAddKey, resolveKey, resolveNodeKey, resolveUpdateKey, setExpressionEngine, setPropertyInternal, sizeInternal, toString
addErrorLogListener, addProperty, append, beginRead, beginWrite, clear, clearProperty, cloneInterpolator, containsKey, 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, getList, getList, getList, getList, getListDelimiterHandler, getLogger, getLong, getLong, getLong, getProperties, getProperties, getProperty, getShort, getShort, getShort, getString, getString, getStringArray, getSynchronizer, immutableSubset, initLogger, installInterpolator, interpolate, interpolate, isEmpty, isScalarValue, isThrowExceptionOnMissing, lock, setConfigurationDecoder, setConversionHandler, setDefaultLookups, setInterpolator, setListDelimiterHandler, setLogger, setParentInterpolator, setPrefixLookups, setProperty, setSynchronizer, setThrowExceptionOnMissing, size, unlock
addEventListener, clearErrorListeners, clearEventListeners, copyEventListeners, createErrorEvent, createEvent, fireError, fireEvent, getEventListenerRegistrations, getEventListeners, isDetailEvents, removeEventListener, setDetailEvents
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
addProperty, clear, clearProperty, getInterpolator, installInterpolator, setInterpolator, setProperty, subset
containsKey, 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, getList, getList, getList, getList, getLong, getLong, getLong, getProperties, getProperty, getShort, getShort, getShort, getString, getString, getStringArray, immutableSubset, isEmpty, size
getSynchronizer, lock, setSynchronizer, unlock
protected static final String COMMENT_CHARS
protected static final String SEPARATOR_CHARS
public INIConfiguration()
public INIConfiguration(HierarchicalConfiguration<ImmutableNode> c)
INIConfiguration
with the content of the specified
HierarchicalConfiguration
.c
- the configuration to be copiedpublic String getSeparatorUsedInOutput()
setSeparatorUsedInOutput
for further explanationpublic void setSeparatorUsedInOutput(String separator)
separator
- String of the new separator for INI outputpublic String getSeparatorUsedInInput()
setSeparatorUsedInInput
for further explanationpublic void setSeparatorUsedInInput(String separator)
separator
- String of the new separator for INI readingpublic String getCommentLeadingCharsUsedInInput()
setCommentLeadingCharsUsedInInput
for further
explanationpublic void setCommentLeadingCharsUsedInInput(String separator)
separator
- String of the new separator for INI readingpublic void write(Writer writer) throws ConfigurationException, IOException
write
in interface FileBased
writer
- - The writer to save the configuration to.ConfigurationException
- If an error occurs while writing the configurationIOException
- if an I/O error occurs.public void read(Reader in) throws ConfigurationException, IOException
clear()
method is not called so the configuration
read in will be merged with the current configuration.read
in interface FileBased
in
- the reader to read the configuration from.ConfigurationException
- If an error occurs while reading the configurationIOException
- if an I/O error occurs.protected boolean isCommentLine(String line)
line
- The line to check.protected boolean isSectionLine(String line)
line
- The line to check.public Set<String> getSections()
public SubnodeConfiguration getSection(String name)
HierarchicalConfiguration.configurationAt(String)
with the name of the section in question. There are the
following differences however:
addProperty()
has been used in
a way that does not conform to the storage scheme used by INIConfiguration
. If used correctly, there will not
be duplicate sections.)name
- the name of the section in question; null represents the global sectionCopyright © 2001–2022 The Apache Software Foundation. All rights reserved.