public final class IniSection extends Object implements Iterable
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 String |
charset |
private File |
configFile |
private static int |
MAX_BUFF_SIZE
Buffer size is based on file size but keep it with within reasonable limits
|
private String |
name
The name of the section.
|
private String |
report |
private Map<String,List<String>> |
section
A map of values by key names.
|
private StringBuilder |
warnings |
Constructor and Description |
---|
IniSection()
Create an empty INI config without a name.
|
IniSection(IniSection config)
Copy constructor
|
IniSection(String name)
Create an empty INI Config.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(String key,
String value)
Add a value for the key.
|
private String |
advance(BufferedReader bin)
Get the next line from the input
|
private boolean |
allowed(String key,
String value) |
void |
clear()
Start over.
|
boolean |
containsKey(String key)
Returns
true if the IniSection contains any values for the specified key. |
boolean |
containsValue(String value)
Returns
true if the IniSection contains the specified value for any key. |
boolean |
containsValue(String key,
String value)
Returns
true if the IniSection contains the specified value for the given key. |
private void |
doLoad(Reader in,
Filter<String> filter) |
private String |
format(String value)
A helper to format the output of the content as expected
|
String |
get(String key)
Get the first value for the key.
|
String |
get(String key,
int index)
Get the value for the key specified by the index.
|
String |
get(String key,
String defaultValue) |
Collection<String> |
getKeys()
Get the unmodifiable unordered list of keys.
|
String |
getName()
Get the [name] of this section
|
private Collection<String> |
getOrCreateValues(String key) |
private int |
getSplitPos(String line)
Does this line of text represent a key/value pair?
|
Collection<String> |
getValues(String key)
Get the unmodifiable collection of values of a key.
|
private boolean |
isCommentLine(String line)
Determine if the given line is a blank or a comment line.
|
boolean |
isEmpty()
Determine whether this section has any keys
|
private boolean |
isSectionLine(String line)
Is this line a [section]?
|
Iterator |
iterator() |
void |
load(byte[] buffer,
String encoding)
Load the conf from a buffer.
|
void |
load(byte[] buffer,
String encoding,
Filter<String> filter)
Load the conf from a buffer.
|
void |
load(File file,
String encoding)
Load the INI from a file using the given encoding.
|
void |
load(File file,
String encoding,
Filter<String> filter)
Load the INI from a file using the given encoding.
|
void |
load(InputStream is,
String encoding)
Load the INI from an InputStream using the given encoding.
|
void |
load(InputStream is,
String encoding,
Filter<String> filter)
Load the INI from an InputStream using the given 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 key)
Remove the key and all its values, if present.
|
boolean |
remove(String key,
String value)
Remove the value if present.
|
boolean |
replace(String key,
String value)
Replace the value(s) for the key with a new value.
|
String |
report()
Obtain a report of issues with this IniSection.
|
void |
save()
Save this INI to the file from which it was loaded.
|
void |
save(File file,
String encoding)
Save the INI to a file using the given encoding.
|
void |
save(Writer out)
Output this section using the print writer.
|
void |
setName(String name)
Set the name of this INI config.
|
int |
size()
Get the number of keys in this section.
|
int |
size(String key)
Get the number of values for a key.
|
private String name
private File configFile
private String charset
private StringBuilder warnings
private String report
private static final int MAX_BUFF_SIZE
public IniSection()
public IniSection(String name)
name
- the section namepublic IniSection(IniSection config)
config
- the config to copypublic void clear()
public void setName(String name)
name
- public String getName()
public int size()
public int size(String key)
key
- the keypublic boolean isEmpty()
true
if this section is emptypublic Collection<String> getKeys()
public boolean containsKey(String key)
true
if the IniSection contains any values for the specified key.key
- key to search for in IniSectiontrue
if the key existspublic boolean containsValue(String value)
true
if the IniSection contains the specified value for any key.value
- value to search for in IniSectiontrue
if the value exists.public boolean containsValue(String key, String value)
true
if the IniSection contains the specified value for the given key.key
- the key for the sectionvalue
- value to search for in IniSectiontrue
if the value exists.public boolean add(String key, String value)
key
- the key for the sectionvalue
- the value for the keypublic Collection<String> getValues(String key)
key
- the keypublic String get(String key, int index)
key
- the keyindex
- the indexArrayIndexOutOfBoundsException
- when the index is out of boundspublic String get(String key)
key
- the keypublic boolean remove(String key, String value)
key
- the key for the sectionvalue
- the value for the keypublic boolean remove(String key)
key
- the key for the sectionpublic boolean replace(String key, String value)
key
- the key for the sectionvalue
- the value for the keypublic void load(InputStream is, String encoding) throws IOException
is
- the InputStream to read fromencoding
- the encoding of the fileIOException
public void load(InputStream is, String encoding, Filter<String> filter) throws IOException
is
- the InputStream to read fromencoding
- the encoding of the filefilter
- the filter, possibly null, for the desired keysIOException
public void load(File file, String encoding) throws IOException
file
- the file to loadencoding
- the encoding of the fileIOException
public void load(File file, String encoding, Filter<String> filter) throws IOException
file
- the file to loadencoding
- the encoding of the filefilter
- the filter, possibly null, for the desired keysIOException
public void load(byte[] buffer, String encoding) throws IOException
buffer
- the buffer to loadencoding
- the character encoding of this INIIOException
public void load(byte[] buffer, String encoding, Filter<String> filter) throws IOException
buffer
- the buffer to loadencoding
- the character encoding of this INIfilter
- the filter, possibly null, for the desired keysIOException
public void save() throws IOException
IOException
public void save(File file, String encoding) throws IOException
file
- the file to loadencoding
- the encoding of the fileIOException
public void save(Writer out)
out
- the output streampublic String report()
private String format(String value)
value
- the value to be formattedprivate Collection<String> getOrCreateValues(String key)
private void doLoad(Reader in, Filter<String> filter) 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