tdawson     01/10/14 15:07:12

  Modified:    i18n/src/org/apache/taglibs/i18n BundleTag.java
  Log:
  added changeResponseLocale attribute, defaulted to true
  added ability to act as a body tag, with nested message & if/ifndef tags
  
  Revision  Changes    Path
  1.3       +94 -43    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.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- BundleTag.java    2001/06/13 03:24:41     1.2
  +++ BundleTag.java    2001/10/14 22:07:12     1.3
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-taglibs/i18n/src/org/apache/taglibs/i18n/BundleTag.java,v 1.2 
2001/06/13 03:24:41 tdawson Exp $
  - * $Revision: 1.2 $
  - * $Date: 2001/06/13 03:24:41 $
  + * $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 $
    *
    * ====================================================================
    * 
  @@ -104,9 +104,19 @@
     // 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;
  +  
     /**
      *  Sets the baseName of the bundle that the MessageTags will use when
      *  retrieving keys for this page.
  @@ -132,10 +142,34 @@
      */
     public void setLocaleAttribute(String attribute)
       {
  -    this.locale = (Locale)pageContext.findAttribute(attribute);
  +    localeAttribute = attribute;
       }
   
  +  public void setChangeResponseLocale(boolean value)
  +    {
  +    this.changeResponseLocale = value;
  +    }
  +
  +  public void release()
  +    {
  +    super.release();
  +    this.baseName = null;
  +    this.localeAttribute = null;
  +    this.locale = null;
  +    this.changeResponseLocale = true;
  +    this.bundle = null;
  +    }
  +
     /**
  +   *  Returns the bundle specified by the tag.  This will not be initialized
  +   *  until after doStartTag() is called.
  +   */
  +  public ResourceBundle getBundle()
  +    {
  +    return this.bundle;
  +    }
  +
  +  /**
      *  Locates and prepares a ResourceBundle for use within this request by
      *  message tags.  The bundle is located as specified by the given bundle
      *  name and the user's browser locale settings. The first preferred
  @@ -148,22 +182,23 @@
      *  (The entire bundle could be cached in the session, but large bundles would
      *  be problematic in servlet containers that serialize the session to provide
      *  clustering.)
  -   *
  -   *  Finally, the bundle's locale is used to set the response character set.
      */
  -  public int doStartTag()
  +  private void findBundle()
             throws JspException
       {
  -    ResourceBundle bundle = null;
  -    
       // the bundle name is required. if not provided, throw an exception
       if ( this.baseName == null )
           {
           throw new JspException("A baseName must be specified.");
           }
   
  -    // if a locale was provided to the bundle tag. if not, check the cache to
  -    // see if this baseName has been used before
  +    // if locale not provided, but an attribute was, search for it
  +    if ( this.locale == null && this.localeAttribute != null )
  +        {
  +        this.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);
  @@ -172,7 +207,7 @@
       // if either method above yielded a locale, just grab the resource bundle
       if ( this.locale != null )
           {
  -        bundle = ResourceBundle.getBundle(this.baseName,this.locale);
  +        this.bundle = ResourceBundle.getBundle(this.baseName,this.locale);
           }
       
       // attempt to load the bundle and compute the locale by looping through
  @@ -197,7 +232,7 @@
               if ( test.getLocale().equals(this.locale) )
                   {
                   // exactly what we were looking for - stop here and use this
  -                bundle = test;
  +                this.bundle = test;
                   break;                
                   }
               else if ( this.locale.getLanguage().equals(language) )
  @@ -208,7 +243,7 @@
                       // 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)
  -                    bundle = test;
  +                    this.bundle = test;
                       continue;
                       }
                   else
  @@ -216,9 +251,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 ( bundle == null )
  +                    if ( this.bundle == null )
                           {
  -                        bundle = test;
  +                        this.bundle = test;
                           }
                       continue;
                       }
  @@ -233,47 +268,63 @@
           // 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 ( bundle == null )
  +        if ( this.bundle == null )
               {
  -            bundle = ResourceBundle.getBundle(this.baseName);
  +            this.bundle = ResourceBundle.getBundle(this.baseName);
               }
   
           // cache the locale for later use within this user's session
  -        BundleTag.setLocale(pageContext,this.baseName,bundle.getLocale());
  -        }
  +        BundleTag.setLocale(pageContext,this.baseName,this.bundle.getLocale());
   
  -    // different handling is necessary if multiple bundle tags are in use
  -    ResourceBundle firstBundle = ResourceHelper.getBundle(pageContext);
  -    if ( firstBundle != null )
  -        {
  -        // throw an exception if the locale.country doesn't match
  -        String got = firstBundle.getLocale().getLanguage();
  -        String exp = bundle.getLocale().getLanguage();
  -        if ( !got.equals(exp) )
  +        // if the localeAttribute was provided, but didn't have a value,
  +        // go ahead and remember the value for next time
  +        if (localeAttribute != null)
               {
  -            throw new JspException("Locale language mismatch: '" + this.baseName + 
"'" +
  -                                   " got: '" + got + "'" +
  -                                   " expected: '" + exp + "'");
  +            HttpSession session = pageContext.getSession();
  +            session.setAttribute(localeAttribute,this.bundle.getLocale());
               }
           }
  -    else
  +    }
  +
  +  /**
  +   *  Called upon invocation of the tag. If an id is specified, sets the
  +   *  bundle into the page context.
  +   */
  +  public int doStartTag()
  +          throws JspException
  +    {
  +    this.findBundle();
  +    
  +    // set the bundle as a variable in the page
  +    if ( this.getId() != null )
           {
  -        // cache the bundle for use by message tags within this request
  -        ResourceHelper.setBundle(pageContext,bundle);
  +        pageContext.setAttribute(this.getId(),this.getBundle());
           }
  -
  -    // set the locale for the response
  -    pageContext.getResponse().setLocale(bundle.getLocale());
  +    
  +    return EVAL_BODY_INCLUDE;
  +    }
   
  -    // set the bundle as a variable in the page
  -    String id = this.getId();
  -    if ( id != null )
  +  /**
  +   *  Sets the response locale if changeResponseLocale attribute is true
  +   */
  +  public int doEndTag()
  +          throws JspException
  +    {
  +    if (this.changeResponseLocale)
           {
  -        pageContext.setAttribute(id,bundle,PageContext.PAGE_SCOPE);
  +        // set the locale for the response
  +        pageContext.getResponse().setLocale(this.bundle.getLocale());
           }
       
  -    // ignore the body of this tag
  -    return Tag.SKIP_BODY;
  +    // different handling is necessary if multiple bundle tags are in use
  +    ResourceBundle firstBundle = ResourceHelper.getBundle(pageContext);
  +    if ( firstBundle == null )
  +        {
  +        // cache the bundle for use by message tags within this request
  +        ResourceHelper.setBundle(pageContext,this.bundle);
  +        }
  +
  +    return EVAL_PAGE;
       }
   
     /**
  
  
  


Reply via email to