CLI2 provides a mechanism to validate argument values. The Validator
interface must be implemented to create an argument validator. This interface has
a single method validate(java.util.List values) throws InvalidArgumentException
.
CLI2 has some standard validators included. They validate the following:
- Java class
- enumeration
- date
- file path
- number
- URI
- URL
The ClassValidator
validates a value using three criteria:
- the value adheres to the Java Language Specification rules
- the class specified by the name is loadable
- the class specified by the name can be instantiated
// 1. ClassValidator validator = new ClassValidator(); // 2. validator.setLoadable(true); // 3. validator.setLoadable(true);
TODO: add section about values being replaced with class instances
The DateValidator
validates values against a
java.text.DateFormat
. There are three helper methods
that create built-in validators.
getDateInstance
returns a validator for the default date format for the default locale.getTimeInstance
returns a validator for the default time format for the default locale.getDateTimeInstance
returns a validator for the default date and time format for the default locale.
A DateValidator
can also be created by passing your
a custom DateFormat
into the constructor.
DateFormat myFormat = DateFormat.getDateInstance( DateFormat.LONG, Locale.UK ); DateValidator myValidator = new DateValidator( myFormat );
In addition to basic format checking you can also check if the date/time specified
is before/after a specific date/time. The lower bound is set using the
setMinimum( java.util.Date )
, and the upper bound is set using
the setMaximum( java.util.Date )
.
TODO: add section about values being replaced with date instances
The EnumValidator
validates values against a list of
allowed string values. The values that are allowed are specified in
a java.util.Set
and passed in to the constructor.
Set enumSet = new TreeSet(); enumSet.add("red"); enumSet.add("green"); enumSet.add("blue"); EnumValidator validator = new EnumValidator( enumSet );
The FileValidator
validates that values represent
existing files. You can also specify a combination of the
following additional criteria:
- value is a file
- value is a directory
- the file is writable
- the file is readable
Each of the criteria listed here are specified using the appropriate setter.
There are three helper methods to create validators:
getExistingInstance
returns a validator that ensures a value represents an existing file.getExistingFileInstance
returns a validator that ensures a value represents an existing file that is not a directory.getExistingDirectoryInstance
returns a validator that ensures a value represents an existing file this is a directory.
// validate that the value represents a file that exists FileValidator validator = FileValidator.getExistingInstance(); // ensure it's a writable file validator.setWritable( true );
The NumberValidator
validates that values adhere to
certain rules like the following:
getCurrencyInstance
returns a validator for the default currency format for the default locale.getPercentInstance
returns a validator for the default percent format for the default locale.getIntegerInstance
returns a validator for the default integer format for the default locale.getNumberInstance
returns a validator for the default number format for the default locale.
A NumberValidator
can also be created by passing your
a custom NumberFormat
into the constructor.
NumberFormat myFormat = NumberFormat.getCurrencyInstance( Locale.UK ); NumberValidator myValidator = new NumberValidator( myFormat );
In addition to basic format checking you can also check if the number specified
is less than or greater than a specific number. The lower bound is set using
the setMinimum( Number )
, and the upper bound is set using
the setMaximum( Number )
.
A URIValidator
validates that a given URI is parsable
and optionally provides support for scheme validation.
URIValidator validator = new URIValidator();
A URLValidator
validates that values are URLs and if you
choose it will also validate the protocol is of the type you have
specified.
UrlValidator validator = new UrlValidator(); // only accept https URLs validator.setProtocol("https");