View Javadoc

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