View Javadoc

1   /*
2    * $Id: ImageTag.java 376841 2006-02-10 21:01:28Z husted $
3    *
4    * Copyright 1999-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts.taglib.html;
19  
20  import org.apache.struts.Globals;
21  import org.apache.struts.config.ModuleConfig;
22  import org.apache.struts.taglib.TagUtils;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  import javax.servlet.jsp.JspException;
27  
28  /***
29   * Tag for input fields of type "image".
30   *
31   * @version $Rev: 376841 $ $Date: 2005-04-26 20:11:47 -0400 (Tue, 26 Apr 2005)
32   *          $
33   */
34  public class ImageTag extends SubmitTag {
35      // ------------------------------------------------------------- Properties
36  
37      /***
38       * The alignment for this image.
39       */
40      protected String align = null;
41  
42      /***
43       * The border size around the image.
44       */
45      protected String border = null;
46  
47      /***
48       * The module-relative URI of the image.
49       */
50      protected String page = null;
51  
52      /***
53       * The message resources key of the module-relative URI of the image.
54       */
55      protected String pageKey = null;
56  
57      /***
58       * The URL of this image.
59       */
60      protected String src = null;
61  
62      /***
63       * The message resources key for the URL of this image.
64       */
65      protected String srcKey = null;
66  
67      // --------------------------------------------------------- Constructor
68      public ImageTag() {
69          super();
70          property = "";
71      }
72  
73      /***
74       * @deprecated Align attribute is deprecated in HTML 4.x.
75       */
76      public String getAlign() {
77          return (this.align);
78      }
79  
80      /***
81       * @deprecated Align attribute is deprecated in HTML 4.x.
82       */
83      public void setAlign(String align) {
84          this.align = align;
85      }
86  
87      public String getBorder() {
88          return (this.border);
89      }
90  
91      public void setBorder(String border) {
92          this.border = border;
93      }
94  
95      public String getPage() {
96          return (this.page);
97      }
98  
99      public void setPage(String page) {
100         this.page = page;
101     }
102 
103     public String getPageKey() {
104         return (this.pageKey);
105     }
106 
107     public void setPageKey(String pageKey) {
108         this.pageKey = pageKey;
109     }
110 
111     public String getSrc() {
112         return (this.src);
113     }
114 
115     public void setSrc(String src) {
116         this.src = src;
117     }
118 
119     public String getSrcKey() {
120         return (this.srcKey);
121     }
122 
123     public void setSrcKey(String srcKey) {
124         this.srcKey = srcKey;
125     }
126 
127     // --------------------------------------------------------- Protected Methods
128 
129     /***
130      * Render the opening element.
131      *
132      * @return The opening part of the element.
133      */
134     protected String getElementOpen() {
135         return "<input type=\"image\"";
136     }
137 
138     /***
139      * Render the button attributes
140      *
141      * @param results The StringBuffer that output will be appended to.
142      */
143     protected void prepareButtonAttributes(StringBuffer results)
144         throws JspException {
145         String tmp = src();
146 
147         if (tmp != null) {
148             HttpServletResponse response =
149                 (HttpServletResponse) pageContext.getResponse();
150 
151             prepareAttribute(results, "src", response.encodeURL(tmp));
152         }
153 
154         prepareAttribute(results, "align", getAlign());
155         prepareAttribute(results, "border", getBorder());
156         prepareAttribute(results, "value", getValue());
157         prepareAttribute(results, "accesskey", getAccesskey());
158         prepareAttribute(results, "tabindex", getTabindex());
159     }
160 
161     /***
162      * Release any acquired resources.
163      */
164     public void release() {
165         super.release();
166         page = null;
167         pageKey = null;
168         property = "";
169         src = null;
170         srcKey = null;
171     }
172 
173     // ------------------------------------------------------ Protected Methods
174 
175     /***
176      * Return the base source URL that will be rendered in the
177      * <code>src</code> property for this generated element, or
178      * <code>null</code> if there is no such URL.
179      *
180      * @throws JspException if an error occurs
181      */
182     protected String src() throws JspException {
183         // Deal with a direct context-relative page that has been specified
184         if (this.page != null) {
185             if ((this.src != null) || (this.srcKey != null)
186                 || (this.pageKey != null)) {
187                 JspException e =
188                     new JspException(messages.getMessage("imgTag.src"));
189 
190                 TagUtils.getInstance().saveException(pageContext, e);
191                 throw e;
192             }
193 
194             ModuleConfig config =
195                 (ModuleConfig) pageContext.getRequest().getAttribute(Globals.MODULE_KEY);
196 
197             HttpServletRequest request =
198                 (HttpServletRequest) pageContext.getRequest();
199 
200             String pageValue = this.page;
201 
202             if (config != null) {
203                 pageValue =
204                     TagUtils.getInstance().pageURL(request, this.page, config);
205             }
206 
207             return (request.getContextPath() + pageValue);
208         }
209 
210         // Deal with an indirect context-relative page that has been specified
211         if (this.pageKey != null) {
212             if ((this.src != null) || (this.srcKey != null)) {
213                 JspException e =
214                     new JspException(messages.getMessage("imgTag.src"));
215 
216                 TagUtils.getInstance().saveException(pageContext, e);
217                 throw e;
218             }
219 
220             ModuleConfig config =
221                 (ModuleConfig) pageContext.getRequest().getAttribute(Globals.MODULE_KEY);
222 
223             HttpServletRequest request =
224                 (HttpServletRequest) pageContext.getRequest();
225 
226             String pageValue =
227                 TagUtils.getInstance().message(pageContext, getBundle(),
228                     getLocale(), this.pageKey);
229 
230             if (config != null) {
231                 pageValue =
232                     TagUtils.getInstance().pageURL(request, pageValue, config);
233             }
234 
235             return (request.getContextPath() + pageValue);
236         }
237 
238         // Deal with an absolute source that has been specified
239         if (this.src != null) {
240             if (this.srcKey != null) {
241                 JspException e =
242                     new JspException(messages.getMessage("imgTag.src"));
243 
244                 TagUtils.getInstance().saveException(pageContext, e);
245                 throw e;
246             }
247 
248             return (this.src);
249         }
250 
251         // Deal with an indirect source that has been specified
252         if (this.srcKey == null) {
253             JspException e =
254                 new JspException(messages.getMessage("imgTag.src"));
255 
256             TagUtils.getInstance().saveException(pageContext, e);
257             throw e;
258         }
259 
260         return TagUtils.getInstance().message(pageContext, getBundle(),
261             getLocale(), this.srcKey);
262     }
263 }