FreeMarker(十)Java工具类

x33g5p2x  于2021-12-24 转载在 其他  
字(2.5k)|赞(0)|评价(0)|浏览(390)

FreeMarker会有多种应用场景,IO到IO,字符串到IO,字符串到字符串等等,在不同用途下配置略有不同;

下面的代码示例,适用于从文件读取模版,然后转为字符串,或者从文件到另一个IO流;

从字符串模版转为字符串虽然不影响使用,但是此时FreeMarker自带的缓存是失效的,如果需要此配置,修改Configuration的 TemplateLoading 即可, TemplateLoading 与缓存挂钩。

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;

import com.sea.common.util.bean.Null;
import com.sea.common.util.core.Resource;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;

/**
 * 较通用的FreeMarkers工具类
 * 
 * @author ChenSS 2017年10月19日
 * @since FreeMarker Version2.3.26
 */
public class FreeMarkers {
    private static Configuration config;
    private static final Version VERSION = new Version("2.3.23");
    private static final String FILE_ROOT = "tmp/";

    public static Configuration defaultConfiguration() {
        if (config == null) {
            synchronized (FreeMarkers.class) {
                if (config == null) {
                    try {
                        config = new Configuration(VERSION);
                        config.setNumberFormat("0.####");
                        config.setDateFormat("yyyy-MM-dd");
                        config.setTimeFormat("HH:mm:ss");
                        config.setDateTimeFormat("yyyy-MM-dd HH:mm:ss");
                        config.setBooleanFormat("true,false");
                        config.setWhitespaceStripping(true);
                        config.setClassicCompatible(true);
                        File file = new File(Resource.getResourcePath(FILE_ROOT));
                        config.setDirectoryForTemplateLoading(file);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return config;
    }

    public static void strToWriter(String tmp, Object model, Writer writer) throws Exception {
        new Template(Null.STRING, new StringReader(tmp), config).process(model, writer);
    }
    
    public static void fileToWriter(String tmp, Object model, Writer writer) throws Exception {
        config.getTemplate(tmp).process(model, writer);
    }

    public static String strToStr(String tmp, Object model) throws Exception {
        StringWriter out = new StringWriter();
        strToWriter(tmp, model, out);
        return out.toString();
    }

    public static String fileToStr(String tmp, Object model) throws Exception {
        StringWriter out = new StringWriter();
        config.getTemplate(tmp).process(model, out);
        return out.toString();
    }

    public static void fileToFile(String tmp, File file, Object model) throws Exception {
        FileWriter writer = new FileWriter(FileUtils.newFile(file));
        fileToWriter(tmp, model, writer);
    }

    public static void fileToFile(String tmp, String file, Object model) throws Exception {
        fileToFile(tmp, new File(file), model);
    }
    
    public static void readerToWrite(Reader reader, Writer out, String tmpName, Map<String, Object> params)
            throws Exception {
        new Template(tmpName, reader, config).process(params, out);
    }
}

相关文章