|
14 | 14 |
|
15 | 15 | package org.casbin.jcasbin.util;
|
16 | 16 |
|
| 17 | +import com.googlecode.aviator.AviatorEvaluator; |
| 18 | +import com.googlecode.aviator.AviatorEvaluatorInstance; |
17 | 19 | import com.googlecode.aviator.runtime.function.AbstractFunction;
|
18 | 20 | import com.googlecode.aviator.runtime.function.FunctionUtils;
|
19 | 21 | import com.googlecode.aviator.runtime.type.AviatorBoolean;
|
|
23 | 25 | import inet.ipaddr.IPAddress;
|
24 | 26 | import inet.ipaddr.IPAddressString;
|
25 | 27 | import org.casbin.jcasbin.rbac.RoleManager;
|
26 |
| -import org.codehaus.commons.compiler.CompileException; |
27 |
| -import org.codehaus.janino.ExpressionEvaluator; |
28 | 28 |
|
29 |
| -import java.lang.reflect.InvocationTargetException; |
30 | 29 | import java.util.*;
|
31 |
| -import java.util.Map.Entry; |
32 | 30 | import java.util.regex.Matcher;
|
33 | 31 | import java.util.regex.Pattern;
|
34 | 32 |
|
35 | 33 | public class BuiltInFunctions {
|
36 | 34 |
|
37 | 35 | private static Pattern keyMatch2Pattern = Pattern.compile("(.*):[^/]+(.*)");
|
38 | 36 | private static Pattern keyMatch3Pattern = Pattern.compile("(.*)\\{[^/]+}(.*)");
|
39 |
| - private static Pattern evalPattern = Pattern.compile("(?<=\\.).*?(?=\\.| )"); |
40 | 37 |
|
41 | 38 | /**
|
42 | 39 | * keyMatch determines whether key1 matches the pattern of key2 (similar to RESTful path), key2
|
@@ -363,81 +360,20 @@ public String getName() {
|
363 | 360 | }
|
364 | 361 |
|
365 | 362 | /**
|
366 |
| - * eval calculates the stringified boolean expression and return its result. The syntax of |
367 |
| - * expressions is exactly the same as Java. Flaw: dynamically generated classes or non-static |
368 |
| - * inner class cannot be used. |
| 363 | + * eval calculates the stringified boolean expression and return its result. |
369 | 364 | *
|
370 |
| - * @author tldyl |
371 |
| - * @since 2020-07-02 |
372 |
| - * |
373 |
| - * @param eval Boolean expression. |
374 |
| - * @param env Parameters. |
375 |
| - * @return The result of the eval. |
| 365 | + * @param eval the stringified boolean expression. |
| 366 | + * @param env the key-value pair of the parameters in the expression. |
| 367 | + * @param aviatorEval the AviatorEvaluatorInstance object which contains built-in functions and custom functions. |
| 368 | + * @return the result of the eval. |
376 | 369 | */
|
377 |
| - public static boolean eval(String eval, Map<String, Object> env) { |
378 |
| - ExpressionEvaluator evaluator = new ExpressionEvaluator(); |
379 |
| - Map<String, Map<String, Object>> evalModels = getEvalModels(env); |
380 |
| - try { |
381 |
| - List<String> parameterNameList = new ArrayList<>(); |
382 |
| - List<Object> parameterValueList = new ArrayList<>(); |
383 |
| - List<Class<?>> parameterClassList = new ArrayList<>(); |
384 |
| - for (Map.Entry<String, Object> entry: env.entrySet()) { |
385 |
| - parameterNameList.add(entry.getKey()); |
386 |
| - parameterValueList.add(entry.getValue()); |
387 |
| - parameterClassList.add(entry.getValue().getClass()); |
388 |
| - } |
389 |
| - List<String> sortedSrc = new ArrayList<>(getReplaceTargets(evalModels)); |
390 |
| - sortedSrc.sort((o1, o2) -> o1.length() > o2.length() ? -1 : 1); |
391 |
| - for (String s : sortedSrc) { |
392 |
| - eval = eval.replace("." + s, "_" + s); |
393 |
| - } |
394 |
| - Matcher matcher = evalPattern.matcher(eval); |
395 |
| - while (matcher.find()) { |
396 |
| - for (int i = 0; i <= matcher.groupCount(); i++) { |
397 |
| - eval = eval.replace(matcher.group(), obtainFieldGetMethodName(matcher.group())); |
398 |
| - } |
399 |
| - } |
400 |
| - evaluator.setParameters(parameterNameList.toArray(new String[0]), parameterClassList.toArray(new Class[0])); |
401 |
| - evaluator.cook(eval); |
402 |
| - return (boolean) evaluator.evaluate(parameterValueList.toArray(new Object[0])); |
403 |
| - } catch (InvocationTargetException | CompileException e) { |
404 |
| - e.printStackTrace(); |
| 370 | + public static boolean eval(String eval, Map<String, Object> env, AviatorEvaluatorInstance aviatorEval) { |
| 371 | + boolean res; |
| 372 | + if (aviatorEval != null) { |
| 373 | + res = (boolean) aviatorEval.execute(eval, env); |
| 374 | + } else { |
| 375 | + res = (boolean) AviatorEvaluator.execute(eval, env); |
405 | 376 | }
|
406 |
| - return false; |
407 |
| - } |
408 |
| - |
409 |
| - /** |
410 |
| - * getEvalModels extracts the value from env and assemble it into a EvalModel object. |
411 |
| - * |
412 |
| - * @param env the map. |
413 |
| - */ |
414 |
| - private static Map<String, Map<String, Object>> getEvalModels(Map<String, Object> env) { |
415 |
| - final Map<String, Map<String, Object>> evalModels = new HashMap<>(); |
416 |
| - for (final Entry<String, Object> entry : env.entrySet()) { |
417 |
| - final String[] names = entry.getKey().split("_"); |
418 |
| - evalModels.computeIfAbsent(names[0], k -> new HashMap<>()).put(names[1], entry.getValue()); |
419 |
| - } |
420 |
| - return evalModels; |
421 |
| - } |
422 |
| - |
423 |
| - private static Set<String> getReplaceTargets(Map<String, Map<String, Object>> evalModels) { |
424 |
| - Set<String> ret = new HashSet<>(); |
425 |
| - for (final Entry<String, Map<String, Object>> entry : evalModels.entrySet()) { |
426 |
| - ret.addAll(entry.getValue().keySet()); |
427 |
| - } |
428 |
| - return ret; |
429 |
| - } |
430 |
| - |
431 |
| - /** |
432 |
| - * Get the function name of its get method according to the field name. |
433 |
| - * For example, the input parameter is "age", the output parameter is "getAge()" |
434 |
| - * |
435 |
| - * @param fieldName the file name. |
436 |
| - * @return the function name of its get method. |
437 |
| - */ |
438 |
| - private static String obtainFieldGetMethodName(String fieldName) { |
439 |
| - return new StringBuffer().append("get") |
440 |
| - .append(fieldName.substring(0, 1).toUpperCase()) |
441 |
| - .append(fieldName.substring(1)).append("()").toString(); |
| 377 | + return res; |
442 | 378 | }
|
443 | 379 | }
|
0 commit comments