JAVA論理文字列判定真偽
3518 ワード
package com.chinahrt.zyn.iteye;
import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import ognl.Ognl;
import ognl.OgnlException;
public class ScriptTest {
/**
* @param args
* Administrator
* 2013-6-19 5:26:06
* @throws ScriptException
* @throws OgnlException
*/
public static void main(String[] args) throws ScriptException, OgnlException {
// , javascript , java
ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");
Compilable compilable = (Compilable) engine;
Bindings bindings = engine.createBindings(); //Local Binding
String script = "true&true||(true||false&(true||false))"; //
CompiledScript JSFunction = compilable.compile(script); //
Object result = JSFunction.eval(bindings);
System.out.println(result); // ,Bindings
// Ognl ( ognl.jar)
Object re = Ognl.getValue("true&&true||(true||false&&(true||false))",null);
System.out.println(re);
}
}