yangys
2024-03-31 2969df3e404db3cd116f27db1495e523ce05bf86
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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);
    }
}