tdawson     01/10/15 21:05:46

  Modified:    i18n/src/org/apache/taglibs/i18n BundleTag.java
  Log:
  added debug logging submitted by Stephen Drye
  stopped setting the locale into the session so that intra-session changes to the 
locale list are reflected when the tag is set to only use the browser settings 
(performance penalties to this can be negated by specifying a localeAttribute)
  Submitted by: Stephen Drye
  
  Revision  Changes    Path
  1.4       +87 -102   jakarta-taglibs/i18n/src/org/apache/taglibs/i18n/BundleTag.java
  
  Index: BundleTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-taglibs/i18n/src/org/apache/taglibs/i18n/BundleTag.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- BundleTag.java    2001/10/14 22:07:12     1.3
  +++ BundleTag.java    2001/10/16 04:05:46     1.4
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-taglibs/i18n/src/org/apache/taglibs/i18n/BundleTag.java,v 1.3 
2001/10/14 22:07:12 tdawson Exp $
  - * $Revision: 1.3 $
  - * $Date: 2001/10/14 22:07:12 $
  + * $Header: 
/home/cvs/jakarta-taglibs/i18n/src/org/apache/taglibs/i18n/BundleTag.java,v 1.4 
2001/10/16 04:05:46 tdawson Exp $
  + * $Revision: 1.4 $
  + * $Date: 2001/10/16 04:05:46 $
    *
    * ====================================================================
    * 
  @@ -97,42 +97,37 @@
    */
   public class BundleTag extends TagSupport
     {
  -  // this key is used to cache the preferred locale for the given bundle name
  -  public static final String LOCALE_SESSION_KEY_PREFIX =
  -          "org.apache.taglibs.i18n.BundleTag.Locale:";
  -
  -  // stores the baseName as set by the tag instance
  -  private String baseName = null;
  -
  -  // names an attribute that contains the locale to use
  -  private String localeAttribute = null;
  -  
  -  // stores the locale as set by the tag instance
  -  private Locale locale = null;
  -
  -  // specifies whether or not the response locale should be changed to match
  -  // the locale used by this tag
  -  private boolean changeResponseLocale = true;
  -
  -  // keeps the bundle handy for subtags
  -  private ResourceBundle bundle = null;
  +  private String _baseName = null;
  +  private String _localeAttribute = null;
  +  private Locale _locale = null;
  +  private boolean _changeResponseLocale = true;
  +  private ResourceBundle _bundle = null;
  +  private boolean _debug = false;
     
     /**
      *  Sets the baseName of the bundle that the MessageTags will use when
      *  retrieving keys for this page.
      */
  -  public void setBaseName(String baseName)
  +  public void setBaseName(String value)
       {
  -    this.baseName = baseName;
  +    _baseName = value;
       }
   
     /**
  +   *  Set to true to enable debug logging.
  +   */
  +  public void setDebug(boolean value)
  +    {
  +    _debug = value;
  +    }
  +    
  +  /**
      *  Sets the locale of the bundle that the MessageTags will use when
      *  retrieving keys for this page.
      */
  -  public void setLocale(Locale locale)
  +  public void setLocale(Locale value)
       {
  -    this.locale = locale;
  +    _locale = value;
       }
   
     /**
  @@ -140,24 +135,29 @@
      *  locale of the bundle that the MessageTags will use when retrieving
      *  keys for this page.
      */
  -  public void setLocaleAttribute(String attribute)
  +  public void setLocaleAttribute(String value)
       {
  -    localeAttribute = attribute;
  +    _localeAttribute = value;
       }
   
  +  /**
  +   *  Specifies whether or not the response locale should be changed to match
  +   *  the locale used by this tag.
  +   */
     public void setChangeResponseLocale(boolean value)
       {
  -    this.changeResponseLocale = value;
  +    _changeResponseLocale = value;
       }
   
     public void release()
       {
       super.release();
  -    this.baseName = null;
  -    this.localeAttribute = null;
  -    this.locale = null;
  -    this.changeResponseLocale = true;
  -    this.bundle = null;
  +    _baseName = null;
  +    _localeAttribute = null;
  +    _locale = null;
  +    _changeResponseLocale = true;
  +    _bundle = null;
  +    _debug = true;
       }
   
     /**
  @@ -166,7 +166,7 @@
      */
     public ResourceBundle getBundle()
       {
  -    return this.bundle;
  +    return _bundle;
       }
   
     /**
  @@ -187,27 +187,26 @@
             throws JspException
       {
       // the bundle name is required. if not provided, throw an exception
  -    if ( this.baseName == null )
  +    if (_baseName == null)
           {
           throw new JspException("A baseName must be specified.");
           }
   
       // if locale not provided, but an attribute was, search for it
  -    if ( this.locale == null && this.localeAttribute != null )
  +    if (_locale == null && _localeAttribute != null)
           {
  -        this.locale = (Locale)pageContext.findAttribute(localeAttribute);
  +        _locale = (Locale)pageContext.findAttribute(_localeAttribute);
           }
       
  -    // if locale not provided, check to see if the basename was used before
  -    if ( this.locale == null )
  -        {
  -        this.locale = BundleTag.getLocale(pageContext,this.baseName);
  -        }
  -
  -    // if either method above yielded a locale, just grab the resource bundle
  -    if ( this.locale != null )
  +    // if we now have a locale, grab the resource bundle
  +    if (_locale != null)
           {
  -        this.bundle = ResourceBundle.getBundle(this.baseName,this.locale);
  +        _bundle = ResourceBundle.getBundle(_baseName,_locale);
  +        if (_debug) 
  +            {
  +            ServletContext sc = pageContext.getServletContext();
  +            sc.log("i18n: debug: found locale " + _locale);
  +            }
           }
       
       // attempt to load the bundle and compute the locale by looping through
  @@ -215,13 +214,19 @@
       else
           {
           Enumeration localeEnumerator = pageContext.getRequest().getLocales();
  -        while ( localeEnumerator.hasMoreElements() )
  +        while (localeEnumerator.hasMoreElements())
               {
  -            this.locale = (Locale)localeEnumerator.nextElement();
  +            _locale = (Locale)localeEnumerator.nextElement();
   
  +            if (_debug)
  +                {
  +                ServletContext sc = pageContext.getServletContext();
  +                sc.log("i18n: debug: enumerating locale = " + _locale);    
  +                }
  +                
               // get a bundle and see whether it has a good locale
  -            ResourceBundle test = ResourceBundle.getBundle(this.baseName,
  -                                                           this.locale);
  +            ResourceBundle test = ResourceBundle.getBundle(_baseName,
  +                                                           _locale);
               String language = test.getLocale().getLanguage();
               String country = test.getLocale().getCountry();
   
  @@ -229,21 +234,21 @@
               // return a bundle with a locale for the same language, or will
               // return the default locale, so we need to compare the locale
               // of the bundle we got back with the one we asked for
  -            if ( test.getLocale().equals(this.locale) )
  +            if (test.getLocale().equals(_locale))
                   {
                   // exactly what we were looking for - stop here and use this
  -                this.bundle = test;
  +                _bundle = test;
                   break;                
                   }
  -            else if ( this.locale.getLanguage().equals(language) )
  +            else if (_locale.getLanguage().equals(language))
                   {
                   // the language matches; see if the country matches as well
  -                if ( this.locale.getCountry().equals(country) )
  +                if (_locale.getCountry().equals(country))
                       {
                       // keep looking but this is a good option. it only gets
                       // better if the variant matches too!  (note: we will only
                       // get to this statement if a variant has been provided)
  -                    this.bundle = test;
  +                    _bundle = test;
                       continue;
                       }
                   else
  @@ -251,9 +256,9 @@
                       // if we don't already have a candidate, this is a good
                       // one otherwise, don't change it because the current
                       // candidate might match the country but not the variant
  -                    if ( this.bundle == null )
  +                    if (_bundle == null)
                           {
  -                        this.bundle = test;
  +                        _bundle = test;
                           }
                       continue;
                       }
  @@ -261,29 +266,34 @@
               else
                   {
                   ServletContext sc = pageContext.getServletContext();
  -                sc.log("i18n: requested locale not available: " + this.locale);
  +                sc.log("i18n: requested locale not available: " + _locale);
                   }
               }
   
           // bundle should never be null at this point - if the last locale on
           // the list wasn't available, the default locale should have kicked
           // in, but I like being safe, hence the code below.
  -        if ( this.bundle == null )
  +        if (_bundle == null)
               {
  -            this.bundle = ResourceBundle.getBundle(this.baseName);
  +            _bundle = ResourceBundle.getBundle(_baseName);
               }
  -
  -        // cache the locale for later use within this user's session
  -        BundleTag.setLocale(pageContext,this.baseName,this.bundle.getLocale());
   
  +        if (_debug)
  +            {
  +            ServletContext sc = pageContext.getServletContext();
  +            sc.log("i18n: debug: bundle (sensed locale) = " + _bundle);
  +            }
  +        
           // if the localeAttribute was provided, but didn't have a value,
           // go ahead and remember the value for next time
  -        if (localeAttribute != null)
  +        if (_localeAttribute != null)
               {
               HttpSession session = pageContext.getSession();
  -            session.setAttribute(localeAttribute,this.bundle.getLocale());
  +            session.setAttribute(_localeAttribute,_bundle.getLocale());
  +            System.out.println("set locale " + _bundle.getLocale() + " for 
attribute " +_localeAttribute);
               }
           }
  +
       }
   
     /**
  @@ -295,8 +305,14 @@
       {
       this.findBundle();
       
  +    if (_debug) 
  +        {
  +        ServletContext sc = pageContext.getServletContext();
  +        sc.log("i18n: debug: basename =" + _baseName);
  +        }    
  +        
       // set the bundle as a variable in the page
  -    if ( this.getId() != null )
  +    if (this.getId() != null)
           {
           pageContext.setAttribute(this.getId(),this.getBundle());
           }
  @@ -310,52 +326,21 @@
     public int doEndTag()
             throws JspException
       {
  -    if (this.changeResponseLocale)
  +    if (_changeResponseLocale)
           {
           // set the locale for the response
  -        pageContext.getResponse().setLocale(this.bundle.getLocale());
  +        pageContext.getResponse().setLocale(_bundle.getLocale());
           }
       
       // different handling is necessary if multiple bundle tags are in use
       ResourceBundle firstBundle = ResourceHelper.getBundle(pageContext);
  -    if ( firstBundle == null )
  +    if (firstBundle == null)
           {
           // cache the bundle for use by message tags within this request
  -        ResourceHelper.setBundle(pageContext,this.bundle);
  +        ResourceHelper.setBundle(pageContext,_bundle);
           }
   
       return EVAL_PAGE;
  -    }
  -
  -  /**
  -   *  Retrieves the Locale cached in the user's session for the given
  -   *  bundle.  The Locale is cached in the session using a combination of
  -   *  the LOCALE_SESSION_KEY_PREFIX and the named bundle.
  -   */
  -  static public Locale getLocale(PageContext pageContext, String baseName)
  -    {
  -    // compute the session key
  -    String key = LOCALE_SESSION_KEY_PREFIX + baseName;
  -
  -    // get the attribute from the session
  -    HttpSession session = pageContext.getSession();
  -    return (Locale)session.getAttribute(key);
  -    }
  -
  -  /**
  -   *  Caches the Locale in the user's session using a combination of the
  -   *  LOCALE_SESSION_KEY_PREFIX and the named bundle.
  -   */
  -  static public void setLocale(PageContext pageContext,
  -                               String baseName,
  -                               Locale locale)
  -    {
  -    // compute the session key
  -    String key = LOCALE_SESSION_KEY_PREFIX + baseName;
  -
  -    // set the attribute in the session
  -    HttpSession session = pageContext.getSession();
  -    session.setAttribute(key,locale);
       }
   
     } // BundleTag
  
  
  


Reply via email to