This commit is contained in:
Looly 2022-02-25 16:54:44 +08:00
parent 225e730a84
commit 45f2de8d09

View File

@ -143,57 +143,78 @@ public class AntPathMatcher {
return this; return this;
} }
/**
* 判断给定路径是否是表达式
*
* @param path 路径
* @return 是否为表达式
*/
public boolean isPattern(String path) { public boolean isPattern(String path) {
if (path == null) { if (path == null) {
return false; return false;
} }
boolean uriVar = false; boolean uriVar = false;
for (int i = 0; i < path.length(); i++) { final int length = path.length();
char c = path.charAt(i); char c;
for (int i = 0; i < length; i++) {
c = path.charAt(i);
// 含有通配符
if (c == '*' || c == '?') { if (c == '*' || c == '?') {
return true; return true;
} }
if (c == '{') { if (c == CharPool.DELIM_START) {
uriVar = true; uriVar = true;
continue; continue;
} }
if (c == '}' && uriVar) { if (c == CharPool.DELIM_END && uriVar) {
return true; return true;
} }
} }
return false; return false;
} }
/**
* 给定路径是否匹配表达式
*
* @param pattern 表达式
* @param path 路径
* @return 是否匹配
*/
public boolean match(String pattern, String path) { public boolean match(String pattern, String path) {
return doMatch(pattern, path, true, null); return doMatch(pattern, path, true, null);
} }
/**
* 前置部分匹配
*
* @param pattern 表达式
* @param path 路径
* @return 是否匹配
*/
public boolean matchStart(String pattern, String path) { public boolean matchStart(String pattern, String path) {
return doMatch(pattern, path, false, null); return doMatch(pattern, path, false, null);
} }
/** /**
* Actually match the given {@code path} against the given {@code pattern}. * 执行匹配判断给定的{@code path}是否匹配{@code pattern}
* *
* @param pattern the pattern to match against * @param pattern 表达式
* @param path the path to test * @param path 路径
* @param fullMatch whether a full pattern match is required (else a pattern match * @param fullMatch 是否全匹配{@code true} 表示全路径匹配{@code false}表示只匹配开始
* as far as the given base path goes is sufficient)
* @param uriTemplateVariables 变量映射 * @param uriTemplateVariables 变量映射
* @return {@code true} if the supplied {@code path} matched, {@code false} if it didn't * @return {@code true} 表示提供的 {@code path} 匹配, {@code false} 表示不匹配
*/ */
protected boolean doMatch(String pattern, String path, boolean fullMatch, Map<String, String> uriTemplateVariables) { protected boolean doMatch(String pattern, String path, boolean fullMatch, Map<String, String> uriTemplateVariables) {
if (path == null || path.startsWith(this.pathSeparator) != pattern.startsWith(this.pathSeparator)) { if (path == null || path.startsWith(this.pathSeparator) != pattern.startsWith(this.pathSeparator)) {
return false; return false;
} }
String[] pattDirs = tokenizePattern(pattern); final String[] pattDirs = tokenizePattern(pattern);
if (fullMatch && this.caseSensitive && !isPotentialMatch(path, pattDirs)) { if (fullMatch && this.caseSensitive && false == isPotentialMatch(path, pattDirs)) {
return false; return false;
} }
String[] pathDirs = tokenizePath(path); final String[] pathDirs = tokenizePath(path);
int pattIdxStart = 0; int pattIdxStart = 0;
int pattIdxEnd = pattDirs.length - 1; int pattIdxEnd = pattDirs.length - 1;
int pathIdxStart = 0; int pathIdxStart = 0;
@ -224,7 +245,7 @@ public class AntPathMatcher {
return true; return true;
} }
for (int i = pattIdxStart; i <= pattIdxEnd; i++) { for (int i = pattIdxStart; i <= pattIdxEnd; i++) {
if (!pattDirs[i].equals("**")) { if (false == pattDirs[i].equals("**")) {
return false; return false;
} }
} }
@ -232,7 +253,7 @@ public class AntPathMatcher {
} else if (pattIdxStart > pattIdxEnd) { } else if (pattIdxStart > pattIdxEnd) {
// String not exhausted, but pattern is. Failure. // String not exhausted, but pattern is. Failure.
return false; return false;
} else if (!fullMatch && "**".equals(pattDirs[pattIdxStart])) { } else if (false == fullMatch && "**".equals(pattDirs[pattIdxStart])) {
// Path start definitely matches due to "**" part in pattern. // Path start definitely matches due to "**" part in pattern.
return true; return true;
} }
@ -252,7 +273,7 @@ public class AntPathMatcher {
if (pathIdxStart > pathIdxEnd) { if (pathIdxStart > pathIdxEnd) {
// String is exhausted // String is exhausted
for (int i = pattIdxStart; i <= pattIdxEnd; i++) { for (int i = pattIdxStart; i <= pattIdxEnd; i++) {
if (!pattDirs[i].equals("**")) { if (false == pattDirs[i].equals("**")) {
return false; return false;
} }
} }
@ -300,7 +321,7 @@ public class AntPathMatcher {
} }
for (int i = pattIdxStart; i <= pattIdxEnd; i++) { for (int i = pattIdxStart; i <= pattIdxEnd; i++) {
if (!pattDirs[i].equals("**")) { if (false == pattDirs[i].equals("**")) {
return false; return false;
} }
} }