public class PlaceholderResolver extends Object
example:
public static void main(String[] args) {
final Map placeholderVals = new HashMap(5);
placeholderVals.put("key1", "china");
placeholderVals.put("key2", "3");
placeholderVals.put("key3", "beijin");
String testStr = "hello ${key1}";
PlaceholderResolver resolver = new PlaceholderResolver(new PlaceholderResolved() {
public String doResolved(String placeholder) {
System.out.println("find placeholder:" + placeholder);
return placeholderVals.get(placeholder);
}
});
resolver.setPlaceholderPrefix("${");
resolver.setPlaceholderSuffix("}");
System.out.println(resolver.doParse(testStr));
testStr = "hello ${key${key2}}";
System.out.println(resolver.doParse(testStr));
}
| 限定符和类型 | 字段和说明 |
|---|---|
static String |
DEFAULT_PLACEHOLDER_PREFIX
Default placeholder prefix: "${".
|
static String |
DEFAULT_PLACEHOLDER_SUFFIX
Default placeholder suffix: "}".
|
| 构造器和说明 |
|---|
PlaceholderResolver(PlaceholderResolved resolvedInterceptor)
Constructor method.
|
| 限定符和类型 | 方法和说明 |
|---|---|
String |
doParse(String strVal)
Do parser placeholder action.
|
boolean |
hasPlaceHolder(String strVal)
test if target string contains placeholderPrefix.
|
protected String |
parseStringValue(String strVal,
Set<String> visitedPlaceholders)
Parse the given String value recursively, to be able to resolve nested
placeholders (when resolved property values in turn contain placeholders
again).
|
void |
setPlaceholderPrefix(String placeholderPrefix)
Sets the placeholder prefix.
|
void |
setPlaceholderSuffix(String placeholderSuffix)
Sets the placeholder suffix.
|
static boolean |
substringMatch(CharSequence str,
int index,
CharSequence substring)
Test whether the given string matches the given substring
at the given index.
|
public static final String DEFAULT_PLACEHOLDER_PREFIX
public PlaceholderResolver(PlaceholderResolved resolvedInterceptor)
resolvedInterceptor - PlaceholderResolved can not be null.public String doParse(String strVal)
strVal - target string to parserpublic boolean hasPlaceHolder(String strVal)
strVal - target string to testprotected String parseStringValue(String strVal, Set<String> visitedPlaceholders)
strVal - the String value to parsevisitedPlaceholders - the placeholders that have already been visited
during the current resolution attempt (used to detect circular references
between placeholders). Only non-null if we're parsing a nested placeholder.public static boolean substringMatch(CharSequence str, int index, CharSequence substring)
str - the original string (or StringBuffer)index - the index in the original string to start matching againstsubstring - the substring to match at the given indexpublic void setPlaceholderPrefix(String placeholderPrefix)
placeholderPrefix - the new placeholder prefixpublic void setPlaceholderSuffix(String placeholderSuffix)
placeholderSuffix - the new placeholder suffixCopyright © 2022 Baidu, Inc.. All rights reserved.