final class Ini extends Object
SWORD defines a conf as an INI file with one or more sections. Originally, all modules were described in a single conf, but now each module has its own conf.
SWORD will be using a single conf to hold overrides for many
modules. This is the motivation for this class as opposed to
allowing only a single section as IniSection
.
Since the most common use case is for a single section, this implementation has an API for delegating to the first IniSection.
This implementation allows for:The GNU Lesser General Public License for details.
Modifier and Type | Field and Description |
---|---|
private List<String> |
list
Indexed list of sections maintaining insertion order.
|
private static org.slf4j.Logger |
LOGGER
The log stream
|
private static int |
MAX_BUFF_SIZE
Buffer size is based on file size but keep it with within reasonable limits
|
private Map<String,IniSection> |
sectionMap
A map of sections by section names.
|
Constructor and Description |
---|
Ini()
Create an empty INI Config.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(String sectionName,
String key,
String value)
Add a key/value pair to a section.
|
boolean |
addValue(String key,
String value)
Add a value for the key.
|
private String |
advance(BufferedReader bin)
Get the next line from the input
|
void |
clear()
Start over.
|
private IniSection |
doGetSection(String sectionName) |
private void |
doLoad(Reader in) |
Collection<String> |
getKeys()
Get the unmodifiable set of keys of the first section.
|
private IniSection |
getOrCreateSection(String sectionName)
Get a section, creating it if necessary.
|
IniSection |
getSection()
Get the first section.
|
String |
getSectionName()
Get the name of the first section.
|
String |
getSectionName(int index) |
List<String> |
getSections()
Get an unmodifiable collection of the sections in this INI.
|
private int |
getSplitPos(String line)
Does this line of text represent a key/value pair?
|
String |
getValue(String key)
Get the first value for the key in the first section.
|
String |
getValue(String key,
int index)
Get the value for the key specified by the index for the first section.
|
String |
getValue(String sectionName,
String key)
Get the first value for the key specified by the index and the section.
|
String |
getValue(String sectionName,
String key,
int index)
Get the value for the key specified by the index and the section.
|
Collection<String> |
getValues(String key)
Get the values of a key of the first section.
|
int |
getValueSize(String key)
Get the number of values for a key in the first section
|
int |
getValueSize(String sectionName,
String key) |
private boolean |
isCommentLine(String line)
Determine if the given line is a blank or a comment line.
|
private boolean |
isSectionLine(String line)
Is this line a [section]?
|
void |
load(byte[] buffer,
String encoding)
Load the conf from a buffer.
|
void |
load(File file,
String encoding)
Load the INI from a file using the given encoding.
|
void |
load(InputStream is,
String encoding) |
private String |
more(BufferedReader bin,
String value)
Get continuation lines, if any.
|
private static boolean |
more(String line)
Is there more following this line
|
boolean |
remove(String sectionName,
String key)
Remove the key if present.
|
boolean |
remove(String sectionName,
String key,
String value)
Remove the value if present.
|
boolean |
removeValue(String key)
Remove the key if present.
|
boolean |
removeValue(String key,
String value)
Remove the value if present in the first section.
|
boolean |
replace(String sectionName,
String key,
String value)
Replace a value for a key.
|
boolean |
replaceValue(String key,
String value)
Replace a value for a key.
|
void |
save(File file,
String encoding)
Save the INI to a file using the given encoding.
|
private void |
save(Writer out)
Output the Ini to the given Writer.
|
int |
size()
Get the number of sections
|
private Map<String,IniSection> sectionMap
private static final int MAX_BUFF_SIZE
private static final org.slf4j.Logger LOGGER
public void clear()
public int size()
public List<String> getSections()
public String getSectionName(int index)
public String getSectionName()
ArrayIndexOutOfBoundsException
- if there are no sectionspublic int getValueSize(String key)
key
- the keypublic String getValue(String sectionName, String key, int index)
sectionName
- the name of the sectionkey
- the key for the sectionindex
- the index in the list of valuesArrayIndexOutOfBoundsException
- when the index is out of boundspublic String getValue(String sectionName, String key)
sectionName
- the name of the sectionkey
- the key for the sectionArrayIndexOutOfBoundsException
- when the index is out of boundspublic String getValue(String key, int index)
key
- the keyindex
- the indexArrayIndexOutOfBoundsException
- when the index is out of boundspublic String getValue(String key)
key
- the keyArrayIndexOutOfBoundsException
- when the index is out of boundspublic boolean add(String sectionName, String key, String value)
sectionName
- the name of the sectionkey
- the key for the sectionvalue
- the value for the keytrue
if the element was added or already was presentpublic boolean replace(String sectionName, String key, String value)
sectionName
- the name of the sectionkey
- the key for the sectionvalue
- the value for the keytrue
if the element was added or already was presentpublic boolean remove(String sectionName, String key, String value)
sectionName
- the name of the sectionkey
- the key for the sectionvalue
- the value for the keypublic boolean remove(String sectionName, String key)
sectionName
- the name of the sectionkey
- the key for the sectionpublic IniSection getSection()
public Collection<String> getKeys()
public Collection<String> getValues(String key)
key
- the keypublic boolean addValue(String key, String value)
key
- the key for the sectionvalue
- the value for the keypublic boolean removeValue(String key, String value)
key
- the key for the sectionvalue
- the value for the keypublic boolean removeValue(String key)
key
- the key for the sectionpublic boolean replaceValue(String key, String value)
key
- the key for the sectionvalue
- the value for the keytrue
if the element was added or already was presentpublic void load(InputStream is, String encoding) throws IOException
IOException
public void load(File file, String encoding) throws IOException
file
- the file to loadencoding
- the encoding of the fileIOException
public void load(byte[] buffer, String encoding) throws IOException
buffer
- the buffer to loadencoding
- the character encoding for this INIIOException
public void save(File file, String encoding) throws IOException
file
- the file to loadencoding
- the encoding of the fileIOException
private void save(Writer out)
out
- the Writer to which this Ini should be writtenprivate IniSection doGetSection(String sectionName)
private IniSection getOrCreateSection(String sectionName)
sectionName
- private void doLoad(Reader in) throws IOException
IOException
private String advance(BufferedReader bin) throws IOException
bin
- The reader to get data fromIOException
- if encounteredprivate boolean isCommentLine(String line)
line
- The line to check.private boolean isSectionLine(String line)
line
- The line to check.private int getSplitPos(String line)
line
- The line to check.private String more(BufferedReader bin, String value) throws IOException
IOException
private static boolean more(String line)
line
- the trimmed string to check