org.apache.commons.validator

Class UrlValidator

Implemented Interfaces:
Serializable

public class UrlValidator
extends java.lang.Object
implements Serializable

Validates URLs. Behavour of validation is modified by passing in 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.
  • 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")) {
           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")) {
           System.out.println("url is valid");
        } else {
           System.out.println("url is invalid");
        }
    
       prints out "url is valid"
      

    Since:
    Validator 1.1

    See Also:
    Uniform Resource Identifiers (URI): Generic Syntax

    Nested Class Summary

    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.
    private static String
    ALPHA_CHARS
    private static String
    ALPHA_NUMERIC_CHARS
    private static String
    ALPHA_PATTERN
    private static String
    ATOM
    private static String
    ATOM_PATTERN
    private static String
    AUTHORITY_CHARS
    private static String
    AUTHORITY_PATTERN
    private static String
    DOMAIN_PATTERN
    private static String
    IP_V4_DOMAIN_PATTERN
    private static String
    LEGAL_ASCII_PATTERN
    static int
    NO_FRAGMENTS
    Enabling this options disallows any URL fragments.
    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 String
    PATH_PATTERN
    private static String
    PORT_PATTERN
    private static String
    QUERY_PATTERN
    private static String
    SCHEME_CHARS
    private static String
    SCHEME_PATTERN
    Protocol (ie.
    private static String
    SPECIAL_CHARS
    private static String
    URL_PATTERN
    This expression derived/taken from the BNF for URI (RFC2396).
    private static String
    VALID_CHARS
    private 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.
    private Flags
    options
    Holds the set of current validation options.

    Constructor Summary

    UrlValidator()
    Create a UrlValidator with default properties.
    UrlValidator(String[] schemes)
    Behavior of validation is modified by passing in several strings options:
    UrlValidator(String[] schemes, int options)
    Behavour of validation is modified by passing in options:
    UrlValidator(int options)
    Initialize a UrlValidator with the given validation 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.

    Field Details

    ALLOW_2_SLASHES

    public static final int ALLOW_2_SLASHES
    Allow two slashes in the path component of the URL.

    Field Value:
    2


    ALLOW_ALL_SCHEMES

    public static final int ALLOW_ALL_SCHEMES
    Allows all validly formatted schemes to pass validation instead of supplying a set of valid schemes.

    Field Value:
    1


    ALPHA_CHARS

    private static final String ALPHA_CHARS


    ALPHA_NUMERIC_CHARS

    private static final String ALPHA_NUMERIC_CHARS


    ALPHA_PATTERN

    private static final String ALPHA_PATTERN


    ATOM

    private static final String ATOM


    ATOM_PATTERN

    private static final String ATOM_PATTERN


    AUTHORITY_CHARS

    private static final String AUTHORITY_CHARS


    AUTHORITY_PATTERN

    private static final String AUTHORITY_PATTERN


    DOMAIN_PATTERN

    private static final String DOMAIN_PATTERN


    IP_V4_DOMAIN_PATTERN

    private static final String IP_V4_DOMAIN_PATTERN


    LEGAL_ASCII_PATTERN

    private static final String LEGAL_ASCII_PATTERN


    NO_FRAGMENTS

    public static final int NO_FRAGMENTS
    Enabling this options disallows any URL fragments.

    Field Value:
    4


    PARSE_AUTHORITY_EXTRA

    private static final int PARSE_AUTHORITY_EXTRA
    Should always be empty.

    Field Value:
    3


    PARSE_AUTHORITY_HOST_IP

    private static final int PARSE_AUTHORITY_HOST_IP

    Field Value:
    1


    PARSE_AUTHORITY_PORT

    private static final int PARSE_AUTHORITY_PORT

    Field Value:
    2


    PARSE_URL_AUTHORITY

    private static final int PARSE_URL_AUTHORITY
    Includes hostname/ip and port number.

    Field Value:
    4


    PARSE_URL_FRAGMENT

    private static final int PARSE_URL_FRAGMENT

    Field Value:
    9


    PARSE_URL_PATH

    private static final int PARSE_URL_PATH

    Field Value:
    5


    PARSE_URL_QUERY

    private static final int PARSE_URL_QUERY

    Field Value:
    7


    PARSE_URL_SCHEME

    private static final int PARSE_URL_SCHEME
    Schema/Protocol (ie. http:, ftp:, file:, etc).

    Field Value:
    2


    PATH_PATTERN

    private static final String PATH_PATTERN


    PORT_PATTERN

    private static final String PORT_PATTERN


    QUERY_PATTERN

    private static final String QUERY_PATTERN


    SCHEME_CHARS

    private static final String SCHEME_CHARS


    SCHEME_PATTERN

    private static final String SCHEME_PATTERN
    Protocol (ie. http:, ftp:,https:).


    SPECIAL_CHARS

    private static final String SPECIAL_CHARS


    URL_PATTERN

    private static final String URL_PATTERN
    This expression derived/taken from the BNF for URI (RFC2396).


    VALID_CHARS

    private static final String VALID_CHARS


    allowedSchemes

    private Set allowedSchemes
    The set of schemes that are allowed to be in a URL.


    defaultSchemes

    protected String[] defaultSchemes
    If no schemes are provided, default to this set.


    options

    private Flags options
    Holds the set of current validation options.

    Constructor Details

    UrlValidator

    public UrlValidator()
    Create a UrlValidator with default properties.


    UrlValidator

    public UrlValidator(String[] schemes)
    Behavior of validation is modified by passing in several strings options:

    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.


    UrlValidator

    public UrlValidator(String[] schemes,
                        int options)
    Behavour of validation is modified by passing in 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.


    UrlValidator

    public UrlValidator(int options)
    Initialize a UrlValidator 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.

    Method Details

    countToken

    protected int countToken(String token,
                             String target)
    Returns the number of times the token appears in the target.


    isValid

    public boolean isValid(String value)
    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.


    isValidAuthority

    protected boolean isValidAuthority(String authority)
    Returns true if the authority is properly formatted. An authority is the combination of hostname and port. A null authority value is considered invalid.


    isValidFragment

    protected boolean isValidFragment(String fragment)
    Returns true if the given fragment is null or fragments are allowed.


    isValidPath

    protected boolean isValidPath(String path)
    Returns true if the path is valid. A null value is considered invalid.


    isValidQuery

    protected boolean isValidQuery(String query)
    Returns true if the query is null or it's a properly formatted query string.


    isValidScheme

    protected boolean isValidScheme(String scheme)
    Validate scheme. If schemes[] was initialized to a non null, then only those scheme's are allowed. Note this is slightly different than for the constructor.

    Parameters:
    scheme - The scheme to validate. A null value is considered invalid.

    Returns:
    true if valid.


    Copyright (c) 2001-2004 Apache Software Foundation