-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Gary Gregory wrote:
> Tom:
> 
> If you'd like to make things easier for me, what you could do is
> resubmit your patches against the current code base. I'll try to take a
> look tonight or tomorrow night.
> 
> Thanks,
> Gary
> 

Hi Gary,

I haven't patch for the latest SVN but I have a class which extends
VariableFormatter. It hasn't got a good name but it should show what I'm
trying to achieve.

Tom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)
Comment: Using GnuPG with Mandriva - http://enigmail.mozdev.org

iD8DBQFDAkvokVPeOFLgZFIRAkleAJ9DGNXcfZogEioPOG8gXYSuW7m0LQCdGlcx
q8lv86JylSuRlZdAsDvqmag=
=Zfpj
-----END PGP SIGNATURE-----
package org.apache.commons.lang.text;

import java.text.MessageFormat;
import java.util.Map;



public class VariableFormatterWithFormating extends VariableFormatter {
	/**
     * <p>
     * A VariableResolver backed by a [EMAIL PROTECTED] Map} who uses Formats like MessageFormat does
     * </p>
     * @author <a href="mailto:[EMAIL PROTECTED]">Tom Schindl</a> 
     * @version $Id: $
     */
    public static class MapVariableResolverWithFormats extends MapVariableResolver {
    	
    	/**
    	 * Creates a new variable resolver to handle formats like
    	 * [EMAIL PROTECTED] MessageFormat} does it. You can use e.g. {today,date,short} to
    	 * format the variable today as a short date
    	 * 
    	 * @param map
    	 *            The variable names and values.
    	 */
    	public MapVariableResolverWithFormats(Map map) {
    		super(map);
    	}
    	
    	/**
    	 * Resolves the variable and formats the object retrieved using
    	 * [EMAIL PROTECTED] MessageFormat}
    	 * 
    	 * @param varName
    	 *            name of the variable and optionally the data-type and
    	 *            data-format seperated by comma
    	 */
    	public Object resolveVariable(String varName) {
    		int index;
    		
    		if (this.getMap() != null && (index = varName.indexOf(",")) != -1) {
    			return MessageFormat.format("{0" + varName.substring(index)
    					+ "}", new Object[] { this.getMap().get(
    							varName.substring(0, index)) });
    		} else {
    			return super.resolveVariable(varName);
    		}
    	}
    }
    
    /**
     * Creates a new instance with defaults for variable prefix and suffix and the escaping character.
     */
    public VariableFormatterWithFormating() {
        this((VariableResolver) null, DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_ESCAPE);
    }

    /**
     * Creates a new instance and initializes it. Uses defaults for variable prefix and suffix and the escaping
     * character.
     * 
     * @param valueMap
     *            the map with the variables' values
     */
    public VariableFormatterWithFormating(Map valueMap) {
        this(valueMap, DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_ESCAPE);
    }

    /**
     * Creates a new instance and initializes it. Uses a default escaping character.
     * 
     * @param valueMap
     *            the map with the variables' values
     * @param prefix
     *            the prefix for variables
     * @param suffix
     *            the suffix for variables
     */
    public VariableFormatterWithFormating(Map valueMap, String prefix, String suffix) {
        this(valueMap, prefix, suffix, DEFAULT_ESCAPE);
    }

    /**
     * Creates a new instance and initializes it.
     * 
     * @param valueMap
     *            the map with the variables' values
     * @param prefix
     *            the prefix for variables
     * @param suffix
     *            the suffix for variables
     * @param escape
     *            the escape character
     */
    public VariableFormatterWithFormating(Map valueMap, String prefix, String suffix, char escape) {
        this(new MapVariableResolverWithFormats(valueMap), prefix, suffix, escape);
    }

    /**
     * Creates a new instance and initializes it.
     * 
     * @param variableResolver
     *            the variable resolver
     * @param prefix
     *            the prefix for variables
     * @param suffix
     *            the suffix for variables
     * @param escape
     *            the escape character
     */
    public VariableFormatterWithFormating(VariableResolver variableResolver, String prefix, String suffix, char escape) {
        super(variableResolver,prefix,suffix,escape);
    }
    
    /**
     * Replaces the occurrences of all variables in the given source data by their current values obtained from the
     * passed in map.
     * 
     * @param valueMap
     *            the map with the values
     * @param source
     *            the source text
     * @return the result of the replace operation
     */
    public static String replace(Map valueMap, Object source) {
        return new VariableFormatter(valueMap).replace(source);
    }
    
    /**
     * Replaces the occurrences of all variables in the given source data by their current values obtained from the
     * passed in map. This method allows to specifiy a custom variable prefix and suffix
     * 
     * @param valueMap
     *            the map with the values
     * @param prefix
     *            the prefix of variables
     * @param suffix
     *            the suffix of variables
     * @param source
     *            the source text
     * @return the result of the replace operation
     */
    public static String replace(Map valueMap, String prefix, String suffix, Object source) {
        return new VariableFormatter(valueMap, prefix, suffix).replace(source);
    }

    /**
     * Replaces all variables in the given source data with values obtained from system properties.
     * 
     * @param source
     *            the source text
     * @return the result of the replace operation
     */
    public static String replaceSystemProperties(Object source) {
        return new VariableFormatter(System.getProperties()).replace(source);
    }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to