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 Pattern
Creates a newPattern
object.protected Pattern
Returns aPattern
object to be used for the validation operation.getRegex()
Returns the regular expression as a string.boolean
Returns a flag whether string matches are case insensitive.boolean
isDotAll()
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 boolean
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
Sets 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.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 theDOTALL
flag of thejava.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 theCASE_INSENSITIVE
flag of thejava.util.regex.Pattern
class.- 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 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.- Parameter:
ctx
- theTransformerContext
- Gibt zurück:
- the
Pattern
object to be used
-
overridesProperties
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.- Parameter:
ctx
- theTransformerContext
- Gibt zurück:
- a flag whether default properties are overridden in the context
-
createPattern
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.- Parameter:
ctx
- theTransformerContext
- Gibt zurück:
- the newly created
Pattern
object
-