com.ls.util.validation
Class Validator

java.lang.Object
  |
  +--com.ls.util.validation.Validator

public class Validator
extends Object

This class is for testing of regular expressions. It uses the "gnu.regexp" library.

Version:
$Revision: 1.2 $
Author:
Last modified by $Author: TMahler $

Constructor Summary
Validator()
          Creates a new Validator object.
 
Method Summary
 void addRule(String ruleCode, ValidatorRuleSet ruleSet)
          The following function group adds a rule to the know rule sets.
protected  void criteriaFromVector(Vector criteria)
          Turns a vector of hashtables into criteria / regex pairs stored in the private member criteria.
 boolean isValid(String value, String ruleCode)
          Function to check a value (from the user input side) against the rule set for a rule code.
 boolean isValid(String value, String subject, String fieldName)
          Function to check a value (from the user input side) against the rule set for a combination of subject and field name.
 boolean isValid(String value, String subject, String action, String fieldName)
          Function to check a value (from the user input side) against the rule set for a combination of subject, action and field name.
 String makeRuleCode(String subject, String action, String fieldName)
          Generates a rule code for a certain subject, action and field.
protected  boolean mandatoryFulfilled(String value)
          Returns if a value contains at least something, i.e. a mandatory field would be checked for presence.
 boolean matchesCriteria(String value, String typecode)
          Checks if a string matches a typecode's regular expression.
 void setCriteria(Vector criteria)
          Sets the validation criteria from the instantiating class.
 void setRules(String subject, String action, Hashtable defaults, Vector fields)
          Sets the rules to apply to fields posted with a certain subject and action.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Validator

public Validator()
Creates a new Validator object.
Method Detail

setCriteria

public void setCriteria(Vector criteria)
                 throws gnu.regexp.REException
Sets the validation criteria from the instantiating class.
Parameters:
criteria - Criteria vector of hashtables.
Throws:
gnu.regexp.REException - If something went wrong.

criteriaFromVector

protected void criteriaFromVector(Vector criteria)
                           throws gnu.regexp.REException
Turns a vector of hashtables into criteria / regex pairs stored in the private member criteria.
Parameters:
criteria - Vector of hashtables, each containing a "typecode" and a "regex" String element.
Throws:
gnu.regexp.REException - If something went wrong.

matchesCriteria

public boolean matchesCriteria(String value,
                               String typecode)
                        throws IllegalArgumentException
Checks if a string matches a typecode's regular expression.
Parameters:
value - Value to check against the regular expression of the criteria
typecode - String with the name of the criteria
Returns:
True if value is valid against criteria, false otherwise.
Throws:
IllegalArgumentException - If argumant is not correct.

isValid

public boolean isValid(String value,
                       String ruleCode)
Function to check a value (from the user input side) against the rule set for a rule code. If no rule for that rule code was defined before, the check will always return true.
Parameters:
value - The user input to check.
ruleCode - The rule code that identifies the rule to use for the check.
Returns:
True or false, depending on validity of input. Also returns true if no rule with the specified rule code exists.

isValid

public boolean isValid(String value,
                       String subject,
                       String action,
                       String fieldName)
Function to check a value (from the user input side) against the rule set for a combination of subject, action and field name. Subject, action and field name are used to build a rule code. If no rule for that rule code was defined before, the check will always return True.
Parameters:
value - The user input to check.
subject - The message's subject.
action - The message's action, or empty string.
fieldName - The input field to check.
Returns:
True or false, depending on validity of input. Also returns true if no rule with the specified rule code exists.

isValid

public boolean isValid(String value,
                       String subject,
                       String fieldName)
Function to check a value (from the user input side) against the rule set for a combination of subject and field name. Subject and field name are used to build a rule code. If no rule for that rule code was defined before, the check will always return true.
Parameters:
value - The user input to check.
subject - The message's subject.
fieldName - The input field to check.
Returns:
True or false, depending on validity of input. Also returns true if no rule with the specified rule code exists.

makeRuleCode

public String makeRuleCode(String subject,
                           String action,
                           String fieldName)
Generates a rule code for a certain subject, action and field. NOTE: None of the fields can be null, at least pass an empty string.
Parameters:
subject - Subject of the incoming message.
action - Action of the incoming message.
fieldName - Input field to check.
Returns:
Rule code for the above unique combination.

mandatoryFulfilled

protected boolean mandatoryFulfilled(String value)
Returns if a value contains at least something, i.e. a mandatory field would be checked for presence.
Parameters:
value - The value to check.
Returns:
True, if value was neither null nor an empty string.

addRule

public void addRule(String ruleCode,
                    ValidatorRuleSet ruleSet)
The following function group adds a rule to the know rule sets. The validate...() functions must be passed the rule code and the value to perform a check.
Parameters:
ruleCode - Rule code generated by makeRuleCode(), i.e. parse_template.insert_item.email
ruleSet - The set of rules

setRules

public void setRules(String subject,
                     String action,
                     Hashtable defaults,
                     Vector fields)
Sets the rules to apply to fields posted with a certain subject and action. The data for this method comes from the config file or a message.
Parameters:
subject - Subject of posted message.
action - Action of posted message, if any.
defaults - Hashtable with default rules to use.
fields - Vector with the field rules, one per field.