Class RegexValidator

  • All Implemented Interfaces:
    Validator

    public class RegexValidator
    extends Object
    implements Validator

    A specialized Validator implementation that uses regular expressions to validate user input.

    This is a very generic yet powerful Validator class. It can be configured with a regular expression string and some flags influencing the matching behavior. From this data a java.util.regex.Pattern object is created which is used to validate the data passed to the isValid(Object, TransformerContext) method. isValid() transforms the passed in object to a string and applies the regular expression to it.

    RegexValidator provides a couple of properties for defining the java.util.regex.Pattern object that is the base for all validation operations. By using a <properties> section in the builder script these properties can be overridden for a local validation. Here the following properties are evaluated:

    Property Description Default
    regex The regular expression as a string. This string is used for creating the Pattern object. It must be a valid regular expression which conforms to the syntax expected by the java.util.regex.Pattern class. .*
    caseInsensitive A boolean flag which determines whether string comparisons should be case insensitive. false
    dotAll A boolean flag which indicates whether strings to be matched can contain line terminators. This flag corresponds to the DOTALL flag of the Pattern class. false

    If validation fails, the following possible error messages can be produced:

    Message key Description Parameters
    ValidationMessageConstants.ERR_PATTERN The input is not correct according the specified pattern. {0} = the regular expression

    This class is not thread-safe. However, a single instance can be shared between multiple input fields. In a builder script, a RegexValidator instance can be declared and initialized with default settings. This instance can then be associated with multiple input fields that all use the same validation pattern. If an input field requires slightly different settings, properties can be used to override the default settings. This can look as follows:

     
     
       
     
     ...
     
     
       
     
     
     
       
         
           
         
       
     
     
    In this example there are two text fields which are both assigned the RegexValidator instance. The first text field uses the validator's default settings. The second field overrides a property.

    Version:
    $Id: RegexValidator.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    • Constructor Detail

      • RegexValidator

        public RegexValidator()
    • Method Detail

      • getRegex

        public String getRegex()
        Returns the regular expression as a string.
        Returns:
        the regular expression
      • setRegex

        public void setRegex​(String regex)
        Sets the regular expression as a string. The string passed to this method must be compatible with the syntax used by the java.util.regex.Pattern class.
        Parameters:
        regex - the regular expression string (must not be null)
        Throws:
        IllegalArgumentException - if the parameter is null
      • isDotAll

        public boolean isDotAll()
        Returns a flag whether the dot place holder should also match line terminators.
        Returns:
        the dot all flag
      • setDotAll

        public void setDotAll​(boolean dotAll)
        Sets a flag whether the dot place holder should also match line terminators. This property corresponds to the DOTALL flag of the java.util.regex.Pattern class.
        Parameters:
        dotAll - the dot all flag
      • isCaseInsensitive

        public boolean isCaseInsensitive()
        Returns a flag whether string matches are case insensitive.
        Returns:
        the case insensitive flag
      • setCaseInsensitive

        public void setCaseInsensitive​(boolean caseInsensitive)
        Sets a flag whether string matches are case insensitive. This property corresponds to the CASE_INSENSITIVE flag of the java.util.regex.Pattern class.
        Parameters:
        caseInsensitive - the case insensitive flag
      • isValid

        public ValidationResult isValid​(Object o,
                                        TransformerContext ctx)
        Performs the validation as described in the class comment. The passed in object is transformed to a string and matched against the regular expression. null objects are accepted.
        Specified by:
        isValid in interface Validator
        Parameters:
        o - the object to be validated
        ctx - the transformer context
        Returns:
        an object with the results of the validation
      • getPattern

        protected Pattern getPattern​(TransformerContext ctx)
        Returns a Pattern object to be used for the validation operation. This class maintains a Pattern object that corresponds to the properties set for an instance. If a property is changed, the pattern is reset. It is then created on demand if it is accessed. It is also possible to override properties in the TransformerContext. If this is the case, a new Pattern object is always created.
        Parameters:
        ctx - the TransformerContext
        Returns:
        the Pattern object to be used
      • overridesProperties

        protected boolean overridesProperties​(TransformerContext ctx)
        Tests whether properties in the specified TransformerContext override properties of this object. This method is called by getPattern(TransformerContext). If it returns true, a new Pattern object is created.
        Parameters:
        ctx - the TransformerContext
        Returns:
        a flag whether default properties are overridden in the context
      • createPattern

        protected Pattern createPattern​(TransformerContext ctx)
        Creates a new Pattern object. This method is called by getPattern(TransformerContext) whenever a new pattern needs to be created. This implementation creates a new Pattern object based on the properties set in the TransformerContext. The properties of this object are used as defaults if a property is not set in the context. Thus the properties in the context override local properties if they are set.
        Parameters:
        ctx - the TransformerContext
        Returns:
        the newly created Pattern object