繁体   English   中英

带数字键的MVEL表达式

[英]MVEL expression with Numeric keys

我们正在使用MVEL通过在上下文对象中传递映射来评估表达式。 该映射包含SNMP陷阱信息,例如OID及其值。 例如,示例图包含以下键和值。

    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");

当我们使用MVEL.eval()评估表达式时,表达式将失败或返回False。 以下是所使用的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

我们的地图将始终包含此类OID和值,我们希望使用MVEL验证其值。 基于此,我们需要知道

  1. 如果提到的表达式是有效的表达式(如果不是),则需要进行哪些更改才能使其起作用。
  2. 我们是否需要向表达式中提到的键添加任何其他转义字符,或者
  3. 这是不可能的,因为表达式中提到的键不是有效的属性/标识符。

DOT(.)将在上面的表达式中产生一个问题。 MVEL在每次调用之后内部调用getter . property

我们可以代替. _运算符。 开头也需要添加_

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;
    }

产量

true

如果使用Java Map,则可以隐式调用get方法。 下面的代码将评估正确的表达式。

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