dion        2003/01/17 22:25:15

  Modified:    jelly/jelly-tags/fmt/src/java/org/apache/commons/jelly/tags/fmt
                        SetLocaleTag.java
  Log:
  Applied patch from Willie Vu
  
  Revision  Changes    Path
  1.2       +152 -7    
jakarta-commons-sandbox/jelly/jelly-tags/fmt/src/java/org/apache/commons/jelly/tags/fmt/SetLocaleTag.java
  
  Index: SetLocaleTag.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/jelly/jelly-tags/fmt/src/java/org/apache/commons/jelly/tags/fmt/SetLocaleTag.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SetLocaleTag.java 16 Jan 2003 16:21:46 -0000      1.1
  +++ SetLocaleTag.java 18 Jan 2003 06:25:15 -0000      1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header$
  - * $Revision$
  - * $Date$
  + * 
/home/cvs/jakarta-commons-sandbox/jelly/jelly-tags/fmt/src/java/org/apache/commons/jelly/tags/fmt/SetLocaleTag.java,v
 1.1 2003/01/16 16:21:46 jstrachan Exp
  + * 1.1
  + * 2003/01/16 16:21:46
    *
    * ====================================================================
    *
  @@ -57,10 +57,11 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    *
  - * $Id$
  + * SetLocaleTag.java,v 1.1 2003/01/16 16:21:46 jstrachan Exp
    */
   package org.apache.commons.jelly.tags.fmt;
   
  +import org.apache.commons.jelly.JellyContext;
   import org.apache.commons.jelly.JellyException;
   import org.apache.commons.jelly.XMLOutput;
   import org.apache.commons.jelly.Tag;
  @@ -79,7 +80,7 @@
   public class SetLocaleTag extends TagSupport {
        
        private static final char HYPHEN = '-';
  -    private static final char UNDERSCORE = '_';
  +     private static final char UNDERSCORE = '_';
        
        private Expression value;
        
  @@ -125,7 +126,7 @@
                else {
                        context.setVariable(Config.FMT_LOCALE, locale);
                }
  -             //setResponseLocale(pageContext, locale);
  +             //setResponseLocale(jc, locale);
        }
        
        public void setValue(Expression value) {
  @@ -200,5 +201,149 @@
                }
                
                return ret;
  +     }
  +     
  +     /**
  +      * Returns the locale specified by the named scoped attribute or context
  +      * configuration parameter.
  +      *
  +      * <p> The named scoped attribute is searched in the page, request,
  +      * session (if valid), and application scope(s) (in this order). If no such
  +      * attribute exists in any of the scopes, the locale is taken from the
  +      * named context configuration parameter.
  +      *
  +      * @param jc the page in which to search for the named scoped
  +      * attribute or context configuration parameter
  +      * @param name the name of the scoped attribute or context configuration
  +      * parameter
  +      *
  +      * @return the locale specified by the named scoped attribute or context
  +      * configuration parameter, or <tt>null</tt> if no scoped attribute or
  +      * configuration parameter with the given name exists
  +      */
  +     static Locale getLocale(JellyContext jc, String name) {
  +             Locale loc = null;
  +             
  +             Object obj = jc.getVariable(name);
  +             if (obj != null) {
  +                     if (obj instanceof Locale) {
  +                             loc = (Locale) obj;
  +                     } else {
  +                             loc = parseLocale((String) obj);
  +                     }
  +             }
  +             
  +             return loc;
  +     }
  +     
  +     /*
  +      * Returns the formatting locale to use with the given formatting action
  +      * in the given page.
  +      *
  +      * @param jc The context containing the formatting action
  +      * @param fromTag The formatting action
  +      * @param format <tt>true</tt> if the formatting action is of type
  +      * <formatXXX> (as opposed to <parseXXX>), and <tt>false</tt> otherwise
  +      * (if set to <tt>true</tt>, the formatting locale that is returned by
  +      * this method is used to set the response locale).
  +      *
  +      * @param avail the array of available locales
  +      *
  +      * @return the formatting locale to use
  +      */
  +     static Locale getFormattingLocale(JellyContext jc,
  +     Tag fromTag,
  +     boolean format,
  +     Locale[] avail) {
  +             
  +             LocalizationContext locCtxt = null;
  +             
  +             // Get formatting locale from enclosing <fmt:bundle>
  +             Tag parent = findAncestorWithClass(fromTag, BundleTag.class);
  +             if (parent != null) {
  +             /*
  +              * use locale from localization context established by parent
  +              * <fmt:bundle> action, unless that locale is null
  +              */
  +                     locCtxt = ((BundleTag) parent).getLocalizationContext();
  +                     if (locCtxt.getLocale() != null) {
  +                             if (format) {
  +                                     //setResponseLocale(jc, locCtxt.getLocale());
  +                             }
  +                             return locCtxt.getLocale();
  +                     }
  +             }
  +             
  +             // Use locale from default I18N localization context, unless it is null
  +             if ((locCtxt = BundleTag.getLocalizationContext(jc)) != null) {
  +                     if (locCtxt.getLocale() != null) {
  +                             if (format) {
  +                                     //setResponseLocale(jc, locCtxt.getLocale());
  +                             }
  +                             return locCtxt.getLocale();
  +                     }
  +             }
  +             
  +     /*
  +      * Establish formatting locale by comparing the preferred locales
  +      * (in order of preference) against the available formatting
  +      * locales, and determining the best matching locale.
  +      */
  +             Locale match = null;
  +             Locale pref = getLocale(jc, Config.FMT_LOCALE);
  +             if (pref != null) {
  +                     // Preferred locale is application-based
  +                     match = findFormattingMatch(pref, avail);
  +             }
  +             if (match == null) {
  +                     //Use fallback locale.
  +                     pref = getLocale(jc, Config.FMT_FALLBACK_LOCALE);
  +                     if (pref != null) {
  +                             match = findFormattingMatch(pref, avail);
  +                     }
  +             }
  +             if (format && (match != null)) {
  +                     //setResponseLocale(jc, match);
  +             }
  +             
  +             return match;
  +     }
  +     
  +     /*
  +      * Returns the best match between the given preferred locale and the
  +      * given available locales.
  +      *
  +      * The best match is given as the first available locale that exactly
  +      * matches the given preferred locale ("exact match"). If no exact match
  +      * exists, the best match is given as the first available locale that
  +      * matches the preferred locale's language component and does not have any
  +      * country component ("language match").
  +      *
  +      * @param pref the preferred locale
  +      * @param avail the available formatting locales
  +      *
  +      * @return Available locale that best matches the given preferred locale,
  +      * or <tt>null</tt> if no match exists
  +      */
  +     private static Locale findFormattingMatch(Locale pref, Locale[] avail) {
  +             Locale match = null;
  +             
  +             for (int i=0; i<avail.length; i++) {
  +                     if (pref.equals(avail[i])) {
  +                             // Exact match
  +                             match = avail[i];
  +                             break;
  +                     } else {
  +                             if (pref.getLanguage().equals(avail[i].getLanguage())
  +                             && ("".equals(avail[i].getCountry()))) {
  +                                     // Language match
  +                                     if (match == null) {
  +                                             match = avail[i];
  +                                     }
  +                             }
  +                     }
  +             }
  +             
  +             return match;
        }
   }
  
  
  

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

Reply via email to