Dev - proposal to add feature to flexible string expander

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Dev - proposal to add feature to flexible string expander

Hans Bakker
Good morning,

currently it is not possible to do currency localization on currency values
displayed in the label statement in a screen widget and every where the
flexible string expander is used.

attached a patch to enable the following code which displays the amount of a
payment taking into account the current locale and requested currency. The
implementation is similar to the 'bsh:' prefix.

${currency=EUR:payment.amount}
or
${currency=${payment.currencyUomId}:payment.amount}

will display as: € 40,33
when the currency is set to 'EUR' and the locale is dutch.

any opinions?

--
Regards,
Hans Bakker
ANT Websystems Co.,Ltd (http://www.antwebsystems.com)

If you want to verify that this message really originates from
from the above person, download the public key from:
http://www.antwebsystems.com/hbakkerAntwebsystems.asc

 
_______________________________________________
Dev mailing list
[hidden email]
http://lists.ofbiz.org/mailman/listinfo/dev

flexCurr.patch (9K) Download Attachment
attachment1 (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Dev - proposal to add feature to flexible string expander

David E. Jones

I think this sort of change is fine, but perhaps with a syntax closer to that of FreeMarker it will be easier for people to use/remember. Something like the "?" separator they use for built-ins. So it might look something like:

> ${payment.amount?currency(EUR)}
> or
> ${payment.amount?currency(${payment.currencyUomId})}

Of course, as we make this more complex eventually we're going to want a more formal parser than just doing String compares and such... I guess we can table that for later though.

-David


Hans Bakker wrote:

> Good morning,
>
> currently it is not possible to do currency localization on currency values
> displayed in the label statement in a screen widget and every where the
> flexible string expander is used.
>
> attached a patch to enable the following code which displays the amount of a
> payment taking into account the current locale and requested currency. The
> implementation is similar to the 'bsh:' prefix.
>
> ${currency=EUR:payment.amount}
> or
> ${currency=${payment.currencyUomId}:payment.amount}
>
> will display as: € 40,33
> when the currency is set to 'EUR' and the locale is dutch.
>
> any opinions?
>
>
>
> ------------------------------------------------------------------------
>
> Index: base/src/base/org/ofbiz/base/util/string/FlexibleStringExpander.java
> ===================================================================
> --- base/src/base/org/ofbiz/base/util/string/FlexibleStringExpander.java (revision 7344)
> +++ base/src/base/org/ofbiz/base/util/string/FlexibleStringExpander.java (working copy)
> @@ -1,25 +1,19 @@
>  /*
>   * $Id$
>   *
> - *  Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
> - *
> - *  Permission is hereby granted, free of charge, to any person obtaining a
> - *  copy of this software and associated documentation files (the "Software"),
> - *  to deal in the Software without restriction, including without limitation
> - *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
> - *  and/or sell copies of the Software, and to permit persons to whom the
> - *  Software is furnished to do so, subject to the following conditions:
> - *
> - *  The above copyright notice and this permission notice shall be included
> - *  in all copies or substantial portions of the Software.
> - *
> - *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
> - *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
> - *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
> - *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
> - *  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
> - *  OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
> - *  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
> + * Copyright 2001-2006 The Apache Software Foundation
> + *
> + * Licensed under the Apache License, Version 2.0 (the "License"); you may not
> + * use this file except in compliance with the License. You may obtain a copy of
> + * the License at
> + *
> + * http://www.apache.org/licenses/LICENSE-2.0
> + *
> + * Unless required by applicable law or agreed to in writing, software
> + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
> + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
> + * License for the specific language governing permissions and limitations
> + * under the License.
>   */
>  package org.ofbiz.base.util.string;
>  
> @@ -33,6 +27,8 @@
>  import org.ofbiz.base.util.BshUtil;
>  import org.ofbiz.base.util.Debug;
>  import org.ofbiz.base.util.collections.FlexibleMapAccessor;
> +import org.ofbiz.base.util.UtilFormatOut;
> +import org.ofbiz.base.util.UtilMisc;
>  
>  import bsh.EvalError;
>  
> @@ -40,8 +36,13 @@
>   * Expands string values with in a Map context supporting the ${} syntax for
>   * variable placeholders and the "." (dot) and "[]" (square-brace) syntax
>   * elements for accessing Map entries and List elements in the context.
> + * It Also supports the execution of bsh files by using the 'bsh:' prefix.
> + * Further it is possible to control the output by specifying the suffix
> + * 'currency=XXX:' to format the output according the current locale
> + * and specified (XXX) currency
>   *
>   * @author     <a href="mailto:[hidden email]">David E. Jones</a>
> + * @author     <a href="mailto:[hidden email]">Hans Bakker</a>  added currency=xxx prefix
>   * @version    $Rev$
>   * @since      2.2
>   */
> @@ -51,6 +52,8 @@
>      
>      protected String original;
>      protected List stringElements = new LinkedList();
> +    protected static boolean localizeCurrency = false;
> +    protected static String currencyCode = null;            
>      
>      public FlexibleStringExpander(String original) {
>          this.original = original;
> @@ -91,6 +94,7 @@
>       * they are dependent on the context which isn't known until expansion time.
>       *
>       * @param context A context Map containing the variable values
> +     * @param locale the current set locale
>       * @return The original String expanded by replacing varaible place holders.
>       */    
>      public String expandString(Map context, Locale locale) {
> @@ -103,7 +107,7 @@
>          }
>          
>          //call back into this method with new String to take care of any/all nested expands
> -        return expandString(expanded.toString(), context);
> +        return expandString(expanded.toString(), context,locale);
>      }
>      
>      /**
> @@ -112,13 +116,17 @@
>       * the "${}" syntax and the variable name inside the curly-braces can use
>       * the "." (dot) syntax to access sub-Map entries and the "[]" square-brace
>       * syntax to access List elements.
> +     * It Also supports the execution of bsh files by using the 'bsh:' prefix.
> +     * Further it is possible to control the output by specifying the suffix
> +     * 'currency=XXX:' to format the output according the current locale
> +     * and specified (XXX) currency
>       *
>       * @param original The original String that will be expanded
>       * @param context A context Map containing the variable values
>       * @return The original String expanded by replacing varaible place holders.
>       */
>      public static String expandString(String original, Map context) {
> -        return expandString(original, context, null);
> +       return expandString(original, context, null);
>      }
>      
>      /**
> @@ -127,6 +135,10 @@
>       * the "${}" syntax and the variable name inside the curly-braces can use
>       * the "." (dot) syntax to access sub-Map entries and the "[]" square-brace
>       * syntax to access List elements.
> +     * It Also supports the execution of bsh files by using the 'bsh:' prefix.
> +     * Further it is possible to control the output by specifying the prefix
> +     * 'currency=XXX:' to format the output according the current locale
> +     * and specified (XXX) currency
>       *
>       * @param original The original String that will be expanded
>       * @param context A context Map containing the variable values
> @@ -134,7 +146,7 @@
>       */
>      public static String expandString(String original, Map context, Locale locale) {
>          // if null or less than 3 return original; 3 chars because that is the minimum necessary for a ${}
> -        if (original == null || original.length() < 3) {
> +           if (original == null || original.length() < 3) {
>              return original;
>          }
>          
> @@ -151,12 +163,19 @@
>              }
>          }
>          
> +        if (locale == null && context.containsKey("locale")) {
> +            locale = (Locale) context.get("locale");
> +        }
> +        if (locale == null && context.containsKey("autoUserLogin")) {
> +            locale = UtilMisc.ensureLocale(((Map) context.get("autoUserLogin")).get("lastLocale"));
> +        }
> +
>          StringBuffer expanded = new StringBuffer();
>          ParseElementHandler handler = new OnTheFlyHandler(expanded, context, locale);
>          parseString(original, handler);
>          
>          //call back into this method with new String to take care of any/all nested expands
> -        return expandString(expanded.toString(), context);
> +        return expandString(expanded.toString(), context,locale);
>      }
>      
>      public static void parseString(String original, ParseElementHandler handler) {
> @@ -189,10 +208,17 @@
>              // append everything from the current index to the start of the var
>              handler.handleConstant(original, currentInd, start);
>              
> +            localizeCurrency = false;
>              // check to see if this starts with a "bsh:", if so treat the rest of the string as a bsh scriptlet
>              if (original.indexOf("bsh:", start+2) == start+2) {
>                  // get the bsh scriptlet and append it
>                  handler.handleBsh(original, start+6, end);
> +            // see if currency=xxx: formatting is required
> +            } else if (original.indexOf("currency=", start+2) == start+2) {
> +                    int colonPos = original.indexOf(":", start+9);
> +                    currencyCode = original.substring(start+11,colonPos);
> +                    localizeCurrency = true;
> +                    handler.handleVariable(original, colonPos+1, end);
>              } else {
>                  // get the environment value and append it
>                  handler.handleVariable(original, start+2, end);
> @@ -318,7 +344,11 @@
>              FlexibleMapAccessor fma = new FlexibleMapAccessor(envName);
>              Object envVal = fma.get(context, locale);
>              if (envVal != null) {
> -                targetBuffer.append(envVal.toString());
> +                if(localizeCurrency) {
> +                    targetBuffer.append(UtilFormatOut.formatCurrency(new Double(envVal.toString()),currencyCode,locale));
> +                } else {
> +                    targetBuffer.append(envVal.toString());
> +                }
>              } else {
>                  Debug.logWarning("Could not find value in environment for the name [" + envName + "], inserting nothing.", module);
>              }
>
>
> ------------------------------------------------------------------------
>
>  
> _______________________________________________
> Dev mailing list
> [hidden email]
> http://lists.ofbiz.org/mailman/listinfo/dev
 
_______________________________________________
Dev mailing list
[hidden email]
http://lists.ofbiz.org/mailman/listinfo/dev
Reply | Threaded
Open this post in threaded view
|

Re: Dev - proposal to add feature to flexible stringexpander

Jacques Le Roux
Administrator
Just a note : in France we write with € symbol after the amount. Perhaps it can
be done in the same time ?

Jacques

----- Original Message -----
From: "David E Jones" <[hidden email]>
To: "OFBiz Project Development Discussion" <[hidden email]>
Sent: Thursday, April 20, 2006 6:48 PM
Subject: Re: [OFBiz] Dev - proposal to add feature to flexible stringexpander


>
> I think this sort of change is fine, but perhaps with a syntax closer to that
of FreeMarker it will be easier for people to use/remember. Something like the
"?" separator they use for built-ins. So it might look something like:
>
> > ${payment.amount?currency(EUR)}
> > or
> > ${payment.amount?currency(${payment.currencyUomId})}
>
> Of course, as we make this more complex eventually we're going to want a more
formal parser than just doing String compares and such... I guess we can table
that for later though.

>
> -David
>
>
> Hans Bakker wrote:
> > Good morning,
> >
> > currently it is not possible to do currency localization on currency values
> > displayed in the label statement in a screen widget and every where the
> > flexible string expander is used.
> >
> > attached a patch to enable the following code which displays the amount of a
> > payment taking into account the current locale and requested currency. The
> > implementation is similar to the 'bsh:' prefix.
> >
> > ${currency=EUR:payment.amount}
> > or
> > ${currency=${payment.currencyUomId}:payment.amount}
> >
> > will display as: € 40,33
> > when the currency is set to 'EUR' and the locale is dutch.
> >
> > any opinions?
> >
> >
> >
> > ------------------------------------------------------------------------
> >
> > Index: base/src/base/org/ofbiz/base/util/string/FlexibleStringExpander.java
> > ===================================================================
> > --- base/src/base/org/ofbiz/base/util/string/FlexibleStringExpander.java
(revision 7344)
> > +++ base/src/base/org/ofbiz/base/util/string/FlexibleStringExpander.java
(working copy)
> > @@ -1,25 +1,19 @@
> >  /*
> >   * $Id$
> >   *
> > - *  Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
> > - *
> > - *  Permission is hereby granted, free of charge, to any person obtaining a
> > - *  copy of this software and associated documentation files (the
"Software"),
> > - *  to deal in the Software without restriction, including without
limitation
> > - *  the rights to use, copy, modify, merge, publish, distribute,
sublicense,

> > - *  and/or sell copies of the Software, and to permit persons to whom the
> > - *  Software is furnished to do so, subject to the following conditions:
> > - *
> > - *  The above copyright notice and this permission notice shall be included
> > - *  in all copies or substantial portions of the Software.
> > - *
> > - *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
> > - *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
> > - *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
> > - *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
> > - *  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT
> > - *  OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR
> > - *  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
> > + * Copyright 2001-2006 The Apache Software Foundation
> > + *
> > + * Licensed under the Apache License, Version 2.0 (the "License"); you may
not
> > + * use this file except in compliance with the License. You may obtain a
copy of
> > + * the License at
> > + *
> > + * http://www.apache.org/licenses/LICENSE-2.0
> > + *
> > + * Unless required by applicable law or agreed to in writing, software
> > + * distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT

> > + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
> > + * License for the specific language governing permissions and limitations
> > + * under the License.
> >   */
> >  package org.ofbiz.base.util.string;
> >
> > @@ -33,6 +27,8 @@
> >  import org.ofbiz.base.util.BshUtil;
> >  import org.ofbiz.base.util.Debug;
> >  import org.ofbiz.base.util.collections.FlexibleMapAccessor;
> > +import org.ofbiz.base.util.UtilFormatOut;
> > +import org.ofbiz.base.util.UtilMisc;
> >
> >  import bsh.EvalError;
> >
> > @@ -40,8 +36,13 @@
> >   * Expands string values with in a Map context supporting the ${} syntax
for
> >   * variable placeholders and the "." (dot) and "[]" (square-brace) syntax
> >   * elements for accessing Map entries and List elements in the context.
> > + * It Also supports the execution of bsh files by using the 'bsh:' prefix.
> > + * Further it is possible to control the output by specifying the suffix
> > + * 'currency=XXX:' to format the output according the current locale
> > + * and specified (XXX) currency
> >   *
> >   * @author     <a href="mailto:[hidden email]">David E. Jones</a>
> > + * @author     <a href="mailto:[hidden email]">Hans Bakker</a>
added currency=xxx prefix

> >   * @version    $Rev$
> >   * @since      2.2
> >   */
> > @@ -51,6 +52,8 @@
> >
> >      protected String original;
> >      protected List stringElements = new LinkedList();
> > +    protected static boolean localizeCurrency = false;
> > +    protected static String currencyCode = null;
> >
> >      public FlexibleStringExpander(String original) {
> >          this.original = original;
> > @@ -91,6 +94,7 @@
> >       * they are dependent on the context which isn't known until expansion
time.
> >       *
> >       * @param context A context Map containing the variable values
> > +     * @param locale the current set locale
> >       * @return The original String expanded by replacing varaible place
holders.
> >       */
> >      public String expandString(Map context, Locale locale) {
> > @@ -103,7 +107,7 @@
> >          }
> >
> >          //call back into this method with new String to take care of
any/all nested expands
> > -        return expandString(expanded.toString(), context);
> > +        return expandString(expanded.toString(), context,locale);
> >      }
> >
> >      /**
> > @@ -112,13 +116,17 @@
> >       * the "${}" syntax and the variable name inside the curly-braces can
use
> >       * the "." (dot) syntax to access sub-Map entries and the "[]"
square-brace
> >       * syntax to access List elements.
> > +     * It Also supports the execution of bsh files by using the 'bsh:'
prefix.
> > +     * Further it is possible to control the output by specifying the
suffix
> > +     * 'currency=XXX:' to format the output according the current locale
> > +     * and specified (XXX) currency
> >       *
> >       * @param original The original String that will be expanded
> >       * @param context A context Map containing the variable values
> >       * @return The original String expanded by replacing varaible place
holders.
> >       */
> >      public static String expandString(String original, Map context) {
> > -        return expandString(original, context, null);
> > +       return expandString(original, context, null);
> >      }
> >
> >      /**
> > @@ -127,6 +135,10 @@
> >       * the "${}" syntax and the variable name inside the curly-braces can
use
> >       * the "." (dot) syntax to access sub-Map entries and the "[]"
square-brace
> >       * syntax to access List elements.
> > +     * It Also supports the execution of bsh files by using the 'bsh:'
prefix.
> > +     * Further it is possible to control the output by specifying the
prefix
> > +     * 'currency=XXX:' to format the output according the current locale
> > +     * and specified (XXX) currency
> >       *
> >       * @param original The original String that will be expanded
> >       * @param context A context Map containing the variable values
> > @@ -134,7 +146,7 @@
> >       */
> >      public static String expandString(String original, Map context, Locale
locale) {
> >          // if null or less than 3 return original; 3 chars because that is
the minimum necessary for a ${}

> > -        if (original == null || original.length() < 3) {
> > +           if (original == null || original.length() < 3) {
> >              return original;
> >          }
> >
> > @@ -151,12 +163,19 @@
> >              }
> >          }
> >
> > +        if (locale == null && context.containsKey("locale")) {
> > +            locale = (Locale) context.get("locale");
> > +        }
> > +        if (locale == null && context.containsKey("autoUserLogin")) {
> > +            locale = UtilMisc.ensureLocale(((Map)
context.get("autoUserLogin")).get("lastLocale"));
> > +        }
> > +
> >          StringBuffer expanded = new StringBuffer();
> >          ParseElementHandler handler = new OnTheFlyHandler(expanded,
context, locale);
> >          parseString(original, handler);
> >
> >          //call back into this method with new String to take care of
any/all nested expands
> > -        return expandString(expanded.toString(), context);
> > +        return expandString(expanded.toString(), context,locale);
> >      }
> >
> >      public static void parseString(String original, ParseElementHandler
handler) {
> > @@ -189,10 +208,17 @@
> >              // append everything from the current index to the start of the
var
> >              handler.handleConstant(original, currentInd, start);
> >
> > +            localizeCurrency = false;
> >              // check to see if this starts with a "bsh:", if so treat the
rest of the string as a bsh scriptlet

> >              if (original.indexOf("bsh:", start+2) == start+2) {
> >                  // get the bsh scriptlet and append it
> >                  handler.handleBsh(original, start+6, end);
> > +            // see if currency=xxx: formatting is required
> > +            } else if (original.indexOf("currency=", start+2) == start+2) {
> > +                    int colonPos = original.indexOf(":", start+9);
> > +                    currencyCode = original.substring(start+11,colonPos);
> > +                    localizeCurrency = true;
> > +                    handler.handleVariable(original, colonPos+1, end);
> >              } else {
> >                  // get the environment value and append it
> >                  handler.handleVariable(original, start+2, end);
> > @@ -318,7 +344,11 @@
> >              FlexibleMapAccessor fma = new FlexibleMapAccessor(envName);
> >              Object envVal = fma.get(context, locale);
> >              if (envVal != null) {
> > -                targetBuffer.append(envVal.toString());
> > +                if(localizeCurrency) {
> > +                    targetBuffer.append(UtilFormatOut.formatCurrency(new
Double(envVal.toString()),currencyCode,locale));
> > +                } else {
> > +                    targetBuffer.append(envVal.toString());
> > +                }
> >              } else {
> >                  Debug.logWarning("Could not find value in environment for
the name [" + envName + "], inserting nothing.", module);

> >              }
> >
> >
> > ------------------------------------------------------------------------
> >
> >
> > _______________________________________________
> > Dev mailing list
> > [hidden email]
> > http://lists.ofbiz.org/mailman/listinfo/dev
>
> _______________________________________________
> Dev mailing list
> [hidden email]
> http://lists.ofbiz.org/mailman/listinfo/dev

 
_______________________________________________
Dev mailing list
[hidden email]
http://lists.ofbiz.org/mailman/listinfo/dev