Repository: commons-text
Updated Branches:
  refs/heads/master 8613c3437 -> f90572543


TEXT-74 Added attribute for disabling the recursive substitution of variable 
values


Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/a25aa337
Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/a25aa337
Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/a25aa337

Branch: refs/heads/master
Commit: a25aa337f08e8cd0baad1d70565552c7dad91d5b
Parents: 317ae84
Author: Ioannis Sermetziadis <sermoj...@gmail.com>
Authored: Sun Oct 8 00:34:06 2017 +0300
Committer: Ioannis Sermetziadis <sermoj...@gmail.com>
Committed: Sun Oct 8 00:34:06 2017 +0300

----------------------------------------------------------------------
 .../org/apache/commons/text/StrSubstitutor.java | 44 ++++++++++++++++++--
 .../apache/commons/text/StrSubstitutorTest.java | 23 +++++++++-
 2 files changed, 62 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-text/blob/a25aa337/src/main/java/org/apache/commons/text/StrSubstitutor.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/StrSubstitutor.java 
b/src/main/java/org/apache/commons/text/StrSubstitutor.java
index e836274..af00f85 100644
--- a/src/main/java/org/apache/commons/text/StrSubstitutor.java
+++ b/src/main/java/org/apache/commons/text/StrSubstitutor.java
@@ -169,6 +169,10 @@ public class StrSubstitutor {
      * Whether escapes should be preserved.  Default is false;
      */
     private boolean preserveEscapes = false;
+    /**
+     * The flag whether substitution in variable values is disabled.
+     */
+    private boolean disableSubstitutionInValues;
 
     //-----------------------------------------------------------------------
     /**
@@ -752,6 +756,7 @@ public class StrSubstitutor {
         final char escape = getEscapeChar();
         final StrMatcher valueDelimMatcher = getValueDelimiterMatcher();
         final boolean substitutionInVariablesEnabled = 
isEnableSubstitutionInVariables();
+        final boolean substitutionInValuesDisabled = 
isDisableSubstitutionInValues();
 
         final boolean top = priorVariables == null;
         boolean altered = false;
@@ -856,14 +861,16 @@ public class StrSubstitutor {
                                     varValue = varDefaultValue;
                                 }
                                 if (varValue != null) {
-                                    // recursive replace
                                     final int varLen = varValue.length();
                                     buf.replace(startPos, endPos, varValue);
                                     altered = true;
-                                    int change = substitute(buf, startPos,
+                                    int change = 0;
+                                    if(!substitutionInValuesDisabled) { // 
recursive replace
+                                        change = substitute(buf, startPos,
                                             varLen, priorVariables);
+                                    }
                                     change = change
-                                            + varLen - (endPos - startPos);
+                                        + varLen - (endPos - startPos);
                                     pos += change;
                                     bufEnd += change;
                                     lengthChange += change;
@@ -1194,6 +1201,37 @@ public class StrSubstitutor {
     }
 
     /**
+     * Returns a flag whether substitution is disabled in variable values.If 
set to
+     * <b>true</b>, the values of variables can contain other variables will 
not be
+     * processed and substituted original variable is evaluated, e.g.
+     * <pre>
+     * Map valuesMap = HashMap();
+     * valuesMap.put(&quot;name&quot;, &quot;Douglas ${surname}&quot;);
+     * valuesMap.put(&quot;surname&quot;, &quot;Crockford&quot;);
+     * String templateString = &quot;Hi ${name}&quot;;
+     * StrSubstitutor sub = new StrSubstitutor(valuesMap);
+     * String resolvedString = sub.replace(templateString);
+     * </pre>
+     * yielding:
+     * <pre>
+     *      Hi Douglas ${surname}
+     * </pre>
+     *
+     * @return the substitution in variable values flag
+     */
+    public boolean isDisableSubstitutionInValues() {
+        return disableSubstitutionInValues;
+    }
+
+    /**
+     * Sets a flag whether substitution is done in variable values (recursive).
+     * @param disableSubstitutionInValues
+     */
+    public void setDisableSubstitutionInValues(boolean 
disableSubstitutionInValues) {
+        this.disableSubstitutionInValues = disableSubstitutionInValues;
+    }
+
+    /**
      * Returns the flag controlling whether escapes are preserved during
      * substitution.
      *

http://git-wip-us.apache.org/repos/asf/commons-text/blob/a25aa337/src/test/java/org/apache/commons/text/StrSubstitutorTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/StrSubstitutorTest.java 
b/src/test/java/org/apache/commons/text/StrSubstitutorTest.java
index 3990e34..33063ae 100644
--- a/src/test/java/org/apache/commons/text/StrSubstitutorTest.java
+++ b/src/test/java/org/apache/commons/text/StrSubstitutorTest.java
@@ -153,6 +153,21 @@ public class StrSubstitutorTest {
         doTestReplace("The quick brown fox jumps over the lazy dog.", "The 
${animal} jumps over the ${target}.", true);
     }
 
+    @Test
+    public void testDisableSubstitutionInValues() {
+        final StrSubstitutor sub = new StrSubstitutor(values);
+        sub.setDisableSubstitutionInValues(true);
+        values.put("animal", "${critter}");
+        values.put("target", "${pet}");
+        values.put("pet", "${petCharacteristic} dog");
+        values.put("petCharacteristic", "lazy");
+        values.put("critter", "${critterSpeed} ${critterColor} 
${critterType}");
+        values.put("critterSpeed", "quick");
+        values.put("critterColor", "brown");
+        values.put("critterType", "fox");
+        doTestReplace(sub,"The ${critter} jumps over the ${pet}.", "The 
${animal} jumps over the ${target}.", true);
+    }
+
     /**
      * Tests escaping.
      */
@@ -638,10 +653,14 @@ public class StrSubstitutorTest {
         assertEquals("value $${escaped}", sub.replace(org));
     }
 
-    //-----------------------------------------------------------------------
     private void doTestReplace(final String expectedResult, final String 
replaceTemplate, final boolean substring) {
-        final String expectedShortResult = expectedResult.substring(1, 
expectedResult.length() - 1);
         final StrSubstitutor sub = new StrSubstitutor(values);
+        doTestReplace(sub, expectedResult, replaceTemplate, substring);
+    }
+
+    //-----------------------------------------------------------------------
+    private void doTestReplace(final StrSubstitutor sub, final String 
expectedResult, final String replaceTemplate, final boolean substring) {
+        final String expectedShortResult = expectedResult.substring(1, 
expectedResult.length() - 1);
 
         // replace using String
         assertEquals(expectedResult, sub.replace(replaceTemplate));

Reply via email to