From 48e8e7e2e66a1726801d9e7a198bf411e0d8d269 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Sun, 15 Aug 2004 23:18:43 +0000 Subject: [PATCH] Format ready for 2.1 git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137894 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/commons/lang/Interpolation.java | 207 ++++++++---------- 1 file changed, 97 insertions(+), 110 deletions(-) diff --git a/src/java/org/apache/commons/lang/Interpolation.java b/src/java/org/apache/commons/lang/Interpolation.java index bef9b1498..4fb8638ca 100644 --- a/src/java/org/apache/commons/lang/Interpolation.java +++ b/src/java/org/apache/commons/lang/Interpolation.java @@ -1,125 +1,124 @@ package org.apache.commons.lang; -import java.util.HashMap; import java.util.Iterator; import java.util.Map; /** - * Performs basic variable interpolation on a String for variables within - * a Map. Variables of the form, ${var}, are supported. + *

Performs basic variable interpolation on a String for variables within a Map. + * Variables of the form, ${var}, are supported.

* * @author Ken Fitzpatrick * @author Henri Yandell + * @since 2.1 + * @version $Id: Interpolation.java,v 1.2 2004/08/15 23:18:43 scolebourne Exp $ */ public class Interpolation { - // QUERY: Anyway to escape the ${..} variable so it is not interpolated? - - // TODO: Consider making these configurable? + /** The marker used to start a variable. */ private static final String SYMBOLIC_VALUE_MARKER_START = "${"; + /** The marker used to end a variable. */ private static final String SYMBOLIC_VALUE_MARKER_END = "}"; /** - *

- * Returns a String that is the result of having performed - * variable interpolation on templateString, - * using the value set found in values. - *

- *

- * The solution is compatible with all JDK versions - * where Jakarta/Commons/Lang also is supported. - *

- *

- * The expected format of templateString is: - *

+     * 

Interpolates a String to replace variables of the form ${...}.

+ * + *

This method is useful for enabling simple strings to be modified based + * on a map of data. A typical use case might be to add data from configuration + * to an error message. This method, and this class, does not seek to replace + * full interpolation mechanisms, for example Velocity.

+ * + *

The expected format of templateString is: + *

      *   The ${animal} jumped over the ${target}.
-     *
- * such that the key/value pairs found in values - * are substituted into the string at the ${key-name} markers. - * In the above example, valuesMap could have been populated as: - *
+     * 
+ * such that the key/value pairs found in values + * are substituted into the string at the ${key-name} markers. + * In the above example, valuesMap could have been populated as: + *
      *   Map valuesMap = HashMap();
      *   valuesMap.put( "animal", "quick brown fox" );
      *   valuesMap.put( "target", "lazy dog" );
      *   String resolvedString = StringUtils.interpolate( templateString, valuesMap );
-     *
- * yielding: - *
+     * 
+ * yielding: + *
      *   The quick brown fox jumped over the lazy dog.
-     *
- *

- *

- * The same templateString from the above example could be reused as: - *

+     * 

+ * + *

The same templateString from the above example could be reused as: + *

      *   Map valuesMap = HashMap();
      *   valuesMap.put( "animal", "cow" );
      *   valuesMap.put( "target", "moon" );
      *   String resolvedString = StringUtils.interpolate( templateString, valuesMap );
-     *
- * yielding: - *
+     * 
+ * yielding: + *
      *   The cow jumped over the moon.
-     *
- *

- *

- * The value of templateString is returned in an unaltered if templateString - * is null, empty, or contains no marked variables that can be resolved by the key/value pairs found in - * valuesMap, or if valuesMap is null, empty or has no key/value pairs that can be - * applied to the marked variables within templateString. - *

- * @param templateString String containing any mixture of variable and non-variable + *

+ * + *

The value of templateString is returned in an unaltered + * if templateString is null, empty, or contains no marked variables + * that can be resolved by the key/value pairs found in valuesMap, + * or if valuesMap is null, empty or has no key/value pairs that can be + * applied to the marked variables within templateString.

+ * + *

If a valuesMap value is null, it will be treated as "".

+ * + * @param templateString String containing any mixture of variable and non-variable * content, to be used as a template for the value substitution process - * @param valuesMap Map containing the key/value pairs to be used to resolve + * @param valuesMap Map containing the key/value pairs to be used to resolve * the values of the marked variables found within templateString - * @return String + * @return the interpolated String */ - public static String interpolate( String templateString, Map valuesMap ) { + public static String interpolate(String templateString, Map valuesMap) { // pre-conditions - if ( valuesMap == null ) + if (templateString == null || valuesMap == null || + templateString.length() == 0 || valuesMap.isEmpty()) { return templateString; - if ( templateString == null ) - return templateString; - if ( templateString.length() < 1 ) - return templateString; - if ( valuesMap.isEmpty() ) - return templateString; - + } + // default the returned String to the templateString String returnString = templateString; String nextKey = null; Object substitutionBean = null; String substitutionValue = null; String nextValueToBeSubstituted = null; - + // get a list of substitution valuesMap Iterator keys = valuesMap.keySet().iterator(); - - while( keys.hasNext() ) { - nextKey = ( String ) keys.next(); - substitutionValue = StringUtils.defaultString( ( String ) valuesMap.get( nextKey ) ); + while (keys.hasNext()) { + nextKey = (String) keys.next(); + substitutionValue = StringUtils.defaultString((String) valuesMap.get(nextKey)); nextValueToBeSubstituted = SYMBOLIC_VALUE_MARKER_START + nextKey + SYMBOLIC_VALUE_MARKER_END; - returnString = StringUtils.replace( returnString, nextValueToBeSubstituted, substitutionValue ); + returnString = StringUtils.replace(returnString, nextValueToBeSubstituted, substitutionValue); } return returnString; } - /** - *

- * Returns a String that is the result of having performed variable interpolation on - * templateString, using the value set found in values, - * repeatedly until there are no changes. - *

- *

- * The expected format of templateString is: - *

+     * 

Interpolates a String to replace variables of the form ${...} + * where the replace strings may also contain variables to interpolate.

+ * + *

This method is useful for enabling simple strings to be modified based + * on a map of data. A typical use case might be to add data from configuration + * to an error message. This method, and this class, does not seek to replace + * full interpolation mechanisms, for example Velocity.

+ * + *

This method calls {@link #interpolate(String, Map)} repeatedly until the + * returned string does not change. This has the effect of allowing the replace + * strings in valuesMap to contain variables that should also be + * interpolated.

+ * + *

The expected format of templateString is: + *

      *   The ${animal} jumped over the ${target}.
-     *
- * such that the key/value pairs found in values are substituted into the string at the - * ${key-name} markers. In the above example, valuesMap - * could have been populated as: - *
+     * 
+ * such that the key/value pairs found in values are substituted into the string at the + * ${key-name} markers. In the above example, valuesMap + * could have been populated as: + *
      *   Map valuesMap = HashMap();
      *   valuesMap.put( "animal", "${critter}" );
      *   valuesMap.put( "target", "${pet}" );
@@ -130,52 +129,40 @@ public class Interpolation {
      *   valuesMap.put( "critterColor", "brown" );
      *   valuesMap.put( "critterType", "fox" );
      *   String resolvedString = StringUtils.interpolate( templateString, valuesMap, true );
-     *
- * yielding: - *
+     * 
+ * yielding: + *
      *   The quick brown fox jumped over the lazy dog.
-     *
- *

- * yielding: - *
-     *   The cow jumped over the moon.
-     *
- *

- *

- * The value of templateString is returned in an unaltered form if - * templateString is null, empty, or - * contains no marked variables that can be resolved by the key/value pairs found in - * valuesMap, or if valuesMap is null, empty or has no key/value - * pairs that can be applied to the marked variables within templateString. - *

- * @param templateString String containing any mixture of variable and non-variable + *

+ * + *

The value of templateString is returned in an unaltered + * if templateString is null, empty, or contains no marked variables + * that can be resolved by the key/value pairs found in valuesMap, + * or if valuesMap is null, empty or has no key/value pairs that can be + * applied to the marked variables within templateString.

+ * + *

If a valuesMap value is null, it will be treated as "".

+ * + * @param templateString String containing any mixture of variable and non-variable * content, to be used as a template for the value substitution process - * @param valuesMap Map containing the key/value pairs to be used to resolve + * @param valuesMap Map containing the key/value pairs to be used to resolve * the values of the marked variables found within templateString - * @return String + * @return the interpolated String */ - public static String interpolateRepeatedly( - String templateString, - Map valuesMap) - { + public static String interpolateRepeatedly(String templateString, Map valuesMap) { // pre-conditions - if ( valuesMap == null ) + if (templateString == null || valuesMap == null || + templateString.length() == 0 || valuesMap.isEmpty()) { return templateString; - if ( templateString == null ) - return templateString; - if ( templateString.length() < 1 ) - return templateString; - if ( valuesMap.isEmpty() ) - return templateString; - + } + String currentResult = templateString; String previousResult = null; - while( ! StringUtils.equals( currentResult, previousResult ) ) - { + while (!StringUtils.equals(currentResult, previousResult)) { previousResult = currentResult; - currentResult = Interpolation.interpolate( previousResult, valuesMap ); + currentResult = Interpolation.interpolate(previousResult, valuesMap); } - + return currentResult; }