Klasse RegexValidator
- Alle implementierten Schnittstellen:
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">
Pattern object. It must be a valid regular expression which conforms
to the syntax expected by the java.util.regex.Pattern class.DOTALL flag of the
Pattern class.If validation fails, the following possible error messages can be produced:
| Message key | Description | Parameters |
|---|---|---|
|
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
-
Feldübersicht
Felder -
Konstruktorübersicht
Konstruktoren -
Methodenübersicht
Modifizierer und TypMethodeBeschreibungprotected PatternCreates a newPatternobject.protected PatternReturns aPatternobject to be used for the validation operation.getRegex()Returns the regular expression as a string.booleanReturns a flag whether string matches are case insensitive.booleanisDotAll()Returns a flag whether the dot place holder should also match line terminators.isValid(Object o, TransformerContext ctx) Performs the validation as described in the class comment.protected booleanTests whether properties in the specifiedTransformerContextoverride properties of this object.voidsetCaseInsensitive(boolean caseInsensitive) Sets a flag whether string matches are case insensitive.voidsetDotAll(boolean dotAll) Sets a flag whether the dot place holder should also match line terminators.voidSets the regular expression as a string.
-
Felddetails
-
PROP_REGEX
Constant for the regex property.- Siehe auch:
-
PROP_CASE_INSENSITIVE
Constant for the caseInsensitive property.- Siehe auch:
-
PROP_DOT_ALL
Constant for the multiLine property.- Siehe auch:
-
-
Konstruktordetails
-
RegexValidator
public RegexValidator()
-
-
Methodendetails
-
getRegex
Returns the regular expression as a string.- Gibt zurück:
- the regular expression
-
setRegex
Sets the regular expression as a string. The string passed to this method must be compatible with the syntax used by thejava.util.regex.Patternclass.- 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 theDOTALLflag of thejava.util.regex.Patternclass.- 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 theCASE_INSENSITIVEflag of thejava.util.regex.Patternclass.- Parameter:
caseInsensitive- the case insensitive flag
-
isValid
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
Returns aPatternobject to be used for the validation operation. This class maintains aPatternobject 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 newPatternobject is always created.- Parameter:
ctx- theTransformerContext- Gibt zurück:
- the
Patternobject to be used
-
overridesProperties
Tests whether properties in the specifiedTransformerContextoverride properties of this object. This method is called bygetPattern(TransformerContext). If it returns true, a newPatternobject is created.- Parameter:
ctx- theTransformerContext- Gibt zurück:
- a flag whether default properties are overridden in the context
-
createPattern
Creates a newPatternobject. This method is called bygetPattern(TransformerContext)whenever a new pattern needs to be created. This implementation creates a newPatternobject 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.- Parameter:
ctx- theTransformerContext- Gibt zurück:
- the newly created
Patternobject
-