Class RegexValidator
- java.lang.Object
-
- net.sf.jguiraffe.transform.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 ajava.util.regex.Pattern
object is created which is used to validate the data passed to theisValid(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 thejava.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 thejava.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 thePattern
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: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
-
-
Field Summary
Fields Modifier and Type Field Description protected static String
PROP_CASE_INSENSITIVE
Constant for the caseInsensitive property.protected static String
PROP_DOT_ALL
Constant for the multiLine property.protected static String
PROP_REGEX
Constant for the regex property.
-
Constructor Summary
Constructors Constructor Description RegexValidator()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Pattern
createPattern(TransformerContext ctx)
Creates a newPattern
object.protected Pattern
getPattern(TransformerContext ctx)
Returns aPattern
object to be used for the validation operation.String
getRegex()
Returns the regular expression as a string.boolean
isCaseInsensitive()
Returns a flag whether string matches are case insensitive.boolean
isDotAll()
Returns a flag whether the dot place holder should also match line terminators.ValidationResult
isValid(Object o, TransformerContext ctx)
Performs the validation as described in the class comment.protected boolean
overridesProperties(TransformerContext ctx)
Tests whether properties in the specifiedTransformerContext
override properties of this object.void
setCaseInsensitive(boolean caseInsensitive)
Sets a flag whether string matches are case insensitive.void
setDotAll(boolean dotAll)
Sets a flag whether the dot place holder should also match line terminators.void
setRegex(String regex)
Sets the regular expression as a string.
-
-
-
Field Detail
-
PROP_REGEX
protected static final String PROP_REGEX
Constant for the regex property.- See Also:
- Constant Field Values
-
PROP_CASE_INSENSITIVE
protected static final String PROP_CASE_INSENSITIVE
Constant for the caseInsensitive property.- See Also:
- Constant Field Values
-
PROP_DOT_ALL
protected static final String PROP_DOT_ALL
Constant for the multiLine property.- See Also:
- Constant Field Values
-
-
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 thejava.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 theDOTALL
flag of thejava.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 theCASE_INSENSITIVE
flag of thejava.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.
-
getPattern
protected Pattern getPattern(TransformerContext ctx)
Returns aPattern
object to be used for the validation operation. This class maintains aPattern
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 theTransformerContext
. If this is the case, a newPattern
object is always created.- Parameters:
ctx
- theTransformerContext
- Returns:
- the
Pattern
object to be used
-
overridesProperties
protected boolean overridesProperties(TransformerContext ctx)
Tests whether properties in the specifiedTransformerContext
override properties of this object. This method is called bygetPattern(TransformerContext)
. If it returns true, a newPattern
object is created.- Parameters:
ctx
- theTransformerContext
- Returns:
- a flag whether default properties are overridden in the context
-
createPattern
protected Pattern createPattern(TransformerContext ctx)
Creates a newPattern
object. This method is called bygetPattern(TransformerContext)
whenever a new pattern needs to be created. This implementation creates a newPattern
object based on the properties set in theTransformerContext
. 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
- theTransformerContext
- Returns:
- the newly created
Pattern
object
-
-