Skip to content

Commit 8357bcb

Browse files
author
Rob Harrop
committed
[SPR-6025] PropertyPlaceholderUtils introduced
1 parent df3881d commit 8357bcb

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2002-2009 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.util;
18+
19+
import java.util.Properties;
20+
21+
/**
22+
* Utility class for working with Strings that have placeholder values in them. A placeholder takes the form
23+
* <code>${name}</code>. Using <code>PropertyPlaceholderUtils</code> these placeholders can be substituted for
24+
* user-supplied values. <p> Values for substitution can be supplied using a {@link Properties} instance or using a
25+
* {@link PlaceholderResolver}.
26+
*
27+
* @author Juergen Hoeller
28+
* @author Rob Harrop
29+
* @since 3.0
30+
*/
31+
public class PropertyPlaceholderUtils {
32+
33+
/** Prefix for property placeholders: "${" */
34+
public static final String PLACEHOLDER_PREFIX = "${";
35+
36+
/** Suffix for property placeholders: "}" */
37+
public static final String PLACEHOLDER_SUFFIX = "}";
38+
39+
/**
40+
* Replaces all placeholders of format <code>${name}</code> with the corresponding property from the supplied {@link
41+
* Properties}.
42+
*
43+
* @param value the value containing the placeholders to be replaced.
44+
* @param properties the <code>Properties</code> to use for replacement.
45+
* @return the supplied value with placeholders replaced inline.
46+
*/
47+
public static String replacePlaceholders(String value, final Properties properties) {
48+
Assert.notNull(properties, "Argument 'properties' must not be null.");
49+
return replacePlaceholders(value, new PlaceholderResolver() {
50+
51+
public String resolvePlaceholder(String placeholderName) {
52+
return properties.getProperty(placeholderName);
53+
}
54+
});
55+
}
56+
57+
/**
58+
* Replaces all placeholders of format <code>${name}</code> with the value returned from the supplied {@link
59+
* PlaceholderResolver}.
60+
*
61+
* @param value the value containing the placeholders to be replaced.
62+
* @param placeholderResolver the <code>PlaceholderResolver</code> to use for replacement.
63+
* @return the supplied value with placeholders replaced inline.
64+
*/
65+
public static String replacePlaceholders(String value, PlaceholderResolver placeholderResolver) {
66+
StringBuilder result = new StringBuilder(value);
67+
68+
int startIndex = result.indexOf(PLACEHOLDER_PREFIX);
69+
while (startIndex != -1) {
70+
int endIndex = result.indexOf(PLACEHOLDER_SUFFIX, startIndex + PLACEHOLDER_PREFIX.length());
71+
if (endIndex != -1) {
72+
String placeholder = result.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex);
73+
int nextIndex = endIndex + PLACEHOLDER_SUFFIX.length();
74+
75+
String propVal = placeholderResolver.resolvePlaceholder(placeholder);
76+
if (propVal != null) {
77+
result.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), propVal);
78+
nextIndex = startIndex + propVal.length();
79+
}
80+
81+
startIndex = result.indexOf(PLACEHOLDER_PREFIX, nextIndex);
82+
}
83+
else {
84+
startIndex = -1;
85+
}
86+
}
87+
88+
return result.toString();
89+
}
90+
91+
/**
92+
* Strategy interface used to resolve replacement values for placeholders contained in Strings.
93+
*
94+
* @see org.springframework.util.PropertyPlaceholderUtils
95+
*/
96+
public static interface PlaceholderResolver {
97+
98+
/**
99+
* Resolves the supplied placeholder name into the replacement value.
100+
*
101+
* @param placeholderName the name of the placeholder to resolve.
102+
* @return the replacement value or <code>null</code> if no replacement is to be made.
103+
*/
104+
String resolvePlaceholder(String placeholderName);
105+
}
106+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2002-2009 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.util;
18+
19+
import java.util.Properties;
20+
21+
import org.junit.Test;
22+
import static org.junit.Assert.assertEquals;
23+
24+
/** @author Rob Harrop */
25+
public class PropertyPlaceholderUtilsTests {
26+
27+
@Test
28+
public void testWithProperties() {
29+
String text = "foo=${foo}";
30+
Properties props = new Properties();
31+
props.setProperty("foo", "bar");
32+
33+
assertEquals("foo=bar", PropertyPlaceholderUtils.replacePlaceholders(text, props));
34+
}
35+
36+
@Test
37+
public void testWithMultipleProperties() {
38+
String text = "foo=${foo},bar=${bar}";
39+
Properties props = new Properties();
40+
props.setProperty("foo", "bar");
41+
props.setProperty("bar", "baz");
42+
43+
assertEquals("foo=bar,bar=baz", PropertyPlaceholderUtils.replacePlaceholders(text, props));
44+
}
45+
46+
@Test
47+
public void testWithResolver() {
48+
String text = "foo=${foo}";
49+
50+
assertEquals("foo=bar",
51+
PropertyPlaceholderUtils.replacePlaceholders(text, new PropertyPlaceholderUtils.PlaceholderResolver() {
52+
53+
public String resolvePlaceholder(String placeholderName) {
54+
if ("foo".equals(placeholderName)) {
55+
return "bar";
56+
}
57+
else {
58+
return null;
59+
}
60+
}
61+
}));
62+
}
63+
64+
@Test
65+
public void testUnresolvedPlaceholderIsIgnored() {
66+
String text = "foo=${foo},bar=${bar}";
67+
Properties props = new Properties();
68+
props.setProperty("foo", "bar");
69+
70+
assertEquals("foo=bar,bar=${bar}", PropertyPlaceholderUtils.replacePlaceholders(text, props));
71+
}
72+
}

0 commit comments

Comments
 (0)