public static void main(String[] args) {
Map<String, Object> valueMap = Maps.newHashMap();
valueMap.put("firstName", "你好");
valueMap.put("secondName", "他好");
valueMap.put("menuId", "123");
valueMap.put("templateId", UUIDLong.longUUID());
String pattern = "/collaboration/collaboration.do?method=newColl&from=bizconfig&firstName={firstName}&secondName={secondName}&menuId={menuId}&templateId={templateId}";
StrSubstitutor strSubstitutor = new StrSubstitutor(new MapStrLookup(valueMap), "{", "}", StrSubstitutor.DEFAULT_ESCAPE);
String replace = strSubstitutor.replace(pattern);
System.out.println(replace);
}
点评:从上例中代码可知道,对于MessageFormat和String.format的应用场景都可以用StrSubstitutor替换使用。
/**
* Creates a new instance and initializes it.
*
* @param variableResolver the variable resolver, may be null
* @param prefix the prefix for variables, not null
* @param suffix the suffix for variables, not null
* @param escape the escape character
* @throws IllegalArgumentException if the prefix or suffix is null
*/
public StrSubstitutor(StrLookup variableResolver, String prefix, String suffix, char escape) {
this.setVariableResolver(variableResolver);
this.setVariablePrefix(prefix);
this.setVariableSuffix(suffix);
this.setEscapeChar(escape);
}
/**
* Creates a new instance and initializes it.
*
* @param variableResolver the variable resolver, may be null
* @param prefixMatcher the prefix for variables, not null
* @param suffixMatcher the suffix for variables, not null
* @param escape the escape character
* @throws IllegalArgumentException if the prefix or suffix is null
*/
public StrSubstitutor(
StrLookup variableResolver, StrMatcher prefixMatcher, StrMatcher suffixMatcher, char escape) {
this.setVariableResolver(variableResolver);
this.setVariablePrefixMatcher(prefixMatcher);
this.setVariableSuffixMatcher(suffixMatcher);
this.setEscapeChar(escape);
}
比如我上例的代码是为了生成URL,如果没有找到Key值的话,URL就会变成:/collaboration/collaboration.do?method=newColl&from=bizconfig&firstName={firstName}&secondName={secondName}&menuId=123&templateId=-6672438965046130710,这种URL结构在浏览器上访问立马就报400了,所以我就定制了StrLookup,如下:
/**
* MapStrLookup默认实现,解决如果没有值的时候没有做替换导致URL请求400
*/
static class MapStrLookup extends StrLookup {
private final Map map;
MapStrLookup(Map map) {
this.map = map;
}
@Override
public String lookup(String key) {
if (map == null) {
return null;
}
Object obj = map.get(key);
if (obj == null) {
return StringUtils.EMPTY;
}
return obj.toString();
}
}
public static void main(String[] args) {
Map<String, Object> valueMap = Maps.newHashMap();
valueMap.put("firstName", "你好");
valueMap.put("secondName", "{firstName}");
valueMap.put("menuId", "123");
valueMap.put("templateId", UUIDLong.longUUID());
String pattern = "/collaboration/collaboration.do?method=newColl&from=bizconfig&firstName={firstName}&secondName={secondName}&menuId={menuId}&templateId={templateId}";
StrSubstitutor strSubstitutor = new StrSubstitutor(new MapStrLookup(valueMap), "{", "}", StrSubstitutor.DEFAULT_ESCAPE);
String replace = strSubstitutor.replace(pattern);
System.out.println(replace);
}
执行结果:
/collaboration/collaboration.do?method=newColl&from=bizconfig&firstName=你好&secondName=你好&menuId=123&templateId=1780653443789639665