-
Notifications
You must be signed in to change notification settings - Fork 539
/
Strings.java
105 lines (94 loc) · 3.37 KB
/
Strings.java
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package io.hawt.util;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
/**
* String utility.
*/
public class Strings {
public static boolean isBlank(String text) {
return text == null || text.trim().isEmpty();
}
public static boolean isNotBlank(String text) {
return text != null && !text.trim().isEmpty();
}
public static List<String> split(String text, String delimiter) {
if (text == null || delimiter == null) {
throw new IllegalArgumentException(
"Both 'text' and 'delimiter' should not be null.");
}
return Arrays.stream(text.split(delimiter)).map(String::trim)
.filter(Strings::isNotBlank).collect(Collectors.toList());
}
public static String resolvePlaceholders(String value) {
return resolvePlaceholders(value, System.getProperties());
}
/**
* Simple, recursively-safe property placeholder resolver. De-facto
* standard {@code ${...}} syntax is used. Unresolvable properties are not replaced and separators pass to
* resulting value.
*
* @param value
* @return
*/
public static String resolvePlaceholders(String value, Properties properties) {
if (value == null || !value.contains("$")) {
return value;
}
StringBuilder result = new StringBuilder();
int l = value.length();
for (int pos1 = 0; pos1 < l; pos1++) {
char c1 = value.charAt(pos1);
char c2 = pos1 == l - 1 ? '\0' : value.charAt(pos1 + 1);
if (c1 == '$' && c2 == '{') {
// find matching }
// - if found, resolve and continue with the rest of the value
// - if not found, just proceed (possibly until next "${")
int depth = 1;
int pos2 = pos1 + 2;
while (depth > 0 && pos2 < l) {
if (value.charAt(pos2) == '$' && pos2 < l - 1 && value.charAt(pos2 + 1) == '{') {
depth++;
pos2 += 2;
} else if (value.charAt(pos2) == '}') {
depth--;
pos2++;
} else {
pos2++;
}
}
if (depth > 0) {
// no matching '}'
result.append('$');
} else {
pos1 = resolve(value, result, pos1, pos2, properties) - 1;
}
} else {
result.append(c1);
}
}
return result.toString();
}
/**
* Single iteration resolve method. {@code from} indicates <code>${</code> placeholder start
*
* @param value
* @param result
* @param from
* @param to
* @param properties
* @return
*/
private static int resolve(String value, StringBuilder result, int from, int to, Properties properties) {
// "from" always points to "${" and "to" points to _matching_ "}"
String key = resolvePlaceholders(value.substring(from + 2, to - 1), properties);
String v = properties.getProperty(key);
if (v == null) {
result.append("${").append(key).append("}");
} else {
result.append(v);
}
return to;
}
}