mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -0,0 +1,273 @@
|
||||
package cn.hutool.extra.script;
|
||||
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.map.WeakConcurrentMap;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
|
||||
import javax.script.Bindings;
|
||||
import javax.script.Compilable;
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptContext;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
/**
|
||||
* 脚本工具类
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public class ScriptUtil {
|
||||
|
||||
private static final ScriptEngineManager MANAGER = new ScriptEngineManager();
|
||||
private static final WeakConcurrentMap<String, ScriptEngine> CACHE = new WeakConcurrentMap<>();
|
||||
|
||||
/**
|
||||
* 获得单例的{@link ScriptEngine} 实例
|
||||
*
|
||||
* @param nameOrExtOrMime 脚本名称
|
||||
* @return {@link ScriptEngine} 实例
|
||||
*/
|
||||
public static ScriptEngine getScript(final String nameOrExtOrMime) {
|
||||
return CACHE.computeIfAbsent(nameOrExtOrMime, (key) -> createScript(nameOrExtOrMime));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 {@link ScriptEngine} 实例
|
||||
*
|
||||
* @param nameOrExtOrMime 脚本名称
|
||||
* @return {@link ScriptEngine} 实例
|
||||
* @since 5.2.6
|
||||
*/
|
||||
public static ScriptEngine createScript(final String nameOrExtOrMime) {
|
||||
ScriptEngine engine = MANAGER.getEngineByName(nameOrExtOrMime);
|
||||
if (null == engine) {
|
||||
engine = MANAGER.getEngineByExtension(nameOrExtOrMime);
|
||||
}
|
||||
if (null == engine) {
|
||||
engine = MANAGER.getEngineByMimeType(nameOrExtOrMime);
|
||||
}
|
||||
if (null == engine) {
|
||||
throw new NullPointerException(StrUtil.format("Script for [{}] not support !", nameOrExtOrMime));
|
||||
}
|
||||
return engine;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得单例的JavaScript引擎
|
||||
*
|
||||
* @return Javascript引擎
|
||||
* @since 5.2.5
|
||||
*/
|
||||
public static ScriptEngine getJsEngine() {
|
||||
return getScript("js");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新的JavaScript引擎
|
||||
*
|
||||
* @return Javascript引擎
|
||||
* @since 5.2.6
|
||||
*/
|
||||
public static ScriptEngine createJsEngine() {
|
||||
return createScript("js");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得单例的Python引擎<br>
|
||||
* 需要引入org.python:jython
|
||||
*
|
||||
* @return Python引擎
|
||||
* @since 5.2.5
|
||||
*/
|
||||
public static ScriptEngine getPythonEngine() {
|
||||
System.setProperty("python.import.site", "false");
|
||||
return getScript("python");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建Python引擎<br>
|
||||
* 需要引入org.python:jython
|
||||
*
|
||||
* @return Python引擎
|
||||
* @since 5.2.6
|
||||
*/
|
||||
public static ScriptEngine createPythonEngine() {
|
||||
System.setProperty("python.import.site", "false");
|
||||
return createScript("python");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得单例的Lua引擎<br>
|
||||
* 需要引入org.luaj:luaj-jse
|
||||
*
|
||||
* @return Lua引擎
|
||||
* @since 5.2.5
|
||||
*/
|
||||
public static ScriptEngine getLuaEngine() {
|
||||
return getScript("lua");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建Lua引擎<br>
|
||||
* 需要引入org.luaj:luaj-jse
|
||||
*
|
||||
* @return Lua引擎
|
||||
* @since 5.2.6
|
||||
*/
|
||||
public static ScriptEngine createLuaEngine() {
|
||||
return createScript("lua");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得单例的Groovy引擎<br>
|
||||
* 需要引入org.codehaus.groovy:groovy-all
|
||||
*
|
||||
* @return Groovy引擎
|
||||
* @since 5.2.5
|
||||
*/
|
||||
public static ScriptEngine getGroovyEngine() {
|
||||
return getScript("groovy");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建Groovy引擎<br>
|
||||
* 需要引入org.codehaus.groovy:groovy-all
|
||||
*
|
||||
* @return Groovy引擎
|
||||
* @since 5.2.6
|
||||
*/
|
||||
public static ScriptEngine createGroovyEngine() {
|
||||
return createScript("groovy");
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行Javascript脚本,返回Invocable,此方法分为两种情况:
|
||||
*
|
||||
* <ol>
|
||||
* <li>执行的脚本返回值是可执行的脚本方法</li>
|
||||
* <li>脚本为函数库,则ScriptEngine本身为可执行方法</li>
|
||||
* </ol>
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @return 执行结果
|
||||
* @throws UtilException 脚本异常
|
||||
* @since 5.3.6
|
||||
*/
|
||||
public static Invocable evalInvocable(final String script) throws UtilException {
|
||||
final ScriptEngine jsEngine = getJsEngine();
|
||||
final Object eval;
|
||||
try {
|
||||
eval = jsEngine.eval(script);
|
||||
} catch (final ScriptException e) {
|
||||
throw new UtilException(e);
|
||||
}
|
||||
if(eval instanceof Invocable){
|
||||
return (Invocable)eval;
|
||||
} else if(jsEngine instanceof Invocable){
|
||||
return (Invocable)jsEngine;
|
||||
}
|
||||
throw new UtilException("Script is not invocable !");
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行有返回值的Javascript脚本
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @return 执行结果
|
||||
* @throws UtilException 脚本异常
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public static Object eval(final String script) throws UtilException {
|
||||
try {
|
||||
return getJsEngine().eval(script);
|
||||
} catch (final ScriptException e) {
|
||||
throw new UtilException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行有返回值的脚本
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @param context 脚本上下文
|
||||
* @return 执行结果
|
||||
* @throws UtilException 脚本异常
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public static Object eval(final String script, final ScriptContext context) throws UtilException {
|
||||
try {
|
||||
return getJsEngine().eval(script, context);
|
||||
} catch (final ScriptException e) {
|
||||
throw new UtilException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行有返回值的脚本
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @param bindings 绑定的参数
|
||||
* @return 执行结果
|
||||
* @throws UtilException 脚本异常
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public static Object eval(final String script, final Bindings bindings) throws UtilException {
|
||||
try {
|
||||
return getJsEngine().eval(script, bindings);
|
||||
} catch (final ScriptException e) {
|
||||
throw new UtilException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行JS脚本中的指定方法
|
||||
*
|
||||
* @param script js脚本
|
||||
* @param func 方法名
|
||||
* @param args 方法参数
|
||||
* @return 结果
|
||||
* @since 5.3.6
|
||||
*/
|
||||
public static Object invoke(final String script, final String func, final Object... args) {
|
||||
final Invocable eval = evalInvocable(script);
|
||||
try {
|
||||
return eval.invokeFunction(func, args);
|
||||
} catch (final ScriptException | NoSuchMethodException e) {
|
||||
throw new UtilException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编译Javascript脚本
|
||||
*
|
||||
* @param script 脚本内容
|
||||
* @return {@link CompiledScript}
|
||||
* @throws UtilException 脚本异常
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public static CompiledScript compile(final String script) throws UtilException {
|
||||
try {
|
||||
return compile(getJsEngine(), script);
|
||||
} catch (final ScriptException e) {
|
||||
throw new UtilException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编译Javascript脚本
|
||||
*
|
||||
* @param engine 引擎
|
||||
* @param script 脚本内容
|
||||
* @return {@link CompiledScript}
|
||||
* @throws ScriptException 脚本异常
|
||||
*/
|
||||
public static CompiledScript compile(final ScriptEngine engine, final String script) throws ScriptException {
|
||||
if (engine instanceof Compilable) {
|
||||
final Compilable compEngine = (Compilable) engine;
|
||||
return compEngine.compile(script);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* 使用Java对脚本引擎的工具封装
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
package cn.hutool.extra.script;
|
121
hutool-extra/src/main/java/cn/hutool/extra/xml/JAXBUtil.java
Normal file
121
hutool-extra/src/main/java/cn/hutool/extra/xml/JAXBUtil.java
Normal file
@@ -0,0 +1,121 @@
|
||||
package cn.hutool.extra.xml;
|
||||
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.XmlUtil;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlElementWrapper;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import javax.xml.bind.annotation.XmlTransient;
|
||||
import java.io.File;
|
||||
import java.io.Reader;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* JAXB(Java Architecture for XML Binding),根据XML Schema产生Java对象,即实现xml和Bean互转。
|
||||
* <p>
|
||||
* 相关介绍:
|
||||
* <ul>
|
||||
* <li><a href="https://www.cnblogs.com/yanghaolie/p/11110991.html">https://www.cnblogs.com/yanghaolie/p/11110991.html</a></li>
|
||||
* <li><a href="https://my.oschina.net/u/4266515/blog/3330113">https://my.oschina.net/u/4266515/blog/3330113</a></li>
|
||||
* </ul>
|
||||
*
|
||||
* @author dazer
|
||||
* @see XmlUtil
|
||||
* @since 5.7.3
|
||||
*/
|
||||
public class JAXBUtil {
|
||||
|
||||
/**
|
||||
* JavaBean转换成xml
|
||||
* <p>
|
||||
* bean上面用的常用注解
|
||||
*
|
||||
* @param bean Bean对象
|
||||
* @return 输出的XML字符串
|
||||
* @see XmlRootElement {@code @XmlRootElement(name = "school")}
|
||||
* @see XmlElement {@code @XmlElement(name = "school_name", required = true)}
|
||||
* @see XmlElementWrapper {@code @XmlElementWrapper(name="schools")}
|
||||
* @see XmlTransient JAXB "有两个名为 "**" 的属性,类的两个属性具有相同名称 "**""解决方案
|
||||
*/
|
||||
public static String beanToXml(final Object bean) {
|
||||
return beanToXml(bean, CharsetUtil.UTF_8, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* JavaBean转换成xml
|
||||
*
|
||||
* @param bean Bean对象
|
||||
* @param charset 编码 eg: utf-8
|
||||
* @param format 是否格式化输出eg: true
|
||||
* @return 输出的XML字符串
|
||||
*/
|
||||
public static String beanToXml(final Object bean, final Charset charset, final boolean format) {
|
||||
final StringWriter writer;
|
||||
try {
|
||||
final JAXBContext context = JAXBContext.newInstance(bean.getClass());
|
||||
final Marshaller marshaller = context.createMarshaller();
|
||||
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, format);
|
||||
marshaller.setProperty(Marshaller.JAXB_ENCODING, charset.name());
|
||||
writer = new StringWriter();
|
||||
marshaller.marshal(bean, writer);
|
||||
} catch (final Exception e) {
|
||||
throw new UtilException("convertToXml 错误:" + e.getMessage(), e);
|
||||
}
|
||||
return writer.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* xml转换成JavaBean
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param xml XML字符串
|
||||
* @param c Bean类型
|
||||
* @return bean
|
||||
*/
|
||||
public static <T> T xmlToBean(final String xml, final Class<T> c) {
|
||||
return xmlToBean(StrUtil.getReader(xml), c);
|
||||
}
|
||||
|
||||
/**
|
||||
* XML文件转Bean
|
||||
*
|
||||
* @param file 文件
|
||||
* @param charset 编码
|
||||
* @param c Bean类
|
||||
* @param <T> Bean类型
|
||||
* @return Bean
|
||||
*/
|
||||
public static <T> T xmlToBean(final File file, final Charset charset, final Class<T> c) {
|
||||
return xmlToBean(FileUtil.getReader(file, charset), c);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从{@link Reader}中读取XML字符串,并转换为Bean
|
||||
*
|
||||
* @param reader {@link Reader}
|
||||
* @param c Bean类
|
||||
* @param <T> Bean类型
|
||||
* @return Bean
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T xmlToBean(final Reader reader, final Class<T> c) {
|
||||
try {
|
||||
final JAXBContext context = JAXBContext.newInstance(c);
|
||||
final Unmarshaller unmarshaller = context.createUnmarshaller();
|
||||
return (T) unmarshaller.unmarshal(reader);
|
||||
} catch (final Exception e) {
|
||||
throw new RuntimeException("convertToJava2 错误:" + e.getMessage(), e);
|
||||
} finally {
|
||||
IoUtil.close(reader);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* XML和JAXB(Java Architecture for XML Binding)相关封装<br>
|
||||
* 由于JDK11+移除了"javax.xml.bind"相关类型,因此封装于extra模块
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
package cn.hutool.extra.xml;
|
Reference in New Issue
Block a user