Skip to content

Commit

Permalink
add freemarker tool
Browse files Browse the repository at this point in the history
  • Loading branch information
xuxueli committed Jul 6, 2024
1 parent 3599fb4 commit f0aeeb0
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 1 deletion.
16 changes: 15 additions & 1 deletion doc/XXL-TOOL官方文档.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Pipeline模块 | 高扩展性流程编排引擎
Excel模块 | 一个灵活的Java对象和Excel文档相互转换的工具。一行代码完成Java对象和Excel之间的转换
Emoji模块 | 一个灵活可扩展的Emoji表情编解码库,可快速实现Emoji表情的编解码
Fiber模块 | Java协程库,基于quasar封装实现
Freemarker模块 | 模板引擎工具,支持根据模板文件生成文本、生成文件…等。
... | ...

### 1.4 下载
Expand Down Expand Up @@ -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、更多


Expand Down Expand Up @@ -401,6 +414,7 @@ hexdecimal decode: 一朵美丽的茉莉🌹
- 1、开源协议变更,由 GPLv3 调整为 Apache2.0 开源协议;
- 2、新增Response模块,统一响应数据结构体,标准化数据结构、状态码等,降低协作成本;
- 3、新增Pipeline模块,高扩展性流程编排引擎;
- 4、新增Freemarker模块,模板引擎工具,支持根据模板文件生成文本、生成文件…等。

### TODO LIST
- excel模块
Expand Down
115 changes: 115 additions & 0 deletions src/main/java/com/xxl/tool/freemarker/FreemarkerTool.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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<String, Object> params)
throws IOException, TemplateException {

Template template = freemarkerConfig.getTemplate(templateName);
String htmlText = processTemplateIntoString(template, params);
return htmlText;
}

}
25 changes: 25 additions & 0 deletions src/test/java/com/xxl/tool/test/freemarker/FreemarkerToolTest.java
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit f0aeeb0

Please sign in to comment.