!240 支持绝对路径的模糊匹配

Merge pull request !240 from Rain/dev
This commit is contained in:
铂赛东
2023-10-09 08:31:29 +00:00
committed by Gitee
23 changed files with 389 additions and 88 deletions

View File

@@ -1,8 +1,11 @@
package com.yomahub.liteflow.solon.config;
import cn.hutool.core.io.FileUtil;
import com.yomahub.liteflow.util.PathMatchUtil;
import org.noear.solon.core.util.ScanUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.regex.Pattern;
@@ -17,51 +20,55 @@ public class PathsUtils {
public static Collection<String> resolvePaths(String pathExpr) {
List<String> paths = new ArrayList<>();
if(!FileUtil.isAbsolutePath(pathExpr)) {
if (pathExpr.contains("/*") == false) { // 说明没有*符
paths.add(pathExpr);
return paths;
}
if (pathExpr.contains("/*") == false) { // 说明没有*符
paths.add(pathExpr);
return paths;
// 确定目录
int dirIdx = pathExpr.indexOf("/*");
String dir = pathExpr.substring(0, dirIdx);
// 确定后缀
int sufIdx = pathExpr.lastIndexOf(".");
String suf = null;
if (sufIdx > 0) {
suf = pathExpr.substring(sufIdx);
if (suf.contains("*")) {
sufIdx = -1;
suf = null;
}
}
int sufIdx2 = sufIdx;
String suf2 = suf;
// 匹配表达式
String expr = pathExpr.replaceAll("/\\*\\.", "/[^\\.]*\\.");
expr = expr.replaceAll("/\\*\\*/", "(/[^/]*)*/");
Pattern pattern = Pattern.compile(expr);
List<String> finalPaths = paths;
ScanUtil.scan(dir, n -> {
// 进行后缀过滤,相对比较快
if (sufIdx2 > 0) {
return n.endsWith(suf2);
}
else {
return true;
}
}).forEach(uri -> {
// 再进行表达式过滤
if (pattern.matcher(uri).find()) {
finalPaths.add(uri);
}
});
} else {
String[] pathExprs = pathExpr.split(",");
paths = PathMatchUtil.searchAbsolutePath(Arrays.asList(pathExprs));
}
// 确定目录
int dirIdx = pathExpr.indexOf("/*");
String dir = pathExpr.substring(0, dirIdx);
// 确定后缀
int sufIdx = pathExpr.lastIndexOf(".");
String suf = null;
if (sufIdx > 0) {
suf = pathExpr.substring(sufIdx);
if (suf.contains("*")) {
sufIdx = -1;
suf = null;
}
}
int sufIdx2 = sufIdx;
String suf2 = suf;
// 匹配表达式
String expr = pathExpr.replaceAll("/\\*\\.", "/[^\\.]*\\.");
expr = expr.replaceAll("/\\*\\*/", "(/[^/]*)*/");
Pattern pattern = Pattern.compile(expr);
ScanUtil.scan(dir, n -> {
// 进行后缀过滤,相对比较快
if (sufIdx2 > 0) {
return n.endsWith(suf2);
}
else {
return true;
}
}).forEach(uri -> {
// 再进行表达式过滤
if (pattern.matcher(uri).find()) {
paths.add(uri);
}
});
return paths;
}