|
| 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 | +} |
0 commit comments