This page contains an explanation of the advanced topics of the Java INI Package, along with code examples and explanations. Although this document aims to be as concise as possible, developers are recommended to consult the javadoc for a more complete and upto date documentation of Java INI Package.
9. Validation
9.1. Why Validate?
9.2. Create your own IniValidator
9.3. Default validation
When creating an IniSection
or IniItem
object, you must specify a
name for the section or item, which can only be done by providing the name via one of their
constructors. However, it is important for the IniSection
and
IniItem
classes to check whether the names are valid. For example, consider
the following Java statement:
// create a new IniSection
IniSection
section
= new
BasicIniSection
( "sub[section
" );If this IniSection
were to be later written to an INI file on the hard disk, the
output would be:
[sub[section]
We now have a problem, because when this is INI file is parsed by the IniFileReader
object, or even parsed by any other parser, the parser will do one of three things:
As the effect of the name is ambiguous, the name "[sub[section]" should be
considered invalid. Fortunately, validation of names is handled by the class
IniValidator
.
An IniFile
object and the IniSection
s and IniItem
s within
the IniFile
object all share the same instance of an IniValidator
, which
validates if a given section name or item name is valid or not. It is not important how this is done,
but it may be useful to know how to create your own instances of an IniValidator
in
case the default IniValidator
is not appropriate or if you need to define your own
custom specification of an INI file (e.g the "@" symbol could be the default item for a section,
as in the case of Microsoft Windows Registry files).
IniValidator
To create your own IniValidator
you will need to know regular expression, which is
a fairly large topic and could not possibly cover here. If you do need a tutorial on regular
expression you can easily find some on the internet with the aid of everyone's favourite
search engine.
To create your own IniValidator
you first need to pass a regular expression pattern
to the methods setSectionRegEx()
and setItemRegEx()
and then pass the
IniValidator
to the IniFile
s constructor. We will demonstrate this with
an example.
import
org.dtools.ini.*;import
java.util.regex.*; // Java's regular expression package
public class
IniValidatorExample
{public static void
main
( String
args
[] ) {//******************************************************************
// Step 1 - set up an IniValidator which only accepts numbers
//******************************************************************
IniValidator
validator
= new
IniValidator
();validator
.setSectionNameRegEx
( Pattern
.compile
("[0-9]+
") );validator
.setItemNameRegEx
( Pattern
.compile
("[0-9]+
") );//******************************************************************
// Step 2 - Create an IniFile object and set the new validator
//******************************************************************
IniFile
ini
= new
BasicIniFile
( validator
);//******************************************************************
// Step 3 - Test the validator by creating sections and items
//******************************************************************
System
.out
.println
( "Number of sections:
" + ini
.getNumberOfSections
() );ini
.addSection
( "00001
" );System
.out
.println
( "Number of sections:
" + ini
.getNumberOfSections
() );ini
.addSection
( "9
" );System
.out
.println
( "Number of sections:
" + ini
.getNumberOfSections
() );// now adding an illegal value
ini
.addSection
( "hello
" );System
.out
.println
( "Number of sections:
" + ini
.getNumberOfSections
() );The above code, when ran, produces the following output:
As you can see, the first two sections were added successfully as they both contain numbers and
therefore are considered valid by the IniValidator
. However the third section, "hello"
is not valid, and when added to the IniFile
the method addSection
throws
an InvalidNameException
object.
By default the IniValidator
considers all names to be valid, with the exception
of the following rules: