From f0aeeb061b6044128ede70aac078fb6ffd57bb0f Mon Sep 17 00:00:00 2001 From: xuxueli <931591021@qq.com> Date: Sun, 7 Jul 2024 03:17:18 +0800 Subject: [PATCH] add freemarker tool --- ...30\346\226\271\346\226\207\346\241\243.md" | 16 ++- .../xxl/tool/freemarker/FreemarkerTool.java | 115 ++++++++++++++++++ .../test/freemarker/FreemarkerToolTest.java | 25 ++++ 3 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/xxl/tool/freemarker/FreemarkerTool.java create mode 100644 src/test/java/com/xxl/tool/test/freemarker/FreemarkerToolTest.java diff --git "a/doc/XXL-TOOL\345\256\230\346\226\271\346\226\207\346\241\243.md" "b/doc/XXL-TOOL\345\256\230\346\226\271\346\226\207\346\241\243.md" index 877cdbc..3bb3c9f 100644 --- "a/doc/XXL-TOOL\345\256\230\346\226\271\346\226\207\346\241\243.md" +++ "b/doc/XXL-TOOL\345\256\230\346\226\271\346\226\207\346\241\243.md" @@ -27,6 +27,7 @@ Pipeline模块 | 高扩展性流程编排引擎 Excel模块 | 一个灵活的Java对象和Excel文档相互转换的工具。一行代码完成Java对象和Excel之间的转换 Emoji模块 | 一个灵活可扩展的Emoji表情编解码库,可快速实现Emoji表情的编解码 Fiber模块 | Java协程库,基于quasar封装实现 +Freemarker模块 | 模板引擎工具,支持根据模板文件生成文本、生成文件…等。 ... | ... ### 1.4 下载 @@ -365,7 +366,19 @@ hexdecimal encode: 一朵美丽的茉莉🌹 hexdecimal decode: 一朵美丽的茉莉🌹 ``` -### 2.7、更多 +### 2.7、Core模块 + +参考单元测试,见目录:com.xxl.tool.test.freemarker.FreemarkerTool +``` +// 初始化设置 模板文件目录地址 +FreemarkerTool.init("/Users/admin/Downloads/"); + +// 根据模板文件,生成文本;支持传入变量 +String text = FreemarkerTool.processString("test.ftl", new HashMap<>()); +logger.info(text); +``` + +### 2.8、更多 略 @@ -401,6 +414,7 @@ hexdecimal decode: 一朵美丽的茉莉🌹 - 1、开源协议变更,由 GPLv3 调整为 Apache2.0 开源协议; - 2、新增Response模块,统一响应数据结构体,标准化数据结构、状态码等,降低协作成本; - 3、新增Pipeline模块,高扩展性流程编排引擎; +- 4、新增Freemarker模块,模板引擎工具,支持根据模板文件生成文本、生成文件…等。 ### TODO LIST - excel模块 diff --git a/src/main/java/com/xxl/tool/freemarker/FreemarkerTool.java b/src/main/java/com/xxl/tool/freemarker/FreemarkerTool.java new file mode 100644 index 0000000..aecc871 --- /dev/null +++ b/src/main/java/com/xxl/tool/freemarker/FreemarkerTool.java @@ -0,0 +1,115 @@ +package com.xxl.tool.freemarker; + +import freemarker.core.TemplateClassResolver; +import freemarker.template.Configuration; +import freemarker.template.Template; +import freemarker.template.TemplateException; +import freemarker.template.TemplateExceptionHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.IOException; +import java.io.StringWriter; +import java.util.Locale; +import java.util.Map; + +/** + * freemarker tool + * + * @author xuxueli 2018-05-02 19:56:00 + */ +public class FreemarkerTool { + private static final Logger logger = LoggerFactory.getLogger(FreemarkerTool.class); + + /** + * freemarker config + */ + private static Configuration freemarkerConfig = null; + + /** + * init with prop + * + * @param templatePath + */ + public static void init(String templatePath){ + try { + freemarkerConfig = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS); + + freemarkerConfig.setDirectoryForTemplateLoading(new File(templatePath)); + freemarkerConfig.setDefaultEncoding("UTF-8"); + freemarkerConfig.setNumberFormat("0.##########"); + freemarkerConfig.setNewBuiltinClassResolver(TemplateClassResolver.SAFER_RESOLVER); + freemarkerConfig.setClassicCompatible(true); + freemarkerConfig.setLocale(Locale.CHINA); + freemarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); + } catch (IOException e) { + logger.error(e.getMessage(), e); + } + } + + /** + * init with specified freemarkerConfig + * + * @param freemarkerConfig + */ + public static void init(Configuration freemarkerConfig){ + FreemarkerTool.freemarkerConfig = freemarkerConfig; + } + + + // ---------------------- process string data by template ---------------------- + + /** + * process Template Into String + * + * @param template + * @param model + * @return + * @throws IOException + * @throws TemplateException + */ + public static String processTemplateIntoString(Template template, Object model) + throws IOException, TemplateException { + + StringWriter result = new StringWriter(); + template.process(model, result); + return result.toString(); + } + + /** + * process and generate String with default freemarkerConfig + * + * @param templateName + * @param params + * @return + * @throws IOException + * @throws TemplateException + */ + public static String processString(String templateName, Map params) + throws IOException, TemplateException { + + Template template = freemarkerConfig.getTemplate(templateName); + String htmlText = processTemplateIntoString(template, params); + return htmlText; + } + + /** + * process and generate String with specified freemarkerConfig + * + * @param freemarkerConfig + * @param templateName + * @param params + * @return + * @throws IOException + * @throws TemplateException + */ + public static String processString(Configuration freemarkerConfig, String templateName, Map params) + throws IOException, TemplateException { + + Template template = freemarkerConfig.getTemplate(templateName); + String htmlText = processTemplateIntoString(template, params); + return htmlText; + } + +} diff --git a/src/test/java/com/xxl/tool/test/freemarker/FreemarkerToolTest.java b/src/test/java/com/xxl/tool/test/freemarker/FreemarkerToolTest.java new file mode 100644 index 0000000..4a47e92 --- /dev/null +++ b/src/test/java/com/xxl/tool/test/freemarker/FreemarkerToolTest.java @@ -0,0 +1,25 @@ +package com.xxl.tool.test.freemarker; + +import com.xxl.tool.freemarker.FreemarkerTool; +import freemarker.template.TemplateException; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.HashMap; + +public class FreemarkerToolTest { + private static final Logger logger = LoggerFactory.getLogger(FreemarkerToolTest.class); + + @Test + public void test() throws TemplateException, IOException { + // init template path + FreemarkerTool.init("/Users/admin/Downloads/"); + + // generate text by template + String text = FreemarkerTool.processString("test.ftl", new HashMap<>()); + logger.info(text); + } + +}