Java 模板工具类实现解析简单的表达式,kafka 实战项目
@return 例如 参数 : dddd$$$$
*/
public static String render(String templet, Map<String, String> context) {
return render(templet, DEFAULT_SPLIT, context);
}
/**
使用 context 中对应的值替换 templet 中用 split 包围的变量名(也是 context 的 key)
@param templet 模板
@param split 用于标识变量名的标志
@param context 用于替换模板中的变量
@return 例如 参数 : dddd$$$$
*/
public static String render(String templet, String split, Map<String, String> context) {
Set<String> paramNames = getParamNames(templet, split);
for (String name : paramNames) {
String value = context.get(name);
value = value == null ? "" : value;
String regex = "\Q" + split + name + split + "\E";
templet = templet.replaceAll(regex, value);
}
return templet;
}
/**
根据分割符从模板中取得变量的名字($$) eg:
$$$$ 返回 aaa,bbb
@param templet 模板
@param split 包围变量名的字符串
@return 模板中的变量名
*/
public static Set<String> getParamNames(String templet, String split) {
Set<String> paramNames = new HashSet<String>();
int start = 0, end = 0;
while (end < templet.length()) {
start = templet.indexOf(split, end) + split.length();
if (start == -1) {
break;
}
//start += split.length();
end = templet.indexOf(split, start);
if (end == -1) {
break;
}
paramNames.add(templet.substring(start, end));
end = end + split.length();
}
return paramNames;
}
public static void main(String[] args) {
Set<String> paramNames = getParamNames("恭喜您:
注册成功人人 app,您的账号为:
", "$$");
System.out.println(paramNames);
Map<String, String> con
text = new HashMap<String, String>();
context.put("name", "小王");
context.put("code", "125284588");
String render = render("恭喜您:
注册成功人人 app,您的账号为:
", context );
System.out.println(render);
}
}
输出结果
[code, name]
恭喜您:小王注册成功人人 app,您的账号为:125284588
package util;
import java.util.*;
/**
非常非常简单的模板实现
@author desheng.tu
@date 2015 年 6 月 23 日 下午 4:38:05
*/
public class SimpleTmpl {
public static void main(String[] args) {
String t1 = "恭喜您
, 注册成功人人 app,您的账号为:
";
String t2 = "你好 #{name}, 您的验证码是:${code}";
Map<String,String> param=new HashMap<>();
param.put("name", "小王");
param.put("code", "13252623145");
for (int i = 0; i < 100; i++) {
SimpleTempletUtil.render(t1, param);
Templ.of(t2).render(param);
}
String result = "";
long start = 0;
start = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
param.put("code", i+"");
result = SimpleTempletUtil.render(t1, param);
}
System.out.println(System.currentTimeMillis() - start);
System.out.println(result);
start = System.currentTimeMillis();
Templ templ = Templ.of(t2);
for (int i = 0; i < 100000; i++) {
param.put("code", i+"");
result = templ.render(param);
}
System.out.println(System.currentTimeMillis() - start);
System.out.println(result);
}
}
abstract class Exp {
abstract String resolve(Map<String, String> context);
static Exp of(String exp) {
Objects.requireNonNull(exp);
if (exp.startsWith("#{") || exp.startsWith("${")) {
return new VarExp(exp);
}
return new StrExp(exp);
}
}
class StrExp extends Exp {
private String value;
StrExp(String exp) {
this.value = exp;
}
@Override
public String resolve(Map<String, String> context) {
return this.value;
}
@Override
public String toString() {
return "StrExp [value=" + value + "]";
}
}
class VarExp extends Exp {
private String varName;
private String defaultValue;
private Boolean nullable = false;
VarExp(String varName, String defaultValue, Boolean nullable) {
this.varName = varName;
this.defaultValue = defaultValue;
this.nullable = nullable;
}
VarExp(String exp) {
Objects.requireNonNull(exp);
if (!(exp.startsWith("#{") || exp.startsWith("${")) || !exp.endsWith("}")) {
throw new IllegalArgumentException("表达式[" + exp + "]必须类似于 #{}或 ${}");
}
String[] nodes = exp.substring(2, exp.length() - 1).split(",");
if (nodes.length > 2) {
throw new IllegalArgumentException("表达式[" + exp + "]只能出现一个','");
}
this.varName = nodes[0].trim();
this.defaultValue = nodes.length == 2 ? nodes[1].trim() : "";
this.nullable = exp.startsWith("$");
}
@Override
public String resolve(Map<String, String> context) {
String value = context.get(varName);
if (value == null && nullable) {
value = defaultValue == null ? "" : defaultValue;
}
if (value == null) {
throw new NullPointerException("上下文中没有指定的变量:var=" + varName + " map=" + context);
}
return value;
}
@Override
public String toString() {
return "VarExp [varName=" + varName + ", defaultValue=" + defaultValue + ", nullable="
nullable + "]";
评论