From b236e67b9f76764dc79c44d6862f4d89e9873e3c Mon Sep 17 00:00:00 2001 From: felmer <felmer> Date: Mon, 5 Mar 2012 08:47:22 +0000 Subject: [PATCH] LMS-2755 Allow default values in property references like ${my-property:default value} SVN: 24610 --- .../common/utilities/ExtendedProperties.java | 26 +++++++++++++++++-- .../utilities/ExtendedPropertiesTest.java | 16 ++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/ExtendedProperties.java b/common/source/java/ch/systemsx/cisd/common/utilities/ExtendedProperties.java index 3f2f5e537be..995f0691d43 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/ExtendedProperties.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/ExtendedProperties.java @@ -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) diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ExtendedPropertiesTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ExtendedPropertiesTest.java index a2d925185a7..8a2895d0bbb 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ExtendedPropertiesTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ExtendedPropertiesTest.java @@ -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() -- GitLab