0

Intro

Hi, I'm currently working on an import framework. The user will be using the framework to map data from a bulk of excel sheets to entities in the system. Whether some values need to be mapped to entities is dependent upon the condition of certain values in the files. It is a web application which is build in Java (Java servlets).

To the point

The user of the system needs to supply the system with a custom condition like:

CELLD4 == "Hello world"

My question is: How can I give the CELLD4 == "Hello world" condition as a String to the system and let the system interpret it as an if statement, like:

if (CELLD4 == "Hello world") {execute something}

What someone else has done is extract the two values from before the double equals sign (==) and after the double equals sign and then execute an equals check to check whether the values are identical. But there seems to be a better and simpler way to go about this. Who has suggestions for me on how to best have the system interpret the custom condition input from the user of the system?

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Ben
  • 6,107
  • 6
  • 29
  • 40
  • I think the solution mentioned IS the best way as CELLD4 has to be evaluated. – user4759923 Apr 13 '16 at 16:13
  • 1
    If I understand correctly you just need to convert a `String` into code. Maybe you can check [here](http://stackoverflow.com/questions/935175/convert-string-to-code). – can lekili Apr 13 '16 at 16:24

1 Answers1

0

If I understood correctly, you can achieve it using ScriptEngine, i.e.:

public static void main(String[] argv) throws Exception {
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("JavaScript");
    String expression2 = "CELLD4.equalsIgnoreCase(\"Hello world\")";

    Map<String, Object> map = new HashMap<>();
    map.put("CELLD4", getCELLD4());

    engine.setBindings(new SimpleBindings(map), ScriptContext.ENGINE_SCOPE);
    if ((boolean) engine.eval(expression2)) {
        System.out.println("YAY");
    } else {
        System.out.println("NAY");
    }
}

public static String getCELLD4() {
    return "Hello World";
}

Output will be YAY.

Maybe there are better solutions since this requires some whacky stuff. And of course you still have to somehow resolve the variable passed inside the expression and bind it to something.

dambros
  • 4,252
  • 1
  • 23
  • 39
  • What is the getEngineByName() for? It calls a javascript engine? Does that mean it interprets the input string as javascript syntax? – Ben Apr 14 '16 at 08:27
  • It runs JavaScript code directly on the JVM, and yes it **can** interpret the input as js syntax. The thing I am not sure about is the precedence order if you use a function that exists both in Java and JS. In the example given I used `equalsIgnoreCase()` which doesn't exist in JS and it used the Java counterpart. If you do the same for an exclusive JS func, the same will happen, but when they exist in both languages I am not sure. Since it seems that you want String comparisson, this should not be a problem though. – dambros Apr 14 '16 at 09:01