mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
Rhino 表达式执行引擎
This commit is contained in:
parent
9ca25e26d6
commit
bc896bf8ef
@ -410,6 +410,13 @@
|
|||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mozilla</groupId>
|
||||||
|
<artifactId>rhino</artifactId>
|
||||||
|
<version>1.7.12</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
<!-- 表达式引擎可选依赖 end -->
|
<!-- 表达式引擎可选依赖 end -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
|
@ -0,0 +1,41 @@
|
|||||||
|
package cn.hutool.extra.expression.engine.rhino;
|
||||||
|
|
||||||
|
import cn.hutool.extra.expression.ExpressionEngine;
|
||||||
|
import org.mozilla.javascript.Context;
|
||||||
|
import org.mozilla.javascript.Scriptable;
|
||||||
|
import org.mozilla.javascript.ScriptableObject;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* rhino引擎封装<br>
|
||||||
|
* 见:https://github.com/mozilla/rhino
|
||||||
|
*
|
||||||
|
* @since 5.5.2
|
||||||
|
* @author lzpeng
|
||||||
|
*/
|
||||||
|
public class RhinoEngine implements ExpressionEngine {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造
|
||||||
|
*/
|
||||||
|
public RhinoEngine(){
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object eval(String expression, Map<String, Object> context) {
|
||||||
|
Context ctx = Context.enter();
|
||||||
|
Scriptable scope = ctx.initStandardObjects();
|
||||||
|
if (context != null && !context.isEmpty()) {
|
||||||
|
for (Map.Entry<String, Object> entry : context.entrySet()) {
|
||||||
|
// 将java对象转为js对象
|
||||||
|
Object jsObj = Context.javaToJS(entry.getValue(), scope);
|
||||||
|
// 将java对象放置JS的作用域中
|
||||||
|
ScriptableObject.putProperty(scope, entry.getKey(), jsObj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Object result = ctx.evaluateString(scope, expression, "rhino.js", 1, null);
|
||||||
|
Context.exit();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
/**
|
||||||
|
* rhino引擎封装<br>
|
||||||
|
* 见:https://github.com/mozilla/rhino
|
||||||
|
*
|
||||||
|
* @author lzpeng
|
||||||
|
*/
|
||||||
|
package cn.hutool.extra.expression.engine.rhino;
|
@ -3,3 +3,4 @@ cn.hutool.extra.expression.engine.jexl.JexlEngine
|
|||||||
cn.hutool.extra.expression.engine.mvel.MvelEngine
|
cn.hutool.extra.expression.engine.mvel.MvelEngine
|
||||||
cn.hutool.extra.expression.engine.jfireel.JfireELEngine
|
cn.hutool.extra.expression.engine.jfireel.JfireELEngine
|
||||||
cn.hutool.extra.expression.engine.spel.SpELEngine
|
cn.hutool.extra.expression.engine.spel.SpELEngine
|
||||||
|
cn.hutool.extra.expression.engine.rhino.RhinoEngine
|
@ -4,6 +4,7 @@ import cn.hutool.core.lang.Dict;
|
|||||||
import cn.hutool.extra.expression.engine.jexl.JexlEngine;
|
import cn.hutool.extra.expression.engine.jexl.JexlEngine;
|
||||||
import cn.hutool.extra.expression.engine.jfireel.JfireELEngine;
|
import cn.hutool.extra.expression.engine.jfireel.JfireELEngine;
|
||||||
import cn.hutool.extra.expression.engine.mvel.MvelEngine;
|
import cn.hutool.extra.expression.engine.mvel.MvelEngine;
|
||||||
|
import cn.hutool.extra.expression.engine.rhino.RhinoEngine;
|
||||||
import cn.hutool.extra.expression.engine.spel.SpELEngine;
|
import cn.hutool.extra.expression.engine.spel.SpELEngine;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -67,4 +68,17 @@ public class ExpressionUtilTest {
|
|||||||
final Object eval = engine.eval("#a-(#b-#c)", dict);
|
final Object eval = engine.eval("#a-(#b-#c)", dict);
|
||||||
Assert.assertEquals(-143.8, (double)eval, 2);
|
Assert.assertEquals(-143.8, (double)eval, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void rhinoTest(){
|
||||||
|
ExpressionEngine engine = new RhinoEngine();
|
||||||
|
|
||||||
|
final Dict dict = Dict.create()
|
||||||
|
.set("a", 100.3)
|
||||||
|
.set("b", 45)
|
||||||
|
.set("c", -199.100);
|
||||||
|
final Object eval = engine.eval("a-(b-c)", dict);
|
||||||
|
Assert.assertEquals(-143.8, (double)eval, 2);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user