commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tom Schindl <lis...@bestsolution.at>
Subject Re: [lang] VariableFormatter - pre 2.2
Date Wed, 02 Aug 2006 07:51:03 GMT
Where should I put my feature request should I:
- append it to VariableFormatter's-Jira-Entry
- create a new Jira-Entry for StrSubstitutor

Tom

Tom Schindl schrieb:
> Hi,
> 
> Looks good to me. Maybe for 2.3 this patch could provide
> MessageFormatting like proposed before without bloating the API. Does
> this now mean that VariableFormatter is gone? I like the nameing
> StrSubstitutor better.
> 
> Tom
> 
> Henri Yandell schrieb:
>> On 7/23/06, Stephen Colebourne <scolebourne@btopenworld.com> wrote:
>>> I have reworked the VariableFormatter class along the lines that I
>>> was  thinking. I have committed it as StrSubstitutor so it doesn't
>>> clash for  the moment and so it can be easiy reviewed.
>>>
>>>  This version does not have a separate parser class, but still
>>> supports  escaping, and matchers for prefix/suffix (which can now be
>>> set by  users). The new class should perform better as a result.
>>>
>>>  I have removed the edge cases wrt resolving Objects, as they were
>>> rather   ill-defined.
>>>
>>>  Otherwise, the basic functionality it supported, and the test case
>>> is  slightly enlarged. I still want to break out the resolver as a
>>> public  abstract class before release.
>>>
>>>  Opinions
>> Oliver, Gary, Tom?
>>
>> Looking to get Lang 2.2 moving out the door and this is the only blocker.
>>
>> Hen
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>>
>>
> 
> 
> ------------------------------------------------------------------------
> 
> Index: /home.local/tom/projects/eclipse-ws/bidi-forms/jakarta-commons-lang/src/java/org/apache/commons/lang/text/StrSubstitutor.java
> ===================================================================
> --- /home.local/tom/projects/eclipse-ws/bidi-forms/jakarta-commons-lang/src/java/org/apache/commons/lang/text/StrSubstitutor.java
(revision 427468)
> +++ /home.local/tom/projects/eclipse-ws/bidi-forms/jakarta-commons-lang/src/java/org/apache/commons/lang/text/StrSubstitutor.java
(working copy)
> @@ -16,6 +16,7 @@
>  
>  package org.apache.commons.lang.text;
>  
> +import java.text.MessageFormat;
>  import java.util.ArrayList;
>  import java.util.List;
>  import java.util.Map;
> @@ -725,5 +726,84 @@
>              return obj.toString();
>          }
>      }
> +    
> +    /**
> +     * Looks up a string value by name using a {@link Map} and can use format 
> +     * expressions known from {@link MessageFormat}
> +     */
> +    static class MapVariableResolverFormat extends MapVariableResolver {
> +
> +		MapVariableResolverFormat(Map map) {
> +			super(map);
> +		}
> +    	
> +		public String resolveVariable(String varName) {
> +			int index;
> +			
> +			if (map != null && (index = varName.indexOf(",")) != -1) {
> +    			return MessageFormat.format("{0" + varName.substring(index)
> +    					+ "}", new Object[] { map.get(
> +    							varName.substring(0, index)) });
> +    		} else {
> +    			return super.resolveVariable(varName);
> +    		}
> +		}
> +    }
> +    
> +    /**
> +     * Substitutes variables by value and allows to use the formats known from 
> +     * {@link MessageFormat}:
> +     * <p>
> +     * The following example demonstrates this:
> +     * </p>
> +     * <pre>
> +     * Map map = new HashMap();
> +     * map.put("num",new Double(1000.103));
> +     * map.put("date", new SimpleDateFormat("yyyy-MM-dd").parse("1970-01-01"));
> +     * 
> +     * StrSubstitutor.Format.replace("Number: ${num,number,integer}, Date: ${date,date,yyyy-MM-dd}",
map)
> +     * </pre>
> +     * yielding in Locale.US
> +     * <pre>
> +     * Number: 1,000, Date: 1970-01-01
> +     * </pre>
> +     */
> +    public static class Format {
> +    	private Format() {}
> +    	
> +        /**
> +         * Replaces all the occurrences of variables in the given source object with
> +         * their matching values from the map.
> +         *
> +         * @param source  the source text containing the variables to substitute
> +         * @param valueMap  the map with the values
> +         * @return the result of the replace operation
> +         */
> +        public static String replace(Object source, Map valueMap) {
> +        	StrSubstitutor tmp = new StrSubstitutor();
> +        	tmp.setVariableResolver(new MapVariableResolverFormat(valueMap));
> +        	
> +            return tmp.replace(source);
> +        }
>  
> +        /**
> +         * Replaces all the occurrences of variables in the given source object with
> +         * their matching values from the map. This method allows to specifiy a
> +         * custom variable prefix and suffix
> +         *
> +         * @param source  the source text containing the variables to substitute
> +         * @param valueMap  the map with the values
> +         * @param prefix  the prefix of variables
> +         * @param suffix  the suffix of variables
> +         * @return the result of the replace operation
> +         */
> +        public static String replace(Object source, Map valueMap, String prefix, String
suffix) {
> +        	StrSubstitutor tmp = new StrSubstitutor();
> +        	tmp.setVariablePrefix(prefix);
> +        	tmp.setVariableSuffix(suffix);
> +        	tmp.setVariableResolver(new MapVariableResolverFormat(valueMap));
> +        	
> +            return tmp.replace(source);
> +        }
> +    }
>  }
> Index: /home.local/tom/projects/eclipse-ws/bidi-forms/jakarta-commons-lang/src/test/org/apache/commons/lang/text/StrSubstitutorTest.java
> ===================================================================
> --- /home.local/tom/projects/eclipse-ws/bidi-forms/jakarta-commons-lang/src/test/org/apache/commons/lang/text/StrSubstitutorTest.java
(revision 427468)
> +++ /home.local/tom/projects/eclipse-ws/bidi-forms/jakarta-commons-lang/src/test/org/apache/commons/lang/text/StrSubstitutorTest.java
(working copy)
> @@ -16,7 +16,10 @@
>  
>  package org.apache.commons.lang.text;
>  
> +import java.text.ParseException;
> +import java.text.SimpleDateFormat;
>  import java.util.HashMap;
> +import java.util.Locale;
>  import java.util.Map;
>  
>  import junit.framework.Test;
> @@ -24,8 +27,6 @@
>  import junit.framework.TestSuite;
>  import junit.textui.TestRunner;
>  
> -import org.apache.commons.lang.text.StrSubstitutor.MapVariableResolver;
> -
>  /**
>   * Test class for StrSubstitutor.
>   * 
> @@ -332,6 +333,26 @@
>          }
>      }
>  
> +    public void testWithFormating() {
> +    	Locale tmp = Locale.getDefault();
> +    	
> +    	try {
> +    		Map map = new HashMap();
> +        	map.put("num",new Double(1000.103));
> +			map.put("date", new SimpleDateFormat("yyyy-MM-dd").parse("1970-01-01"));
> +			
> +	    	Locale.setDefault(Locale.US);
> +	    	
> +	    	String value = StrSubstitutor.Format.replace("Number: ${num,number,integer}, Date:
${date,date,yyyy-MM-dd}", map);
> +	    	assertEquals("Number: 1,000, Date: 1970-01-01", value);
> +	    	
> +		} catch (ParseException e) {
> +			fail("Parsing failed");
> +		} finally {
> +			Locale.setDefault(tmp);
> +		}
> +    }
> +    
>      /**
>       * Tests interpolation with weird boundary patterns.
>       */
> 
> 
> 
> ------------------------------------------------------------------------
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message