|
@@ -1,9 +1,8 @@
|
|
package cn.cslg.pas.common.utils.parseQueryToTree;
|
|
package cn.cslg.pas.common.utils.parseQueryToTree;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.HashMap;
|
|
|
|
-import java.util.List;
|
|
|
|
-import java.util.Stack;
|
|
|
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
+
|
|
|
|
+import java.util.*;
|
|
|
|
|
|
public class expressManager {
|
|
public class expressManager {
|
|
HashMap<String, Symbol> hSymbols = new HashMap<String, Symbol>();
|
|
HashMap<String, Symbol> hSymbols = new HashMap<String, Symbol>();
|
|
@@ -162,7 +161,14 @@ public class expressManager {
|
|
/// <param name="isAnd">如果输入的是多个条件,之间的关系是and还是or,true为and,false为or</param>
|
|
/// <param name="isAnd">如果输入的是多个条件,之间的关系是and还是or,true为and,false为or</param>
|
|
/// <returns>表达式树节点</returns>
|
|
/// <returns>表达式树节点</returns>
|
|
public treeNode Parse(String strExpress, boolean isAnd) throws Exception {
|
|
public treeNode Parse(String strExpress, boolean isAnd) throws Exception {
|
|
- ArrayList<String> Tokens = GetTokens(strExpress);
|
|
|
|
|
|
+ ArrayList<String> tokens = getTokens(strExpress);
|
|
|
|
+ ArrayList<String> Tokens = new ArrayList<>();
|
|
|
|
+ if (!CollectionUtils.isEmpty(tokens)) {
|
|
|
|
+ for (String token : tokens) {
|
|
|
|
+ String s = token.replaceAll("^\"*|\"*$", "");
|
|
|
|
+ Tokens.add(s);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
Stack<Symbol> symbolStack = new Stack<Symbol>();
|
|
Stack<Symbol> symbolStack = new Stack<Symbol>();
|
|
Stack<treeNode> valueStack = new Stack<treeNode>();
|
|
Stack<treeNode> valueStack = new Stack<treeNode>();
|
|
for (String strTem : Tokens) {
|
|
for (String strTem : Tokens) {
|
|
@@ -417,4 +423,55 @@ public class expressManager {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public ArrayList<String> getTokens(String strExpression) {
|
|
|
|
+ if (strExpression != null && !strExpression.isEmpty()) {
|
|
|
|
+ ArrayList<String> tokens = new ArrayList<>();
|
|
|
|
+ String trimmedExpression = strExpression.trim();
|
|
|
|
+ StringBuilder currentToken = new StringBuilder();
|
|
|
|
+ boolean isInsideQuotes = false;
|
|
|
|
+
|
|
|
|
+ for (int i = 0; i < trimmedExpression.length(); i++) {
|
|
|
|
+ char currentChar = trimmedExpression.charAt(i);
|
|
|
|
+
|
|
|
|
+ if (currentChar == '"') {
|
|
|
|
+ if (!isInsideQuotes) {
|
|
|
|
+ isInsideQuotes = true;
|
|
|
|
+ currentToken.append(currentChar); // 添加双引号到当前单词
|
|
|
|
+ } else {
|
|
|
|
+ isInsideQuotes = false;
|
|
|
|
+ currentToken.append(currentChar); // 添加双引号到当前单词
|
|
|
|
+ tokens.add(currentToken.toString());
|
|
|
|
+ currentToken.setLength(0); // 清空 StringBuilder
|
|
|
|
+ }
|
|
|
|
+ } else if (currentChar == ' ' && !isInsideQuotes) {
|
|
|
|
+ if (currentToken.length() > 0) {
|
|
|
|
+ tokens.add(currentToken.toString());
|
|
|
|
+ currentToken.setLength(0); // Clear StringBuilder
|
|
|
|
+ }
|
|
|
|
+ } else if (currentChar == '(' || currentChar == '[' || currentChar == ')' || currentChar == ']') {
|
|
|
|
+ if (!isInsideQuotes) {
|
|
|
|
+ if (currentToken.length() > 0) {
|
|
|
|
+ tokens.add(currentToken.toString());
|
|
|
|
+ currentToken.setLength(0); // Clear StringBuilder
|
|
|
|
+ }
|
|
|
|
+ tokens.add(String.valueOf(currentChar));
|
|
|
|
+ } else {
|
|
|
|
+ currentToken.append(currentChar);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ currentToken.append(currentChar);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (currentToken.length() > 0) {
|
|
|
|
+ tokens.add(currentToken.toString());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return tokens;
|
|
|
|
+ } else {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|