View Javadoc

1   /*
2    * Copyright (c) 2001 - 2005 ivata limited.
3    * All rights reserved.
4    * -----------------------------------------------------------------------------
5    * ivata masks may be redistributed under the GNU General Public
6    * License as published by the Free Software Foundation;
7    * version 2 of the License.
8    *
9    * These programs are free software; you can redistribute them and/or
10   * modify them under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; version 2 of the License.
12   *
13   * These programs are distributed in the hope that they will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   *
17   * See the GNU General Public License in the file LICENSE.txt for more
18   * details.
19   *
20   * If you would like a copy of the GNU General Public License write to
21   *
22   * Free Software Foundation, Inc.
23   * 59 Temple Place - Suite 330
24   * Boston, MA 02111-1307, USA.
25   *
26   *
27   * To arrange commercial support and licensing, contact ivata at
28   *                  http://www.ivata.com/contact.jsp
29   * -----------------------------------------------------------------------------
30   * $Log: MapEntryTag.java,v $
31   * Revision 1.4  2005/04/22 09:48:12  colinmacleod
32   * Added checkigng for a missing page
33   * context attribute.
34   *
35   * Revision 1.3  2005/04/09 18:04:20  colinmacleod
36   * Changed copyright text to GPL v2 explicitly.
37   *
38   * Revision 1.2  2005/03/10 10:28:26  colinmacleod
39   * Fixed for tomcat4.
40   * Now uses getAttribute and only in page context.
41   * (Was findAttribute before.)
42   *
43   * Revision 1.1  2005/01/19 12:58:20  colinmacleod
44   * Moved from ivata groupware.
45   *
46   * Revision 1.2  2004/11/12 18:16:07  colinmacleod
47   * Ordered imports.
48   *
49   * Revision 1.1  2004/09/30 15:16:03  colinmacleod
50   * Split off addressbook elements into security subproject.
51   *
52   * Revision 1.2  2004/03/21 21:16:18  colinmacleod
53   * Shortened name to ivata op.
54   *
55   * Revision 1.1.1.1  2004/01/27 20:57:58  colinmacleod
56   * Moved ivata openportal to SourceForge..
57   *
58   * Revision 1.1.1.1  2003/10/13 20:50:13  colin
59   * Restructured portal into subprojects
60   *
61   * Revision 1.1  2003/02/25 08:15:50  colin
62   * moved to jsp category
63   *
64   * Revision 1.5  2003/02/13 10:52:20  colin
65   * changed to always include the body (helps with cached tags)
66   *
67   * Revision 1.4  2003/02/04 17:43:51  colin
68   * copyright notice
69   *
70   * Revision 1.3  2003/01/24 12:03:28  colin
71   * added mapName attribute
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         // evaluate the body - there may be something interesting there
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 MapEntryTag.setValue(Object){
285         this.value = value;
286     }
287 
288 */