001: /*
002: * Copyright (c) 1998-2003 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package javax.servlet.jsp.tagext;
030:
031: import javax.servlet.jsp.JspException;
032: import javax.servlet.jsp.PageContext;
033: import java.io.Serializable;
034: import java.util.Enumeration;
035: import java.util.Hashtable;
036:
037: /**
038: * Convenient support class for creating tags which don't analyze their
039: * bodies. Normally, tags will extend TagSupport instead of implementing
040: * Tag directly.
041: */
042: public class TagSupport implements IterationTag, Serializable {
043: /**
044: * The value of the "id" attribute for the tag, if specified.
045: */
046: protected String id;
047: /**
048: * The owning PageContext.
049: */
050: protected PageContext pageContext;
051: private Tag parent;
052: private Hashtable values;
053:
054: /**
055: * Tags need to implement a zero-arg constructor.
056: */
057: public TagSupport() {
058: }
059:
060: /**
061: * Processed at the beginning of the tag.
062: *
063: * <p>The default behavior returns SKIP_BODY to skip the tag's body.
064: */
065: public int doStartTag() throws JspException {
066: return SKIP_BODY;
067: }
068:
069: /**
070: * Processed to check if the tag should loop. The default behavior
071: * returns SKIP_BODY so it does not loop.
072: *
073: * @return EVAL_BODY_AGAIN to repeat the body or SKIP_BODY to stop.
074: */
075: public int doAfterBody() throws JspException {
076: return SKIP_BODY;
077: }
078:
079: /**
080: * Processed at the end of the tag. The default behavior continues
081: * with the rest of the JSP.
082: *
083: * @return EVAL_PAGE to continue the page SKIP_PAGE to stop.
084: */
085: public int doEndTag() throws JspException {
086: return EVAL_PAGE;
087: }
088:
089: /**
090: * Sets the id attribute.
091: */
092: public void setId(String id) {
093: this .id = id;
094: }
095:
096: /**
097: * Sets the id attribute.
098: */
099: public String getId() {
100: return id;
101: }
102:
103: /**
104: * Stores the page context for the JSP page.
105: */
106: public void setPageContext(PageContext page) {
107: this .pageContext = page;
108: }
109:
110: /**
111: * If the tag is contained in another tag, this sets the parent.
112: *
113: * @param parent the tag to be used as a parent.
114: */
115: public void setParent(Tag parent) {
116: this .parent = parent;
117: if (values != null)
118: values.clear();
119: }
120:
121: /**
122: * Returns the tag's parent.
123: */
124: public Tag getParent() {
125: return this .parent;
126: }
127:
128: /**
129: * Finds an ancestor of a tag matching the class. The search is strict,
130: * i.e. only parents will be searched, not the tag itself.
131: *
132: * @param tag child tag to start searching.
133: * @param cl the class that the tag should implement.
134: *
135: * @return the matching tag or null.
136: */
137: public static final Tag findAncestorWithClass(Tag tag, Class cl) {
138: if (tag == null || cl == null)
139: return null;
140:
141: tag = tag.getParent();
142:
143: for (; tag != null; tag = tag.getParent()) {
144: if (cl.isAssignableFrom(tag.getClass()))
145: return tag;
146: }
147:
148: return tag;
149: }
150:
151: /**
152: * Returns an attribute from the tag.
153: */
154: public Object getValue(String name) {
155: if (values == null)
156: return null;
157: else
158: return values.get(name);
159: }
160:
161: /**
162: * Enumerates the tag's attributes.
163: */
164: public Enumeration getValues() {
165: if (values == null)
166: values = new Hashtable();
167:
168: return values.keys();
169: }
170:
171: /**
172: * Removes a value from the tag.
173: */
174: public void removeValue(String name) {
175: if (values == null)
176: return;
177:
178: values.remove(name);
179: }
180:
181: /**
182: * Sets the value for a tag attribute.
183: *
184: * @param name the name of the attribute.
185: * @param value the new value for the attribute.
186: */
187: public void setValue(String name, Object value) {
188: if (values == null)
189: values = new Hashtable();
190:
191: values.put(name, value);
192: }
193:
194: /**
195: * Resets any custom attributes to the default value.
196: */
197: public void resetCustomAttributes() {
198: }
199:
200: /**
201: * Cleans the tag after it completes.
202: */
203: public void release() {
204: parent = null;
205: if (values != null)
206: values.clear();
207: }
208: }
|