You have seen, that xml validation descriptor describes several "Validations". Validation ( de.jtec.validator.Validation ) is itself pretty dumb and acts as container for condition tree (which does all the work). Validation also stores reference to property name (descriptive - what property we are validating), message (which could be also key for message bundle, i18n is out of scope here) and which condition evalation result indicates failure (default is false).
Once validation object is set up, you can pass whatever object you like to valdate() method. As result you will get ValidationFailure ( holding configured property name and message or just null if condition was satisfied.
Conditions are workhorses of JTec Validation. They roughly split into 2 main groups:
Composite conditions act as containers for other conditions, and delegate validation to them. Outcome of such condition depends on its children. At the moment there are tre types of them:
Of course composite conditions behave politely and cancel evaluation of subconditions as soon as the outcome is known. This if first condition (and) succeeds, second will be not evaluated. Those 3 conditions define functionally complete decision trees. Of cours it would be posible with just one condition, but this would be not readable.
<or>
<and>
<match property="country" pattern="germany"/>
<match property="zip" pattern="[0-9]{5}"/>
</and>
<and>
<match property="country" pattern="GB"/>
<match property="zip" pattern="[a-zA-Z]{1}[0-9]{2}\s[a-zA-Z]{1}[0-9]{2}"/>
</and>
</or>
Primitive conditions can not have child conditions, but instead they do real validation work on supplied objects. Most primitive conditions operate on some property of supplied object. Property is extracted by means of commons-beanutils, so you may use whatever notation is acceptable by this library.
There are 2 boolean conditions, always evaluating to "true" or "false" (and as you may imagine, they are referenced in xml as <true> and <false>) Puprose of boolean condition is to provide some fallback decisions for composite conditions.
<or>
<match property="country" pattern="germany"/>
<match property="country" pattern="usa"/>
<match property="country" pattern="france"/>
<false/>
</or>
This would force failure if none of previous conditions evaluates as true (i.e country is not in the list)
Pattern mathcing is important feature in evaluation. Pattern mathcing conditions include:
<match property="country" pattern="germany"/>
<match property="zip" pattern="[0-9]{5}"/>
<or>
<and>
<class className="Foo"/>
<match property="bar" patttern="glum"/>
</and>
<and>
<class className="Bar"/>
<match property="blurge" patttern="bang"/>
</and>
</or>
StringReader config = new StringReader(
"<validator>" +
"<validation property=\"foo\" glem=\"glam\" message=\"bar\" fail=\"false\"/>" +
"<validation property=\"baz\" message=\"bang\" fail=\"true\"/>" +
"</validator>"
);
XmlValidator validator = new XmlValidator(config);