org.apache.commons.configuration
Class INIConfiguration

java.lang.Object
  extended by org.apache.commons.configuration.event.EventSource
      extended by org.apache.commons.configuration.AbstractConfiguration
          extended by org.apache.commons.configuration.BaseConfiguration
              extended by org.apache.commons.configuration.AbstractFileConfiguration
                  extended by org.apache.commons.configuration.INIConfiguration
All Implemented Interfaces:
Cloneable, Configuration, FileConfiguration

public class INIConfiguration
extends AbstractFileConfiguration

An initialization or ini file is a configuration file tpically 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 typcial 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 comosed of three components:

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:

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. Thus, a section's parameters can easily be retrieved using the subset method using the section name as the prefix.

Implementation Details:

Consider the following ini file:
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

This ini file will be parsed without error. Note:

The set of sections in this configuration can be retrieved using the getSections method.

Since:
1.4
Version:
$Id: INIConfiguration.java 492216 2007-01-03 16:51:24Z oheger $
Author:
trevor.miller

Field Summary
protected static String COMMENT_CHARS
          The characters that signal the start of a comment line.
protected static String SEPARATOR_CHARS
          The characters used to separate keys from values.
 
Fields inherited from class org.apache.commons.configuration.AbstractFileConfiguration
autoSave, basePath, EVENT_RELOAD, fileName, strategy
 
Fields inherited from class org.apache.commons.configuration.AbstractConfiguration
END_TOKEN, EVENT_ADD_PROPERTY, EVENT_CLEAR, EVENT_CLEAR_PROPERTY, EVENT_READ_PROPERTY, EVENT_SET_PROPERTY, START_TOKEN
 
Constructor Summary
INIConfiguration()
          Create a new empty INI Configuration.
INIConfiguration(File file)
          Create and load the ini configuration from the given file.
INIConfiguration(String filename)
          Create and load the ini configuration from the given file.
INIConfiguration(URL url)
          Create and load the ini configuration from the given url.
 
Method Summary
 Set getSections()
          Return a set containing the sections in this ini configuration.
protected  boolean isCommentLine(String s)
          Determine if the given line is a comment line.
protected  boolean isSectionLine(String s)
          Determine if the given line is a section.
 void load(Reader reader)
          Load the configuration from the given reader.
 void save(Writer writer)
          Save the configuration to the specified writer.
 
Methods inherited from class org.apache.commons.configuration.AbstractFileConfiguration
addProperty, clearProperty, clone, 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, setBasePath, setEncoding, setFile, setFileName, setPath, setProperty, setReloadingStrategy, setURL
 
Methods inherited from class org.apache.commons.configuration.BaseConfiguration
addPropertyDirect, clear, clearPropertyDirect
 
Methods inherited from class org.apache.commons.configuration.AbstractConfiguration
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
 
Methods inherited from class org.apache.commons.configuration.event.EventSource
addConfigurationListener, addErrorListener, clearConfigurationListeners, clearErrorListeners, createErrorEvent, createEvent, fireError, getConfigurationListeners, getErrorListeners, isDetailEvents, removeConfigurationListener, removeErrorListener, 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.configuration.Configuration
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
 

Field Detail

COMMENT_CHARS

protected static final String COMMENT_CHARS
The characters that signal the start of a comment line.

See Also:
Constant Field Values

SEPARATOR_CHARS

protected static final String SEPARATOR_CHARS
The characters used to separate keys from values.

See Also:
Constant Field Values
Constructor Detail

INIConfiguration

public INIConfiguration()
Create a new empty INI Configuration.


INIConfiguration

public INIConfiguration(String filename)
                 throws ConfigurationException
Create and load the ini configuration from the given file.

Parameters:
filename - The name pr path of the ini file to load.
Throws:
ConfigurationException - If an error occurs while loading the file

INIConfiguration

public INIConfiguration(File file)
                 throws ConfigurationException
Create and load the ini configuration from the given file.

Parameters:
file - The ini file to load.
Throws:
ConfigurationException - If an error occurs while loading the file

INIConfiguration

public INIConfiguration(URL url)
                 throws ConfigurationException
Create and load the ini configuration from the given url.

Parameters:
url - The url of the ini file to load.
Throws:
ConfigurationException - If an error occurs while loading the file
Method Detail

save

public void save(Writer writer)
          throws ConfigurationException
Save the configuration to the specified writer.

Parameters:
writer - - The writer to save the configuration to.
Throws:
ConfigurationException - If an error occurs while writing the configuration

load

public void load(Reader reader)
          throws ConfigurationException
Load the configuration from the given reader. Note that the clear method is not called so the configuration read in will be merged with the current configuration.

Parameters:
reader - The reader to read the configuration from.
Throws:
ConfigurationException - If an error occurs while reading the configuration

isCommentLine

protected boolean isCommentLine(String s)
Determine if the given line is a comment line.

Parameters:
s - The line to check.
Returns:
true if the line is empty or starts with one of the comment characters

isSectionLine

protected boolean isSectionLine(String s)
Determine if the given line is a section.

Parameters:
s - The line to check.
Returns:
true if the line contains a secion

getSections

public Set getSections()
Return a set containing the sections in this ini configuration. Note that changes to this set do not affect the configuration.

Returns:
a set containing the sections.


Copyright © 2001-2007 The Apache Software Foundation. All Rights Reserved.