public class UrlValidator extends AbstractValidator
URL Validation routines.
Behavior of validation is modified by passing in options:Originally based in on php script by Debbie Dyer, validation.php v1.2b, Date: 03/07/02, http://javascript.internet.com. However, this validation now bears little resemblance to the php original.
Example of usage:
Construct a UrlValidator with valid schemes of "http", and "https".
String[] schemes = {"http","https"}.
UrlValidator urlValidator = new UrlValidator(schemes);
if (urlValidator.isValid("ftp://foo.bar.com/")) {
System.out.println("url is valid");
} else {
System.out.println("url is invalid");
}
prints "url is invalid"
If instead the default constructor is used.
UrlValidator urlValidator = new UrlValidator();
if (urlValidator.isValid("ftp://foo.bar.com/")) {
System.out.println("url is valid");
} else {
System.out.println("url is invalid");
}
prints out "url is valid"
| Modifier and Type | Field and Description |
|---|---|
static long |
ALLOW_2_SLASHES
Allow two slashes in the path component of the URL.
|
static long |
ALLOW_ALL_SCHEMES
Allows all validly formatted schemes to pass validation instead of
supplying a set of valid schemes.
|
static long |
ALLOW_LOCAL_URLS
Allow local URLs, such as http://localhost/ or http://machine/ .
|
private java.util.Set<java.lang.String> |
allowedSchemes
The set of schemes that are allowed to be in a URL.
|
private static java.util.regex.Pattern |
ASCII_PATTERN |
private static java.lang.String |
AUTHORITY_CHARS_REGEX |
private static java.util.regex.Pattern |
AUTHORITY_PATTERN |
private static java.lang.String |
AUTHORITY_REGEX |
private RegexValidator |
authorityValidator
Regular expressions used to manually validate authorities if IANA
domain name validation isn't desired.
|
private static java.lang.String[] |
DEFAULT_SCHEMES
If no schemes are provided, default to this set.
|
private static UrlValidator |
DEFAULT_URL_VALIDATOR
Singleton instance of this class with default schemes and options.
|
private static java.lang.String |
LEGAL_ASCII_REGEX |
static long |
NO_FRAGMENTS
Enabling this options disallows any URL fragments.
|
private long |
options
Holds the set of current validation options.
|
private static int |
PARSE_AUTHORITY_EXTRA
Should always be empty.
|
private static int |
PARSE_AUTHORITY_HOST_IP |
private static int |
PARSE_AUTHORITY_PORT |
private static int |
PARSE_URL_AUTHORITY
Includes hostname/ip and port number.
|
private static int |
PARSE_URL_FRAGMENT |
private static int |
PARSE_URL_PATH |
private static int |
PARSE_URL_QUERY |
private static int |
PARSE_URL_SCHEME
Schema/Protocol (ie.
|
private static java.util.regex.Pattern |
PATH_PATTERN |
private static java.lang.String |
PATH_REGEX |
private static java.util.regex.Pattern |
PORT_PATTERN |
private static java.lang.String |
PORT_REGEX |
private static java.util.regex.Pattern |
QUERY_PATTERN |
private static java.lang.String |
QUERY_REGEX |
private static java.util.regex.Pattern |
SCHEME_PATTERN |
private static java.lang.String |
SCHEME_REGEX
Protocol (ie.
|
private static java.util.regex.Pattern |
URL_PATTERN |
private static java.lang.String |
URL_REGEX
This expression derived/taken from the BNF for URI (RFC2396).
|
| Constructor and Description |
|---|
UrlValidator()
Create a UrlValidator with default properties.
|
UrlValidator(long options)
Initialize a UrlValidator with the given validation options.
|
UrlValidator(RegexValidator authorityValidator,
long options)
Initialize a UrlValidator with the given validation options.
|
UrlValidator(java.lang.String[] schemes)
Behavior of validation is modified by passing in several strings options:
|
UrlValidator(java.lang.String[] schemes,
long options)
Behavior of validation is modified by passing in options:
|
UrlValidator(java.lang.String[] schemes,
RegexValidator authorityValidator,
long options)
Customizable constructor.
|
| Modifier and Type | Method and Description |
|---|---|
protected int |
countToken(java.lang.String token,
java.lang.String target)
Returns the number of times the token appears in the target.
|
static UrlValidator |
getInstance()
Returns the singleton instance of this class with default schemes and options.
|
private boolean |
isOff(long flag)
Tests whether the given flag is off.
|
private boolean |
isOn(long flag)
Tests whether the given flag is on.
|
boolean |
isValid(java.lang.String value)
Checks if a field has a valid url address.
|
protected boolean |
isValidAuthority(java.lang.String authority)
Returns true if the authority is properly formatted.
|
protected boolean |
isValidFragment(java.lang.String fragment)
Returns true if the given fragment is null or fragments are allowed.
|
protected boolean |
isValidPath(java.lang.String path)
Returns true if the path is valid.
|
protected boolean |
isValidQuery(java.lang.String query)
Returns true if the query is null or it's a properly formatted query string.
|
protected boolean |
isValidScheme(java.lang.String scheme)
Validate scheme.
|
getErrorMessage, getFix, setErrorMessage, setFixpublic static final long ALLOW_ALL_SCHEMES
public static final long ALLOW_2_SLASHES
public static final long NO_FRAGMENTS
public static final long ALLOW_LOCAL_URLS
RegexValidator instead (UrlValidator(RegexValidator, long))private static final java.lang.String AUTHORITY_CHARS_REGEX
private static final java.lang.String URL_REGEX
private static final java.util.regex.Pattern URL_PATTERN
private static final int PARSE_URL_SCHEME
private static final int PARSE_URL_AUTHORITY
private static final int PARSE_URL_PATH
private static final int PARSE_URL_QUERY
private static final int PARSE_URL_FRAGMENT
private static final java.lang.String SCHEME_REGEX
private static final java.util.regex.Pattern SCHEME_PATTERN
private static final java.lang.String AUTHORITY_REGEX
private static final java.util.regex.Pattern AUTHORITY_PATTERN
private static final int PARSE_AUTHORITY_HOST_IP
private static final int PARSE_AUTHORITY_PORT
private static final int PARSE_AUTHORITY_EXTRA
private static final java.lang.String PATH_REGEX
private static final java.util.regex.Pattern PATH_PATTERN
private static final java.lang.String QUERY_REGEX
private static final java.util.regex.Pattern QUERY_PATTERN
private static final java.lang.String LEGAL_ASCII_REGEX
private static final java.util.regex.Pattern ASCII_PATTERN
private static final java.lang.String PORT_REGEX
private static final java.util.regex.Pattern PORT_PATTERN
private final long options
private final java.util.Set<java.lang.String> allowedSchemes
private final RegexValidator authorityValidator
private static final java.lang.String[] DEFAULT_SCHEMES
private static final UrlValidator DEFAULT_URL_VALIDATOR
public UrlValidator()
public UrlValidator(java.lang.String[] schemes)
schemes - Pass in one or more url schemes to consider valid, passing in
a null will default to "http,https,ftp" being valid.
If a non-null schemes is specified then all valid schemes must
be specified. Setting the ALLOW_ALL_SCHEMES option will
ignore the contents of schemes.public UrlValidator(long 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(java.lang.String[] schemes, long options)
schemes - The set of valid 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 UrlValidator(RegexValidator authorityValidator, long options)
authorityValidator - Regular expression validator used to validate the authority partoptions - Validation options. Set using the public constants of this class.
To set multiple options, simply add them together:
ALLOW_2_SLASHES + NO_FRAGMENTS
public UrlValidator(java.lang.String[] schemes, RegexValidator authorityValidator, long options)
schemes - the set of valid schemesauthorityValidator - Regular expression validator used to validate the authority partoptions - Validation options. Set using the public constants of this class.
To set multiple options, simply add them together:
ALLOW_2_SLASHES + NO_FRAGMENTS
public static UrlValidator getInstance()
public boolean isValid(java.lang.String value)
Checks if a field has a valid url address.
isValid in class AbstractValidatorvalue - The value validation is being performed on. A null
value is considered invalid.protected boolean isValidScheme(java.lang.String scheme)
scheme - The scheme to validate. A null value is considered
invalid.protected boolean isValidAuthority(java.lang.String authority)
null authority value is considered invalid.authority - Authority value to validate.protected boolean isValidPath(java.lang.String path)
null value is considered invalid.path - Path value to validate.protected boolean isValidQuery(java.lang.String query)
query - Query value to validate.protected boolean isValidFragment(java.lang.String fragment)
fragment - Fragment value to validate.protected int countToken(java.lang.String token, java.lang.String target)
token - Token value to be counted.target - Target value to count tokens in.private boolean isOn(long flag)
flag - Flag value to check.private boolean isOff(long flag)
flag - Flag value to check.