Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 74668 invoked from network); 9 Aug 2005 11:12:34 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 9 Aug 2005 11:12:34 -0000 Received: (qmail 37998 invoked by uid 500); 9 Aug 2005 11:12:29 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 37936 invoked by uid 500); 9 Aug 2005 11:12:28 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 37923 invoked by uid 99); 9 Aug 2005 11:12:28 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 09 Aug 2005 04:12:28 -0700 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_HELO_PASS,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: domain of tomAtLinux@gmx.at designates 213.165.64.20 as permitted sender) Received: from [213.165.64.20] (HELO mail.gmx.net) (213.165.64.20) by apache.org (qpsmtpd/0.29) with SMTP; Tue, 09 Aug 2005 04:12:51 -0700 Received: (qmail invoked by alias); 09 Aug 2005 11:12:25 -0000 Received: from mail.bestsolution.at (EHLO [192.168.100.12]) [83.64.113.2] by mail.gmx.net (mp025) with SMTP; 09 Aug 2005 13:12:25 +0200 X-Authenticated: #6137719 Message-ID: <42F88F99.6050806@gmx.at> Date: Tue, 09 Aug 2005 13:12:25 +0200 From: Tom Schindl User-Agent: Mozilla Thunderbird 1.0.2 (X11/20050322) X-Accept-Language: de-DE, de, en-us, en MIME-Version: 1.0 To: Jakarta Commons Developers List Subject: Re: [lang] Questions concerning VariableFormatter References: <42EFC077.80004@gmx.at> <42F1C34D.2070905@btopenworld.com> <42F1E9C4.50206@gmx.at> <42F4E98F.10600@t-online.de> In-Reply-To: <42F4E98F.10600@t-online.de> X-Enigmail-Version: 0.90.0.0 X-Enigmail-Supports: pgp-inline, pgp-mime Content-Type: multipart/mixed; boundary="------------000708010203090003090406" X-Y-GMX-Trusted: 0 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N --------------000708010203090003090406 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Oliver Heger schrieb: > Tom Schindl wrote: > >> Stephen Colebourne schrieb: >> >> >Please prefix emails by [lang] > [...] > An alternative would be to provide an additional implementation of the > VariableResolver interface. The default implementation stays as is and > does not handle formats. An extended implementation could support > further arguments that are appended to variables. > > Oliver so i did now and added a new resolver based upon the existing one. What this patch does: - - added a new Resolver named MapVariableResolverWithFormats - - static functions to turn Formats on/off => default methods changed to use Formats - - new constructors Hope that's better than my first try. I didn't have a enough time to look closer at how to make VariableFormatter extend Format which I'd desire most so that the interface between MessageFormat and VariableFormatter is equal. > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org > For additional commands, e-mail: commons-dev-help@jakarta.apache.org > > -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFC+I+YkVPeOFLgZFIRAlszAJsGCQ7fdUnKtsho4rEvxliptJMQTACeKv1d U1wwIvsx++Bw6Rn65CD7yCg= =5lYH -----END PGP SIGNATURE----- --------------000708010203090003090406 Content-Type: text/plain; name="patch.txt" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="patch.txt" Index: E:/eclipse-workspaces/beso-internal-devel/jakarta-lang/src/java/org/apache/commons/lang/text/VariableFormatter.java =================================================================== --- E:/eclipse-workspaces/beso-internal-devel/jakarta-lang/src/java/org/apache/commons/lang/text/VariableFormatter.java (revision 231018) +++ E:/eclipse-workspaces/beso-internal-devel/jakarta-lang/src/java/org/apache/commons/lang/text/VariableFormatter.java (working copy) @@ -16,6 +16,7 @@ package org.apache.commons.lang.text; +import java.text.MessageFormat; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -175,6 +176,48 @@ Object resolveVariable(String varName); } + /** + *

+ * A VariableResolver backed by a {@link Map} who uses Formats like MessageFormat does + *

+ * @author Tom Schindl + * @version $Id$ + */ + public static class MapVariableResolverWithFormats extends MapVariableResolver { + + /** + * Creates a new variable resolver to handle formats like + * {@link 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 + * {@link 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); + } + } + } + /** Constant for the default escape character. */ static final char DEFAULT_ESCAPE = '$'; @@ -185,37 +228,95 @@ static final String DEFAULT_SUFFIX = "}"; /** - * 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. Formats are applied + * automatically if you have defined them in your variable definition. + * + * @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 replace(valueMap, source, true); + } - /** - * 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 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 + * @param withFormats + * format the values replaced using format informations. This may + * slow down the process of replacing a little bit so if you + * don't need it turn it of + * @return the result of the replace operation + */ + public static String replace(Map valueMap, Object source, + boolean withFormats) { + if (withFormats) { + return new VariableFormatter(new MapVariableResolverWithFormats( + valueMap)).replace(source); + } else { + 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. Formats are applied + * automatically if you have defined them in your variable definition. + * + * @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 replace(valueMap, prefix, suffix, source, true); + } + + /** + * 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 + * @param withFormats + * format the values replaced using format informations. This may + * slow down the process of replacing a little bit so if you + * don't need it turn it of + * @return the result of the replace operation + */ + public static String replace(Map valueMap, String prefix, String suffix, + Object source, boolean withFormats) { + if (withFormats) { + return new VariableFormatter(new MapVariableResolverWithFormats( + valueMap), prefix, suffix).replace(source); + } else { + return new VariableFormatter(valueMap, prefix, suffix) + .replace(source); + } + } + /** * Replaces all variables in the given source data with values obtained from system properties. * @@ -251,6 +352,17 @@ public VariableFormatter(Map valueMap) { this(valueMap, 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 variableResolver + * the resolver used to map variable names + */ + public VariableFormatter(VariableResolver variableResolver) { + this(variableResolver, DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_ESCAPE); + } /** * Creates a new instance with defaults for variable prefix and suffix and the escaping character. @@ -274,6 +386,22 @@ } /** + * Creates a new instance and initializes it. Uses a default escaping + * character. + * + * @param variableResolver + * the resolver used to map variable names + * @param prefix + * the prefix for variables + * @param suffix + * the suffix for variables + */ + public VariableFormatter(VariableResolver variableResolver, String prefix, + String suffix) { + this(variableResolver, prefix, suffix, DEFAULT_ESCAPE); + } + + /** * Creates a new instance and initializes it. * * @param valueMap --------------000708010203090003090406 Content-Type: text/plain; charset=us-ascii --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org --------------000708010203090003090406--