2
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/ImgTag.java,v 1.41 2004/08/24 22:53:35 husted Exp $
4
* $Date: 2004/08/24 22:53:35 $
6
* Copyright 1999-2004 The Apache Software Foundation.
8
* Licensed under the Apache License, Version 2.0 (the "License");
9
* you may not use this file except in compliance with the License.
10
* You may obtain a copy of the License at
12
* http://www.apache.org/licenses/LICENSE-2.0
14
* Unless required by applicable law or agreed to in writing, software
15
* distributed under the License is distributed on an "AS IS" BASIS,
16
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
* See the License for the specific language governing permissions and
18
* limitations under the License.
21
package org.apache.struts.taglib.html;
23
import java.util.Iterator;
26
import javax.servlet.http.HttpServletRequest;
27
import javax.servlet.http.HttpServletResponse;
28
import javax.servlet.jsp.JspException;
30
import org.apache.struts.Globals;
31
import org.apache.struts.config.ModuleConfig;
32
import org.apache.struts.taglib.TagUtils;
33
import org.apache.struts.util.MessageResources;
34
import org.apache.struts.util.ModuleUtils;
37
* Generate an IMG tag to the specified image URI.
41
* <li>Make the <strong>alt</strong>, <strong>src</strong>, and
42
* <strong>lowsrc</strong> settable from properties (for i18n)</li>
45
* @version $Revision: 1.41 $
48
public class ImgTag extends BaseHandlerTag {
50
// ------------------------------------------------------------- Properties
53
* The property to specify where to align the image.
55
protected String align = null;
57
public String getAlign() {
61
public void setAlign(String align) {
66
* The border size around the image.
68
protected String border = null;
70
public String getBorder() {
74
public void setBorder(String border) {
79
* The property to specify how to root the image.
80
* If 'true' or if there is no current module the image is
81
* rooted to the application context path
82
* If 'false' or absent the image is rooted to the current
83
* module's context path.
84
* @deprecated Use module property instead; will be removed in a release after 1.2.0.
86
protected String contextRelative = null;
88
/** @deprecated Use module property instead; will be removed in a release after 1.2.0.
90
public String getContextRelative() {
91
return (this.contextRelative);
94
/** @deprecated Use module property instead; will be removed in a release after 1.2.0.
96
public void setContextRelative(String contextRelative) {
97
this.contextRelative = contextRelative;
102
* Convenience method to return true if contextRelative set to "true".
103
* @return True if contextRelative set to "true"
104
* @deprecated Use module property instead; will be removed in a release after 1.2.0.
106
public boolean isContextRelativeSet() {
107
return Boolean.valueOf(this.contextRelative).booleanValue();
113
protected String height = null;
115
public String getHeight() {
116
return (this.height);
119
public void setHeight(String height) {
120
this.height = height;
124
* The horizontal spacing around the image.
126
protected String hspace = null;
128
public String getHspace() {
129
return (this.hspace);
132
public void setHspace(String hspace) {
133
this.hspace = hspace;
137
* The image name for named images.
139
protected String imageName = null;
141
public String getImageName() {
142
return (this.imageName);
145
public void setImageName(String imageName) {
146
this.imageName = imageName;
150
* Server-side image map declaration.
152
protected String ismap = null;
154
public String getIsmap() {
158
public void setIsmap(String ismap) {
163
* The low resolution image source URI.
164
* @deprecated This is not defined in the HTML 4.01 spec and will be removed in a
165
* future version of Struts.
167
protected String lowsrc = null;
170
* @deprecated This is not defined in the HTML 4.01 spec and will be removed in a
171
* future version of Struts.
173
public String getLowsrc() {
174
return (this.lowsrc);
177
public void setLowsrc(String lowsrc) {
178
this.lowsrc = lowsrc;
182
* The message resources for this package.
184
protected static MessageResources messages =
185
MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
188
* The JSP bean name for query parameters.
190
protected String name = null;
192
public String getName() {
196
public void setName(String name) {
201
* The module-relative path, starting with a slash character, of the
202
* image to be displayed by this rendered tag.
204
protected String page = null;
206
public String getPage() {
210
public void setPage(String page) {
215
* The message resources key under which we should look up the
216
* <code>page</code> attribute for this generated tag, if any.
218
protected String pageKey = null;
220
public String getPageKey() {
221
return (this.pageKey);
224
public void setPageKey(String pageKey) {
225
this.pageKey = pageKey;
229
* The module-relative action (beginning with a slash) which will be
230
* used as the source for this image.
232
protected String action = null;
234
public String getAction() {
235
return (this.action);
238
public void setAction(String action) {
239
this.action = action;
243
* The module prefix (beginning with a slash) which will be
244
* used to find the action for this link.
246
protected String module = null;
248
public String getModule() {
249
return (this.module);
252
public void setModule(String module) {
253
this.module = module;
257
* In situations where an image is dynamically generated (such as to create
258
* a chart graph), this specifies the single-parameter request parameter
261
protected String paramId = null;
263
public String getParamId() {
264
return (this.paramId);
267
public void setParamId(String paramId) {
268
this.paramId = paramId;
272
* The single-parameter JSP bean name.
274
protected String paramName = null;
276
public String getParamName() {
277
return (this.paramName);
280
public void setParamName(String paramName) {
281
this.paramName = paramName;
285
* The single-parameter JSP bean property.
287
protected String paramProperty = null;
289
public String getParamProperty() {
290
return (this.paramProperty);
293
public void setParamProperty(String paramProperty) {
294
this.paramProperty = paramProperty;
298
* The single-parameter JSP bean scope.
300
protected String paramScope = null;
302
public String getParamScope() {
303
return (this.paramScope);
306
public void setParamScope(String paramScope) {
307
this.paramScope = paramScope;
311
* The JSP bean property name for query parameters.
313
protected String property = null;
315
public String getProperty() {
316
return (this.property);
319
public void setProperty(String property) {
320
this.property = property;
324
* The scope of the bean specified by the name property, if any.
326
protected String scope = null;
328
public String getScope() {
332
public void setScope(String scope) {
337
* The image source URI.
339
protected String src = null;
341
public String getSrc() {
345
public void setSrc(String src) {
350
* The message resources key under which we should look up the
351
* <code>src</code> attribute for this generated tag, if any.
353
protected String srcKey = null;
355
public String getSrcKey() {
356
return (this.srcKey);
359
public void setSrcKey(String srcKey) {
360
this.srcKey = srcKey;
364
* Client-side image map declaration.
366
protected String usemap = null;
368
public String getUsemap() {
369
return (this.usemap);
372
public void setUsemap(String usemap) {
373
this.usemap = usemap;
377
* The vertical spacing around the image.
379
protected String vspace = null;
381
public String getVspace() {
382
return (this.vspace);
385
public void setVspace(String vspace) {
386
this.vspace = vspace;
392
protected String width = null;
394
public String getWidth() {
398
public void setWidth(String width) {
402
protected boolean useLocalEncoding = false;
404
public boolean isUseLocalEncoding() {
405
return useLocalEncoding;
408
public void setUseLocalEncoding(boolean b) {
409
useLocalEncoding = b;
412
// --------------------------------------------------------- Public Methods
415
* Render the beginning of the IMG tag.
417
* @exception JspException if a JSP exception has occurred
419
public int doStartTag() throws JspException {
421
// Evaluate the body of this tag
422
return (EVAL_BODY_TAG);
427
* Render the end of the IMG tag.
429
* @exception JspException if a JSP exception has occurred
431
public int doEndTag() throws JspException {
433
// Generate the name definition or image element
434
HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
435
StringBuffer results = new StringBuffer("<img");
437
String srcurl = url(tmp);
438
if (srcurl != null) {
439
results.append(" src=\"");
440
results.append(response.encodeURL(srcurl));
441
results.append("\"");
443
String lowsrcurl = url(this.lowsrc);
444
if (lowsrcurl != null) {
445
results.append(" lowsrc=\"");
446
results.append(response.encodeURL(lowsrcurl));
447
results.append("\"");
449
if (imageName != null) {
450
results.append(" name=\"");
451
results.append(imageName);
452
results.append("\"");
454
if (height != null) {
455
results.append(" height=\"");
456
results.append(height);
457
results.append("\"");
460
results.append(" width=\"");
461
results.append(width);
462
results.append("\"");
465
results.append(" align=\"");
466
results.append(align);
467
results.append("\"");
469
if (border != null) {
470
results.append(" border=\"");
471
results.append(border);
472
results.append("\"");
474
if (hspace != null) {
475
results.append(" hspace=\"");
476
results.append(hspace);
477
results.append("\"");
479
if (vspace != null) {
480
results.append(" vspace=\"");
481
results.append(vspace);
482
results.append("\"");
485
results.append(" ismap=\"");
486
results.append(ismap);
487
results.append("\"");
489
if (usemap != null) {
490
results.append(" usemap=\"");
491
results.append(usemap);
492
results.append("\"");
494
results.append(prepareStyles());
495
results.append(prepareEventHandlers());
496
results.append(getElementClose());
498
TagUtils.getInstance().write(pageContext, results.toString());
505
* Release any acquired resources.
507
public void release() {
523
paramProperty = null;
535
// ------------------------------------------------------ Protected Methods
538
* Convenience method to throw a "imgTag.src" exception.
539
* @throws JspException
541
private void throwImgTagSrcException() throws JspException {
542
JspException e = new JspException(messages.getMessage("imgTag.src"));
543
TagUtils.getInstance().saveException(pageContext, e);
548
* Convenience method to test whether this is the default module
549
* or if contestRelative has been set.
550
* @param config Our Moduleconfig
551
* @return True if this is the default module or contextRelative is set
553
private boolean srcDefaultReference(ModuleConfig config) {
554
return (config == null || isContextRelativeSet());
558
* Return the base source URL that will be rendered in the <code>src</code>
559
* property for this generated element, or <code>null</code> if there is
562
* @exception JspException if an error occurs
564
protected String src() throws JspException {
566
// Deal with a direct context-relative page that has been specified
567
if (this.page != null) {
568
if ((this.src != null) || (this.srcKey != null) || (this.pageKey != null)) {
569
throwImgTagSrcException();
571
ModuleConfig config =
572
ModuleUtils.getInstance().getModuleConfig(
574
(HttpServletRequest) pageContext.getRequest(),
575
pageContext.getServletContext());
577
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
578
if (srcDefaultReference(config)) {
579
return (request.getContextPath() + this.page);
581
return (request.getContextPath() + config.getPrefix() + this.page);
585
// Deal with an indirect context-relative page that has been specified
586
if (this.pageKey != null) {
587
if ((this.src != null) || (this.srcKey != null)) {
588
throwImgTagSrcException();
590
ModuleConfig config =
591
ModuleUtils.getInstance().getModuleConfig(
593
(HttpServletRequest) pageContext.getRequest(),
594
pageContext.getServletContext());
596
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
597
if (srcDefaultReference(config)) {
599
request.getContextPath()
600
+ TagUtils.getInstance().message(
607
request.getContextPath()
609
+ TagUtils.getInstance().message(
617
if (this.action != null) {
618
if ((this.src != null) || (this.srcKey != null)) {
619
throwImgTagSrcException();
621
return TagUtils.getInstance().getActionMappingURL(action, module, pageContext, isContextRelativeSet());
624
// Deal with an absolute source that has been specified
625
if (this.src != null) {
626
if (this.srcKey != null) {
627
throwImgTagSrcException();
632
// Deal with an indirect source that has been specified
633
if (this.srcKey == null) {
634
throwImgTagSrcException();
637
return TagUtils.getInstance().message(
646
* Return the specified src URL, modified as necessary with optional
647
* request parameters.
649
* @param url The URL to be modified (or null if this url will not be used)
651
* @exception JspException if an error occurs preparing the URL
653
protected String url(String url) throws JspException {
659
String charEncoding = "UTF-8";
660
if(useLocalEncoding){
661
charEncoding = pageContext.getResponse().getCharacterEncoding();
664
// Start with an unadorned URL as specified
665
StringBuffer src = new StringBuffer(url);
667
// Append a single-parameter name and value, if requested
668
if ((paramId != null) && (paramName != null)) {
669
if (src.toString().indexOf('?') < 0) {
676
Object value = TagUtils.getInstance().lookup(pageContext, paramName, paramProperty, paramScope);
678
src.append(TagUtils.getInstance().encodeURL(value.toString(), charEncoding));
681
// Just return the URL if there is no bean to look up
682
if ((property != null) && (name == null)) {
683
JspException e = new JspException(messages.getMessage("getter.name"));
684
TagUtils.getInstance().saveException(pageContext, e);
689
return (src.toString());
692
// Look up the map we will be using
693
Object mapObject = TagUtils.getInstance().lookup(pageContext, name, property, scope);
696
map = (Map) mapObject;
697
} catch (ClassCastException e) {
698
TagUtils.getInstance().saveException(pageContext, e);
699
throw new JspException(messages.getMessage("imgTag.type"));
702
// Append the required query parameters
703
boolean question = (src.toString().indexOf("?") >= 0);
704
Iterator keys = map.keySet().iterator();
705
while (keys.hasNext()) {
706
String key = (String) keys.next();
707
Object value = map.get(key);
717
// Interpret null as "no value specified"
718
} else if (value instanceof String[]) {
719
String values[] = (String[]) value;
720
for (int i = 0; i < values.length; i++) {
729
src.append(TagUtils.getInstance().encodeURL(values[i], charEncoding));
741
src.append(TagUtils.getInstance().encodeURL(value.toString(), charEncoding));
745
// Return the final result
746
return (src.toString());