简体   繁体   English

带数字键的MVEL表达式

[英]MVEL expression with Numeric keys

We are using MVEL to evaluate the expression by passing the map in the context object. 我们正在使用MVEL通过在上下文对象中传递映射来评估表达式。 The map contains SNMP trap information such as OID and its values. 该映射包含SNMP陷阱信息,例如OID及其值。 eg sample Map contains following Keys and values. 例如,示例图包含以下键和值。

    Map<String,String> trapMap = new HashMap();
    trapMap.put("1.3.6.1.4.1.9.9.311.1.1.2.1.3", "(7362915) 20:27:09.15");
    trapMap.put("1.3.6.1.4.1.9.9.311.1.1.2.1.2", "2.2");
    trapMap.put("1.3.6.1.4.1.9.9.311.1.1.2.1.19", "0");
    trapMap.put("1.3.6.1.4.1.9.9.311.1.1.2.1.16", "SIMPLE TRAP --Port Down due to Admin Status down");
    trapMap.put("errorStatus", "0");
    trapMap.put("IPAddress", "10.127.34.219");

When we evaluate the expression using MVEL.eval() it either fails or return False. 当我们使用MVEL.eval()评估表达式时,表达式将失败或返回False。 Following is the MVEL expression used and its result. 以下是所使用的MVEL表达式及其结果。

    System.out.println("----------########### = "+(MVEL.eval("1.3.6.1.4.1.9.9.311.1.1.2.1.19 == '0'", trapMap)));
    //Throws error as 
    //Exception in thread "main" [Error: invalid number literal: 1.3.6.1.4.1.9.9.311.1.1.2.1.19]
    //      [Near : {... 1.3.6.1.4.1.9.9.311.1.1.2.1.19 == '0 ....}]

    System.out.println("----------########### = "+(MVEL.eval("\"1.3.6.1.4.1.9.9.311.1.1.2.1.19\" == '0'", trapMap)));
    //Enclosed trap OID in double quotes and compared with String value then it returns false

    System.out.println("----------########### = "+(MVEL.eval("\"1.3.6.1.4.1.9.9.311.1.1.2.1.19\" == 0", trapMap)));
    //Enclosed trap OID in double quotes and compared with number then it returns false

Our Map will always contain such OID and values and we want to validate their values using MVEL. 我们的地图将始终包含此类OID和值,我们希望使用MVEL验证其值。 Based on this, we need to know 基于此,我们需要知道

  1. if the expression mentioned is a valid expression if not then what changes are required to make it work. 如果提到的表达式是有效的表达式(如果不是),则需要进行哪些更改才能使其起作用。
  2. Do we need to add any additional escape characters to the keys mentioned in expression OR 我们是否需要向表达式中提到的键添加任何其他转义字符,或者
  3. This is not possible as the key mentioned in the expression is not valid property/identifier. 这是不可能的,因为表达式中提到的键不是有效的属性/标识符。

DOT(.) will create a problem in above expression. DOT(.)将在上面的表达式中产生一个问题。 MVEL internally calls getter after each . MVEL在每次调用之后内部调用getter . property . property

we can replace . 我们可以代替. with _ operator. _运算符。 Also need to add _ in the starting. 开头也需要添加_

public static void main(String args[]) throws Exception {
        String s = "1.3.6.1.4.1.9.9.311.1.1.2.1.19 == 0";

        Map<String, String> trapMap = new HashMap();
        trapMap.put(convertDot("1.3.6.1.4.1.9.9.311.1.1.2.1.3"), "(7362915) 20:27:09.15");
        trapMap.put(convertDot("1.3.6.1.4.1.9.9.311.1.1.2.1.2"), "2.2");
        trapMap.put(convertDot("1.3.6.1.4.1.9.9.311.1.1.2.1.19"), "0");
        trapMap.put(convertDot("1.3.6.1.4.1.9.9.311.1.1.2.1.16"), "SIMPLE TRAP --Port Down due to Admin Status down");
        trapMap.put("errorStatus", "0");
        trapMap.put("IPAddress", "10.127.34.219");
        System.out.println(MVEL.eval(convertDot(s), trapMap));

    }

    public static String convertDot(String input) {
        input = "_" + input.replaceAll("\\.", "_");
        return input;
    }

output 产量

true

If you are using Java Map, you can implicitly call the get method. 如果使用Java Map,则可以隐式调用get方法。 The below code will evaluate the correct expression. 下面的代码将评估正确的表达式。

System.out.println("----------########### = "+(MVEL.eval("get(\"1.3.6.1.4.1.9.9.311.1.1.2.1.19\") == '0'", trapMap)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM