Validation framework

JTec-Validator is powerfull and flexible rule based validation framework. It allows creation of complex validation rules which can be used in different scenarios. Configuration is xml based and self explaining. This code started its life in xdoclet project, where it decided which @-tags are allowed on program elements. Somehow XDoclet GUI newer got attention of users, and there was eclipse plugin, so this code became orphaned. Then I needed validation framework, and found that it could be put to a new use.

One minute introduction

Say, you like to validate some postal adress in java bean. And of yourse you like zip codes to be compatible with postal system of destination country. And neither struts nor webwork validator is going to help you with this. Unless you write custom validator and register it. This sucks. But help is near:

<validator>
    <validation property="zip" message="zip has wrong format">
        <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>
    </validation>
</validator>
            

This file belongs somewhere on classpath so your classloader can find it later, and you can create validator:

Validator validator = ValidatorFactory.create(Thread.currentThread().getContextClassloader().getResource("I hope you have not forgot where placed you validator xml");

Address address = new Address();
address.setCountry("germany")
address.setZip("A23 B72");

Collection failures = valdator.validate(address);

ValdationFailure failure = (ValdationFailure)failures.iterator().next();
System.err.println(failure.getProperty() + ":" + failure.getMessage());
                
            

As you may guess result would be:

zip:zip has wrong format