Browse Source

property placeholders can deal with nested expressions which happen to use the same suffix (SPR-7098)

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3282 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/head
Juergen Hoeller 16 years ago
parent
commit
546d9968d9
  1. 26
      org.springframework.core/src/main/java/org/springframework/util/PropertyPlaceholderHelper.java
  2. 50
      org.springframework.core/src/test/java/org/springframework/util/SystemPropertyUtilsTests.java

26
org.springframework.core/src/main/java/org/springframework/util/PropertyPlaceholderHelper.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,7 +16,9 @@ @@ -16,7 +16,9 @@
package org.springframework.util;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
@ -37,10 +39,21 @@ public class PropertyPlaceholderHelper { @@ -37,10 +39,21 @@ public class PropertyPlaceholderHelper {
private static final Log logger = LogFactory.getLog(PropertyPlaceholderHelper.class);
private static final Map<String, String> wellKnownSimplePrefixes = new HashMap<String, String>(4);
static {
wellKnownSimplePrefixes.put("}", "{");
wellKnownSimplePrefixes.put("]", "[");
wellKnownSimplePrefixes.put(")", "(");
}
private final String placeholderPrefix;
private final String placeholderSuffix;
private final String simplePrefix;
private final String valueSeparator;
private final boolean ignoreUnresolvablePlaceholders;
@ -70,6 +83,13 @@ public class PropertyPlaceholderHelper { @@ -70,6 +83,13 @@ public class PropertyPlaceholderHelper {
Assert.notNull(placeholderSuffix, "placeholderSuffix must not be null");
this.placeholderPrefix = placeholderPrefix;
this.placeholderSuffix = placeholderSuffix;
String simplePrefixForSuffix = wellKnownSimplePrefixes.get(this.placeholderSuffix);
if (simplePrefixForSuffix != null && this.placeholderPrefix.endsWith(simplePrefixForSuffix)) {
this.simplePrefix = simplePrefixForSuffix;
}
else {
this.simplePrefix = this.placeholderPrefix;
}
this.valueSeparator = valueSeparator;
this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
}
@ -176,9 +196,9 @@ public class PropertyPlaceholderHelper { @@ -176,9 +196,9 @@ public class PropertyPlaceholderHelper {
return index;
}
}
else if (StringUtils.substringMatch(buf, index, this.placeholderPrefix)) {
else if (StringUtils.substringMatch(buf, index, this.simplePrefix)) {
withinNestedPlaceholder++;
index = index + this.placeholderPrefix.length();
index = index + this.simplePrefix.length();
}
else {
index++;

50
org.springframework.core/src/test/java/org/springframework/util/SystemPropertyUtilsTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -39,12 +39,60 @@ public class SystemPropertyUtilsTests { @@ -39,12 +39,60 @@ public class SystemPropertyUtilsTests {
}
}
@Test
public void testReplaceFromSystemPropertyWithDefault() {
System.setProperty("test.prop", "bar");
try {
String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop:foo}");
assertEquals("bar", resolved);
}
finally {
System.getProperties().remove("test.prop");
}
}
@Test
public void testReplaceFromSystemPropertyWithExpressionDefault() {
System.setProperty("test.prop", "bar");
try {
String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop:#{foo.bar}}");
assertEquals("bar", resolved);
}
finally {
System.getProperties().remove("test.prop");
}
}
@Test
public void testReplaceFromSystemPropertyWithExpressionContainingDefault() {
System.setProperty("test.prop", "bar");
try {
String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop:Y#{foo.bar}X}");
assertEquals("bar", resolved);
}
finally {
System.getProperties().remove("test.prop");
}
}
@Test
public void testReplaceWithDefault() {
String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop:foo}");
assertEquals("foo", resolved);
}
@Test
public void testReplaceWithExpressionDefault() {
String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop:#{foo.bar}}");
assertEquals("#{foo.bar}", resolved);
}
@Test
public void testReplaceWithExpressionContainingDefault() {
String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop:Y#{foo.bar}X}");
assertEquals("Y#{foo.bar}X", resolved);
}
@Test(expected=IllegalArgumentException.class)
public void testReplaceWithNoDefault() {
String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop}");

Loading…
Cancel
Save