Klasse RegexValidator

java.lang.Object
net.sf.jguiraffe.transform.RegexValidator
Alle implementierten Schnittstellen:
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:

Ungültige Eingabe: "<"table * border="1"> 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:

 
 Ungültige Eingabe: "<"di:bean beanClass="net.sf.jguiraffe.transform.RegexValidator"
   name="regexValidator">
   Ungültige Eingabe: "<"di:setProperty property="regex" value="[0-9]*"/>
 Ungültige Eingabe: "<"/di:bean>
 ...
 
 Ungültige Eingabe: "<"f:textfield name="text1">
   Ungültige Eingabe: "<"f:validator phase="syntax" beanName="regexValidator"/>
 Ungültige Eingabe: "<"/f:textfield>
 
 Ungültige Eingabe: "<"f:textfield name="text2">
   Ungültige Eingabe: "<"f:validator phase="syntax" beanName="regexValidator">
     Ungültige Eingabe: "<"f:properties>
       Ungültige Eingabe: "<"f:property key="dotAll" value="true"/>
     Ungültige Eingabe: "<"/f:properties>
   Ungültige Eingabe: "<"/f:validator>
 Ungültige Eingabe: "<"/f:textfield>
 
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 $
Autor:
Oliver Heger
  • Felddetails

    • PROP_REGEX

      protected static final String PROP_REGEX
      Constant for the regex property.
      Siehe auch:
    • PROP_CASE_INSENSITIVE

      protected static final String PROP_CASE_INSENSITIVE
      Constant for the caseInsensitive property.
      Siehe auch:
    • PROP_DOT_ALL

      protected static final String PROP_DOT_ALL
      Constant for the multiLine property.
      Siehe auch:
  • Konstruktordetails

    • RegexValidator

      public RegexValidator()
  • Methodendetails

    • getRegex

      public String getRegex()
      Returns the regular expression as a string.
      Gibt zurück:
      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.
      Parameter:
      regex - the regular expression string (must not be null)
      Löst aus:
      IllegalArgumentException - if the parameter is null
    • isDotAll

      public boolean isDotAll()
      Returns a flag whether the dot place holder should also match line terminators.
      Gibt zurück:
      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.
      Parameter:
      dotAll - the dot all flag
    • isCaseInsensitive

      public boolean isCaseInsensitive()
      Returns a flag whether string matches are case insensitive.
      Gibt zurück:
      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.
      Parameter:
      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.
      Angegeben von:
      isValid in Schnittstelle Validator
      Parameter:
      o - the object to be validated
      ctx - the transformer context
      Gibt zurück:
      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.
      Parameter:
      ctx - the TransformerContext
      Gibt zurück:
      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.
      Parameter:
      ctx - the TransformerContext
      Gibt zurück:
      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.
      Parameter:
      ctx - the TransformerContext
      Gibt zurück:
      the newly created Pattern object