java.lang.Object
org.apache.wicket.validation.validator.UrlValidator
- All Implemented Interfaces:
Serializable
,IClusterable
,IValidator<String>
Validator for checking URLs. The default schemes allowed are
http://
,
https://
, and ftp://
.
The behavior of validation is modified by passing in one of these options:
ALLOW_2_SLASHES - [FALSE]
: Allows double '/' characters in the path component.NO_FRAGMENT- [FALSE]
: By default fragments are allowed. If this option is included then fragments are flagged as illegal.ALLOW_ALL_SCHEMES - [FALSE]
: By default only http, https, and ftp are considered valid schemes. Enabling this option will let any scheme pass validation.
This was originally based org.apache.commons.validator.UrlValidator
, but the
dependency on Jakarta-ORO was removed and it now uses java.util.regexp instead. Usage example:
<code> Component.add(new UrlValidator({"http", "https"})); </code>
- Since:
- 1.2.6
- Author:
- Vincent Demay
- See Also:
-
- "http://www.ietf.org/rfc/rfc2396.txt"
- Serialized Form
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Allow two slashes in the path component of theURL
.static final int
Allows all validly-formatted schemes to pass validation instead of supplying a set of valid schemes.protected String[]
If no schemes are provided, default to this set of protocols.static final int
Enabling this option disallows anyURL
fragments. -
Constructor Summary
ConstructorDescriptionConstructs aUrlValidator
with default properties.UrlValidator
(int options) Constructs aUrlValidator
with the given validation options.UrlValidator
(String[] schemes) Constructs aUrlValidator
with the givenString
array of scheme options.UrlValidator
(String[] schemes, int options) Constructs aUrlValidator
with the given scheme and validation options (see class description). -
Method Summary
Modifier and TypeMethodDescriptionprotected int
countToken
(String token, String target) Returns the number of times the token appears in the target.protected IValidationError
decorate
(IValidationError error, IValidatable<String> validatable) Allows subclasses to decorate reported errorsstatic boolean
isBlankOrNull
(String value) Checks if the field isn'tnull
and if length of the field is greater than zero, not including whitespace.boolean
isOff
(long flag) Tests whether the given flag is off.boolean
isOn
(long flag) Tests whether the given flag is on.final boolean
Checks if a field has a validURL
.protected boolean
isValidAuthority
(String authority) Returnstrue
if the authority is properly formatted.protected boolean
isValidFragment
(String fragment) Returnstrue
if the given fragment isnull
or fragments are allowed.protected boolean
isValidPath
(String path) Returnstrue
if the path is valid.protected boolean
isValidQuery
(String query) Returnstrue
if the query isnull
or if it's a properly-formatted query string.protected boolean
isValidScheme
(String scheme) Validates a scheme.void
validate
(IValidatable<String> validatable) Validates theIValidatable
instance.
-
Field Details
-
ALLOW_ALL_SCHEMES
Allows all validly-formatted schemes to pass validation instead of supplying a set of valid schemes.- See Also:
-
ALLOW_2_SLASHES
Allow two slashes in the path component of theURL
.- See Also:
-
NO_FRAGMENTS
Enabling this option disallows anyURL
fragments.- See Also:
-
defaultSchemes
If no schemes are provided, default to this set of protocols.
-
-
Constructor Details
-
UrlValidator
public UrlValidator()Constructs aUrlValidator
with default properties. -
UrlValidator
Constructs aUrlValidator
with the givenString
array of scheme options. The validation is modified by passing in options in theschemes
argument.- Parameters:
schemes
- Pass in one or moreURL
schemes to consider valid. Passing in anull
will default to "http,https,ftp
" being used. If a non-null
scheme is specified, then all valid schemes must be specified. Setting theALLOW_ALL_SCHEMES
option will ignore the contents ofschemes
.
-
UrlValidator
Constructs aUrlValidator
with the given validation options.- Parameters:
options
- The options should be set using the public constants declared in this class. To set multiple options you simply add them together. For example,ALLOW_2_SLASHES
+NO_FRAGMENTS
enables both of those options.
-
UrlValidator
Constructs aUrlValidator
with the given scheme and validation options (see class description).- Parameters:
schemes
- Pass in one or moreURL
schemes to consider valid. Passing in anull
will default to "http,https,ftp
" being used. If a non-null
scheme is specified, then all valid schemes must be specified. Setting theALLOW_ALL_SCHEMES
option will ignore the contents ofschemes
.options
- The options should be set using the public constants declared in this class. To set multiple options you simply add them together. For example,ALLOW_2_SLASHES
+NO_FRAGMENTS
enables both of those options.
-
-
Method Details
-
validate
Description copied from interface:IValidator
Validates theIValidatable
instance. Validation errors should be reported using theIValidatable.error(IValidationError)
method.- Specified by:
validate
in interfaceIValidator<String>
- Parameters:
validatable
- theIValidatable
instance being validated
-
decorate
Allows subclasses to decorate reported errors- Parameters:
error
-- Returns:
- decorated error
-
isValid
Checks if a field has a validURL
. This method is public because it is directly used in tests.- Parameters:
value
- The value validation is being performed on. Anull
value is considered invalid.- Returns:
true
if theURL
is valid
-
isValidScheme
Validates a scheme. If schemes[] was initialized to non-null
, then only those schemes are allowed. Note that this is slightly different than for the constructor.- Parameters:
scheme
- The scheme to validate. Anull
value is considered invalid.- Returns:
true
if theURL
is valid
-
isValidAuthority
Returnstrue
if the authority is properly formatted. An authority is the combination of host name and port. Anull
authority value is considered invalid.- Parameters:
authority
- an authority value to validate- Returns:
- true if authority (host name and port) is valid.
-
isValidPath
Returnstrue
if the path is valid. Anull
value is considered invalid.- Parameters:
path
- a path value to validate.- Returns:
true
if path is valid.
-
isValidQuery
Returnstrue
if the query isnull
or if it's a properly-formatted query string.- Parameters:
query
- a query value to validate- Returns:
true
if the query is valid
-
isValidFragment
Returnstrue
if the given fragment isnull
or fragments are allowed.- Parameters:
fragment
- a fragment value to validate- Returns:
true
if the fragment is valid
-
countToken
Returns the number of times the token appears in the target.- Parameters:
token
- a token value to be countedtarget
- a targetString
to count tokens in- Returns:
- the number of tokens
-
isBlankOrNull
Checks if the field isn'tnull
and if length of the field is greater than zero, not including whitespace.- Parameters:
value
- the value validation is being performed on- Returns:
true
if blank ornull
-
isOn
Tests whether the given flag is on. If the flag is not a power of 2 (ie. 3) this tests whether the combination of flags is on.- Parameters:
flag
- flag value to check- Returns:
- whether the specified flag value is on
-
isOff
Tests whether the given flag is off. If the flag is not a power of 2 (ie. 3) this tests whether the combination of flags is off.- Parameters:
flag
- flag value to check.- Returns:
- whether the specified flag value is off
-