Skip to content
Snippets Groups Projects
Commit b236e67b authored by felmer's avatar felmer
Browse files

LMS-2755 Allow default values in property references like ${my-property:default value}

SVN: 24610
parent 66b51b74
No related branches found
No related tags found
No related merge requests found
......@@ -39,6 +39,15 @@ import org.apache.commons.lang.SystemUtils;
* will result in <code>getProperty("C")</code> returning the value "1234567890 plus more". Cyclic
* references are handled by removing the current key before resolving it, i.e. when setting A=${B}
* and B=${A} and then asking for A, you will get ${A}.
*
* Also, default values can be defined. Example:
*
* <pre>
* greeting = hello ${user:world}
* </pre>
*
* If property 'user' hasn't been defined <code>getProperty("greeting")</code> returns "hello world".
*
* <li>Inherit properties. For example,
*
* <pre>
......@@ -71,6 +80,9 @@ public final class ExtendedProperties extends Properties
/** Default placeholder suffix: "}" */
private static final String SUFFIX = "}";
/** Default placeholder delim for default value: ":" */
private static final String DEFAULT_VALUE_DELIM = ":";
/** Default placeholder prefix: "${" */
private static final String PREFIX = "${";
......@@ -178,12 +190,22 @@ public final class ExtendedProperties extends Properties
final int suffixLen = SUFFIX.length();
while (startName >= 0 && endName > startName)
{
final String paramName = result.substring(startName + prefixLen, endName);
String paramName = result.substring(startName + prefixLen, endName);
String paramValue = null;
int indexOfDefaultValueDelim = paramName.indexOf(DEFAULT_VALUE_DELIM);
if (indexOfDefaultValueDelim > 0)
{
paramValue = paramName.substring(indexOfDefaultValueDelim + 1);
paramName = paramName.substring(0, indexOfDefaultValueDelim);
}
if (keys.contains(paramName) == false)
{
keys.add(key);
paramValue = getProperty(paramName, keys);
String propertyValue = getProperty(paramName, keys);
if (propertyValue != null)
{
paramValue = propertyValue;
}
keys.remove(key);
}
if (paramValue != null)
......
......@@ -62,6 +62,22 @@ public final class ExtendedPropertiesTest extends AssertJUnit
assertEquals("eins", extendedProperties.getProperty("un"));
assertEquals("einsdrei", extendedProperties.getProperty("four", "abc"));
}
@Test
public void testMultipleReplacements()
{
extendedProperties.setProperty("greetings", "hello ${one}, hi ${un}, hi ${two}");
assertEquals("hello eins, hi eins, hi zwei", extendedProperties.getProperty("greetings"));
}
@Test
public void testReplacementWithDefaultValue()
{
extendedProperties.setProperty("greetings", "hello ${un}, hi ${1:eins}");
assertEquals("hello eins, hi eins", extendedProperties.getProperty("greetings"));
}
@Test
public final void testGetSubsetString()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment