Class TravelDocumentValidator

java.lang.Object
org.apache.commons.validator.routines.TravelDocumentValidator

public class TravelDocumentValidator extends Object
Travel Document Validator.

The validator includes a default set of formats to check numbers of passports, ID-cards and visas.

For further information see Wikipedia. See International Civil Aviation Organization, Doc 9303: Machine Readable Travel Documents, Part 3: Specifications Common to all MRTDs for more details.

This can be adjusted f.i. adding a new country routines by creating a validator and using the setValidator(Type, String, int, String, CheckDigit) method to add (or remove) an entry.

For example:

 TravelDocumentValidator v = new TravelDocumentValidator();
 v.setValidator(P, "TWN", "\\d{10}", Modulus10_731CheckDigit.getInstance());
 

The singleton default instance cannot be modified in this way.

Since:
2.10.5
  • Field Details

    • DEFAULT_VALIDATOR

      public static final TravelDocumentValidator DEFAULT_VALIDATOR
      The singleton instance which uses the default formats
  • Constructor Details

    • TravelDocumentValidator

      public TravelDocumentValidator()
      Create a default validator.
    • TravelDocumentValidator

      public TravelDocumentValidator(TravelDocumentValidator.Validator[] validators)
      Create an IN validator from the specified map of TIN formats.
      Parameters:
      validators - map of TIN formats
  • Method Details

    • getInstance

      public static TravelDocumentValidator getInstance()
      Gets the singleton instance of the validator using the default formats
      Returns:
      A singleton instance of the validator
    • getDefaultValidators

      public TravelDocumentValidator.Validator[] getDefaultValidators()
      Gets a copy of the default Validators.
      Returns:
      a copy of the default Validator array
    • getValidator

      Gets the Validator for a given type and country
      Parameters:
      t - the ICAO document type
      cc - the ICAO country code
      Returns:
      the validator or null if there is no one registered.
    • hasValidator

      public boolean hasValidator(TravelDocumentValidator.Type t, String cc)
      Does the class have the required validator?
      Parameters:
      t - the ICAO document type
      cc - the ICAO country code
      Returns:
      true if there is a validator for the country
    • isValid

      public boolean isValid(TravelDocumentValidator.Type t, String cc, String code)
      Validate a travel document
      Parameters:
      t - the ICAO document type
      cc - the ICAO country code
      code - The value validation is being performed on (e.g. "9102392482" an USA Passport)
      Returns:
      true if the value is valid
    • setValidator

      public TravelDocumentValidator.Validator setValidator(TravelDocumentValidator.Type t, String cc, int length, String format, CheckDigit routine)
      Installs a validator. Will replace any existing entry which has the same countryCode.
      Parameters:
      t - the ICAO document type
      cc - the ICAO country code
      length - the length of the TIN. Must be ≥ 8 and ≤ 32. If the length is < 0, the validator is removed, and the format is not used.
      format - the format of the TIN for the country (as a regular expression)
      routine - the CheckDigit module
      Returns:
      the previous Validator, or null if there was none
      Throws:
      IllegalArgumentException - if there is a problem
      IllegalStateException - if an attempt is made to modify the singleton validator
    • setValidator

      Installs a validator. Will replace any existing entry which has the same countryCode
      Parameters:
      validator - the instance to install.
      Returns:
      the previous Validator, or null if there was none
      Throws:
      IllegalStateException - if an attempt is made to modify the singleton validator