mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
remove expression
This commit is contained in:
parent
32e8caccdd
commit
f60e426e34
@ -89,10 +89,6 @@
|
|||||||
<artifactId>commons-lang3</artifactId>
|
<artifactId>commons-lang3</artifactId>
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
</exclusion>
|
</exclusion>
|
||||||
<exclusion>
|
|
||||||
<artifactId>mvel2</artifactId>
|
|
||||||
<groupId>org.mvel</groupId>
|
|
||||||
</exclusion>
|
|
||||||
<exclusion>
|
<exclusion>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
@ -488,68 +484,6 @@
|
|||||||
</exclusion>
|
</exclusion>
|
||||||
</exclusions>
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- 表达式引擎可选依赖 begin -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.googlecode.aviator</groupId>
|
|
||||||
<artifactId>aviator</artifactId>
|
|
||||||
<version>5.3.3</version>
|
|
||||||
<scope>compile</scope>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
<!-- 手动引入aviator的关联依赖,解决版本问题 -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.commons</groupId>
|
|
||||||
<artifactId>commons-jexl3</artifactId>
|
|
||||||
<version>3.3</version>
|
|
||||||
<scope>compile</scope>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.mvel</groupId>
|
|
||||||
<artifactId>mvel2</artifactId>
|
|
||||||
<version>2.4.14.Final</version>
|
|
||||||
<scope>compile</scope>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.jfirer</groupId>
|
|
||||||
<artifactId>jfireEl</artifactId>
|
|
||||||
<version>1.0</version>
|
|
||||||
<scope>compile</scope>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-expression</artifactId>
|
|
||||||
<version>5.3.27</version>
|
|
||||||
<scope>compile</scope>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.mozilla</groupId>
|
|
||||||
<artifactId>rhino</artifactId>
|
|
||||||
<version>1.7.14</version>
|
|
||||||
<scope>compile</scope>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.alibaba</groupId>
|
|
||||||
<artifactId>QLExpress</artifactId>
|
|
||||||
<version>3.3.1</version>
|
|
||||||
<scope>compile</scope>
|
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<artifactId>commons-logging</artifactId>
|
|
||||||
<groupId>commons-logging</groupId>
|
|
||||||
</exclusion>
|
|
||||||
<exclusion>
|
|
||||||
<artifactId>commons-beanutils</artifactId>
|
|
||||||
<groupId>commons-beanutils</groupId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
<!-- 表达式引擎可选依赖 end -->
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
<artifactId>commons-compress</artifactId>
|
<artifactId>commons-compress</artifactId>
|
||||||
|
@ -1,32 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 表达式定义,用于表示编译后的表达式。
|
|
||||||
*
|
|
||||||
* @author looly
|
|
||||||
* @since 6.0.0
|
|
||||||
*/
|
|
||||||
public interface Expression {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 执行表达式
|
|
||||||
*
|
|
||||||
* @param context 表达式上下文,用于存储表达式中所需的变量值等
|
|
||||||
* @return 执行结果
|
|
||||||
*/
|
|
||||||
Object eval(final Map<String, Object> context);
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 表达式引擎API接口,通过实现此接口,完成表达式的解析和执行
|
|
||||||
*
|
|
||||||
* @author looll, independenter
|
|
||||||
* @since 5.5.0
|
|
||||||
*/
|
|
||||||
public interface ExpressionEngine {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 编译表达式
|
|
||||||
*
|
|
||||||
* @param expression 表达式
|
|
||||||
* @return {@link Expression}
|
|
||||||
*/
|
|
||||||
Expression compile(String expression);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 执行表达式
|
|
||||||
*
|
|
||||||
* @param expression 表达式
|
|
||||||
* @param context 表达式上下文,用于存储表达式中所需的变量值等
|
|
||||||
* @return 执行结果
|
|
||||||
*/
|
|
||||||
default Object eval(final String expression, final Map<String, Object> context){
|
|
||||||
return compile(expression).eval(context);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.exceptions.ExceptionUtil;
|
|
||||||
import org.dromara.hutool.core.text.StrUtil;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 表达式语言异常
|
|
||||||
*
|
|
||||||
* @author Looly
|
|
||||||
*/
|
|
||||||
public class ExpressionException extends RuntimeException {
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
|
|
||||||
public ExpressionException(final Throwable e) {
|
|
||||||
super(ExceptionUtil.getMessage(e), e);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExpressionException(final String message) {
|
|
||||||
super(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExpressionException(final String messageTemplate, final Object... params) {
|
|
||||||
super(StrUtil.format(messageTemplate, params));
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExpressionException(final String message, final Throwable throwable) {
|
|
||||||
super(message, throwable);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExpressionException(final String message, final Throwable throwable, final boolean enableSuppression, final boolean writableStackTrace) {
|
|
||||||
super(message, throwable, enableSuppression, writableStackTrace);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExpressionException(final Throwable throwable, final String messageTemplate, final Object... params) {
|
|
||||||
super(StrUtil.format(messageTemplate, params), throwable);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,47 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.extra.expression.engine.ExpressionFactory;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 表达式引擎工具类<br>
|
|
||||||
* 需要注意的是,考虑到表达式引擎执行安全性,请自行检查表达式是否可靠。
|
|
||||||
*
|
|
||||||
* @author looly
|
|
||||||
* @since 5.5.0
|
|
||||||
*/
|
|
||||||
public class ExpressionUtil {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获得全局单例的表达式引擎
|
|
||||||
*
|
|
||||||
* @return 全局单例的表达式引擎
|
|
||||||
*/
|
|
||||||
public static ExpressionEngine getEngine() {
|
|
||||||
return ExpressionFactory.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 执行表达式
|
|
||||||
*
|
|
||||||
* @param expression 表达式
|
|
||||||
* @param context 表达式上下文,用于存储表达式中所需的变量值等
|
|
||||||
* @return 执行结果
|
|
||||||
*/
|
|
||||||
public static Object eval(final String expression, final Map<String, Object> context) {
|
|
||||||
return getEngine().eval(expression, context);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,65 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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;
|
|
||||||
|
|
||||||
import org.dromara.hutool.core.lang.Singleton;
|
|
||||||
import org.dromara.hutool.core.spi.SpiUtil;
|
|
||||||
import org.dromara.hutool.core.text.StrUtil;
|
|
||||||
import org.dromara.hutool.extra.expression.ExpressionEngine;
|
|
||||||
import org.dromara.hutool.extra.expression.ExpressionException;
|
|
||||||
import org.dromara.hutool.log.StaticLog;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 表达式语言引擎工厂类,用于根据用户引入的表达式jar,自动创建对应的引擎对象
|
|
||||||
*
|
|
||||||
* @since 5.5.0
|
|
||||||
* @author looly
|
|
||||||
*/
|
|
||||||
public class ExpressionFactory {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获得单例的{@link ExpressionEngine}
|
|
||||||
*
|
|
||||||
* @return 单例的{@link ExpressionEngine}
|
|
||||||
*/
|
|
||||||
public static ExpressionEngine get(){
|
|
||||||
return Singleton.get(ExpressionEngine.class.getName(), ExpressionFactory::of);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据用户引入的表达式引擎jar,自动创建对应的拼音引擎对象<br>
|
|
||||||
* 推荐创建的引擎单例使用,此方法每次调用会返回新的引擎
|
|
||||||
*
|
|
||||||
* @return {@link ExpressionEngine}
|
|
||||||
*/
|
|
||||||
public static ExpressionEngine of() {
|
|
||||||
final ExpressionEngine engine = doCreate();
|
|
||||||
StaticLog.debug("Use [{}] Engine As Default.", StrUtil.removeSuffix(engine.getClass().getSimpleName(), "Engine"));
|
|
||||||
return engine;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据用户引入的拼音引擎jar,自动创建对应的拼音引擎对象<br>
|
|
||||||
* 推荐创建的引擎单例使用,此方法每次调用会返回新的引擎
|
|
||||||
*
|
|
||||||
* @return {@link ExpressionEngine}
|
|
||||||
*/
|
|
||||||
private static ExpressionEngine doCreate() {
|
|
||||||
final ExpressionEngine engine = SpiUtil.loadFirstAvailable(ExpressionEngine.class);
|
|
||||||
if(null != engine){
|
|
||||||
return engine;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new ExpressionException("No expression jar found !Please add one of it to your project !");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.aviator;
|
|
||||||
|
|
||||||
import com.googlecode.aviator.AviatorEvaluator;
|
|
||||||
import com.googlecode.aviator.AviatorEvaluatorInstance;
|
|
||||||
import org.dromara.hutool.core.func.SimpleWrapper;
|
|
||||||
import org.dromara.hutool.extra.expression.Expression;
|
|
||||||
import org.dromara.hutool.extra.expression.ExpressionEngine;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Aviator引擎封装<br>
|
|
||||||
* 见:<a href="https://github.com/killme2008/aviatorscript">https://github.com/killme2008/aviatorscript</a>
|
|
||||||
*
|
|
||||||
* @author looly
|
|
||||||
*/
|
|
||||||
public class AviatorEngine extends SimpleWrapper<AviatorEvaluatorInstance>
|
|
||||||
implements ExpressionEngine {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构造
|
|
||||||
*/
|
|
||||||
public AviatorEngine() {
|
|
||||||
super(AviatorEvaluator.getInstance());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Expression compile(final String expression) {
|
|
||||||
return new AviatorExpression(this.raw.compile(expression));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.aviator;
|
|
||||||
|
|
||||||
import org.dromara.hutool.core.func.SimpleWrapper;
|
|
||||||
import org.dromara.hutool.extra.expression.Expression;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link com.googlecode.aviator.Expression} 包装
|
|
||||||
*
|
|
||||||
* @author looly
|
|
||||||
* @since 6.0.0
|
|
||||||
*/
|
|
||||||
public class AviatorExpression extends SimpleWrapper<com.googlecode.aviator.Expression> implements Expression {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构造
|
|
||||||
*
|
|
||||||
* @param expression {@link com.googlecode.aviator.Expression}
|
|
||||||
*/
|
|
||||||
public AviatorExpression(final com.googlecode.aviator.Expression expression) {
|
|
||||||
super(expression);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object eval(final Map<String, Object> context) {
|
|
||||||
return this.raw.execute(context);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Aviator引擎封装,见:https://github.com/killme2008/aviatorscript
|
|
||||||
*
|
|
||||||
* @author looly
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
package org.dromara.hutool.extra.expression.engine.aviator;
|
|
@ -1,53 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.jexl;
|
|
||||||
|
|
||||||
import org.apache.commons.jexl3.JexlBuilder;
|
|
||||||
import org.dromara.hutool.core.func.SimpleWrapper;
|
|
||||||
import org.dromara.hutool.extra.expression.Expression;
|
|
||||||
import org.dromara.hutool.extra.expression.ExpressionEngine;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Jexl3引擎封装<br>
|
|
||||||
* 见:https://github.com/apache/commons-jexl
|
|
||||||
*
|
|
||||||
* @since 5.5.0
|
|
||||||
* @author looly
|
|
||||||
*/
|
|
||||||
public class JexlEngine extends SimpleWrapper<org.apache.commons.jexl3.JexlEngine>
|
|
||||||
implements ExpressionEngine {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构造
|
|
||||||
*/
|
|
||||||
public JexlEngine(){
|
|
||||||
super(
|
|
||||||
(new JexlBuilder())
|
|
||||||
.cache(512)
|
|
||||||
.strict(true)
|
|
||||||
.silent(false)
|
|
||||||
.create()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Expression compile(final String expression) {
|
|
||||||
try{
|
|
||||||
return new JexlExpression(this.raw.createExpression(expression));
|
|
||||||
} catch (final Exception ignore){
|
|
||||||
// https://gitee.com/dromara/hutool/issues/I4B70D
|
|
||||||
// 支持脚本
|
|
||||||
return new JexlScript(this.raw.createScript(expression));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.jexl;
|
|
||||||
|
|
||||||
import org.apache.commons.jexl3.MapContext;
|
|
||||||
import org.dromara.hutool.core.func.SimpleWrapper;
|
|
||||||
import org.dromara.hutool.extra.expression.Expression;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Jexl3引擎的{@link org.apache.commons.jexl3.JexlExpression}包装
|
|
||||||
*
|
|
||||||
* @author looly
|
|
||||||
*/
|
|
||||||
public class JexlExpression extends SimpleWrapper<org.apache.commons.jexl3.JexlExpression>
|
|
||||||
implements Expression {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构造
|
|
||||||
*
|
|
||||||
* @param raw {@link org.apache.commons.jexl3.JexlExpression}
|
|
||||||
*/
|
|
||||||
public JexlExpression(final org.apache.commons.jexl3.JexlExpression raw) {
|
|
||||||
super(raw);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object eval(final Map<String, Object> context) {
|
|
||||||
final MapContext mapContext = new MapContext(context);
|
|
||||||
return raw.evaluate(mapContext);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.jexl;
|
|
||||||
|
|
||||||
import org.apache.commons.jexl3.MapContext;
|
|
||||||
import org.dromara.hutool.core.func.Wrapper;
|
|
||||||
import org.dromara.hutool.extra.expression.Expression;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Jexl3引擎的{@link org.apache.commons.jexl3.JexlScript}包装
|
|
||||||
*
|
|
||||||
* @author looly
|
|
||||||
*/
|
|
||||||
public class JexlScript implements Expression, Wrapper<org.apache.commons.jexl3.JexlScript> {
|
|
||||||
|
|
||||||
private final org.apache.commons.jexl3.JexlScript raw;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构造
|
|
||||||
*
|
|
||||||
* @param raw {@link org.apache.commons.jexl3.JexlScript}
|
|
||||||
*/
|
|
||||||
public JexlScript(final org.apache.commons.jexl3.JexlScript raw) {
|
|
||||||
this.raw = raw;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object eval(final Map<String, Object> context) {
|
|
||||||
final MapContext mapContext = new MapContext(context);
|
|
||||||
return raw.execute(mapContext);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public org.apache.commons.jexl3.JexlScript getRaw() {
|
|
||||||
return raw;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Jexl3引擎封装,见:https://github.com/apache/commons-jexl
|
|
||||||
*
|
|
||||||
* @author looly
|
|
||||||
*/
|
|
||||||
package org.dromara.hutool.extra.expression.engine.jexl;
|
|
@ -1,32 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.jfireel;
|
|
||||||
|
|
||||||
import org.dromara.hutool.extra.expression.Expression;
|
|
||||||
import org.dromara.hutool.extra.expression.ExpressionEngine;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* JfireEL引擎封装<br>
|
|
||||||
* 见:https://gitee.com/eric_ds/jfireEL
|
|
||||||
*
|
|
||||||
* @since 5.5.0
|
|
||||||
* @author looly
|
|
||||||
*/
|
|
||||||
public class JfireELEngine implements ExpressionEngine {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Expression compile(final String expression) {
|
|
||||||
return new JfireELExpression(
|
|
||||||
com.jfirer.jfireel.expression.Expression.parse(expression));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,40 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.jfireel;
|
|
||||||
|
|
||||||
import org.dromara.hutool.core.func.SimpleWrapper;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* JfireEL引擎表达式{@link com.jfirer.jfireel.expression.Expression} 封装
|
|
||||||
*
|
|
||||||
* @author looly
|
|
||||||
*/
|
|
||||||
public class JfireELExpression extends SimpleWrapper<com.jfirer.jfireel.expression.Expression>
|
|
||||||
implements org.dromara.hutool.extra.expression.Expression {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构造
|
|
||||||
*
|
|
||||||
* @param expression {@link com.jfirer.jfireel.expression.Expression}
|
|
||||||
*/
|
|
||||||
public JfireELExpression(final com.jfirer.jfireel.expression.Expression expression) {
|
|
||||||
super(expression);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object eval(final Map<String, Object> context) {
|
|
||||||
return this.raw.calculate(context);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* JfireEL引擎封装<br>
|
|
||||||
* 见:https://gitee.com/eric_ds/jfireEL
|
|
||||||
*
|
|
||||||
* @author looly
|
|
||||||
*/
|
|
||||||
package org.dromara.hutool.extra.expression.engine.jfireel;
|
|
@ -1,37 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.mvel;
|
|
||||||
|
|
||||||
import org.dromara.hutool.extra.expression.Expression;
|
|
||||||
import org.dromara.hutool.extra.expression.ExpressionEngine;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* MVEL (MVFLEX Expression Language)引擎封装<br>
|
|
||||||
* 见:https://github.com/mvel/mvel
|
|
||||||
*
|
|
||||||
* @since 5.5.0
|
|
||||||
* @author looly
|
|
||||||
*/
|
|
||||||
public class MvelEngine implements ExpressionEngine {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构造
|
|
||||||
*/
|
|
||||||
public MvelEngine(){
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Expression compile(final String expression) {
|
|
||||||
return new MvelExpression(expression);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.mvel;
|
|
||||||
|
|
||||||
import org.dromara.hutool.core.func.SimpleWrapper;
|
|
||||||
import org.dromara.hutool.extra.expression.Expression;
|
|
||||||
import org.mvel2.MVEL;
|
|
||||||
import org.mvel2.templates.TemplateCompiler;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* MVEL2的{@link TemplateCompiler}包装
|
|
||||||
*
|
|
||||||
* @author looly
|
|
||||||
*/
|
|
||||||
public class MvelExpression extends SimpleWrapper<String>
|
|
||||||
implements Expression {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构造
|
|
||||||
*
|
|
||||||
* @param expression 表达式字符串
|
|
||||||
*/
|
|
||||||
public MvelExpression(final String expression) {
|
|
||||||
super(expression);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object eval(final Map<String, Object> context) {
|
|
||||||
return MVEL.eval(this.raw, context);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* MVEL (MVFLEX Expression Language)引擎封装<br>
|
|
||||||
* 见:https://github.com/mvel/mvel
|
|
||||||
*
|
|
||||||
* @author looly
|
|
||||||
*/
|
|
||||||
package org.dromara.hutool.extra.expression.engine.mvel;
|
|
@ -1,36 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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;
|
|
@ -1,19 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 表达式语言引擎封装
|
|
||||||
*
|
|
||||||
* @author looly
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
package org.dromara.hutool.extra.expression.engine;
|
|
@ -1,42 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.qlexpress;
|
|
||||||
|
|
||||||
import com.ql.util.express.ExpressRunner;
|
|
||||||
import org.dromara.hutool.extra.expression.Expression;
|
|
||||||
import org.dromara.hutool.extra.expression.ExpressionEngine;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* QLExpress引擎封装<br>
|
|
||||||
* 见:https://github.com/alibaba/QLExpress
|
|
||||||
*
|
|
||||||
* @author looly
|
|
||||||
* @since 5.8.9
|
|
||||||
*/
|
|
||||||
public class QLExpressEngine implements ExpressionEngine {
|
|
||||||
|
|
||||||
private final ExpressRunner engine;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构造
|
|
||||||
*/
|
|
||||||
public QLExpressEngine() {
|
|
||||||
engine = new ExpressRunner();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Expression compile(final String expression) {
|
|
||||||
return new QLExpressExpression(this.engine, expression);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,56 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.qlexpress;
|
|
||||||
|
|
||||||
import com.ql.util.express.DefaultContext;
|
|
||||||
import com.ql.util.express.ExpressRunner;
|
|
||||||
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;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* QLExpress引擎表达式封装<br>
|
|
||||||
* 由于QLExpress引擎使用字符串缓存方式存储表达式树,故此处无需存储表达式对象
|
|
||||||
*
|
|
||||||
* @author looly
|
|
||||||
*/
|
|
||||||
public class QLExpressExpression extends SimpleWrapper<String>
|
|
||||||
implements Expression {
|
|
||||||
|
|
||||||
private final ExpressRunner engine;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构造
|
|
||||||
*
|
|
||||||
* @param engine {@link ExpressRunner}
|
|
||||||
* @param expression 表达式字符串
|
|
||||||
*/
|
|
||||||
public QLExpressExpression(final ExpressRunner engine, final String expression) {
|
|
||||||
super(expression);
|
|
||||||
this.engine = engine;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object eval(final Map<String, Object> context) {
|
|
||||||
final DefaultContext<String, Object> defaultContext = new DefaultContext<>();
|
|
||||||
defaultContext.putAll(context);
|
|
||||||
|
|
||||||
try {
|
|
||||||
return engine.execute(this.raw, defaultContext, null, true, false);
|
|
||||||
} catch (final Exception e) {
|
|
||||||
throw new ExpressionException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* QLExpress引擎封装<br>
|
|
||||||
* 见:https://github.com/alibaba/QLExpress
|
|
||||||
*
|
|
||||||
* @author looly
|
|
||||||
*/
|
|
||||||
package org.dromara.hutool.extra.expression.engine.qlexpress;
|
|
@ -1,31 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.rhino;
|
|
||||||
|
|
||||||
import org.dromara.hutool.extra.expression.Expression;
|
|
||||||
import org.dromara.hutool.extra.expression.ExpressionEngine;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* rhino引擎封装<br>
|
|
||||||
* 见:https://github.com/mozilla/rhino
|
|
||||||
*
|
|
||||||
* @author lzpeng
|
|
||||||
* @since 5.5.2
|
|
||||||
*/
|
|
||||||
public class RhinoEngine implements ExpressionEngine {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Expression compile(final String expression) {
|
|
||||||
return new RhinoExpression(expression);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,57 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.rhino;
|
|
||||||
|
|
||||||
import org.dromara.hutool.core.func.SimpleWrapper;
|
|
||||||
import org.dromara.hutool.core.map.MapUtil;
|
|
||||||
import org.dromara.hutool.extra.expression.Expression;
|
|
||||||
import org.mozilla.javascript.Context;
|
|
||||||
import org.mozilla.javascript.Scriptable;
|
|
||||||
import org.mozilla.javascript.ScriptableObject;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* rhino引擎表达式包装
|
|
||||||
*
|
|
||||||
* @author looly
|
|
||||||
*/
|
|
||||||
public class RhinoExpression extends SimpleWrapper<String>
|
|
||||||
implements Expression {
|
|
||||||
|
|
||||||
private static final String SOURCE_RHINO_JS = "rhino.js";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构造
|
|
||||||
*
|
|
||||||
* @param expression 表达式
|
|
||||||
*/
|
|
||||||
public RhinoExpression(final String expression) {
|
|
||||||
super(expression);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object eval(final Map<String, Object> context) {
|
|
||||||
try (final Context ctx = Context.enter()) {
|
|
||||||
final Scriptable scope = ctx.initStandardObjects();
|
|
||||||
if (MapUtil.isNotEmpty(context)) {
|
|
||||||
context.forEach((key, value) -> {
|
|
||||||
// 将java对象转为js对象后放置于JS的作用域中
|
|
||||||
ScriptableObject.putProperty(scope, key, Context.javaToJS(value, scope));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return ctx.evaluateString(scope, this.raw,
|
|
||||||
SOURCE_RHINO_JS, 1, null);
|
|
||||||
} // auto close
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* rhino引擎封装<br>
|
|
||||||
* 见:https://github.com/mozilla/rhino
|
|
||||||
*
|
|
||||||
* @author lzpeng
|
|
||||||
*/
|
|
||||||
package org.dromara.hutool.extra.expression.engine.rhino;
|
|
@ -1,41 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.spel;
|
|
||||||
|
|
||||||
import org.dromara.hutool.core.func.SimpleWrapper;
|
|
||||||
import org.dromara.hutool.extra.expression.Expression;
|
|
||||||
import org.dromara.hutool.extra.expression.ExpressionEngine;
|
|
||||||
import org.springframework.expression.ExpressionParser;
|
|
||||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Spring-Expression引擎封装<br>
|
|
||||||
* 见:https://github.com/spring-projects/spring-framework/tree/master/spring-expression
|
|
||||||
*
|
|
||||||
* @since 5.5.0
|
|
||||||
* @author looly
|
|
||||||
*/
|
|
||||||
public class SpELEngine extends SimpleWrapper<ExpressionParser> implements ExpressionEngine {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构造
|
|
||||||
*/
|
|
||||||
public SpELEngine(){
|
|
||||||
super(new SpelExpressionParser());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Expression compile(final String expression) {
|
|
||||||
return new SpELExpression(this.raw.parseExpression(expression));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,46 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.spel;
|
|
||||||
|
|
||||||
import org.dromara.hutool.core.func.SimpleWrapper;
|
|
||||||
import org.dromara.hutool.extra.expression.Expression;
|
|
||||||
import org.springframework.expression.EvaluationContext;
|
|
||||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Spring-Expression引擎表达式{@link org.springframework.expression.Expression} 包装
|
|
||||||
*
|
|
||||||
* @author looly
|
|
||||||
*/
|
|
||||||
public class SpELExpression extends SimpleWrapper<org.springframework.expression.Expression>
|
|
||||||
implements Expression {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构造
|
|
||||||
*
|
|
||||||
* @param expression {@link org.springframework.expression.Expression}
|
|
||||||
*/
|
|
||||||
public SpELExpression(final org.springframework.expression.Expression expression) {
|
|
||||||
super(expression);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object eval(final Map<String, Object> context) {
|
|
||||||
final EvaluationContext evaluationContext = new StandardEvaluationContext();
|
|
||||||
context.forEach(evaluationContext::setVariable);
|
|
||||||
|
|
||||||
return this.raw.getValue(evaluationContext);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Spring-Expression引擎封装<br>
|
|
||||||
* 见:https://github.com/spring-projects/spring-framework/tree/master/spring-expression
|
|
||||||
*
|
|
||||||
* @author looly
|
|
||||||
*/
|
|
||||||
package org.dromara.hutool.extra.expression.engine.spel;
|
|
@ -1,19 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 表达式语言引擎封装
|
|
||||||
*
|
|
||||||
* @author looly
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
package org.dromara.hutool.extra.expression;
|
|
@ -1,62 +0,0 @@
|
|||||||
package org.dromara.hutool.extra.expression;
|
|
||||||
|
|
||||||
import org.dromara.hutool.core.date.DateUtil;
|
|
||||||
import org.dromara.hutool.core.lang.Console;
|
|
||||||
import org.dromara.hutool.core.map.Dict;
|
|
||||||
import org.dromara.hutool.extra.expression.engine.aviator.AviatorEngine;
|
|
||||||
import lombok.Data;
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Aviator引擎单元测试,来自https://github.com/looly/hutool/pull/1203
|
|
||||||
*/
|
|
||||||
public class AviatorTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void simpleTest(){
|
|
||||||
final Foo foo = new Foo(100, 3.14f, DateUtil.parse("2020-11-12"));
|
|
||||||
final ExpressionEngine engine = new AviatorEngine();
|
|
||||||
String exp =
|
|
||||||
"\"[foo i=\"+ foo.i + \", f=\" + foo.f + \", date.year=\" + (foo.date.year+1900) + \", date.month=\" + foo.date.month + \", bars[0].name=\" + #foo.bars[0].name + \"]\"";
|
|
||||||
String result = (String) engine.eval(exp, Dict.of().set("foo", foo));
|
|
||||||
Assertions.assertEquals("[foo i=100, f=3.14, date.year=2020, date.month=10, bars[0].name=bar]", result);
|
|
||||||
|
|
||||||
// Assignment.
|
|
||||||
exp = "#foo.bars[0].name='hello aviator' ; #foo.bars[0].name";
|
|
||||||
result = (String) engine.eval(exp, Dict.of().set("foo", foo));
|
|
||||||
Assertions.assertEquals("hello aviator", result);
|
|
||||||
Assertions.assertEquals("hello aviator", foo.bars[0].getName());
|
|
||||||
|
|
||||||
exp = "foo.bars[0] = nil ; foo.bars[0]";
|
|
||||||
result = (String) engine.eval(exp, Dict.of().set("foo", foo));
|
|
||||||
Console.log("Execute expression: " + exp);
|
|
||||||
Assertions.assertNull(result);
|
|
||||||
Assertions.assertNull(foo.bars[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Data
|
|
||||||
public static class Bar {
|
|
||||||
public Bar() {
|
|
||||||
this.name = "bar";
|
|
||||||
}
|
|
||||||
private String name;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Data
|
|
||||||
public static class Foo {
|
|
||||||
int i;
|
|
||||||
float f;
|
|
||||||
Date date;
|
|
||||||
Bar[] bars = new Bar[1];
|
|
||||||
|
|
||||||
public Foo(final int i, final float f, final Date date) {
|
|
||||||
this.i = i;
|
|
||||||
this.f = f;
|
|
||||||
this.date = date;
|
|
||||||
this.bars[0] = new Bar();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,111 +0,0 @@
|
|||||||
package org.dromara.hutool.extra.expression;
|
|
||||||
|
|
||||||
import org.dromara.hutool.core.map.Dict;
|
|
||||||
import org.dromara.hutool.extra.expression.engine.jexl.JexlEngine;
|
|
||||||
import org.dromara.hutool.extra.expression.engine.jfireel.JfireELEngine;
|
|
||||||
import org.dromara.hutool.extra.expression.engine.mvel.MvelEngine;
|
|
||||||
import org.dromara.hutool.extra.expression.engine.qlexpress.QLExpressEngine;
|
|
||||||
import org.dromara.hutool.extra.expression.engine.rhino.RhinoEngine;
|
|
||||||
import org.dromara.hutool.extra.expression.engine.spel.SpELEngine;
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class ExpressionUtilTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void evalTest(){
|
|
||||||
final Dict dict = Dict.of()
|
|
||||||
.set("a", 100.3)
|
|
||||||
.set("b", 45)
|
|
||||||
.set("c", -199.100);
|
|
||||||
final Object eval = ExpressionUtil.eval("a-(b-c)", dict);
|
|
||||||
Assertions.assertEquals(-143.8, (double)eval, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void jexlTest(){
|
|
||||||
final ExpressionEngine engine = new JexlEngine();
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void jexlScriptTest(){
|
|
||||||
final ExpressionEngine engine = new JexlEngine();
|
|
||||||
|
|
||||||
final String exps2="if(a>0){return 100;}";
|
|
||||||
final Map<String,Object> map2=new HashMap<>();
|
|
||||||
map2.put("a", 1);
|
|
||||||
final Object eval1 = engine.eval(exps2, map2);
|
|
||||||
Assertions.assertEquals(100, eval1);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void mvelTest(){
|
|
||||||
final ExpressionEngine engine = new MvelEngine();
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void jfireELTest(){
|
|
||||||
final ExpressionEngine engine = new JfireELEngine();
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void spELTest(){
|
|
||||||
final ExpressionEngine engine = new SpELEngine();
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void rhinoTest(){
|
|
||||||
final ExpressionEngine engine = new RhinoEngine();
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void qlExpressTest(){
|
|
||||||
final ExpressionEngine engine = new QLExpressEngine();
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,32 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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…
x
Reference in New Issue
Block a user