1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
5 *
6 * Portions Copyright Apache Software Foundation.
7 *
8 * The contents of this file are subject to the terms of either the GNU
9 * General Public License Version 2 only ("GPL") or the Common Development
10 * and Distribution License("CDDL") (collectively, the "License"). You
11 * may not use this file except in compliance with the License. You can obtain
12 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
13 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
14 * language governing permissions and limitations under the License.
15 *
16 * When distributing the software, include this License Header Notice in each
17 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
18 * Sun designates this particular file as subject to the "Classpath" exception
19 * as provided by Sun in the GPL Version 2 section of the License file that
20 * accompanied this code. If applicable, add the following below the License
21 * Header, with the fields enclosed by brackets [] replaced by your own
22 * identifying information: "Portions Copyrighted [year]
23 * [name of copyright owner]"
24 *
25 * Contributor(s):
26 *
27 * If you wish your version of this file to be governed by only the CDDL or
28 * only the GPL Version 2, indicate your decision by adding "[Contributor]
29 * elects to include this software in this distribution under the [CDDL or GPL
30 * Version 2] license." If you don't indicate a single choice of license, a
31 * recipient has the option to distribute your version of this file under
32 * either the CDDL, the GPL Version 2 or to extend the choice of license to
33 * its licensees as provided above. However, if you add GPL Version 2 code
34 * and therefore, elected the GPL Version 2 license, then the option applies
35 * only if the new code is made subject to such option by the copyright
36 * holder.
37 */
38 package javax.servlet.jsp.tagext;
39
40 import javax.servlet.jsp;
41 import javax.servlet.jsp.tagext;
42
43 import javax.servlet;
44
45 import java.io.Writer;
46 import java.io.Serializable;
47
48 import java.util.Hashtable;
49 import java.util.Enumeration;
50
51 /**
52 * A base class for defining new tag handlers implementing Tag.
53 *
54 * <p> The TagSupport class is a utility class intended to be used as
55 * the base class for new tag handlers. The TagSupport class
56 * implements the Tag and IterationTag interfaces and adds additional
57 * convenience methods including getter methods for the properties in
58 * Tag. TagSupport has one static method that is included to
59 * facilitate coordination among cooperating tags.
60 *
61 * <p> Many tag handlers will extend TagSupport and only redefine a
62 * few methods.
63 */
64
65 public class TagSupport implements IterationTag, Serializable {
66
67 /**
68 * Find the instance of a given class type that is closest to a given
69 * instance.
70 * This method uses the getParent method from the Tag
71 * interface.
72 * This method is used for coordination among cooperating tags.
73 *
74 * <p>
75 * The current version of the specification only provides one formal
76 * way of indicating the observable type of a tag handler: its
77 * tag handler implementation class, described in the tag-class
78 * subelement of the tag element. This is extended in an
79 * informal manner by allowing the tag library author to
80 * indicate in the description subelement an observable type.
81 * The type should be a subtype of the tag handler implementation
82 * class or void.
83 * This addititional constraint can be exploited by a
84 * specialized container that knows about that specific tag library,
85 * as in the case of the JSP standard tag library.
86 *
87 * <p>
88 * When a tag library author provides information on the
89 * observable type of a tag handler, client programmatic code
90 * should adhere to that constraint. Specifically, the Class
91 * passed to findAncestorWithClass should be a subtype of the
92 * observable type.
93 *
94 *
95 * @param from The instance from where to start looking.
96 * @param klass The subclass of Tag or interface to be matched
97 * @return the nearest ancestor that implements the interface
98 * or is an instance of the class specified
99 */
100
101 public static final Tag findAncestorWithClass(Tag from, Class klass) {
102 boolean isInterface = false;
103
104 if (from == null ||
105 klass == null ||
106 (!Tag.class.isAssignableFrom(klass) &&
107 !(isInterface = klass.isInterface()))) {
108 return null;
109 }
110
111 for (;;) {
112 Tag tag = from.getParent();
113
114 if (tag == null) {
115 return null;
116 }
117
118 if ((isInterface && klass.isInstance(tag)) ||
119 klass.isAssignableFrom(tag.getClass()))
120 return tag;
121 else
122 from = tag;
123 }
124 }
125
126 /**
127 * Default constructor, all subclasses are required to define only
128 * a public constructor with the same signature, and to call the
129 * superclass constructor.
130 *
131 * This constructor is called by the code generated by the JSP
132 * translator.
133 */
134
135 public TagSupport() { }
136
137 /**
138 * Default processing of the start tag, returning SKIP_BODY.
139 *
140 * @return SKIP_BODY
141 * @throws JspException if an error occurs while processing this tag
142 *
143 * @see Tag#doStartTag()
144 */
145
146 public int doStartTag() throws JspException {
147 return SKIP_BODY;
148 }
149
150 /**
151 * Default processing of the end tag returning EVAL_PAGE.
152 *
153 * @return EVAL_PAGE
154 * @throws JspException if an error occurs while processing this tag
155 *
156 * @see Tag#doEndTag()
157 */
158
159 public int doEndTag() throws JspException {
160 return EVAL_PAGE;
161 }
162
163
164 /**
165 * Default processing for a body.
166 *
167 * @return SKIP_BODY
168 * @throws JspException if an error occurs while processing this tag
169 *
170 * @see IterationTag#doAfterBody()
171 */
172
173 public int doAfterBody() throws JspException {
174 return SKIP_BODY;
175 }
176
177 // Actions related to body evaluation
178
179
180 /**
181 * Release state.
182 *
183 * @see Tag#release()
184 */
185
186 public void release() {
187 parent = null;
188 id = null;
189 if( values != null ) {
190 values.clear();
191 }
192 values = null;
193 }
194
195 /**
196 * Set the nesting tag of this tag.
197 *
198 * @param t The parent Tag.
199 * @see Tag#setParent(Tag)
200 */
201
202 public void setParent(Tag t) {
203 parent = t;
204 }
205
206 /**
207 * The Tag instance most closely enclosing this tag instance.
208 * @see Tag#getParent()
209 *
210 * @return the parent tag instance or null
211 */
212
213 public Tag getParent() {
214 return parent;
215 }
216
217 /**
218 * Set the id attribute for this tag.
219 *
220 * @param id The String for the id.
221 */
222
223 public void setId(String id) {
224 this.id = id;
225 }
226
227 /**
228 * The value of the id attribute of this tag; or null.
229 *
230 * @return the value of the id attribute, or null
231 */
232
233 public String getId() {
234 return id;
235 }
236
237 /**
238 * Set the page context.
239 *
240 * @param pageContext The PageContext.
241 * @see Tag#setPageContext
242 */
243
244 public void setPageContext(PageContext pageContext) {
245 this.pageContext = pageContext;
246 }
247
248 /**
249 * Associate a value with a String key.
250 *
251 * @param k The key String.
252 * @param o The value to associate.
253 */
254
255 public void setValue(String k, Object o) {
256 if (values == null) {
257 values = new Hashtable();
258 }
259 values.put(k, o);
260 }
261
262 /**
263 * Get a the value associated with a key.
264 *
265 * @param k The string key.
266 * @return The value associated with the key, or null.
267 */
268
269 public Object getValue(String k) {
270 if (values == null) {
271 return null;
272 } else {
273 return values.get(k);
274 }
275 }
276
277 /**
278 * Remove a value associated with a key.
279 *
280 * @param k The string key.
281 */
282
283 public void removeValue(String k) {
284 if (values != null) {
285 values.remove(k);
286 }
287 }
288
289 /**
290 * Enumerate the keys for the values kept by this tag handler.
291 *
292 * @return An enumeration of all the keys for the values set,
293 * or null or an empty Enumeration if no values have been set.
294 */
295
296 public Enumeration getValues() {
297 if (values == null) {
298 return null;
299 }
300 return values.keys();
301 }
302
303 // private fields
304
305 private Tag parent;
306 private Hashtable values;
307 /**
308 * The value of the id attribute of this tag; or null.
309 */
310 protected String id;
311
312 // protected fields
313
314 /**
315 * The PageContext.
316 */
317 protected PageContext pageContext;
318 }
319