public class UrlValidator extends Object implements IValidator<String>
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>
Modifier and Type | Field and Description |
---|---|
static int |
ALLOW_2_SLASHES
Allow two slashes in the path component of the
URL . |
static int |
ALLOW_ALL_SCHEMES
Allows all validly-formatted schemes to pass validation instead of supplying a set of valid
schemes.
|
protected String[] |
defaultSchemes
If no schemes are provided, default to this set of protocols.
|
static int |
NO_FRAGMENTS
Enabling this option disallows any
URL fragments. |
Constructor and Description |
---|
UrlValidator()
Constructs a
UrlValidator with default properties. |
UrlValidator(int options)
Constructs a
UrlValidator with the given validation options. |
UrlValidator(String[] schemes)
Constructs a
UrlValidator with the given String array of scheme
options. |
UrlValidator(String[] schemes,
int options)
Constructs a
UrlValidator with the given scheme and validation options (see
class description). |
Modifier and Type | Method and Description |
---|---|
protected 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 errors
|
static boolean |
isBlankOrNull(String value)
Checks if the field isn't
null 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.
|
boolean |
isValid(String value)
Checks if a field has a valid
URL . |
protected boolean |
isValidAuthority(String authority)
Returns
true if the authority is properly formatted. |
protected boolean |
isValidFragment(String fragment)
Returns
true if the given fragment is null or fragments are
allowed. |
protected boolean |
isValidPath(String path)
Returns
true if the path is valid. |
protected boolean |
isValidQuery(String query)
Returns
true if the query is null or if it's a properly-formatted
query string. |
protected boolean |
isValidScheme(String scheme)
Validates a scheme.
|
void |
validate(IValidatable<String> validatable)
Validates the
IValidatable instance. |
public static final int ALLOW_ALL_SCHEMES
public static final int ALLOW_2_SLASHES
URL
.public static final int NO_FRAGMENTS
URL
fragments.protected String[] defaultSchemes
public UrlValidator()
UrlValidator
with default properties.public UrlValidator(String[] schemes)
UrlValidator
with the given String
array of scheme
options. The validation is modified by passing in options in the schemes
argument.schemes
- Pass in one or more URL
schemes to consider valid. Passing in a
null
will default to "http,https,ftp
" being used. If a
non-null
scheme is specified, then all valid schemes must be
specified. Setting the ALLOW_ALL_SCHEMES
option will ignore the
contents of schemes
.public UrlValidator(int options)
UrlValidator
with the given validation options.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.public UrlValidator(String[] schemes, int options)
UrlValidator
with the given scheme and validation options (see
class description).schemes
- Pass in one or more URL
schemes to consider valid. Passing in a
null
will default to "http,https,ftp
" being used. If a
non-null
scheme is specified, then all valid schemes must be
specified. Setting the ALLOW_ALL_SCHEMES
option will ignore the
contents of schemes
.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.public void validate(IValidatable<String> validatable)
IValidator
IValidatable
instance. Validation errors should be reported using
the IValidatable.error(IValidationError)
method.validate
in interface IValidator<String>
validatable
- the IValidatable
instance being validatedprotected IValidationError decorate(IValidationError error, IValidatable<String> validatable)
error
- public final boolean isValid(String value)
URL
. This method is public because it is directly
used in tests.value
- The value validation is being performed on. A null
value is
considered invalid.true
if the URL
is validprotected boolean isValidScheme(String scheme)
null
, then only those
schemes are allowed. Note that this is slightly different than for the constructor.scheme
- The scheme to validate. A null
value is considered invalid.true
if the URL
is validprotected boolean isValidAuthority(String authority)
true
if the authority is properly formatted. An authority is the
combination of host name and port. A null
authority value is considered invalid.authority
- an authority value to validateprotected boolean isValidPath(String path)
true
if the path is valid. A null
value is considered
invalid.path
- a path value to validate.true
if path is valid.protected boolean isValidQuery(String query)
true
if the query is null
or if it's a properly-formatted
query string.query
- a query value to validatetrue
if the query is validprotected boolean isValidFragment(String fragment)
true
if the given fragment is null
or fragments are
allowed.fragment
- a fragment value to validatetrue
if the fragment is validprotected int countToken(String token, String target)
token
- a token value to be countedtarget
- a target String
to count tokens inpublic static boolean isBlankOrNull(String value)
null
and if length of the field is greater than zero,
not including whitespace.value
- the value validation is being performed ontrue
if blank or null
public boolean isOn(long flag)
flag
- flag value to checkpublic boolean isOff(long flag)
flag
- flag value to check.Copyright © 2006–2022 Apache Software Foundation. All rights reserved.