You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by cr...@apache.org on 2001/02/20 01:18:56 UTC

cvs commit: jakarta-struts/src/share/org/apache/struts/taglib/html ImgTag.java LocalStrings.properties

craigmcc    01/02/19 16:18:55

  Modified:    src/doc  struts-html.xml
               src/share/org/apache/struts/taglib/html ImgTag.java
                        LocalStrings.properties
  Log:
  Enhance the <html:img> tag so that you can specify the ultimate values for
  the "src" and "alt" attributes indirectly, by looking them up in the
  application resources.  This is very useful if you want to use different
  images (or alternate text) depending on the user's current Locale.
  
  You have four options for setting the "src" attribute:
  * "src" - Absolute SRC URL
  * "srcKey" - Resources key to look up absolute SRC URL
  * "page" - Context-relative page URI
  * "pageKey" - Resources key to look up context-relative page URI
  
  You have two options for setting the "alt" attribute:
  * "alt" - Absolute alternate text
  * "altKey" - Resources key to look up the alternate text
  
  For any resources lookup (srcKey, pageKey, or altKey) you can also
  specify:
  * "bundle" - Servlet context attributes key for our message resources
    (defaults to the application resources configured for this app)
  * "locale" - Session attributes key for our current locale (defaults
    to the Struts standard value)
  
  Submitted by:  Oleg V. Alexeev <go...@penza.net>
  PR: Bugzilla #601, #614.
  
  Revision  Changes    Path
  1.12      +66 -3     jakarta-struts/src/doc/struts-html.xml
  
  Index: struts-html.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/doc/struts-html.xml,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- struts-html.xml	2001/02/14 00:39:51	1.11
  +++ struts-html.xml	2001/02/20 00:18:48	1.12
  @@ -1419,8 +1419,10 @@
                   generation of an image where the content displayed for this
                   image will be taken from the attributes of this tag.</p>
   
  -                <p>The base URL for this image is calculated based on
  -                the value specified in <code>src</code> or <code>page</code>.
  +                <p>The base URL for this image is calculated directly based on
  +                the value specified in <code>src</code> or <code>page</code>,
  +                or indirectly by looking up a message resource string based on
  +                the <code>srcKey</code> or <code>pageKey</code> attributes.
                   You <strong>must</strong> specify exactly one of these
                   attributes.</p>
   
  @@ -1477,6 +1479,13 @@
                   one value for the same query parameter name will be
                   created.</p>
   
  +                <p>You can specify the alternate text for this image (which
  +                most browsers display as pop-up text block when the user
  +                hovers the mouse over this image) either directly, through
  +                the <code>alt</code> attribute, or indirectly from a message
  +                resources bundle, using the <code>bundle</code> and
  +                <code>altKey</code> attributes.</p>
  +
                   </info>
   
                   <attribute>
  @@ -1527,6 +1536,17 @@
                   </attribute>
   
                   <attribute>
  +                  <name>altKey</name>
  +                  <required>false</required>
  +                  <rtexprvalue>true</rtexprvalue>
  +                  <info>
  +                  <p>The message key, in the message resources bundle named by
  +                  the <code>bundle</code> attribute, of the String to be
  +                  displayed as the alternate text for this image.</p>
  +                  </info>
  +                </attribute>
  +
  +                <attribute>
                     <name>border</name>
                     <required>false</required>
                     <rtexprvalue>true</rtexprvalue>
  @@ -1536,6 +1556,17 @@
                   </attribute>
   
                   <attribute>
  +                  <name>bundle</name>
  +                  <required>false</required>
  +                  <rtexprvalue>true</rtexprvalue>
  +                  <info>
  +                  <p>The servlet context attribute key for the MessageResources
  +                  instance to use.  If not specified, defaults to the
  +                  application resources configured for our action servlet.</p>
  +                  </info>
  +                </attribute>
  +
  +                <attribute>
                     <name>height</name>
                     <required>false</required>
                     <rtexprvalue>true</rtexprvalue>
  @@ -1563,7 +1594,7 @@
                     <rtexprvalue>true</rtexprvalue>
                     <info>
                     <p>The scriptable name to be defined within this page, so 
  -                  that
you can reference it with intra-page scripts.  In other
  +                  that you can reference it with intra-page scripts.  In other
                     words, the value specified here will render a "name" element
                     in the generated image tag.</p>
                     </info>
  @@ -1580,6 +1611,16 @@
                   </attribute>
   
                   <attribute>
  +                  <name>locale</name>
  +                  <required>false</required>
  +                  <rtexprvalue>true</rtexprvalue>
  +                  <info>
  +                  <p>The name of the request or session Locale attribute used
  +                  to look up internationalized messages.</p>
  +                  </info>
  +                </attribute>
  +
  +                <attribute>
                     <name>lowsrc</name>
                     <required>false</required>
                     <rtexprvalue>true</rtexprvalue>
  @@ -1662,6 +1703,17 @@
                   </attribute>
   
                   <attribute>
  +                  <name>pageKey</name>
  +                  <required>false</required>
  +                  <rtexprvalue>true</rtexprvalue>
  +                  <info>
  +                  <p>The message key, in the message resources bundle named by
  +                  the <code>bundle</code> attribute, of the String to be
  +                  used as the context-relative path for this image.</p>
  +                  </info>
  +                </attribute>
  +
  +                <attribute>
                     <name>paramName</name>
                     <required>false</required>
                     <rtexprvalue>true</rtexprvalue>
  @@ -1738,6 +1790,17 @@
                     attribute in the rendered tag.  You <strong>must</strong>
                     specify either the <code>page</code> attribute or the
                     the <code>src</code> attribute.</p>
  +                  </info>
  +                </attribute>
  +
  +                <attribute>
  +                  <name>srcKey</name>
  +                  <required>false</required>
  +                  <rtexprvalue>true</rtexprvalue>
  +                  <info>
  +                  <p>The message key, in the message resources bundle named by
  +                  the <code>bundle</code> attribute, of the String to be
  +                  used as the URL of this image.</p>
                     </info>
                   </attribute>
   
  
  
  
  1.5       +238 -20   jakarta-struts/src/share/org/apache/struts/taglib/html/ImgTag.java
  
  Index: ImgTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/ImgTag.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ImgTag.java	2001/02/15 00:01:49	1.4
  +++ ImgTag.java	2001/02/20 00:18:52	1.5
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/ImgTag.java,v 1.4 2001/02/15 00:01:49 craigmcc Exp $
  - * $Revision: 1.4 $
  - * $Date: 2001/02/15 00:01:49 $
  + * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/ImgTag.java,v 1.5 2001/02/20 00:18:52 craigmcc Exp $
  + * $Revision: 1.5 $
  + * $Date: 2001/02/20 00:18:52 $
    *
    * ====================================================================
    *
  @@ -67,6 +67,7 @@
   import java.lang.reflect.InvocationTargetException;
   import java.net.URLEncoder;
   import java.util.Iterator;
  +import java.util.Locale;
   import java.util.Map;
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;
  @@ -95,7 +96,7 @@
    *
    * @author Michael Westbay
    * @author Craig McClanahan
  - * @version $Revision: 1.4 $
  + * @version $Revision: 1.5 $
    */
   
   public class ImgTag extends BaseHandlerTag {
  @@ -134,6 +135,20 @@
   
   
       /**
  +     * The message lookup key used to look up internationalized messages.
  +     */
  +    protected String altKey = null;
  +
  +    public String getAltKey() {
  +        return (this.altKey);
  +    }
  +
  +    public void setAltKey(String altKey) {
  +        this.altKey = altKey;
  +    }
  +
  +
  +    /**
        * The border size around the image.
        */
       protected String border = null;
  @@ -148,6 +163,27 @@
   
   
       /**
  +     * The name of the servlet context attribute containing our message
  +     * resources.
  +     */
  +    protected String bundle = Action.MESSAGES_KEY;
  +
  +    public String getBundle() {
  +        return (this.bundle);
  +    }
  +
  +    public void setBundle(String bundle) {
  +        this.bundle = bundle;
  +    }
  +
  +
  +    /**
  +     * The default Locale for our server.
  +     */
  +    protected static final Locale defaultLocale = Locale.getDefault();
  +
  +
  +    /**
        * The image height.
        */
       protected String height = null;
  @@ -204,6 +240,21 @@
   
   
       /**
  +     * The name of the attribute containing the Locale to be used for
  +     * looking up internationalized messages.
  +     */
  +    protected String locale = Action.LOCALE_KEY;
  +
  +    public String getLocale() {
  +        return (this.locale);
  +    }
  +
  +    public void setLocale(String locale) {
  +        this.locale = locale;
  +    }
  +
  +
  +    /**
        * The low resolution image source URI.
        */
       protected String lowsrc = null;
  @@ -254,6 +305,21 @@
   
   
       /**
  +     * The message resources key under which we should look up the
  +     * <code>page</code> attribute for this generated tag, if any.
  +     */
  +    protected String pageKey = null;
  +
  +    public String getPageKey() {
  +        return (this.pageKey);
  +    }
  +
  +    public void setPageKey(String pageKey) {
  +        this.pageKey = pageKey;
  +    }
  +
  +
  +    /**
        * In situations where an image is dynamically generated (such as to create
        * a chart graph), this specifies the single-parameter request parameter
        * name to generate.
  @@ -354,6 +420,21 @@
   
   
       /**
  +     * The message resources key under which we should look up the
  +     * <code>src</code> attribute for this generated tag, if any.
  +     */
  +    protected String srcKey = null;
  +
  +    public String getSrcKey() {
  +        return (this.srcKey);
  +    }
  +
  +    public void setSrcKey(String srcKey) {
  +        this.srcKey = srcKey;
  +    }
  +
  +
  +    /**
        * Client-side image map declaration.
        */
       protected String usemap = null;
  @@ -424,32 +505,23 @@
   	HttpServletResponse response =
   	  (HttpServletResponse) pageContext.getResponse();
   	StringBuffer results = new StringBuffer("<img");
  -	String srcurl = null;
  -	if (this.src != null)
  -	    srcurl = url(this.src);
  -	else if (this.page != null)
  -	    srcurl = url(request.getContextPath() + this.page);
  -	else {
  -	    JspException e = new JspException
  -	      (messages.getMessage("imgTag.source"));
  -	    pageContext.setAttribute(Action.EXCEPTION_KEY, e,
  -				     PageContext.REQUEST_SCOPE);
  -	    throw e;
  -	}
  -        String lowsrcurl = url(this.lowsrc);
  +        String tmp = src();
  +        String srcurl = url(tmp);
           if (srcurl != null) {
               results.append(" src=\"");
               results.append(response.encodeURL(BeanUtils.filter(srcurl)));
               results.append("\"");
           }
  -        if (lowsrc != null) {
  +        String lowsrcurl = url(this.lowsrc);
  +        if (lowsrcurl != null) {
               results.append(" lowsrc=\"");
               results.append(response.encodeURL(BeanUtils.filter(lowsrcurl)));
               results.append("\"");
           }
  -        if (alt != null) {
  +        tmp = alt();
  +        if (tmp != null) {
               results.append(" alt=\"");
  -            results.append(alt);
  +            results.append(tmp);
               results.append("\"");
           }
           if (imageName != null) {
  @@ -524,14 +596,18 @@
   
   	super.release();
           alt = null;
  +        altKey = null;
           border = null;
  +        bundle = Action.MESSAGES_KEY;
           height = null;
           hspace = null;
           imageName = null;
           ismap = null;
  +        locale = Action.LOCALE_KEY;
           lowsrc = null;
   	name = null;
           page = null;
  +        pageKey = null;
           paramId = null;
           paramName = null;
           paramProperty = null;
  @@ -539,6 +615,7 @@
   	property = null;
           scope = null;
   	src = null;
  +        srcKey = null;
           usemap = null;
           vspace = null;
           width = null;
  @@ -547,6 +624,147 @@
   
   
       // ------------------------------------------------------ Protected Methods
  +
  +
  +    /**
  +     * Return the alternate text to be included on this generated element,
  +     * or <code>null</code> if there is no such text.
  +     *
  +     * @exception JspException if an error occurs
  +     */
  +    protected String alt() throws JspException {
  +
  +        if (this.alt != null) {
  +            if (this.altKey != null) {
  +                JspException e = new JspException
  +                    (messages.getMessage("imgTag.alt"));
  +                RequestUtils.saveException(pageContext, e);
  +                throw e;
  +            } else {
  +                return (this.alt);
  +            }
  +        } else if (this.altKey != null) {
  +            MessageResources resources = (MessageResources)
  +                pageContext.getAttribute(this.bundle,
  +                                         PageContext.APPLICATION_SCOPE);
  +            if (resources == null) {
  +                JspException e = new JspException
  +                    (messages.getMessage("imgTag.bundle", this.bundle));
  +                throw e;
  +            }
  +            Locale locale = null;
  +            try {
  +                locale = (Locale)
  +                    pageContext.getAttribute(this.locale,
  +                                             PageContext.SESSION_SCOPE);
  +            } catch (IllegalStateException e) {
  +                locale = null; // Invalidated session
  +            }
  +            if (locale == null)
  +                locale = defaultLocale;
  +            return (resources.getMessage(locale, this.altKey));
  +        } else {
  +            return (null);
  +        }
  +
  +    }
  +
  +
  +    /**
  +     * Return the base source URL that will be rendered in the <code>src</code>
  +     * property for this generated element, or <code>null</code> if there is
  +     * no such URL.
  +     *
  +     * @exception JspException if an error occurs
  +     */
  +    protected String src() throws JspException {
  +
  +        // Deal with a direct context-relative page that has been specified
  +        if (this.page != null) {
  +            if ((this.src != null) || (this.srcKey != null) ||
  +                (this.pageKey != null)) {
  +                JspException e = new JspException
  +                    (messages.getMessage("imgTag.src"));
  +                RequestUtils.saveException(pageContext, e);
  +                throw e;
  +            }
  +            HttpServletRequest request =
  +                (HttpServletRequest) pageContext.getRequest();
  +            return (request.getContextPath() + this.page);
  +        }
  +
  +        // Deal with an indirect context-relative page that has been specified
  +        if (this.pageKey != null) {
  +            if ((this.src != null) || (this.srcKey != null)) {
  +                JspException e = new JspException
  +                    (messages.getMessage("imgTag.src"));
  +                RequestUtils.saveException(pageContext, e);
  +                throw e;
  +            }
  +            HttpServletRequest request =
  +                (HttpServletRequest) pageContext.getRequest();
  +            MessageResources resources = (MessageResources)
  +                pageContext.getAttribute(this.bundle,
  +                                         PageContext.APPLICATION_SCOPE);
  +            if (resources == null) {
  +                JspException e = new JspException
  +                    (messages.getMessage("imgTag.bundle", this.bundle));
  +                RequestUtils.saveException(pageContext, e);
  +            }
  +            Locale locale = null;
  +            try {
  +                locale = (Locale)
  +                    pageContext.getAttribute(this.locale,
  +                                             PageContext.SESSION_SCOPE);
  +            } catch (IllegalStateException e) {
  +                locale = null; // Invalidated session
  +            }
  +            if (locale == null)
  +                locale = defaultLocale;
  +            return (request.getContextPath() +
  +                    resources.getMessage(locale, this.pageKey));
  +        }
  +
  +        // Deal with an absolute source that has been specified
  +        if (this.src != null) {
  +            if (this.srcKey != null) {
  +                JspException e = new JspException
  +                    (messages.getMessage("imgTag.src"));
  +                RequestUtils.saveException(pageContext, e);
  +                throw e;
  +            }
  +            return (this.src);
  +        }
  +
  +        // Deal with an indirect source that has been specified
  +        if (this.srcKey == null) {
  +            JspException e = new JspException
  +                (messages.getMessage("imgTag.src"));
  +            RequestUtils.saveException(pageContext, e);
  +            throw e;
  +        }
  +        MessageResources resources = (MessageResources)
  +            pageContext.getAttribute(this.bundle,
  +                                     PageContext.APPLICATION_SCOPE);
  +        if (resources == null) {
  +            JspException e = new JspException
  +                (messages.getMessage("imgTag.bundle", this.bundle));
  +            RequestUtils.saveException(pageContext, e);
  +            throw e;
  +        }
  +        Locale locale = null;
  +        try {
  +            locale = (Locale)
  +                pageContext.getAttribute(this.locale,
  +                                         PageContext.SESSION_SCOPE);
  +        } catch (IllegalStateException e) {
  +            locale = null; // Invalidated session
  +        }
  +        if (locale == null)
  +            locale = defaultLocale;
  +        return (resources.getMessage(locale, this.srcKey));
  +
  +    }
   
   
       /**
  
  
  
  1.6       +3 -1      jakarta-struts/src/share/org/apache/struts/taglib/html/LocalStrings.properties
  
  Index: LocalStrings.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/LocalStrings.properties,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- LocalStrings.properties	2001/02/10 23:31:50	1.5
  +++ LocalStrings.properties	2001/02/20 00:18:53	1.6
  @@ -14,7 +14,9 @@
   getter.name=You must specify a name attribute if the property attribute is present
   getter.property=Property {0} returned a null value
   getter.result=Getter for property {0} threw exception: {1}
  -imgTag.source=You must specify either page or src
  +imgTag.alt=You may specify either alt or altKey but not both
  +imgTag.bundle=Cannot find message resources bundle under key {0}
  +imgTag.src=You must specify exactly one of src, srcKey, page, or pageKey
   imgTag.type=Object must be of type Map
   includeTag.include=Error including page {0}: {1}
   includeTag.lookup=Cannot find global forward named {0}