org.apache.commons.validator
public class UrlValidator extends Object implements Serializable
Validates URLs.
Behavour 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"
Since: Validator 1.1
Version: $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
See Also:
Uniform Resource Identifiers (URI): Generic Syntax
Field Summary | |
---|---|
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. |
static String | ALPHA_CHARS |
static String | ALPHA_NUMERIC_CHARS |
static String | ALPHA_PATTERN |
static String | ATOM |
static String | ATOM_PATTERN |
static String | AUTHORITY_CHARS |
static String | AUTHORITY_PATTERN |
Set | allowedSchemes
The set of schemes that are allowed to be in a URL. |
protected String[] | defaultSchemes
If no schemes are provided, default to this set. |
static String | DOMAIN_PATTERN |
static String | IP_V4_DOMAIN_PATTERN |
static String | LEGAL_ASCII_PATTERN |
static int | NO_FRAGMENTS
Enabling this options disallows any URL fragments. |
Flags | options
Holds the set of current validation options. |
static int | PARSE_AUTHORITY_EXTRA
Should always be empty. |
static int | PARSE_AUTHORITY_HOST_IP |
static int | PARSE_AUTHORITY_PORT |
static int | PARSE_URL_AUTHORITY
Includes hostname/ip and port number. |
static int | PARSE_URL_FRAGMENT |
static int | PARSE_URL_PATH |
static int | PARSE_URL_QUERY |
static int | PARSE_URL_SCHEME
Schema/Protocol (ie. http:, ftp:, file:, etc). |
static String | PATH_PATTERN |
static String | PORT_PATTERN |
static String | QUERY_PATTERN |
static String | SCHEME_CHARS |
static String | SCHEME_PATTERN
Protocol (ie. http:, ftp:,https:). |
static String | SPECIAL_CHARS |
static String | URL_PATTERN
This expression derived/taken from the BNF for URI (RFC2396). |
static String | VALID_CHARS |
Constructor Summary | |
---|---|
UrlValidator()
Create a UrlValidator with default properties. | |
UrlValidator(String[] schemes)
Behavior of validation is modified by passing in several strings options: | |
UrlValidator(int options)
Initialize a UrlValidator with the given validation options. | |
UrlValidator(String[] schemes, int options)
Behavour of validation is modified by passing in options: |
Method Summary | |
---|---|
protected int | countToken(String token, String target)
Returns the number of times the token appears in the target. |
boolean | isValid(String value) Checks if a field has a valid url address. |
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 it's a properly formatted query string. |
protected boolean | isValidScheme(String scheme)
Validate scheme. |
Parameters: 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.
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.
Parameters: 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.
Parameters: token Token value to be counted. target Target value to count tokens in.
Returns: the number of tokens.
Checks if a field has a valid url address.
Parameters: value The value validation is being performed on. A null
value is considered invalid.
Returns: true if the url is valid.
null
authority value is considered invalid.Parameters: authority Authority value to validate.
Returns: true if authority (hostname and port) is valid.
Parameters: fragment Fragment value to validate.
Returns: true if fragment is valid.
null
value is considered invalid.Parameters: path Path value to validate.
Returns: true if path is valid.
Parameters: query Query value to validate.
Returns: true if query is valid.
Parameters: scheme The scheme to validate. A null
value is considered
invalid.
Returns: true if valid.