日期:2014-05-20 浏览次数:20882 次
import java.io.Serializable; import java.util.Arrays; import java.util.HashSet; import java.util.Set; import org.apache.commons.validator.util.Flags; import org.apache.oro.text.perl.Perl5Util; /** * <p>Validates URLs.</p> * Behavour of validation is modified by passing in options: * <li>ALLOW_2_SLASHES - [FALSE] Allows double '/' characters in the path * component.</li> * <li>NO_FRAGMENT- [FALSE] By default fragments are allowed, if this option is * included then fragments are flagged as illegal.</li> * <li>ALLOW_ALL_SCHEMES - [FALSE] By default only http, https, and ftp are * considered valid schemes. Enabling this option will let any scheme pass validation.</li> * * <p>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.</p> * <pre> * 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" * </pre> * * @see * <a href='http://www.ietf.org/rfc/rfc2396.txt' > * Uniform Resource Identifiers (URI): Generic Syntax * </a> * * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $ * @since Validator 1.1 */ public class UrlValidator implements Serializable { /** * Allows all validly formatted schemes to pass validation instead of * supplying a set of valid schemes. */ public static final int ALLOW_ALL_SCHEMES = 1 << 0; /** * Allow two slashes in the path component of the URL. */ public static final int ALLOW_2_SLASHES = 1 << 1; /** * Enabling this options disallows any URL fragments. */ public static final int NO_FRAGMENTS = 1 << 2; private static final String ALPHA_CHARS = "a-zA-Z"; private static final String ALPHA_NUMERIC_CHARS = ALPHA_CHARS + "\\d"; private static final String SPECIAL_CHARS = ";/@&=,.?:+$"; private static final String VALID_CHARS = "[^\\s" + SPECIAL_CHARS + "]"; private static final String SCHEME_CHARS = ALPHA_CHARS; // Drop numeric, and "+-." for now private static final String AUTHORITY_CHARS = ALPHA_NUMERIC_CHARS + "\\-\\."; private static final String ATOM = VALID_CHARS + '+'; /** * This expression derived/taken from the BNF for URI (RFC2396). */ private static final String URL_PATTERN = "/^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?/"; // 12 3 4 5 6 7 8 9 /** * Schema/Protocol (ie. http:, ftp:, file:, etc). */ private static final int PARSE_URL_SCHEME = 2; /** * Includes hostname/ip and port number. */ private static final int PARSE_URL_AUTHORITY = 4; private static final int PARSE_URL_PATH = 5; private static final int PARSE_URL_QUERY = 7; private static final int PARSE_URL_FRAGMENT = 9; /** * Protocol (ie. http:, ftp:,https:). */ private static final String SCHEME_PATTERN = "/^[" + SCHEME_CHARS + "]/"; private static final String AUTHORITY_PATTERN = "/^([" + AUTHORITY_CHARS + "]*)(:\\d*)?(.*)?/"; // 1 2 3 4 private static final int PARSE_AUTHORITY_HOST_IP = 1; private static final int PARSE_AUTHORITY_PORT = 2; /** * Should always be empty. */ private static final int PARSE_AUTHORITY_EXTRA = 3; private static final String PATH_PATTERN = "/^(/[-\\w:@&?=+,.!/~*'%$_;]*)?$/"; private static final String QUERY_PATTERN = "/^(.*)$/"; private static final String LEGAL_ASCII_PATTERN = "/^[\\000-\\177]+$/"; private static final String IP_V4_DOMAIN_PATTERN = "/^(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})$/"; private static final String DOMAIN_PATTERN = "/^" + ATOM + "(\\." + ATOM + ")*$/"; private static final String PORT_PATTERN = "/^:(\\d{1,5})$/"; private static final String ATOM_PATTERN = "/(" + ATOM + ")/"; private static final String ALPHA_PATTERN = "/^[" + ALPHA_CHARS + "]/"; /** * Holds the set of current validation options. */ private Flags options = null; /** * The set of schemes that are allowed to be in a URL. */ private Set allowedSchemes = new HashSet(); /** * If no schemes are provided, default to this set. */ protected String[] defaultSchemes = {"http", "https", "ftp"}; /** * Create a UrlValidator with default properties. */ public UrlValidator() { this(null); } /** * Behavior of validation is modified by passing in several strings options: * @param 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(String[] schemes) { this(schemes, 0); } /** * Initialize a UrlValidator with the given validation options. * @param 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(int options) { this(null, options); } /** * Behavour of validation is modified by passing in options: * @param schemes The set of valid schemes. * @param 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) { this.options = new Flags(options); if (this.options.isOn(ALLOW_ALL_SCHEMES)) { return; } if (schemes == null) { schemes = this.defaultSchemes; } this.allowedSchemes.addAll(Arrays.asList(schemes)); }