MAP | SASAX Documents > Parsing with SASAX > Custom validation | << | >> |
This section explains how to customize validation process of value in XML document with SASAX.
SASAX provides the mechanism to customize validation like as constraint in XML Schema definition.
In the parsing XML document,
ValueElement
(classes derived from it, in fact)
creates Java object of the corresponded type from text in XML document
at the end of element
(= invocation of "endElement()
" of itself)
at first.
For example,
IntElement
creates
java.lang.Integer
,
DateElement
creates
java.util.Date
, and so on.
Then,
ValueElement
invokes validate()
method
on "Validation
" instance,
which is registered by addValidation()
method of
ValueElement
itself.
Validation
is the interface
to represent custom validation strategy,
and has only this method declared as shown below.
public Object validate(ParseContext context, Object value) throws SAXException;
ValueElement
expects
Validation
to
(1) return appropriate object if the specified value satisfies with
condition of custom validation, or
(2) throw exception otherwise.
NOTE:
Interface definition of Validation
allows you to
return another object than the one specified as parameter,
or return the object of incorrect class
(e.g.: java.util.Date
for validation of IntElement
).
even though it causes runtime problem.
From point of view to use,
please use Validation
implementation carefully
if you use one developped by another.
From point of view to develop,
you should return the specified value object itself
than created one in validate()
method
as possible.
ValueElement
repeates to invoke
validate()
on another Validation
with value returned by previous one,
if there are some registered Validation
s.
So, you can not only represent complex validation
by combination of some Validation
instances,
but also re-use your custom Validation
.
For example,
Validation
implementation
to examine even-ness of specified value
for IntElement
is shown below.
public Object validate(ParseContext context, Object value) throws SAXException { int i = ((Integer)(value)).intValue(); if(0 != (i % 2)){ String message = value.toString(); throw new SAXNotRecognizedException(message); } return value; }
NOTE:
In fact,
to use this implementation
not only with IntElement
,
but also with other classes derived from NumberElement
,
you should (1)down-cast 'value
' to
'java.lang.Number
',
(2)examine even-ness in 'long
' value gotten by
Number#longValue()
, and
(3)return 'value
' itself.
XML Schema does not support this kind (logical) restriction directly, though you may describe it by "[02468]$" as "pattern" constraint, which means that last digit of even-number should be one of 0, 2, 4, 6 or 8.
You can parse XML document with 'even-ness restriction'
by the code shown in "Parse document"
as you know.
"ValidationDemo
" is implementation of
Validation
.
String uri = "http://www.lares.dti.ne.jp/~foozy/";
IntElement intElement =
new IntElement(null, uri, "int");
intElement.addValidation(new ValidationDemo());
ElementDrivenHandler handler =
new ElementDrivenHandler(intElement);
handler.parse(reader);
This invocation can accept XML documents which:
Complete code for this tutorial section is
jp.ne.dti.lares.foozy.sasax.ValidationDemo
under src/demo/sasax/src
in distribution.
This class is included in demo.jar
of binary distribution.
Class diagram in this section is shown below:
Classes which you must define are colored, and other are already defined.
In this tutorial, abbreviated class names are used. Complete names are shown below.
Notation | Full name |
---|---|
DateElement | jp.ne.dti.lares.foozy.sasax.DateElement |
ElementDrivenHandler | jp.ne.dti.lares.foozy.sasax.ElementDrivenHandler |
IntElement | jp.ne.dti.lares.foozy.sasax.IntElement |
NumberElement | jp.ne.dti.lares.foozy.sasax.NumberElement |
ParseContext | jp.ne.dti.lares.foozy.sasax.ParseContext |
Validation | jp.ne.dti.lares.foozy.sasax.Validation |
ValueElement | jp.ne.dti.lares.foozy.sasax.ValueElement |
MAP | SASAX Documents > Parsing with SASAX > Custom validation | << | >> |