简单说下配置的内容吧, 这个表达式格式真的折磨人
import com.alibaba.fastjson.JSON; import com.googlecode.aviator.AviatorEvaluator; import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.StringSubstitutor; import java.util.HashMap; import java.util.List; import java.util.Map; public String strJoin(String split){ StringUtils.join(split, "'=='"); System.out.println("split >>> "+split); return split; } public String strReplace(String strvalue){ strvalue = strvalue.replace(" ",""); if(!strvalue.contains("(")){ strvalue = "'"+strvalue.replace("==","'=='")+"'"; strvalue = strvalue.replace("||","'||'"); strvalue = strvalue.replace("&&","'&&'"); }else{ strvalue = strvalue.replace("(","('"); strvalue = strvalue.replace("==","'=='"); strvalue = strvalue.replace(")","')"); strvalue = strvalue.replace("||","'||'"); strvalue = strvalue.replace("&&","'&&'"); } return strvalue; } public String strChange(String groups) { StringBuffer groupsSplit = new StringBuffer(); String result = ""; // String groups = "dfffdsfd == 学习"; String[] split = groups.split("=="); for (int i = 0; i < split.length; i++) { if (!"".equals(split[i]) && split[i] != null) { groupsSplit.append("'" + split[i] + "'=="); } } result = groupsSplit.toString(); String tp = result.substring(result.length() - 2, result.length()); if ("==".equals(tp)) { result = result.substring(0, result.length() - 2); } result = result.replace(" ", ""); return result; }
private boolean checkIfLogic(String expr, Map<String, String> valuesMap) { try { Map<String, Object> jsonMap = (Map<String, Object>) JSON.parse(expr); List<String> expression = null; if (null != jsonMap && jsonMap.size() > 0) { expression = (List<String>) jsonMap.get("expression"); } expr = org.apache.commons.lang3.StringUtils.join(expression.toArray(), " "); StringSubstitutor sub = new StringSubstitutor(valuesMap); String resolvedString = sub.replace(expr); if (com.cmit.testing.utils.StringUtils.isNotEmpty(resolvedString)) { System.out.println(" 替换前的表达式 ==="+resolvedString); if (resolvedString.indexOf("'") == -1 && resolvedString.contains("==")) { resolvedString = strReplace(resolvedString); System.out.println(" 替换后的表单时==="+resolvedString); // resolvedString = strChange(resolvedString); // resolvedString = strJoin(resolvedString); } System.out.println(" 执行的表达式 >>>> resolvedString >>> " + resolvedString); try { Boolean b = (Boolean) AviatorEvaluator.execute(resolvedString); System.out.println("b >>>" + b); return b; } catch (Exception e) { System.out.println("AviatorEvaluator 解析表达式异常 >>> " + e.getMessage()); } } } catch (Exception e) { System.out.println("StringSubstitutor 解析表达式异常 >>> " + e.getMessage()); } return false; }
测试
public static void main(String[] args) { Test_C c = new Test_C(); Boolean bs = (boolean) AviatorEvaluator.execute("'1356969696a' == '1356969696' || '1356969696a' == '1356969696a' || ''==\"''\""); Boolean bastt = (boolean) AviatorEvaluator.execute("'1356969696a' == '1356969696' || '1356969696a' == '1356969696a' || ''==''"); System.out.println("bastt >>>" + bastt); boolean bst = (boolean) AviatorEvaluator.execute("'1356969696' == '1356969696' || '1356969696' == '1356969696a' || 1356969696==\"''\""); System.out.println(bs + "," + bst); // String expr1 = "{\"expression\":[\"'${account}'\",\"==\",\"''\"]}"; String expr1 = "{\"expression\":[\"(${account}\",\"==\",\"yhs123456789101112%)\",\"||\",\"${account}\",\"==\",\"yhs1234567891011121314151617181\"]}"; String expr2 = "{\"expression\":[\"${account}\",\"==\",\"yhs123456789101112%\",\"||\",\"(${account}\",\"==\",\"234242425)\",\"||\",\"${account}\",\"==\",\"uttueuedd22811122333\",\"&&\",\"(${account}\",\"==\",\"uttueuedd22811122333)\"]}"; String expr3 = "{\"expression\":[\"${account}\",\"==\",\"yhs123456789101112%\",\"||\",\"${account}\",\"==\",\"234242425\",\"||\",\"${account}\",\"==\",\"uttueuedd22811122333\",\"&&\",\"${account}\",\"==\",\"uttueuedd22811122333\",\"&&\",\"${account}\",\"==\",\"eteeessss3222\"]}"; String expr4 = "{\"expression\":[\"${account}\",\"==\",\"yhs123456789101112%\"]}"; // String expr1 = "{\"expression\":[\"('${account}'\",\"==\",\"'yhs123456789101112%')\",\"||\",\"'${account}'\",\"==\",\"'yhs1234567891011121314151617181'\"]}"; System.out.println(expr1); Map<String, String> valuesMap1 = new HashMap<>(); valuesMap1.put("account", "234242425"); boolean f = c.checkIfLogic(expr1, valuesMap1); System.out.println("f >>>" + f); }
测试结果
至此结束, 替换的结果真不是很好, 这个处理造成很大的问题。有左右括号时,就替换有问题。还在想着怎么弄,感觉还是挺这折腾的。