KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > view > jsp > html > ImgTag


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.view.jsp.html;
8
9
10 import java.io.IOException JavaDoc;
11
12 import javax.servlet.jsp.JspException JavaDoc;
13
14 import com.inversoft.verge.mvc.view.HtmlConstants;
15 import com.inversoft.verge.mvc.view.HtmlViewToolkit;
16 import com.inversoft.verge.mvc.view.jsp.JspTools;
17
18
19 /**
20  * <p>
21  * This class is an image tag for the framework that allows
22  * pages to insert images into the page that use the context
23  * path of the application. Mostly a convience tag. The syntax
24  * is like:
25  * </p>
26  *
27  * &lt;html:img SRC="../../../../../../../../somePage.jsp"&gt;<br>
28  * This will print to the page:<br>
29  * &lt;img SRC="../../../../../../../../ContextPath/somePage.jsp"&gt;<br>
30  *
31  * <p>
32  * The context attribute can be supplied to turn off this
33  * behavior and generate plain old paths.
34  * </p>
35  *
36  * @author Brian Pontarelli
37  */

38 public class ImgTag extends ContextBasedTag {
39
40     private String JavaDoc src;
41     protected String JavaDoc localSrc;
42     private String JavaDoc longdesc;
43     protected String JavaDoc localLongdesc;
44     private String JavaDoc width;
45     protected String JavaDoc localWidth;
46     private String JavaDoc height;
47     protected String JavaDoc localHeight;
48     private String JavaDoc border;
49     protected String JavaDoc localBorder;
50     private String JavaDoc usemap;
51     protected String JavaDoc localUsemap;
52     private String JavaDoc ismap;
53     protected String JavaDoc localIsmap;
54
55
56     /**
57      * Retrieves the tags src attribute
58      *
59      * @return Returns the tags src attribute
60      */

61     public String JavaDoc getSrc() {
62         return src;
63     }
64
65     /**
66      * Populates the tags src attribute
67      *
68      * @param src The value of the tags src attribute
69      */

70     public void setSrc(String JavaDoc src) {
71         this.src = src;
72     }
73
74     /**
75      * Retrieves the tags longdesc
76      *
77      * @return Returns the tags longdesc
78      */

79     public String JavaDoc getLongdesc() {
80         return longdesc;
81     }
82
83     /**
84      * Populates the tags longdesc
85      *
86      * @param longdesc The value of the tags longdesc
87      */

88     public void setLongdesc(String JavaDoc longdesc) {
89         this.longdesc = longdesc;
90     }
91
92     /**
93      * Retrieves the tags width attribute
94      *
95      * @return Returns the tags width attribute
96      */

97     public String JavaDoc getWidth() {
98         return width;
99     }
100
101     /**
102      * Populates the tags width attribute
103      *
104      * @param width The value of the tags width attribute
105      */

106     public void setWidth(String JavaDoc width) {
107         this.width = width;
108     }
109
110     /**
111      * Retrieves the tags height attribute
112      *
113      * @return Returns the tags height attribute
114      */

115     public String JavaDoc getHeight() {
116         return height;
117     }
118
119     /**
120      * Populates the tags height attribute
121      *
122      * @param height The value of the tags height attribute
123      */

124     public void setHeight(String JavaDoc height) {
125         this.height = height;
126     }
127
128     /**
129      * Retrieves the tags border attribute
130      *
131      * @return Returns the tags border attribute
132      */

133     public String JavaDoc getBorder() {
134         return border;
135     }
136
137     /**
138      * Populates the tags border attribute
139      *
140      * @param border The value of the tags border attribute
141      */

142     public void setBorder(String JavaDoc border) {
143         this.border = border;
144     }
145
146     /**
147      * Retrieves the tags usemap attribute
148      *
149      * @return Returns the tags usemap attribute
150      */

151     public String JavaDoc getUsemap() {
152         return usemap;
153     }
154
155     /**
156      * Populates the tags usemap attribute
157      *
158      * @param usemap The value of the tags usemap attribute
159      */

160     public void setUsemap(String JavaDoc usemap) {
161         this.usemap = usemap;
162     }
163
164     /**
165      * Retrieves the tags ismap attribute
166      *
167      * @return Returns the tags ismap attribute
168      */

169     public String JavaDoc getIsmap() {
170         return ismap;
171     }
172
173     /**
174      * Populates the tags ismap attribute
175      *
176      * @param ismap The value of the tags ismap attribute
177      */

178     public void setIsmap(String JavaDoc ismap) {
179         this.ismap = ismap;
180     }
181
182     /**
183      * Expands the variables if the container is not JSP 2.0 or greater.
184      *
185      * @throws JspException If there were any problems expanding the variables
186      */

187     protected void initialize() throws JspException JavaDoc {
188         super.initialize();
189
190         localSrc = src;
191         localLongdesc = longdesc;
192         localLongdesc = longdesc;
193         localWidth = width;
194         localHeight = height;
195         localBorder = border;
196         localUsemap = usemap;
197         localIsmap = ismap;
198
199         if (!JspTools.JSP_20) {
200             localSrc = (String JavaDoc) JspTools.expand("src", src, String JavaDoc.class, this,
201                 pageContext);
202             localLongdesc = (String JavaDoc) JspTools.expand("longdesc", longdesc,
203                 String JavaDoc.class, this, pageContext);
204             localWidth = (String JavaDoc) JspTools.expand("width", width, String JavaDoc.class,
205                 this, pageContext);
206             localHeight = (String JavaDoc) JspTools.expand("height", height,
207                 String JavaDoc.class, this, pageContext);
208             localBorder = (String JavaDoc) JspTools.expand("border", border,
209                 String JavaDoc.class, this, pageContext);
210             localUsemap = (String JavaDoc) JspTools.expand("usemap", usemap,
211                 String JavaDoc.class, this, pageContext);
212             localIsmap = (String JavaDoc) JspTools.expand("ismap", ismap,
213                 String JavaDoc.class, this, pageContext);
214         }
215
216         attributes.put(HtmlConstants.LONG_DESC, longdesc);
217         attributes.put(HtmlConstants.WIDTH, width);
218         attributes.put(HtmlConstants.HEIGHT, height);
219         attributes.put(HtmlConstants.BORDER, border);
220         attributes.put(HtmlConstants.USE_MAP, usemap);
221         attributes.put(HtmlConstants.IS_MAP, ismap);
222     }
223
224     /**
225      * Initializes the properties and outputs the image tag.
226      */

227     public int doStartTag() throws JspException JavaDoc {
228
229         initialize();
230
231         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(128);
232         createImgTag(buf, getId(), localName, localSrc);
233
234         try {
235             pageContext.getOut().write(buf.toString());
236         } catch (IOException JavaDoc ioe) {
237             throw new JspException JavaDoc(ioe);
238         }
239
240         return SKIP_BODY;
241     }
242
243     /**
244      * Appends the img tag to the given StringBuffer.
245      *
246      * @param buf The StringBuffer to append to
247      * @param id The id of the tag
248      * @param name The name of the tag
249      * @param src The src of the tag
250      * @throws JspException If there was any problem creating the final src URL
251      */

252     protected void createImgTag(StringBuffer JavaDoc buf, String JavaDoc id, String JavaDoc name,
253             String JavaDoc src)
254     throws JspException JavaDoc{
255         StringBuffer JavaDoc url = new StringBuffer JavaDoc();
256         appendContextPlusURL(url, src);
257         src = url.toString();
258
259         HtmlViewToolkit.createImgTag(buf, id, name, src, attributes, singleAttrs);
260     }
261 }
Popular Tags