You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ni...@apache.org on 2011/01/09 03:34:20 UTC

svn commit: r1056868 - in /commons/proper/lang/branches/LANG_2_X/src: main/java/org/apache/commons/lang/text/StrSubstitutor.java test/java/org/apache/commons/lang/text/StrSubstitutorTest.java

Author: niallp
Date: Sun Jan  9 02:34:19 2011
New Revision: 1056868

URL: http://svn.apache.org/viewvc?rev=1056868&view=rev
Log:
Port LANG-596 to LANG 2.x Branch - add a replace(String, Properties) variant to StrSubstitutor

Modified:
    commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/text/StrSubstitutor.java
    commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/text/StrSubstitutorTest.java

Modified: commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/text/StrSubstitutor.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/text/StrSubstitutor.java?rev=1056868&r1=1056867&r2=1056868&view=diff
==============================================================================
--- commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/text/StrSubstitutor.java (original)
+++ commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/text/StrSubstitutor.java Sun Jan  9 02:34:19 2011
@@ -17,8 +17,11 @@
 package org.apache.commons.lang.text;
 
 import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 
 /**
  * Substitutes variables within a string by values.
@@ -151,6 +154,30 @@ public class StrSubstitutor {
     }
 
     /**
+     * Replaces all the occurrences of variables in the given source object with their matching
+     * values from the properties.
+     *
+     * @param source the source text containing the variables to substitute, null returns null
+     * @param valueProperties the properties with values, may be null
+     * @return the result of the replace operation
+     */
+    public static String replace(Object source, Properties valueProperties)
+    {
+        if (valueProperties == null) {
+            return source.toString();
+        }
+        Map valueMap = new HashMap();
+        Enumeration propNames = valueProperties.propertyNames();
+        while (propNames.hasMoreElements())
+        {
+            String propName = (String)propNames.nextElement();
+            String propValue = valueProperties.getProperty(propName);
+            valueMap.put(propName, propValue);
+        }
+        return StrSubstitutor.replace(source, valueMap);
+    }
+
+    /**
      * Replaces all the occurrences of variables in the given source object with
      * their matching values from the system properties.
      *

Modified: commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/text/StrSubstitutorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/text/StrSubstitutorTest.java?rev=1056868&r1=1056867&r2=1056868&view=diff
==============================================================================
--- commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/text/StrSubstitutorTest.java (original)
+++ commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/text/StrSubstitutorTest.java Sun Jan  9 02:34:19 2011
@@ -19,6 +19,7 @@ package org.apache.commons.lang.text;
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Properties;
 
 import junit.framework.TestCase;
 
@@ -410,6 +411,19 @@ public class StrSubstitutorTest extends 
             + "directory is ${user.home}."));
     }
 
+    /**
+     * Test the replace of a properties object
+     */
+    public void testSubstitutetDefaultProperties(){
+        String org = "${doesnotwork}";
+        System.setProperty("doesnotwork", "It work's!");
+
+        // create a new Properties object with the System.getProperties as default
+        Properties props = new Properties(System.getProperties());
+
+        assertEquals("It work's!",StrSubstitutor.replace(org, props));
+    }
+
     //-----------------------------------------------------------------------
     private void doTestReplace(String expectedResult, String replaceTemplate, boolean substring) {
         String expectedShortResult = expectedResult.substring(1, expectedResult.length() - 1);