ANT格式接口路径解析org.springframework.util.AntPathMatcher.domatch
public static boolean antPathMatcher(String reqPath, String configPath){
if(reqPath == null || reqPath.startsWith(DEFAULT_PATH_SEPARATOR) != configPath.startsWith(DEFAULT_PATH_SEPARATOR)){
return false;
}
String[] pathDirs = tokenizeToStringArray(reqPath, DEFAULT_PATH_SEPARATOR);
String[] pattDirs = tokenizeToStringArray(configPath, DEFAULT_PATH_SEPARATOR);
int pattIdxStart = 0;
int pattIdxEnd = pattDirs.length - 1;
int pathIdxStart = 0;
int pathIdxEnd = pathDirs.length - 1;
while (pattIdxStart <= pattIdxEnd && pathIdxStart <= pathIdxEnd) {
String pattDir = pattDirs[pattIdxStart];
if ("**".equals(pattDir)) {
break;
}
if (!matchStrings(pattDir, pathDirs[pathIdxStart])) {
return false;
}
pattIdxStart++;
pathIdxStart++;
}
if (pathIdxStart > pathIdxEnd) {
if (pattIdxStart > pattIdxEnd) {
return (configPath.endsWith(DEFAULT_PATH_SEPARATOR) == reqPath.endsWith(DEFAULT_PATH_SEPARATOR));
}
if (pattIdxStart == pattIdxEnd && pattDirs[pattIdxStart].equals("*") && reqPath.endsWith(DEFAULT_PATH_SEPARATOR)) {
return true;
}
for (int i = pattIdxStart; i <= pattIdxEnd; i++) {
if (!pattDirs[i].equals("**")) {
return false;
}
}
return true;
}else if (pattIdxStart > pattIdxEnd) {
return false;
}
while (pattIdxStart <= pattIdxEnd && pathIdxStart <= pathIdxEnd) {
String pattDir = pattDirs[pattIdxEnd];
if (pattDir.equals("**")) {
break;
}
if (!matchStrings(pattDir, pathDirs[pathIdxEnd])) {
return false;
}
pattIdxEnd--;
pathIdxEnd--;
}
if (pathIdxStart > pathIdxEnd) {
for (int i = pattIdxStart; i <= pattIdxEnd; i++) {
if (!pattDirs[i].equals("**")) {
return false;
}
}
return true;
}
while (pattIdxStart != pattIdxEnd && pathIdxStart <= pathIdxEnd) {
int patIdxTmp = -1;
for (int i = pattIdxStart + 1; i <= pattIdxEnd; i++) {
if (pattDirs[i].equals("**")) {
patIdxTmp = i;
break;
}
}
if (patIdxTmp == pattIdxStart + 1) {
pattIdxStart++;
continue;
}
int patLength = (patIdxTmp - (pattIdxStart + 1));
int strLength = (pathIdxEnd - pathIdxStart + 1);
int foundIdx = -1;
strLoop://平移操作?
for (int i = 0; i <= strLength - patLength; i++) {
for (int j = 0; j < patLength; j++) {
String subPat = pattDirs[(pattIdxStart + 1) + j];
String subStr = pathDirs[pathIdxStart + i + j];
if (!matchStrings(subPat, subStr)) {
continue strLoop;
}
}
foundIdx = pathIdxStart + i;
break;
}
if (foundIdx == -1) {
return false;
}
pattIdxStart = patIdxTmp;
pathIdxStart = foundIdx + patLength;
}
for (int i = pattIdxStart; i <= pattIdxEnd; i++) {
if (!pattDirs[i].equals("**")) {
return false;
}
}
return true;
}
public static Pattern changeAntStringToRegular(String pattern){
StringBuilder patternBuilder = new StringBuilder();
Matcher matcher = GLOB_PATTERN.matcher(pattern);
int end = 0;
while (matcher.find()) {
patternBuilder.append(quote(pattern, end, matcher.start()));
String match = matcher.group();
if ("?".equals(match)) {
patternBuilder.append('.');
}
else if ("*".equals(match)) {
patternBuilder.append(".*");
}
else if (match.startsWith("{") && match.endsWith("}")) {
match = match.substring(1, match.length() - 1);
patternBuilder.append('(');
patternBuilder.append(match);
patternBuilder.append(')');
}
end = matcher.end();
}
patternBuilder.append(quote(pattern, end, pattern.length()));
return Pattern.compile(patternBuilder.toString());
}
public static boolean matchStrings(String pattern, String path){
return changeAntStringToRegular(pattern).matcher(path).matches();
}
public static String quote(String s, int start, int end){
return start == end ? "" : Pattern.quote(s.substring(start, end));
}
public static String[] tokenizeToStringArray(String param, String delimiters){
if (param == null) {
return new String[0];
} else {
StringTokenizer st = new StringTokenizer(param, delimiters);
List<String> tokens = new ArrayList<String>();
while (true) {
String token;
do {
if (!st.hasMoreTokens()) {
return CollectionUtil.isEmpty(tokens) ? new String[0] : tokens.toArray(new String[tokens.size()]);
}
token = st.nextToken();
token = token.trim();
} while (token.length() <= 0);
tokens.add(token);
}
}
}