Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 41698 invoked from network); 9 Jan 2011 02:34:41 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 9 Jan 2011 02:34:41 -0000 Received: (qmail 73806 invoked by uid 500); 9 Jan 2011 02:34:41 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 73735 invoked by uid 500); 9 Jan 2011 02:34:41 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 73725 invoked by uid 99); 9 Jan 2011 02:34:41 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 09 Jan 2011 02:34:41 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 09 Jan 2011 02:34:40 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 5865F23889E2; Sun, 9 Jan 2011 02:34:20 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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 Date: Sun, 09 Jan 2011 02:34:20 -0000 To: commits@commons.apache.org From: niallp@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110109023420.5865F23889E2@eris.apache.org> 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);