jexl execute string expression

Related package download address: http://commons.apache.org/proper/commons-jexl

You also need to download a dependent package: commons-logging-1.2.jar is also downloaded from the apache official website

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.apache.commons.jexl3.JexlBuilder;
import org.apache.commons.jexl3.JexlContext;
import org.apache.commons.jexl3.JexlEngine;
import org.apache.commons.jexl3.JexlExpression;
import org.apache.commons.jexl3.MapContext;


public class JexlTest {
	
	public static void main(String[] args) {
		//1. Create jexl
		JexlEngine jexl = new JexlBuilder().create();
		
		// 2. Make a jexlContext and put it into it, the MapConext class clear method can clear the set value
		JexlContext jc = new MapContext();
		
		// 3. Set the value of the variable, this value can be called in the executed expression
		jc.set("i",3);
		jc.set("j",4);
		
		// 4. Create an expression that will be executed
		JexlExpression e = jexl.createExpression("i+j");
		// 5. Execute and return the result
		Object o = e.evaluate(jc);
		System.out.println(jc.get("i") + "+"+ jc.get("j") + " = " + o);
		
		// a regular implementation
		jc.set("str", "jacktan");
		jc.set("HDI_Board", "yes");
		e = jexl.createExpression("str=~\".*tan\"");
		o = e.evaluate (jc);
		System.out.println(o);
		
		// execute multiple conditions
		e=jexl.createExpression("str=~'.*tan' && str != 'jacktan'");
		System.out.println(e.evaluate(jc) + "  aaa");
		
		e=jexl.createExpression("HDI_Board=='yes'");
		System.out.println(e.evaluate(jc) + "  bbb");
		
		// execute object method
		JexlTest sjt = new JexlTest();
		jc.set("SimpleJexlTest",sjt);
		jc.set("UDA_NAME", "HDI_Board");
		jc.set("UDA_VALUE", "yes");
		
		e = jexl.createExpression("SimpleJexlTest.current(UDA_NAME,UDA_VALUE)");
		
		o = e.evaluate (jc);
		System.out.println(o);
		
		//List test
	        List<Object> l = new ArrayList<Object>();
	        l.add("Hello from location 0");
	        Integer two = new Integer(2);
	        l.add(two);
	        jc.set("array", l);
	        // elements of the list
	        e = jexl.createExpression("array[1]");
	        o = e.evaluate (jc);
	        System.out.println(o);
	        // length of list element
	        e = jexl.createExpression("array[0].length()");
	        o = e.evaluate (jc);
	        System.out.println(o);
	        //out.print("The length of the string at location 0 is : ", o, Integer.valueOf(21));
	}
	
	public static boolean current(String uda,String val) {
		System.out.println(uda + "  " + val);
		Map<String, String> map = new HashMap<String, String>();
		map.put("abc", "yes");
		map.put("HDI_Board", "no");
		map.put("customer_num", "9175350");
		for ( Entry<String, String> entry : map.entrySet() ) {
			if ( entry.getKey().equals(uda) && entry.getValue().matches(val) ) {
				return true;
			}
		}
		return false;
	}
}

result:

3+4 = 7
true
false  aaa
true  bbb
HDI_Board  yes
false
2
21

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326075001&siteId=291194637