1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package javax.servlet.jsp.tagext; 19 20 import javax.servlet.jsp; 21 22 23 /** 24 * The interface of a classic tag handler that does not want to manipulate 25 * its body. The Tag interface defines the basic protocol between a Tag 26 * handler and JSP page implementation class. It defines the life cycle 27 * and the methods to be invoked at start and end tag. 28 * 29 * <p><B>Properties</B></p> 30 * 31 * <p>The Tag interface specifies the setter and getter methods for the core 32 * pageContext and parent properties.</p> 33 * 34 * <p>The JSP page implementation object invokes setPageContext and 35 * setParent, in that order, before invoking doStartTag() or doEndTag().</p> 36 * 37 * <p><B>Methods</B></p> 38 * 39 * <p>There are two main actions: doStartTag and doEndTag. Once all 40 * appropriate properties have been initialized, the doStartTag and 41 * doEndTag methods can be invoked on the tag handler. Between these 42 * invocations, the tag handler is assumed to hold a state that must 43 * be preserved. After the doEndTag invocation, the tag handler is 44 * available for further invocations (and it is expected to have 45 * retained its properties).</p> 46 * 47 * <p><B>Lifecycle</B></p> 48 * 49 * <p>Lifecycle details are described by the transition diagram below, 50 * with the following comments: 51 * <ul> 52 * <li> [1] This transition is intended to be for releasing long-term data. 53 * no guarantees are assumed on whether any properties have been retained 54 * or not. 55 * <li> [2] This transition happens if and only if the tag ends normally 56 * without raising an exception 57 * <li> [3] Some setters may be called again before a tag handler is 58 * reused. For instance, <code>setParent()</code> is called if it's 59 * reused within the same page but at a different level, 60 * <code>setPageContext()</code> is called if it's used in another page, 61 * and attribute setters are called if the values differ or are expressed 62 * as request-time attribute values. 63 * <li> Check the TryCatchFinally interface for additional details related 64 * to exception handling and resource management. 65 * </ul></p> 66 * 67 * <IMG src="doc-files/TagProtocol.gif" 68 * alt="Lifecycle Details Transition Diagram for Tag"/> 69 * 70 * <p>Once all invocations on the tag handler 71 * are completed, the release method is invoked on it. Once a release 72 * method is invoked <em>all</em> properties, including parent and 73 * pageContext, are assumed to have been reset to an unspecified value. 74 * The page compiler guarantees that release() will be invoked on the Tag 75 * handler before the handler is released to the GC.</p> 76 * 77 * <p><B>Empty and Non-Empty Action</B></p> 78 * <p>If the TagLibraryDescriptor file indicates that the action must 79 * always have an empty action, by an <body-content> entry of "empty", 80 * then the doStartTag() method must return SKIP_BODY.</p> 81 * 82 * <p>Otherwise, the doStartTag() method may return SKIP_BODY or 83 * EVAL_BODY_INCLUDE.</p> 84 * 85 * <p>If SKIP_BODY is returned the body, if present, is not evaluated.</p> 86 * 87 * <p>If EVAL_BODY_INCLUDE is returned, the body is evaluated and 88 * "passed through" to the current out.</p> 89 */ 90 91 public interface Tag extends JspTag { 92 93 /** 94 * Skip body evaluation. 95 * Valid return value for doStartTag and doAfterBody. 96 */ 97 98 public final static int SKIP_BODY = 0; 99 100 /** 101 * Evaluate body into existing out stream. 102 * Valid return value for doStartTag. 103 */ 104 105 public final static int EVAL_BODY_INCLUDE = 1; 106 107 /** 108 * Skip the rest of the page. 109 * Valid return value for doEndTag. 110 */ 111 112 public final static int SKIP_PAGE = 5; 113 114 /** 115 * Continue evaluating the page. 116 * Valid return value for doEndTag(). 117 */ 118 119 public final static int EVAL_PAGE = 6; 120 121 // Setters for Tag handler data 122 123 124 /** 125 * Set the current page context. 126 * This method is invoked by the JSP page implementation object 127 * prior to doStartTag(). 128 * <p> 129 * This value is *not* reset by doEndTag() and must be explicitly reset 130 * by a page implementation if it changes between calls to doStartTag(). 131 * 132 * @param pc The page context for this tag handler. 133 */ 134 135 void setPageContext(PageContext pc); 136 137 138 /** 139 * Set the parent (closest enclosing tag handler) of this tag handler. 140 * Invoked by the JSP page implementation object prior to doStartTag(). 141 * <p> 142 * This value is *not* reset by doEndTag() and must be explicitly reset 143 * by a page implementation. 144 * 145 * @param t The parent tag, or null. 146 */ 147 148 149 void setParent(Tag t); 150 151 152 /** 153 * Get the parent (closest enclosing tag handler) for this tag handler. 154 * 155 * <p> 156 * The getParent() method can be used to navigate the nested tag 157 * handler structure at runtime for cooperation among custom actions; 158 * for example, the findAncestorWithClass() method in TagSupport 159 * provides a convenient way of doing this. 160 * 161 * <p> 162 * The current version of the specification only provides one formal 163 * way of indicating the observable type of a tag handler: its 164 * tag handler implementation class, described in the tag-class 165 * subelement of the tag element. This is extended in an 166 * informal manner by allowing the tag library author to 167 * indicate in the description subelement an observable type. 168 * The type should be a subtype of the tag handler implementation 169 * class or void. 170 * This addititional constraint can be exploited by a 171 * specialized container that knows about that specific tag library, 172 * as in the case of the JSP standard tag library. 173 * 174 * @return the current parent, or null if none. 175 * @see TagSupport#findAncestorWithClass 176 */ 177 178 Tag getParent(); 179 180 181 // Actions for basic start/end processing. 182 183 184 /** 185 * Process the start tag for this instance. 186 * This method is invoked by the JSP page implementation object. 187 * 188 * <p> 189 * The doStartTag method assumes that the properties pageContext and 190 * parent have been set. It also assumes that any properties exposed as 191 * attributes have been set too. When this method is invoked, the body 192 * has not yet been evaluated. 193 * 194 * <p> 195 * This method returns Tag.EVAL_BODY_INCLUDE or 196 * BodyTag.EVAL_BODY_BUFFERED to indicate 197 * that the body of the action should be evaluated or SKIP_BODY to 198 * indicate otherwise. 199 * 200 * <p> 201 * When a Tag returns EVAL_BODY_INCLUDE the result of evaluating 202 * the body (if any) is included into the current "out" JspWriter as it 203 * happens and then doEndTag() is invoked. 204 * 205 * <p> 206 * BodyTag.EVAL_BODY_BUFFERED is only valid if the tag handler 207 * implements BodyTag. 208 * 209 * <p> 210 * The JSP container will resynchronize the values of any AT_BEGIN and 211 * NESTED variables (defined by the associated TagExtraInfo or TLD) 212 * after the invocation of doStartTag(), except for a tag handler 213 * implementing BodyTag whose doStartTag() method returns 214 * BodyTag.EVAL_BODY_BUFFERED. 215 * 216 * @return EVAL_BODY_INCLUDE if the tag wants to process body, SKIP_BODY 217 * if it does not want to process it. 218 * @throws JspException if an error occurred while processing this tag 219 * @see BodyTag 220 */ 221 222 int doStartTag() throws JspException; 223 224 225 /** 226 * Process the end tag for this instance. 227 * This method is invoked by the JSP page implementation object 228 * on all Tag handlers. 229 * 230 * <p> 231 * This method will be called after returning from doStartTag. The 232 * body of the action may or may not have been evaluated, depending on 233 * the return value of doStartTag. 234 * 235 * <p> 236 * If this method returns EVAL_PAGE, the rest of the page continues 237 * to be evaluated. If this method returns SKIP_PAGE, the rest of 238 * the page is not evaluated, the request is completed, and 239 * the doEndTag() methods of enclosing tags are not invoked. If this 240 * request was forwarded or included from another page (or Servlet), 241 * only the current page evaluation is stopped. 242 * 243 * <p> 244 * The JSP container will resynchronize the values of any AT_BEGIN and 245 * AT_END variables (defined by the associated TagExtraInfo or TLD) 246 * after the invocation of doEndTag(). 247 * 248 * @return indication of whether to continue evaluating the JSP page. 249 * @throws JspException if an error occurred while processing this tag 250 */ 251 252 int doEndTag() throws JspException; 253 254 /** 255 * Called on a Tag handler to release state. 256 * The page compiler guarantees that JSP page implementation 257 * objects will invoke this method on all tag handlers, 258 * but there may be multiple invocations on doStartTag and doEndTag in between. 259 */ 260 261 void release(); 262 263 }