org.apache.wicket.validation.validator
Class UrlValidator

java.lang.Object
  extended by org.apache.wicket.validation.validator.AbstractValidator<String>
      extended by org.apache.wicket.validation.validator.UrlValidator
All Implemented Interfaces:
Serializable, IClusterable, INullAcceptingValidator<String>, IValidator<String>

public class UrlValidator
extends AbstractValidator<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:

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
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 Summary
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).
 
Method Summary
protected  int countToken(String token, String target)
          Returns the number of times the token appears in the target.
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.
protected  void onValidate(IValidatable<String> validatable)
          Validates the IValidatable instance.
 
Methods inherited from class org.apache.wicket.validation.validator.AbstractValidator
error, error, error, error, messageModel, resourceKey, resourceKey, validate, validate, validateOnNullValue, variablesMap
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

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.

See Also:
Constant Field Values

ALLOW_2_SLASHES

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

See Also:
Constant Field Values

NO_FRAGMENTS

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

See Also:
Constant Field Values

defaultSchemes

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

Constructor Detail

UrlValidator

public UrlValidator()
Constructs a UrlValidator with default properties.


UrlValidator

public UrlValidator(String[] schemes)
Constructs a UrlValidator with the given String array of scheme options. The validation is modified by passing in options in the schemes argument.

Parameters:
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.

UrlValidator

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

UrlValidator

public UrlValidator(String[] schemes,
                    int options)
Constructs a UrlValidator with the given scheme and validation options (see class description).

Parameters:
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.
Method Detail

onValidate

protected void onValidate(IValidatable<String> validatable)
Description copied from class: AbstractValidator
Validates the IValidatable instance.

Specified by:
onValidate in class AbstractValidator<String>
Parameters:
validatable - the given IValidatable instance
See Also:
AbstractValidator.onValidate(IValidatable)

isValid

public final boolean isValid(String value)
Checks if a field has a valid URL. This method is public because it is directly used in tests.

Parameters:
value - The value validation is being performed on. A null value is considered invalid.
Returns:
true if the URL is valid

isValidScheme

protected boolean isValidScheme(String scheme)
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. 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 host name and port. A null authority value is considered invalid.

Parameters:
authority - an authority value to validate
Returns:
true if authority (host name and port) is valid.

isValidPath

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

Parameters:
path - a path value to validate.
Returns:
true if path is valid.

isValidQuery

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

Parameters:
query - a query value to validate
Returns:
true if the query is valid

isValidFragment

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

Parameters:
fragment - a fragment value to validate
Returns:
true if the fragment is valid

countToken

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

Parameters:
token - a token value to be counted
target - a target String to count tokens in
Returns:
the number of tokens

isBlankOrNull

public 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.

Parameters:
value - the value validation is being performed on
Returns:
true if blank or null

isOn

public boolean isOn(long flag)
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

public boolean isOff(long flag)
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


Copyright © 2004-2011 Apache Software Foundation. All Rights Reserved.