KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > html > ImgTag


1 /*
2  * $Id: ImgTag.java 164927 2005-04-27 00:41:05Z niallp $
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
19 package org.apache.struts.taglib.html;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26 import javax.servlet.jsp.JspException JavaDoc;
27
28 import org.apache.struts.Globals;
29 import org.apache.struts.config.ModuleConfig;
30 import org.apache.struts.taglib.TagUtils;
31 import org.apache.struts.util.MessageResources;
32 import org.apache.struts.util.ModuleUtils;
33
34 /**
35  * Generate an IMG tag to the specified image URI.
36  * <p>
37  * TODO:
38  * <ul>
39  * <li>Make the <strong>alt</strong>, <strong>src</strong>, and
40  * <strong>lowsrc</strong> settable from properties (for i18n)</li>
41  * </ul>
42  *
43  * @version $Rev: 164927 $
44  */

45
46 public class ImgTag extends BaseHandlerTag {
47
48     // ----------------------------------------------------- Constructor
49

50     public ImgTag() {
51         super();
52         doDisabled = false;
53     }
54
55     // ------------------------------------------------------------- Properties
56

57      /**
58      * The property to specify where to align the image.
59      */

60     protected String JavaDoc align = null;
61
62     public String JavaDoc getAlign() {
63         return (this.align);
64     }
65
66     public void setAlign(String JavaDoc align) {
67         this.align = align;
68     }
69
70     /**
71      * The border size around the image.
72      */

73     protected String JavaDoc border = null;
74
75     public String JavaDoc getBorder() {
76         return (this.border);
77     }
78
79     public void setBorder(String JavaDoc border) {
80         this.border = border;
81     }
82
83     /**
84       * The property to specify how to root the image.
85       * If 'true' or if there is no current module the image is
86       * rooted to the application context path
87       * If 'false' or absent the image is rooted to the current
88       * module's context path.
89       * @deprecated Use module property instead; will be removed in a release after 1.2.0.
90       */

91      protected String JavaDoc contextRelative = null;
92
93      /** @deprecated Use module property instead; will be removed in a release after 1.2.0.
94       */

95      public String JavaDoc getContextRelative() {
96          return (this.contextRelative);
97      }
98
99     /** @deprecated Use module property instead; will be removed in a release after 1.2.0.
100      */

101      public void setContextRelative(String JavaDoc contextRelative) {
102          this.contextRelative = contextRelative;
103      }
104
105
106     /**
107      * Convenience method to return true if contextRelative set to "true".
108      * @return True if contextRelative set to "true"
109      * @deprecated Use module property instead; will be removed in a release after 1.2.0.
110      */

111     public boolean isContextRelativeSet() {
112         return Boolean.valueOf(this.contextRelative).booleanValue();
113     }
114
115     /**
116      * The image height.
117      */

118     protected String JavaDoc height = null;
119
120     public String JavaDoc getHeight() {
121         return (this.height);
122     }
123
124     public void setHeight(String JavaDoc height) {
125         this.height = height;
126     }
127
128     /**
129      * The horizontal spacing around the image.
130      */

131     protected String JavaDoc hspace = null;
132
133     public String JavaDoc getHspace() {
134         return (this.hspace);
135     }
136
137     public void setHspace(String JavaDoc hspace) {
138         this.hspace = hspace;
139     }
140
141     /**
142      * The image name for named images.
143      */

144     protected String JavaDoc imageName = null;
145
146     public String JavaDoc getImageName() {
147         return (this.imageName);
148     }
149
150     public void setImageName(String JavaDoc imageName) {
151         this.imageName = imageName;
152     }
153
154     /**
155      * Server-side image map declaration.
156      */

157     protected String JavaDoc ismap = null;
158
159     public String JavaDoc getIsmap() {
160         return (this.ismap);
161     }
162
163     public void setIsmap(String JavaDoc ismap) {
164         this.ismap = ismap;
165     }
166
167     /**
168      * The low resolution image source URI.
169      * @deprecated This is not defined in the HTML 4.01 spec and will be removed in a
170      * future version of Struts.
171      */

172     protected String JavaDoc lowsrc = null;
173
174     /**
175      * @deprecated This is not defined in the HTML 4.01 spec and will be removed in a
176      * future version of Struts.
177      */

178     public String JavaDoc getLowsrc() {
179         return (this.lowsrc);
180     }
181
182     public void setLowsrc(String JavaDoc lowsrc) {
183         this.lowsrc = lowsrc;
184     }
185
186     /**
187      * The message resources for this package.
188      */

189     protected static MessageResources messages =
190         MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
191
192     /**
193      * The JSP bean name for query parameters.
194      */

195     protected String JavaDoc name = null;
196
197     public String JavaDoc getName() {
198         return (this.name);
199     }
200
201     public void setName(String JavaDoc name) {
202         this.name = name;
203     }
204
205     /**
206      * The module-relative path, starting with a slash character, of the
207      * image to be displayed by this rendered tag.
208      */

209     protected String JavaDoc page = null;
210
211     public String JavaDoc getPage() {
212         return (this.page);
213     }
214
215     public void setPage(String JavaDoc page) {
216         this.page = page;
217     }
218
219     /**
220      * The message resources key under which we should look up the
221      * <code>page</code> attribute for this generated tag, if any.
222      */

223     protected String JavaDoc pageKey = null;
224
225     public String JavaDoc getPageKey() {
226         return (this.pageKey);
227     }
228
229     public void setPageKey(String JavaDoc pageKey) {
230         this.pageKey = pageKey;
231     }
232
233     /**
234      * The module-relative action (beginning with a slash) which will be
235      * used as the source for this image.
236      */

237     protected String JavaDoc action = null;
238
239     public String JavaDoc getAction() {
240         return (this.action);
241     }
242
243     public void setAction(String JavaDoc action) {
244         this.action = action;
245     }
246
247     /**
248      * The module prefix (beginning with a slash) which will be
249      * used to find the action for this link.
250      */

251     protected String JavaDoc module = null;
252
253     public String JavaDoc getModule() {
254         return (this.module);
255     }
256
257     public void setModule(String JavaDoc module) {
258         this.module = module;
259     }
260
261     /**
262      * In situations where an image is dynamically generated (such as to create
263      * a chart graph), this specifies the single-parameter request parameter
264      * name to generate.
265      */

266     protected String JavaDoc paramId = null;
267
268     public String JavaDoc getParamId() {
269         return (this.paramId);
270     }
271
272     public void setParamId(String JavaDoc paramId) {
273         this.paramId = paramId;
274     }
275
276     /**
277      * The single-parameter JSP bean name.
278      */

279     protected String JavaDoc paramName = null;
280
281     public String JavaDoc getParamName() {
282         return (this.paramName);
283     }
284
285     public void setParamName(String JavaDoc paramName) {
286         this.paramName = paramName;
287     }
288
289     /**
290      * The single-parameter JSP bean property.
291      */

292     protected String JavaDoc paramProperty = null;
293
294     public String JavaDoc getParamProperty() {
295         return (this.paramProperty);
296     }
297
298     public void setParamProperty(String JavaDoc paramProperty) {
299         this.paramProperty = paramProperty;
300     }
301
302     /**
303      * The single-parameter JSP bean scope.
304      */

305     protected String JavaDoc paramScope = null;
306
307     public String JavaDoc getParamScope() {
308         return (this.paramScope);
309     }
310
311     public void setParamScope(String JavaDoc paramScope) {
312         this.paramScope = paramScope;
313     }
314
315     /**
316      * The JSP bean property name for query parameters.
317      */

318     protected String JavaDoc property = null;
319
320     public String JavaDoc getProperty() {
321         return (this.property);
322     }
323
324     public void setProperty(String JavaDoc property) {
325         this.property = property;
326     }
327
328     /**
329      * The scope of the bean specified by the name property, if any.
330      */

331     protected String JavaDoc scope = null;
332
333     public String JavaDoc getScope() {
334         return (this.scope);
335     }
336
337     public void setScope(String JavaDoc scope) {
338         this.scope = scope;
339     }
340
341     /**
342      * The image source URI.
343      */

344     protected String JavaDoc src = null;
345
346     public String JavaDoc getSrc() {
347         return (this.src);
348     }
349
350     public void setSrc(String JavaDoc src) {
351         this.src = src;
352     }
353
354     /**
355      * The message resources key under which we should look up the
356      * <code>src</code> attribute for this generated tag, if any.
357      */

358     protected String JavaDoc srcKey = null;
359
360     public String JavaDoc getSrcKey() {
361         return (this.srcKey);
362     }
363
364     public void setSrcKey(String JavaDoc srcKey) {
365         this.srcKey = srcKey;
366     }
367
368     /**
369      * Client-side image map declaration.
370      */

371     protected String JavaDoc usemap = null;
372
373     public String JavaDoc getUsemap() {
374         return (this.usemap);
375     }
376
377     public void setUsemap(String JavaDoc usemap) {
378         this.usemap = usemap;
379     }
380
381     /**
382      * The vertical spacing around the image.
383      */

384     protected String JavaDoc vspace = null;
385
386     public String JavaDoc getVspace() {
387         return (this.vspace);
388     }
389
390     public void setVspace(String JavaDoc vspace) {
391         this.vspace = vspace;
392     }
393
394     /**
395      * The image width.
396      */

397     protected String JavaDoc width = null;
398
399     public String JavaDoc getWidth() {
400         return (this.width);
401     }
402
403     public void setWidth(String JavaDoc width) {
404         this.width = width;
405     }
406
407     protected boolean useLocalEncoding = false;
408     
409     public boolean isUseLocalEncoding() {
410        return useLocalEncoding;
411     }
412
413     public void setUseLocalEncoding(boolean b) {
414        useLocalEncoding = b;
415     }
416    
417    // --------------------------------------------------------- Public Methods
418

419     /**
420      * Render the beginning of the IMG tag.
421      *
422      * @exception JspException if a JSP exception has occurred
423      */

424     public int doStartTag() throws JspException JavaDoc {
425
426         // Evaluate the body of this tag
427
return (EVAL_BODY_TAG);
428
429     }
430
431     /**
432      * Render the end of the IMG tag.
433      *
434      * @exception JspException if a JSP exception has occurred
435      */

436     public int doEndTag() throws JspException JavaDoc {
437
438         // Generate the name definition or image element
439
HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) pageContext.getResponse();
440         StringBuffer JavaDoc results = new StringBuffer JavaDoc("<img");
441         String JavaDoc tmp = src();
442         String JavaDoc srcurl = url(tmp);
443         if (srcurl != null) {
444             prepareAttribute(results, "src", response.encodeURL(srcurl));
445         }
446         String JavaDoc lowsrcurl = url(this.lowsrc);
447         if (lowsrcurl != null) {
448             prepareAttribute(results, "lowsrcurl", response.encodeURL(lowsrcurl));
449         }
450
451         prepareAttribute(results, "name", getImageName());
452         prepareAttribute(results, "height", getHeight());
453         prepareAttribute(results, "width", getWidth());
454         prepareAttribute(results, "align", getAlign());
455         prepareAttribute(results, "border", getBorder());
456         prepareAttribute(results, "hspace", getHspace());
457         prepareAttribute(results, "vspace", getVspace());
458         prepareAttribute(results, "ismap", getIsmap());
459         prepareAttribute(results, "usemap", getUsemap());
460         results.append(prepareStyles());
461         results.append(prepareEventHandlers());
462         prepareOtherAttributes(results);
463         results.append(getElementClose());
464
465         TagUtils.getInstance().write(pageContext, results.toString());
466
467         return (EVAL_PAGE);
468
469     }
470
471     /**
472      * Release any acquired resources.
473      */

474     public void release() {
475
476         super.release();
477
478         border = null;
479         height = null;
480         hspace = null;
481         imageName = null;
482         ismap = null;
483         lowsrc = null;
484         name = null;
485         page = null;
486         pageKey = null;
487         action = null;
488         paramId = null;
489         paramName = null;
490         paramProperty = null;
491         paramScope = null;
492         property = null;
493         scope = null;
494         src = null;
495         srcKey = null;
496         usemap = null;
497         vspace = null;
498         width = null;
499
500     }
501
502     // ------------------------------------------------------ Protected Methods
503

504     /**
505      * Convenience method to throw a "imgTag.src" exception.
506      * @throws JspException
507      */

508     private void throwImgTagSrcException() throws JspException JavaDoc {
509         JspException JavaDoc e = new JspException JavaDoc(messages.getMessage("imgTag.src"));
510         TagUtils.getInstance().saveException(pageContext, e);
511         throw e;
512     }
513
514     /**
515      * Convenience method to test whether this is the default module
516      * or if contestRelative has been set.
517      * @param config Our Moduleconfig
518      * @return True if this is the default module or contextRelative is set
519      */

520     private boolean srcDefaultReference(ModuleConfig config) {
521        return (config == null || isContextRelativeSet());
522     }
523
524     /**
525      * Return the base source URL that will be rendered in the <code>src</code>
526      * property for this generated element, or <code>null</code> if there is
527      * no such URL.
528      *
529      * @exception JspException if an error occurs
530      */

531     protected String JavaDoc src() throws JspException JavaDoc {
532
533         // Deal with a direct context-relative page that has been specified
534
if (this.page != null) {
535             if ((this.src != null) || (this.srcKey != null) || (this.pageKey != null)) {
536                 throwImgTagSrcException();
537             }
538             ModuleConfig config =
539                 ModuleUtils.getInstance().getModuleConfig(
540                         this.module,
541                         (HttpServletRequest JavaDoc) pageContext.getRequest(),
542                         pageContext.getServletContext());
543             
544             HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
545             String JavaDoc pageValue = this.page;
546             if (!srcDefaultReference(config)) {
547                 pageValue = TagUtils.getInstance().pageURL(request,
548                                                            this.page,
549                                                            config);
550             }
551             return (request.getContextPath() + pageValue);
552         }
553
554         // Deal with an indirect context-relative page that has been specified
555
if (this.pageKey != null) {
556             if ((this.src != null) || (this.srcKey != null)) {
557                 throwImgTagSrcException();
558             }
559             ModuleConfig config =
560                 ModuleUtils.getInstance().getModuleConfig(
561                         this.module,
562                         (HttpServletRequest JavaDoc) pageContext.getRequest(),
563                         pageContext.getServletContext());
564             
565             HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
566             String JavaDoc pageValue = TagUtils.getInstance().message(
567                                           pageContext,
568                                           getBundle(),
569                                           getLocale(),
570                                           this.pageKey);
571             if (!srcDefaultReference(config)) {
572                 pageValue = TagUtils.getInstance().pageURL(request,
573                                                            pageValue,
574                                                            config);
575             }
576             return (request.getContextPath() + pageValue);
577         }
578
579         if (this.action != null) {
580             if ((this.src != null) || (this.srcKey != null)) {
581                 throwImgTagSrcException();
582             }
583             return TagUtils.getInstance().getActionMappingURL(action, module, pageContext, isContextRelativeSet());
584         }
585
586         // Deal with an absolute source that has been specified
587
if (this.src != null) {
588             if (this.srcKey != null) {
589                 throwImgTagSrcException();
590             }
591             return (this.src);
592         }
593
594         // Deal with an indirect source that has been specified
595
if (this.srcKey == null) {
596             throwImgTagSrcException();
597         }
598         
599         return TagUtils.getInstance().message(
600             pageContext,
601             getBundle(),
602             getLocale(),
603             this.srcKey);
604
605     }
606
607     /**
608      * Return the specified src URL, modified as necessary with optional
609      * request parameters.
610      *
611      * @param url The URL to be modified (or null if this url will not be used)
612      *
613      * @exception JspException if an error occurs preparing the URL
614      */

615     protected String JavaDoc url(String JavaDoc url) throws JspException JavaDoc {
616
617         if (url == null) {
618             return (url);
619         }
620
621         String JavaDoc charEncoding = "UTF-8";
622         if(useLocalEncoding){
623             charEncoding = pageContext.getResponse().getCharacterEncoding();
624         }
625         
626         // Start with an unadorned URL as specified
627
StringBuffer JavaDoc src = new StringBuffer JavaDoc(url);
628
629         // Append a single-parameter name and value, if requested
630
if ((paramId != null) && (paramName != null)) {
631             if (src.toString().indexOf('?') < 0) {
632                 src.append('?');
633             } else {
634                 src.append("&amp;");
635             }
636             src.append(paramId);
637             src.append('=');
638             Object JavaDoc value = TagUtils.getInstance().lookup(pageContext, paramName, paramProperty, paramScope);
639             if (value != null)
640                 src.append(TagUtils.getInstance().encodeURL(value.toString(), charEncoding));
641         }
642
643         // Just return the URL if there is no bean to look up
644
if ((property != null) && (name == null)) {
645             JspException JavaDoc e = new JspException JavaDoc(messages.getMessage("getter.name"));
646             TagUtils.getInstance().saveException(pageContext, e);
647             throw e;
648         }
649
650         if (name == null) {
651             return (src.toString());
652         }
653
654         // Look up the map we will be using
655
Object JavaDoc mapObject = TagUtils.getInstance().lookup(pageContext, name, property, scope);
656         Map JavaDoc map = null;
657         try {
658             map = (Map JavaDoc) mapObject;
659         } catch (ClassCastException JavaDoc e) {
660             TagUtils.getInstance().saveException(pageContext, e);
661             throw new JspException JavaDoc(messages.getMessage("imgTag.type"));
662         }
663
664         // Append the required query parameters
665
boolean question = (src.toString().indexOf("?") >= 0);
666         Iterator JavaDoc keys = map.keySet().iterator();
667         while (keys.hasNext()) {
668             String JavaDoc key = (String JavaDoc) keys.next();
669             Object JavaDoc value = map.get(key);
670             if (value == null) {
671                 if (question) {
672                     src.append("&amp;");
673                 } else {
674                     src.append('?');
675                     question = true;
676                 }
677                 src.append(key);
678                 src.append('=');
679                 // Interpret null as "no value specified"
680
} else if (value instanceof String JavaDoc[]) {
681                 String JavaDoc values[] = (String JavaDoc[]) value;
682                 for (int i = 0; i < values.length; i++) {
683                     if (question) {
684                         src.append("&amp;");
685                     } else {
686                         src.append('?');
687                         question = true;
688                     }
689                     src.append(key);
690                     src.append('=');
691                     src.append(TagUtils.getInstance().encodeURL(values[i], charEncoding));
692                 }
693             } else {
694
695                 if (question) {
696                     src.append("&amp;");
697                 } else {
698                     src.append('?');
699                     question = true;
700                 }
701                 src.append(key);
702                 src.append('=');
703                 src.append(TagUtils.getInstance().encodeURL(value.toString(), charEncoding));
704             }
705         }
706
707         // Return the final result
708
return (src.toString());
709
710     }
711
712 }
713
Popular Tags