Initial project
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
package com.imyeyu.utils;
|
||||
|
||||
import com.imyeyu.java.TimiJava;
|
||||
import com.imyeyu.java.bean.CallbackArgReturn;
|
||||
import com.imyeyu.java.ref.Ref;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 字符串插值器
|
||||
*
|
||||
* @author 夜雨
|
||||
* @version 2022-12-01 00:41
|
||||
*/
|
||||
public class StringInterpolator {
|
||||
|
||||
/** ${} 插值正则 */
|
||||
public static final String DOLLAR_OBJ = "\\$\\{(.+?)\\}";
|
||||
|
||||
/** {} 插值正则 */
|
||||
public static final String SIMPLE_OBJ = "\\{(.+?)\\}";
|
||||
|
||||
/** 正则匹配器 */
|
||||
private final Pattern pattern;
|
||||
|
||||
/** 为 true 时允许安全插值空,变量可以插值 null 而不抛出异常 */
|
||||
private boolean nullable = false;
|
||||
|
||||
/** 过滤器 */
|
||||
private Map<String, CallbackArgReturn<String, String>> filterMap;
|
||||
|
||||
/**
|
||||
* 默认构造
|
||||
*
|
||||
* @param regex 插槽正则
|
||||
*/
|
||||
public StringInterpolator(String regex) {
|
||||
this.pattern = Pattern.compile(regex);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入变量
|
||||
*
|
||||
* @param string 字符串
|
||||
* @param argsMap 变量表
|
||||
* @return 插值结果
|
||||
*/
|
||||
public String inject(String string, Map<String, Object> argsMap) {
|
||||
return pattern.matcher(string).replaceAll(result -> {
|
||||
String group = result.group(1);
|
||||
String key = group.trim();
|
||||
String[] filters = null;
|
||||
if (group.contains("|")) {
|
||||
// 过滤器
|
||||
String[] groups = group.split("\\|");
|
||||
key = groups[0].trim();
|
||||
filters = new String[groups.length - 1];
|
||||
System.arraycopy(groups, 1, filters, 0, filters.length);
|
||||
}
|
||||
Object value = argsMap.get(key);
|
||||
if (key.contains(".")) {
|
||||
// 反射获取
|
||||
try {
|
||||
String[] deepKey = key.split("\\.");
|
||||
value = argsMap.get(deepKey[0]);
|
||||
for (int i = 1; i < deepKey.length; i++) {
|
||||
value = Ref.getFieldValue(value, deepKey[i], Object.class);
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException("ref field file: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
if (TimiJava.isNotEmpty(filterMap) && TimiJava.isNotEmpty(filters)) {
|
||||
// 过滤
|
||||
for (int i = 0; i < filters.length; i++) {
|
||||
CallbackArgReturn<String, String> filter = filterMap.get(filters[i].trim());
|
||||
if (filter == null) {
|
||||
throw new NullPointerException("not found %s filter for %s".formatted(filters[i], key));
|
||||
}
|
||||
value = filter.handler((String) value);
|
||||
}
|
||||
}
|
||||
if (value == null) {
|
||||
if (!nullable) {
|
||||
throw new NullPointerException("null pointer exception for arg: " + key);
|
||||
}
|
||||
value = "";
|
||||
}
|
||||
return String.valueOf(value);
|
||||
});
|
||||
}
|
||||
|
||||
public void putFilter(String name, CallbackArgReturn<String, String> callback) {
|
||||
if (filterMap == null) {
|
||||
filterMap = new HashMap<>();
|
||||
}
|
||||
filterMap.put(name, callback);
|
||||
}
|
||||
|
||||
public void putAllFilter(Map<String, CallbackArgReturn<String, String>> filterMap) {
|
||||
if (this.filterMap == null) {
|
||||
this.filterMap = new HashMap<>();
|
||||
}
|
||||
this.filterMap.putAll(filterMap);
|
||||
}
|
||||
|
||||
public void removeFilter(String name) {
|
||||
if (TimiJava.isNotEmpty(filterMap)) {
|
||||
filterMap.remove(name);
|
||||
}
|
||||
}
|
||||
|
||||
public void clearFilter() {
|
||||
if (TimiJava.isNotEmpty(filterMap)) {
|
||||
filterMap.clear();
|
||||
}
|
||||
}
|
||||
|
||||
public void setNullable(boolean nullable) {
|
||||
this.nullable = nullable;
|
||||
}
|
||||
|
||||
public boolean isNullable() {
|
||||
return nullable;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user