标签:bsp == tst puts params param stack nbsp ring
Beetl的设计上有许多值得学习的思想,虽然我是FreeMarker的粉丝,但是也花了点时间去研究,
根据2.7.22版本设计的工具类, 由于Beetl版本迭代太快,不保证其它版本可用,
支持:字符串 到 IO,文件 到 IO, 字符 到 文件, 文件 到 字符串 等多种操作,
做短信模版、页面渲染、代码生成器,能实现什么功能,就看各位想象力了。
import com.sea.common.util.Resource; import org.beetl.core.Configuration; import org.beetl.core.GroupTemplate; import org.beetl.core.Template; import org.beetl.core.resource.FileResourceLoader; import org.beetl.core.resource.StringTemplateResourceLoader; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStream; import java.io.Writer; import java.util.HashMap; import java.util.Map; /** * Created by CSS on 2018/4/24. */ public class Beetls { /** * 文件根目录 */ private static final String TMP_SOURCE = "tmp"; /** * Bee配置 */ private static Configuration cfg; /** * 字符串模版 */ private static GroupTemplate strGroup; /** * 文件模版 */ private static GroupTemplate fileGroup; public static void printStr(String tmp, Map<String,Object> params){ Template t = strGroup.getTemplate(tmp); t.binding(params); System.out.println(t.render()); } public static void printFile(String source, Map<String,Object> params){ Template t = fileGroup.getTemplate(source); t.binding(params); System.out.println(t.render()); } public static void fileToWriter(String source, Writer writer, Map<String,Object> params){ Template t = fileGroup.getTemplate(source); t.binding(params); t.renderTo(writer); } public static void fileToStream(String source, OutputStream stream, Map<String,Object> params){ Template t = fileGroup.getTemplate(source); t.binding(params); t.renderTo(stream); } public static String fileToStr(String source, Map<String,Object> params){ Template t = fileGroup.getTemplate(source); t.binding(params); return t.render(); } public static void fileToFile(String source, String target, Map<String,Object> params){ fileToFile(source, new File(target),params); } public static void fileToFile(String source, File target, Map<String,Object> params){ Template t = fileGroup.getTemplate(source); t.binding(params); try { t.renderTo(new FileWriter(target)); } catch (IOException e) { e.printStackTrace(); } } public static void strToWriter(String source, Writer writer, Map<String,Object> params){ Template t = strGroup.getTemplate(source); t.binding(params); t.renderTo(writer); } public static void strToStream(String source, OutputStream stream, Map<String,Object> params){ Template t = strGroup.getTemplate(source); t.binding(params); t.renderTo(stream); } public static String strToStr(String source, Map<String,Object> params){ Template t = strGroup.getTemplate(source); t.binding(params); return t.render(); } public static void strToFile(String source, File target, Map<String,Object> params){ Template t = strGroup.getTemplate(source); t.binding(params); try { t.renderTo(new FileWriter(target)); } catch (IOException e) { e.printStackTrace(); } } public static void strToFile(String source, String target, Map<String,Object> params){ strToFile(source,new File(target),params); } public static void share(Map<String,Object> map){ if(strGroup!=null) strGroup.setSharedVars(map); if(fileGroup!=null) fileGroup.setSharedVars(map); } private static Configuration getCfg(){ if(cfg == null){ synchronized (Beetls.class){ if(cfg == null){ try { cfg = Configuration.defaultConfiguration(); } catch (IOException e) { e.printStackTrace(); } } } } return cfg; } public static void stringConfig() { StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader(); strGroup = new GroupTemplate(resourceLoader, getCfg()); } public static void fileConfig() { String root = Resource.getResourcePath(TMP_SOURCE); FileResourceLoader resourceLoader = new FileResourceLoader(root,"utf-8"); fileGroup = new GroupTemplate(resourceLoader, getCfg()); } public static void defaultConfig(){ stringConfig(); fileConfig(); } public static void main(String[] args) { Beetls.defaultConfig(); Map<String,Object> params = new HashMap<>(); params.put("name", "xiaoming"); System.out.println(Beetls.fileToStr("test.tl", params)); } }
标签:bsp == tst puts params param stack nbsp ring
原文地址:https://www.cnblogs.com/chenss15060100790/p/9086000.html