All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tools.freemarker.FreeMarkerUtil Maven / Gradle / Ivy

There is a newer version: 0.2.2
Show newest version
package tools.freemarker;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class FreeMarkerUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(FreeMarkerUtil.class);

    public static void initTemplate(String ftlTemplateFullName, Map dataModelMap,
                                    String testResourceSubPath, String targetFileFullName) throws IOException, TemplateException {
        Configuration config = new Configuration(Configuration.VERSION_2_3_29);
        //指定模板文件的来源
//        String path = this.getClass().getClassLoader().getResource("templates").getPath(); // static method, 不能使用this关键字。
        String path = ClassLoader.getSystemClassLoader().getResource("templates").getPath();
        LOGGER.info("path = {}", path);
        config.setDirectoryForTemplateLoading(new File(path));
        //这是模板的编码
        config.setDefaultEncoding("UTF-8");
        //获取模板
        Template template = config.getTemplate(ftlTemplateFullName); //.ftl结尾

        //输出文件
        boolean targetPathFlag = false;
        File targetPath = new File("src/test/resources/"+ testResourceSubPath);
        if (!targetPath.exists() || !targetPath.isDirectory()){
            targetPathFlag = targetPath.mkdirs();
            LOGGER.info("创建目录:{}", targetPath);
        } else {
            targetPathFlag = true;
            LOGGER.info("targetPath已存在:{}", targetPath);
        }
        if (targetPathFlag){
            String targetPathAndFileName = targetPath + "/" + targetFileFullName;
            File targetFile = new File(targetPathAndFileName);
            FileWriter fileWriter = new FileWriter(targetFile);
            Writer bufferedWriter = new BufferedWriter(fileWriter);
            //动态填充模板
            template.process(dataModelMap, bufferedWriter);
            LOGGER.info("填充模板完毕。");
            bufferedWriter.flush();
            bufferedWriter.close();
            LOGGER.info("成功写入文件{}。", targetPathAndFileName);
        } else {
            LOGGER.error("targetPathFlag is false. 生成文件目录未准备完毕,请检查。");
        }
    }

    @Test
    public void testTest(){
        Map dataModelMap = new HashMap<>();
        dataModelMap.put("StartDate", "2021-09-15");
        dataModelMap.put("EndDate", "2021-09-15");
        try {
//            FreeMarkerUtil freeMarkerUtil = new FreeMarkerUtil(); // initTemplate已改为静态方法,不需创建对象了。
            FreeMarkerUtil.initTemplate("testjson.ftl", dataModelMap, "restjson/", "target.json");
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy