mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-24 18:04:54 +08:00
add ognl support
This commit is contained in:
parent
a3ae323d3a
commit
708a7ec334
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (c) 2023 looly(loolly@aliyun.com)
|
||||
* Hutool is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.extra.expression.engine.ognl;
|
||||
|
||||
import ognl.Ognl;
|
||||
import ognl.OgnlException;
|
||||
import org.dromara.hutool.extra.expression.Expression;
|
||||
import org.dromara.hutool.extra.expression.ExpressionEngine;
|
||||
import org.dromara.hutool.extra.expression.ExpressionException;
|
||||
|
||||
/**
|
||||
* OGNL(Object-Graph Navigation Language)表达式引擎封装<br>
|
||||
* 见:https://github.com/orphan-oss/ognl
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
public class OgnlEngine implements ExpressionEngine {
|
||||
@Override
|
||||
public Expression compile(final String expression) {
|
||||
try {
|
||||
return new OgnlExpression(Ognl.parseExpression(expression));
|
||||
} catch (final OgnlException e) {
|
||||
throw new ExpressionException(e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (c) 2023 looly(loolly@aliyun.com)
|
||||
* Hutool is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.extra.expression.engine.ognl;
|
||||
|
||||
import ognl.Ognl;
|
||||
import ognl.OgnlContext;
|
||||
import ognl.OgnlException;
|
||||
import org.dromara.hutool.core.func.SimpleWrapper;
|
||||
import org.dromara.hutool.extra.expression.Expression;
|
||||
import org.dromara.hutool.extra.expression.ExpressionException;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* OGNL表达式包装
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
public class OgnlExpression extends SimpleWrapper<Object> implements Expression {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param expression 表达式对象
|
||||
*/
|
||||
public OgnlExpression(final Object expression) {
|
||||
super(expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object eval(final Map<String, Object> context) {
|
||||
final OgnlContext ognlContext = new OgnlContext(context);
|
||||
try {
|
||||
return Ognl.getValue(this.raw, ognlContext, ognlContext.getRoot());
|
||||
} catch (final OgnlException e) {
|
||||
throw new ExpressionException(e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
/*
|
||||
* Copyright (c) 2023 looly(loolly@aliyun.com)
|
||||
* Hutool is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* OGNL(Object-Graph Navigation Language)表达式引擎封装<br>
|
||||
* 见:https://github.com/orphan-oss/ognl
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
package org.dromara.hutool.extra.expression.engine.ognl;
|
@ -17,3 +17,4 @@ org.dromara.hutool.extra.expression.engine.jfireel.JfireELEngine
|
||||
org.dromara.hutool.extra.expression.engine.spel.SpELEngine
|
||||
org.dromara.hutool.extra.expression.engine.rhino.RhinoEngine
|
||||
org.dromara.hutool.extra.expression.engine.qlexpress.QLExpressEngine
|
||||
org.dromara.hutool.extra.expression.engine.ognl.OgnlEngine
|
||||
|
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (c) 2023 looly(loolly@aliyun.com)
|
||||
* Hutool is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.extra.expression;
|
||||
|
||||
import org.dromara.hutool.core.map.Dict;
|
||||
import org.dromara.hutool.extra.expression.engine.ognl.OgnlEngine;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class OgnlTest {
|
||||
|
||||
@Test
|
||||
void evalTest() {
|
||||
final ExpressionEngine engine = new OgnlEngine();
|
||||
final Dict dict = Dict.of()
|
||||
.set("a", 100.3)
|
||||
.set("b", 45)
|
||||
.set("c", -199.100);
|
||||
final Object eval = engine.eval("#a-(#b-#c)", dict);
|
||||
Assertions.assertEquals(-143.8, (double)eval, 0);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user