字符串变量替换
大约 2 分钟
package com.ldf.myaustin.utils;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ldf.myaustin.entity.enums.SendChannel;
import com.ldf.myaustin.entity.pojo.MessageTemplate;
import com.ldf.myaustin.model.message.sms.pojo.MessageContentModel;
import org.springframework.util.PropertyPlaceholderHelper;
import javax.validation.constraints.NotNull;
import java.util.Map;
import java.util.Set;
/**
* Spring适配方式
* 字符串内任意符号{$var} 被替换为map中任意变量内容
*/
public class PropertyPlaceUtil {
private static final String prefixStr = "{$";
private static final String suffixStr = "}";
/**
* 根据指定符号替换变量内容
*
* @param templateStr 指定符号
* @param val 变量集
* @return 替换变量后的内容
*/
public static String replaceStrFromMap(String templateStr, Map<String, String> val) {
PropertyPlaceholderHelper placeholderHelper = new PropertyPlaceholderHelper(prefixStr, suffixStr);
templateStr = placeholderHelper.replacePlaceholders(templateStr, new PropertyPlaceholderHelper.PlaceholderResolver() {
@Override
public String resolvePlaceholder(@NotNull String placeholderName) {
return val.get(placeholderName);
}
});
return templateStr;
}
/**
* 根据发送类型创建指定对象,封装消息内容(父类引用指向子类对象)
*
* @param messageTemplate 消息模版信息
* @param varMap 变量集合
* @param isReplaceVar 是否替换变量
* @return 消息内容对象
*/
public static MessageContentModel fillIMessageContentModel(MessageTemplate messageTemplate, Map<String, String> varMap, boolean isReplaceVar) {
SendChannel sendChannel = messageTemplate.getSendChannel();
//获取内容实现类
Class className = sendChannel.getClassName();
//创建其子类对象指向父类对象
MessageContentModel newInstance = (MessageContentModel) ReflectUtil.newInstance(className);
//解析模版内容
String msgContent = messageTemplate.getMsgContent();
JSONObject contentObj = JSONUtil.parseObj(msgContent);
//对象中的属性值做变量替换
if (isReplaceVar) {
replaceVarFromMapMethod(varMap, contentObj);
}
//赋值给新对象
BeanUtil.copyProperties(contentObj, newInstance);
return newInstance;
}
private static void replaceVarFromMapMethod(Map<String, String> varMap, JSONObject contentObj) {
Set<Map.Entry<String, Object>> entries = contentObj.entrySet();
for (Map.Entry<String, Object> entry : entries) {
String value = (String) entry.getValue();
String formatContent = PropertyPlaceUtil.replaceStrFromMap(value, varMap);
entry.setValue(formatContent);
}
}
}
/**
* java se适配
*
* @Author: 清峰
* @Description: 自定义属性值替换工具类
* <p>
* 字符串内任意符号${xxx} 被替换为map中任意变量内容
*/
public class PropertyPlaceUtil {
private static final String DYNAMIC_PARAM_PREFIX = "${";
private static final String DYNAMIC_PARAM_SUFFIX = "}";
/**
* 单变量
*
* @param content 内容模版
* @param variable 变量
* @return 结果集
*/
public static String processDynamicParamWithSingleParam(String content, String variable) {
int begin = content.indexOf(DYNAMIC_PARAM_PREFIX);
int end = content.indexOf(DYNAMIC_PARAM_SUFFIX);
if (begin == -1 || end == -1 || begin > end) {
return content;
}
String substring = content.substring(begin, end + 1);
return content.replace(substring, variable);
}
/**
* 多变量,data 为 key - value 形式
*
* @param content 内容模版
* @param variableMap 变量集
* @return 结果集
*/
public static String processDynamicParams(String content, Map<String, String> variableMap) {
int begin = content.indexOf(DYNAMIC_PARAM_PREFIX);
int end = content.indexOf(DYNAMIC_PARAM_SUFFIX);
if (begin == -1 || end == -1 || begin > end) {
return content;
}
String startContent = content.substring(0, begin);
String substring = content.substring(begin, end + 1);
if (StrUtil.isNotEmpty(substring)) {
String key = substring.replace(DYNAMIC_PARAM_PREFIX, "").replace(DYNAMIC_PARAM_SUFFIX, "");
String value = variableMap.get(key);
if (value != null) {
startContent = startContent + value;
} else {
startContent += substring;
}
}
int length = content.length();
if (length == end + 1) {
return startContent;
} else {
return startContent + processDynamicParams(content.substring(end + 1, length), variableMap);
}
}
public static void main(String[] args) {
String str = "变量1为${code1},变量2为${code2},变量3为${code2}";
Map<String, String> map = new HashMap<>(2);
map.put("code1", "代码1");
map.put("code2", "代码2");
String params = processDynamicParams(str, map);
System.out.println(params);
}
}