package com.qianwen.core.tool.utils;
|
|
import java.util.Map;
|
import java.util.Properties;
|
import java.util.function.Function;
|
import java.util.stream.Stream;
|
|
/* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/utils/PlaceholderUtil.class */
|
public class PlaceholderUtil {
|
public static final String DEFAULT_PLACEHOLDER_PREFIX = "${";
|
public static final String DEFAULT_PLACEHOLDER_SUFFIX = "}";
|
private static final PlaceholderUtil DEFAULT_RESOLVER = new PlaceholderUtil();
|
private String placeholderPrefix;
|
private String placeholderSuffix;
|
|
private PlaceholderUtil() {
|
this.placeholderPrefix = "${";
|
this.placeholderSuffix = "}";
|
}
|
|
private PlaceholderUtil(String placeholderPrefix, String placeholderSuffix) {
|
this.placeholderPrefix = "${";
|
this.placeholderSuffix = "}";
|
this.placeholderPrefix = placeholderPrefix;
|
this.placeholderSuffix = placeholderSuffix;
|
}
|
|
public static PlaceholderUtil getDefaultResolver() {
|
return DEFAULT_RESOLVER;
|
}
|
|
public static PlaceholderUtil getResolver(String placeholderPrefix, String placeholderSuffix) {
|
return new PlaceholderUtil(placeholderPrefix, placeholderSuffix);
|
}
|
|
public String resolve(String content, String... values) {
|
int start = content.indexOf(this.placeholderPrefix);
|
if (start == -1) {
|
return content;
|
}
|
int valueIndex = 0;
|
StringBuilder result = new StringBuilder(content);
|
while (start != -1) {
|
int end = result.indexOf(this.placeholderSuffix);
|
int i = valueIndex;
|
valueIndex++;
|
String replaceContent = values[i];
|
result.replace(start, end + this.placeholderSuffix.length(), replaceContent);
|
start = result.indexOf(this.placeholderPrefix, start + replaceContent.length());
|
}
|
return result.toString();
|
}
|
|
public String resolve(String content, Object[] values) {
|
return resolve(content, (String[]) Stream.of(values).map(String::valueOf).toArray(x$0 -> {
|
return new String[x$0];
|
}));
|
}
|
|
public String resolveByRule(String content, Function<String, String> rule) {
|
int start = content.indexOf(this.placeholderPrefix);
|
if (start == -1) {
|
return content;
|
}
|
StringBuilder result = new StringBuilder(content);
|
while (start != -1) {
|
int end = result.indexOf(this.placeholderSuffix, start + 1);
|
String placeholder = result.substring(start + this.placeholderPrefix.length(), end);
|
String replaceContent = placeholder.trim().isEmpty() ? StringPool.EMPTY : rule.apply(placeholder);
|
result.replace(start, end + this.placeholderSuffix.length(), replaceContent);
|
start = result.indexOf(this.placeholderPrefix, start + replaceContent.length());
|
}
|
return result.toString();
|
}
|
|
public String resolveByMap(String content, final Map<String, Object> valueMap) {
|
return resolveByRule(content, placeholderValue -> {
|
return String.valueOf(valueMap.get(placeholderValue));
|
});
|
}
|
|
public String resolveByProperties(String content, final Properties properties) {
|
properties.getClass();
|
//return resolveByRule(content, this::getProperty);
|
return resolveByRule(content, properties::getProperty);
|
}
|
}
|