📄 urlvalidator.java
字号:
*/ protected boolean isValidScheme(String scheme) { if (scheme == null) { return false; } if (!Pattern.compile(SCHEME_PATTERN).matcher(scheme).matches()) { return false; } if (isOff(ALLOW_ALL_SCHEMES)) { if (!this.allowedSchemes.contains(scheme)) { return false; } } return true; } /** * Returns <code>true</code> if the authority is properly formatted. An authority is the * combination of host name and port. A <code>null</code> authority value is considered * invalid. * * @param authority * an authority value to validate * @return true if authority (host name and port) is valid. */ protected boolean isValidAuthority(String authority) { if (authority == null) { return false; } Matcher authorityMatcher = Pattern.compile(AUTHORITY_PATTERN).matcher(authority); if (!authorityMatcher.matches()) { return false; } boolean ipV4Address = false; boolean hostname = false; // check if authority is IP address or hostname String hostIP = authorityMatcher.group(PARSE_AUTHORITY_HOST_IP); Matcher matchIPV4Pat = Pattern.compile(IP_V4_DOMAIN_PATTERN).matcher(hostIP); ipV4Address = matchIPV4Pat.matches(); if (ipV4Address) { // this is an IP address so check components for (int i = 1; i <= 4; i++) { String ipSegment = matchIPV4Pat.group(i); if (ipSegment == null || ipSegment.length() <= 0) { return false; } try { if (Integer.parseInt(ipSegment) > 255) { return false; } } catch (NumberFormatException e) { return false; } } } else { // Domain is hostname name hostname = Pattern.compile(DOMAIN_PATTERN).matcher(hostIP).matches(); } // rightmost hostname will never start with a digit. if (hostname) { // LOW-TECH FIX FOR VALIDATOR-202 // TODO: Rewrite to use ArrayList and .add semantics: see // VALIDATOR-203 char[] chars = hostIP.toCharArray(); int size = 1; for (int i = 0; i < chars.length; i++) { if (chars[i] == '.') { size++; } } String[] domainSegment = new String[size]; boolean match = true; int segmentCount = 0; int segmentLength = 0; while (match) { Matcher atomMatcher = Pattern.compile(ATOM_PATTERN).matcher(hostIP); match = atomMatcher.find(); if (match) { domainSegment[segmentCount] = atomMatcher.group(1); segmentLength = domainSegment[segmentCount].length() + 1; hostIP = (segmentLength >= hostIP.length()) ? "" : hostIP .substring(segmentLength); segmentCount++; } } String topLevel = domainSegment[segmentCount - 1]; if (topLevel.length() < 2 || topLevel.length() > 4) { return false; } // First letter of top level must be a alpha Matcher alphaMatcher = Pattern.compile(ALPHA_PATTERN).matcher(topLevel.substring(0, 1)); if (!alphaMatcher.matches()) { return false; } // Make sure there's a host name preceding the authority. if (segmentCount < 2) { return false; } } if (!hostname && !ipV4Address) { return false; } String port = authorityMatcher.group(PARSE_AUTHORITY_PORT); if (port != null) { Matcher portMatcher = Pattern.compile(PORT_PATTERN).matcher(port); if (!portMatcher.matches()) { return false; } } String extra = authorityMatcher.group(PARSE_AUTHORITY_EXTRA); if (!isBlankOrNull(extra)) { return false; } return true; } /** * Returns <code>true</code> if the path is valid. A <code>null</code> value is considered * invalid. * * @param path * a path value to validate. * @return <code>true</code> if path is valid. */ protected boolean isValidPath(String path) { if (path == null) { return false; } Matcher pathMatcher = Pattern.compile(PATH_PATTERN).matcher(path); if (!pathMatcher.matches()) { return false; } int slash2Count = countToken("//", path); if (isOff(ALLOW_2_SLASHES) && (slash2Count > 0)) { return false; } int slashCount = countToken("/", path); int dot2Count = countToken("..", path); if (dot2Count > 0) { if ((slashCount - slash2Count - 1) <= dot2Count) { return false; } } return true; } /** * Returns <code>true</code> if the query is <code>null</code> or if it's a * properly-formatted query string. * * @param query * a query value to validate * @return <code>true</code> if the query is valid */ protected boolean isValidQuery(String query) { if (query == null) { return true; } Matcher queryMatcher = Pattern.compile(QUERY_PATTERN).matcher(query); return queryMatcher.matches(); } /** * Returns <code>true</code> if the given fragment is <code>null</code> or fragments are * allowed. * * @param fragment * a fragment value to validate * @return <code>true</code> if the fragment is valid */ protected boolean isValidFragment(String fragment) { if (fragment == null) { return true; } return isOff(NO_FRAGMENTS); } /** * Returns the number of times the token appears in the target. * * @param token * a token value to be counted * @param target * a target <code>String</code> to count tokens in * @return the number of tokens */ protected int countToken(String token, String target) { int tokenIndex = 0; int count = 0; while (tokenIndex != -1) { tokenIndex = target.indexOf(token, tokenIndex); if (tokenIndex > -1) { tokenIndex++; count++; } } return count; } /** * Checks if the field isn't <code>null</code> and if length of the field is greater than * zero, not including whitespace. * * @param value * the value validation is being performed on * @return <code>true</code> if blank or <code>null</code> */ public static boolean isBlankOrNull(String value) { return ((value == null) || (value.trim().length() == 0)); } // Flag Management /** * 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. * * @param flag * flag value to check * @return whether the specified flag value is on */ public boolean isOn(long flag) { return (this.options & flag) > 0; } /** * 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. * * @param flag * flag value to check. * @return whether the specified flag value is off */ public boolean isOff(long flag) { return (options & flag) == 0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -