001    /*
002    * Licensed to the Apache Software Foundation (ASF) under one or more
003    * contributor license agreements.  See the NOTICE file distributed with
004    * this work for additional information regarding copyright ownership.
005    * The ASF licenses this file to You under the Apache License, Version 2.0
006    * (the "License"); you may not use this file except in compliance with
007    * the License.  You may obtain a copy of the License at
008    *
009    *     http://www.apache.org/licenses/LICENSE-2.0
010    *
011    * Unless required by applicable law or agreed to in writing, software
012    * distributed under the License is distributed on an "AS IS" BASIS,
013    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014    * See the License for the specific language governing permissions and
015    * limitations under the License.
016    */
017     
018    package javax.servlet.jsp.tagext;
019    
020    import javax.servlet.jsp.*;
021    
022    
023    /**
024     * The interface of a classic tag handler that does not want to manipulate 
025     * its body.  The Tag interface defines the basic protocol between a Tag 
026     * handler and JSP page implementation class.  It defines the life cycle 
027     * and the methods to be invoked at start and end tag.
028     *
029     * <p><B>Properties</B></p>
030     *
031     * <p>The Tag interface specifies the setter and getter methods for the core
032     * pageContext and parent properties.</p>
033     *
034     * <p>The JSP page implementation object invokes setPageContext and
035     * setParent, in that order, before invoking doStartTag() or doEndTag().</p>
036     *
037     * <p><B>Methods</B></p>
038     *
039     * <p>There are two main actions: doStartTag and doEndTag.  Once all
040     * appropriate properties have been initialized, the doStartTag and
041     * doEndTag methods can be invoked on the tag handler.  Between these
042     * invocations, the tag handler is assumed to hold a state that must
043     * be preserved.  After the doEndTag invocation, the tag handler is
044     * available for further invocations (and it is expected to have
045     * retained its properties).</p>
046     *
047     * <p><B>Lifecycle</B></p>
048     *
049     * <p>Lifecycle details are described by the transition diagram below,
050     * with the following comments:
051     * <ul>
052     * <li> [1] This transition is intended to be for releasing long-term data.
053     * no guarantees are assumed on whether any properties have been retained
054     * or not.
055     * <li> [2] This transition happens if and only if the tag ends normally
056     * without raising an exception
057     * <li> [3] Some setters may be called again before a tag handler is 
058     * reused.  For instance, <code>setParent()</code> is called if it's 
059     * reused within the same page but at a different level, 
060     * <code>setPageContext()</code> is called if it's used in another page, 
061     * and attribute setters are called if the values differ or are expressed 
062     * as request-time attribute values.
063     * <li> Check the TryCatchFinally interface for additional details related
064     * to exception handling and resource management.
065     * </ul></p>
066     *
067     * <IMG src="doc-files/TagProtocol.gif"
068     *      alt="Lifecycle Details Transition Diagram for Tag"/>
069     * 
070     * <p>Once all invocations on the tag handler
071     * are completed, the release method is invoked on it.  Once a release
072     * method is invoked <em>all</em> properties, including parent and
073     * pageContext, are assumed to have been reset to an unspecified value.
074     * The page compiler guarantees that release() will be invoked on the Tag
075     * handler before the handler is released to the GC.</p>
076     *
077     * <p><B>Empty and Non-Empty Action</B></p>
078     * <p>If the TagLibraryDescriptor file indicates that the action must
079     * always have an empty action, by an &lt;body-content&gt; entry of "empty",
080     * then the doStartTag() method must return SKIP_BODY.</p>
081     *
082     * <p>Otherwise, the doStartTag() method may return SKIP_BODY or
083     * EVAL_BODY_INCLUDE.</p>
084     *
085     * <p>If SKIP_BODY is returned the body, if present, is not evaluated.</p>
086     * 
087     * <p>If EVAL_BODY_INCLUDE is returned, the body is evaluated and
088     * "passed through" to the current out.</p>
089    */
090    
091    public interface Tag extends JspTag {
092    
093        /**
094         * Skip body evaluation.
095         * Valid return value for doStartTag and doAfterBody.
096         */
097     
098        public final static int SKIP_BODY = 0;
099     
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    }