1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 package com.ivata.mask.web.tag.util;
75
76 import java.util.Map;
77
78 import javax.servlet.jsp.JspException;
79 import javax.servlet.jsp.JspWriter;
80 import javax.servlet.jsp.tagext.BodyContent;
81 import javax.servlet.jsp.tagext.BodyTagSupport;
82
83
84 /***
85 * <p>This tag must always be contained within a {@link MapTag MapTag}
86 * and defines the name and value of an entry in the map.</p>
87 *
88 * <p><b>Tag attributes:</b><br/>
89 * <table cellpadding='2' cellspacing='5' border='0' align='center'
90 * width='85%'>
91 * <tr class='TableHeadingColor'>
92 * <th>attribute</th>
93 * <th>reqd.</th>
94 * <th>param. class</th>
95 * <th width='100%'>description</th>
96 * </tr>
97 * <tr class='TableRowColor'>
98 * <td>mapName</td>
99 * <td>false</td>
100 * <td><code>String</code></td>
101 * <td>If the <code>mapEntry</code> is not inside a
102 * <code>map</code> tag, use this attribute to specify a
103 * <code>java.util.Map</code> instance in the current page
104 * context.</td>
105 * </tr>
106 * <tr class='TableRowColor'>
107 * <td>name</td>
108 * <td>true</td>
109 * <td><code>String</code></td>
110 * <td>Specifies the name of the entry to put in the parent
111 * map.</td>
112 * </tr>
113 * <tr class='TableRowColor'>
114 * <td>value</td>
115 * <td>false</td>
116 * <td><code>String</code></td>
117 * <td>Specifies the value of the entry with the name
118 * speciified. If this attribute is not specified, you should specify
119 * a tag body.</td>
120 * </tr>
121 * </table>
122 * </p>
123 *
124 *
125 * @since ivata masks 0.5 (2002-01-23)
126 * @author Colin MacLeod
127 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
128 * @version $Revision: 1.4 $
129 */
130 public class MapEntryTag extends BodyTagSupport {
131 /***
132 * <p>If the <code>mapEntry</code> is not inside a <code>map</code>
133 * tag, use this attribute to specify a <code>java.util.Map</code>
134 * instance in the current page context.</p>
135 */
136 private String mapName = null;
137 /***
138 * <p>Refers to the parent of this map item.</p>
139 */
140 private MapTag mapTag;
141 /***
142 * <p>Specifies the name of the entry to put in the parent map.</p>
143 */
144 private String name;
145 /***
146 * <p>Specifies the value of the entry to put in the parent map.</p>
147 */
148 private String value;
149
150 /***
151 * <p>This method is called after the JSP engine processes the body
152 * content of the tag.</p>
153 * @exception JspException encapsulates any exception when calling
154 * {@link #writeTagBodyContent writeTagBodyContent}.
155 * @return <code>SKIP_BODY</code> since we only ever want to evaluate
156 * this tag once.
157 * @see #writeTagBodyContent( JspWriter out, BodyContent bodyContent
158 * )
159 *
160 */
161 public int doAfterBody() throws JspException {
162 if (getBodyContent() != null) {
163 value = getBodyContent().getString();
164 }
165 return SKIP_BODY;
166 }
167
168 /***
169 * <p>This method is called after the JSP engine finished processing
170 * the tag.</p>
171 *
172 * @exception JspException encapsulates any exception when calling
173 * <code>out.println</code>
174 * @return <code>EVAL_PAGE</code> since we always want to evaluate
175 * the page after this tag.
176 */
177 public int doEndTag() throws JspException {
178 Map map;
179 if (mapName != null) {
180 map = (Map) pageContext.findAttribute(mapName);
181 if (map == null) {
182 throw new JspException("Cannot find map called '"
183 + mapName
184 + "' at any scope.");
185 }
186 } else {
187 map = mapTag.getMap();
188 }
189
190 if (value == null) {
191 map.remove(name);
192 } else {
193 map.put(name, value);
194 }
195 return EVAL_PAGE;
196 }
197
198 /***
199 * <p>This method is called when the JSP engine encounters the start
200 * tag, after the attributes are processed.<p>
201 *
202 * <p>Scripting variables (if any) have their values set here.</p>
203 *
204 * @return <code>EVAL_BODY_BUFFERED</code> if no value was specified,
205 * otherwise <code>SKIP_BODY</code>.
206 * @throws JspException encapsulates any exception when calling
207 * <code>out.println</code>
208 */
209 public int doStartTag() throws JspException {
210 mapTag = (MapTag) findAncestorWithClass(this, MapTag.class);
211 if ((mapTag == null)
212 && (mapName == null)) {
213 throw new JspException("ERROR in MapEntryTag name '"
214 + name
215 + "', value '"
216 + value
217 + "': missing map. Specify parent MapTag, "
218 + "or mapName attribute.");
219 }
220
221
222 return EVAL_BODY_BUFFERED;
223 }
224
225 /***
226 * <p>If the <code>mapEntry</code> is not inside a <code>map</code>
227 * tag, use this attribute to specify a <code>java.util.Map</code>
228 * instance in the current page context.</p>
229 *
230 * @return the current value of mapName.
231 */
232 public final String getMapName() {
233 return mapName;
234 }
235
236 /***
237 * <p>Specifies the name of the entry to put in the parent map.</p>
238 *
239 * @return the current value of name.
240 */
241 public final String getName() {
242 return name;
243 }
244
245 /***
246 * <p>Specifies the value of the entry to put in the parent map.</p>
247 *
248 * @return the current value of value.
249 */
250 public final String getValue() {
251 return value;
252 }
253
254 /***
255 * <p>If the <code>mapEntry</code> is not inside a <code>map</code>
256 * tag, use this attribute to specify a <code>java.util.Map</code>
257 * instance in the current page context.</p>
258 *
259 * @param mapNameParam the new value of mapName.
260 */
261 public final void setMapName(final String mapNameParam) {
262 this.mapName = mapNameParam;
263 }
264
265 /***
266 * <p>Specifies the name of the entry to put in the parent map.</p>
267 *
268 * @param nameParam the new value of name.
269 */
270 public final void setName(final String nameParam) {
271 this.name = nameParam;
272 }
273
274 /***
275 * <p>Specifies the value of the entry to put in the parent map.</p>
276 *
277 * @param valueParam the new value of value.
278 */
279 public final void setValue(final String valueParam) {
280 this.value = valueParam;
281 }
282 }
283
284
285
286
287
288