-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 oh. and the last VariableFormatter now really working. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (GNU/Linux) Comment: Using GnuPG with Mandriva - http://enigmail.mozdev.org
iD8DBQFDAlIUkVPeOFLgZFIRAp9PAJ9/VFwjRFT62dF+dmREvbsHV3L0pQCgrYmm g2j04O/D+oOmFZwT9jxs/WA= =Ij5q -----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 VariableFormatterWithFormating(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 VariableFormatterWithFormating(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 VariableFormatterWithFormating(System.getProperties()).replace(source); } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]