Re: [lang] Questions concerning VariableFormatter
-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 { /** * * A VariableResolver backed by a [EMAIL PROTECTED] 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 * [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 oper
Re: [lang] Questions concerning VariableFormatter
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Gary Gregory wrote: > Hello Tom: > > Please provide unit tests. I want to see this code in action in order to > figure out what the intent of the class really is. > > Thanks, > Gary > > No. A very limited one but the idea should be clear ;-) -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.0 (GNU/Linux) Comment: Using GnuPG with Mandriva - http://enigmail.mozdev.org iD8DBQFDAlHXkVPeOFLgZFIRAqbuAJ4ulPg3hGUkiCbWNunfNW+mtEUN0wCfQM/Q XEZ2JafiiJF66IDnlfJ5adg= =tD9P -END PGP SIGNATURE- import java.util.Date; import java.util.HashMap; import java.util.Locale; import org.apache.commons.lang.text.VariableFormatterWithFormating; import junit.framework.TestCase; public class VariableFormatterWithFormatingTest extends TestCase { /* * Test method for 'org.apache.commons.lang.text.VariableFormatterWithFormating.replace(Map, Object)' */ public void testReplaceMapObject() { HashMap map = new HashMap(); map.put("date",new Date(0)); map.put("number",new Double(100.2)); Locale.setDefault(Locale.US); String message = new String("On ${date,date,-mm-dd} I earned ${number,number,currency}"); assertEquals("On 1970-00-01 I earned $100.20",VariableFormatterWithFormating.replace(map,message)); } } - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: [lang] Questions concerning VariableFormatter
Hello Tom: Please provide unit tests. I want to see this code in action in order to figure out what the intent of the class really is. Thanks, Gary > -Original Message- > From: Tom Schindl [mailto:[EMAIL PROTECTED] > Sent: Tuesday, August 16, 2005 1:26 PM > To: Jakarta Commons Developers List > Subject: Re: [lang] Questions concerning VariableFormatter > > -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- - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [lang] Questions concerning VariableFormatter
-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 { /** * * A VariableResolver backed by a [EMAIL PROTECTED] 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 * [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
RE: [lang] Questions concerning VariableFormatter
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 > -Original Message- > From: Gary Gregory [mailto:[EMAIL PROTECTED] > Sent: Tuesday, August 16, 2005 9:36 AM > To: Jakarta Commons Developers List > Subject: RE: [lang] Questions concerning VariableFormatter > > Hello Tom: > > I'm very busy for the next 2 weeks with my real job, so I cannot > guarantee anything. Feel free to post and I or someone will hopefully > answer in a more or less timely manner ;-) > > Gary > > > -Original Message- > > From: Tom Schindl [mailto:[EMAIL PROTECTED] > > Sent: Tuesday, August 16, 2005 3:23 AM > > To: Jakarta Commons Developers List > > Subject: Re: [lang] Questions concerning VariableFormatter > > > > -BEGIN PGP SIGNED MESSAGE- > > Hash: SHA1 > > > > Gary Gregory wrote: > > > In a day or so, a new version will be in SVN, and I should be able > to > > > chat about this other feature. Thanks for your patience. > > > > > > Gary > > > > Hi, > > > > I saw that a new version of VariableFormatter is in SVN. What I've > done > > now is to subclass it. Do you have time to chat about it? > > > > Tom > > -BEGIN PGP SIGNATURE- > > Version: GnuPG v1.4.0 (GNU/Linux) > > Comment: Using GnuPG with Mandriva - http://enigmail.mozdev.org > > > > iD8DBQFDAb55kVPeOFLgZFIRAvEFAJ9mpPdYRXipADcE5sKgV29jtUEZTwCeNIeJ > > 6KbSURSEaFqLvK16og1+hGg= > > =eHcW > > -END PGP SIGNATURE- > > > > - > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > > > > - > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: [lang] Questions concerning VariableFormatter
Hello Tom: I'm very busy for the next 2 weeks with my real job, so I cannot guarantee anything. Feel free to post and I or someone will hopefully answer in a more or less timely manner ;-) Gary > -Original Message- > From: Tom Schindl [mailto:[EMAIL PROTECTED] > Sent: Tuesday, August 16, 2005 3:23 AM > To: Jakarta Commons Developers List > Subject: Re: [lang] Questions concerning VariableFormatter > > -BEGIN PGP SIGNED MESSAGE- > Hash: SHA1 > > Gary Gregory wrote: > > In a day or so, a new version will be in SVN, and I should be able to > > chat about this other feature. Thanks for your patience. > > > > Gary > > Hi, > > I saw that a new version of VariableFormatter is in SVN. What I've done > now is to subclass it. Do you have time to chat about it? > > Tom > -BEGIN PGP SIGNATURE- > Version: GnuPG v1.4.0 (GNU/Linux) > Comment: Using GnuPG with Mandriva - http://enigmail.mozdev.org > > iD8DBQFDAb55kVPeOFLgZFIRAvEFAJ9mpPdYRXipADcE5sKgV29jtUEZTwCeNIeJ > 6KbSURSEaFqLvK16og1+hGg= > =eHcW > -END PGP SIGNATURE- > > - > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [lang] Questions concerning VariableFormatter
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Gary Gregory wrote: > In a day or so, a new version will be in SVN, and I should be able to > chat about this other feature. Thanks for your patience. > > Gary Hi, I saw that a new version of VariableFormatter is in SVN. What I've done now is to subclass it. Do you have time to chat about it? Tom -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.0 (GNU/Linux) Comment: Using GnuPG with Mandriva - http://enigmail.mozdev.org iD8DBQFDAb55kVPeOFLgZFIRAvEFAJ9mpPdYRXipADcE5sKgV29jtUEZTwCeNIeJ 6KbSURSEaFqLvK16og1+hGg= =eHcW -END PGP SIGNATURE- - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: [lang] Questions concerning VariableFormatter
In a day or so, a new version will be in SVN, and I should be able to chat about this other feature. Thanks for your patience. Gary > -Original Message- > From: Tom Schindl [mailto:[EMAIL PROTECTED] > Sent: Tuesday, August 09, 2005 9:44 AM > To: Jakarta Commons Developers List > Subject: Re: [lang] Questions concerning VariableFormatter > > -BEGIN PGP SIGNED MESSAGE- > Hash: SHA1 > > Gary Gregory schrieb: > > Hi Tom: > > > > I'm in the middle of work and integrating a rework from Oliver on free > > time, so I cannot look at this at the moment. I'll try to peek tonight. > > > > Cheers, > > Gary > > > > > > [...] > > no problem I simply need some pointers how I should go on to get into > VariableFormatter what I think would make it useable as a real > replacement for MessageFormat. > > Thanks > > Tom > > > - > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > > > -BEGIN PGP SIGNATURE- > Version: GnuPG v1.4.0 (GNU/Linux) > Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org > > iD8DBQFC+N1FkVPeOFLgZFIRAigSAJ4g1lzB+X7srfdrosYmeuXb0hFvcQCfaeOw > sbEKMN2pEn3EXAqdFq08W8k= > =Kib9 > -END PGP SIGNATURE- > > - > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [lang] Questions concerning VariableFormatter
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Gary Gregory schrieb: > Hi Tom: > > I'm in the middle of work and integrating a rework from Oliver on free > time, so I cannot look at this at the moment. I'll try to peek tonight. > > Cheers, > Gary > > [...] no problem I simply need some pointers how I should go on to get into VariableFormatter what I think would make it useable as a real replacement for MessageFormat. Thanks Tom > - > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.0 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFC+N1FkVPeOFLgZFIRAigSAJ4g1lzB+X7srfdrosYmeuXb0hFvcQCfaeOw sbEKMN2pEn3EXAqdFq08W8k= =Kib9 -END PGP SIGNATURE- - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: [lang] Questions concerning VariableFormatter
Hi Tom: I'm in the middle of work and integrating a rework from Oliver on free time, so I cannot look at this at the moment. I'll try to peek tonight. Cheers, Gary > -Original Message- > From: Tom Schindl [mailto:[EMAIL PROTECTED] > Sent: Tuesday, August 09, 2005 8:52 AM > To: Jakarta Commons Developers List > Subject: Re: [lang] Questions concerning VariableFormatter > > -BEGIN PGP SIGNED MESSAGE- > Hash: SHA1 > > Gary Gregory schrieb: > > Hello: > [...] > > > Simon's full message is here: > > http://www.mail-archive.com/commons-dev@jakarta.apache.org/msg63493.html > > > > Gary > > > > Ok. Thanks for clearing up this so what's with the proposed formating of > substiution values. Is the way I tried it now acceptable? If not could > anybody tell me how a patch to VariableFormatter should look like to be > acceptable? > > Tom > -BEGIN PGP SIGNATURE- > Version: GnuPG v1.4.0 (GNU/Linux) > Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org > > iD8DBQFC+NEakVPeOFLgZFIRAmanAKCcs/7eGl/B0FzB40i+Cl0Q3yVFAwCgmSiG > Y8kiQUjvKWEyUwpfYnfdKSE= > =j3kd > -END PGP SIGNATURE- > > - > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [lang] Questions concerning VariableFormatter
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Gary Gregory schrieb: > Hello: [...] > Simon's full message is here: > http://www.mail-archive.com/commons-dev@jakarta.apache.org/msg63493.html > > Gary > Ok. Thanks for clearing up this so what's with the proposed formating of substiution values. Is the way I tried it now acceptable? If not could anybody tell me how a patch to VariableFormatter should look like to be acceptable? Tom -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.0 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFC+NEakVPeOFLgZFIRAmanAKCcs/7eGl/B0FzB40i+Cl0Q3yVFAwCgmSiG Y8kiQUjvKWEyUwpfYnfdKSE= =j3kd -END PGP SIGNATURE- - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: [lang] Questions concerning VariableFormatter
Hello: Currently, the class VariableFormatter is called a "Formatter" and not a "Format" to make it clear that the implementation is not based on the JRE Format class. As Simon Kitching on Tue, 05 Jul 2005: " MessageFormat is based on defining a template "My {0} is {1}.", then evaulating this for different values of {0} and {1}. The (proposed) VariableFormat is based on defining a source of variable info (Map), then evaulating different template strings against that map. I think the current VariableFormat approach is the right approach for mapped data but this difference implies that care should be taken when drawing any parallels between these classes. I think this is what Gary meant above; that if this class cannot be implemented as a subclass of java.text.MessageFormat (or at least a subclass of java.text.Format) then it shouldn't have Format in the name. And it doesn't seem that it can (or should) do this, so I would like to see whatever solution is agreed on avoid the name "Format". " Simon's full message is here: http://www.mail-archive.com/commons-dev@jakarta.apache.org/msg63493.html Gary > -Original Message- > From: Tom Schindl [mailto:[EMAIL PROTECTED] > Sent: Tuesday, August 09, 2005 4:12 AM > To: Jakarta Commons Developers List > Subject: Re: [lang] Questions concerning VariableFormatter > > -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: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > > > > > -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- - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [lang] Questions concerning VariableFormatter
-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: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > -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- 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 [EMAIL PROTECTED] Map} who uses Formats like MessageFormat does + * + * @author mailto:[EMAIL PROTECTED]">Tom Schindl + * @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); + } + } +} + /** 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
Re: [lang] Questions concerning VariableFormatter
Tom Schindl wrote: Stephen Colebourne schrieb: >Please prefix emails by [lang] >Tom Schindl wrote: >>I just found out that this project has exactly what I was searching >>for long time because MessageFormat doesn't really suite my needs. >> >>I see that this Class has been added in 2.2: >> >>a. Is there any plan when 2.2 is released >When its ready ;-) No firm dates, but work rate is pretty high at present. >>b. What would be with adding formats applied to values like >>MessageFormat does it {myDate,date,short}. The attached patch does >>that simply using MessageFormat. >I agree that this is a good idea if done carefully. However we have no >plans to do this at present. You might want to propose some >suggestions/code separately. Isn't the proposed patch appended to the last mail what could be done to stay as compatible with MessageFormat as possible? Maybe a flag to VariableFormatter could flag whether a search for formats should be done so users not using formats are not hurt by the search. 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 - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [lang] Questions concerning VariableFormatter
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Stephen Colebourne schrieb: > Please prefix emails by [lang] > > Tom Schindl wrote: > >> I just found out that this project has exactly what I was searching >> for long time because MessageFormat doesn't really suite my needs. >> >> I see that this Class has been added in 2.2: >> >> a. Is there any plan when 2.2 is released > > When its ready ;-) No firm dates, but work rate is pretty high at present. > >> b. What would be with adding formats applied to values like >> MessageFormat does it {myDate,date,short}. The attached patch does >> that simply using MessageFormat. > > I agree that this is a good idea if done carefully. However we have no > plans to do this at present. You might want to propose some > suggestions/code separately. Isn't the proposed patch appended to the last mail what could be done to stay as compatible with MessageFormat as possible? Maybe a flag to VariableFormatter could flag whether a search for formats should be done so users not using formats are not hurt by the search. > >> c. Are there any planned changes to VariableFormatter to make it more >> look like MessageFormat? > > Not at present, although its my personal opinion that it should extend > Format before 2.2 release. You could help code this. I'll investigate on this. > > Stephen > > - > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.0 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFC8enEkVPeOFLgZFIRApONAJ448NB1ptbeKCPySkIMkWBjMAE+3gCfXkWv KW4U6CHnKZIkUIBMV2hefcs= =/ryH -END PGP SIGNATURE- - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [lang] Questions concerning VariableFormatter
Please prefix emails by [lang] Tom Schindl wrote: I just found out that this project has exactly what I was searching for long time because MessageFormat doesn't really suite my needs. I see that this Class has been added in 2.2: a. Is there any plan when 2.2 is released When its ready ;-) No firm dates, but work rate is pretty high at present. b. What would be with adding formats applied to values like MessageFormat does it {myDate,date,short}. The attached patch does that simply using MessageFormat. I agree that this is a good idea if done carefully. However we have no plans to do this at present. You might want to propose some suggestions/code separately. c. Are there any planned changes to VariableFormatter to make it more look like MessageFormat? Not at present, although its my personal opinion that it should extend Format before 2.2 release. You could help code this. Stephen - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Questions concerning VariableFormatter
Hi, I just found out that this project has exactly what I was searching for long time because MessageFormat doesn't really suite my needs. I see that this Class has been added in 2.2: a. Is there any plan when 2.2 is released b. What would be with adding formats applied to values like MessageFormat does it {myDate,date,short}. The attached patch does that simply using MessageFormat. c. Are there any planned changes to VariableFormatter to make it more look like MessageFormat? Thanks Tom 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 226795) +++ 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; @@ -139,7 +140,14 @@ if (this.getMap() == null) { return null; } -return this.getMap().get(varName); + +int index; + +if( (index = varName.indexOf(",")) != -1 ) { + return MessageFormat.format("{0"+varName.substring(index)+"}",new Object[] { this.getMap().get(varName.substring(0,index)) } ); +} else { + return this.getMap().get(varName); +} } /** - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]