1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. *
  54. */
  55. package javax.servlet.jsp.tagext;
  56. import javax.servlet.jsp.*;
  57. /**
  58. * The interface of a simple tag handler that does not want to manipulate its body.
  59. * The Tag interface defines the basic protocol between a Tag handler and
  60. * JSP page implementation class. It defines the life cycle and the
  61. * methods to be invoked at start and end tag.
  62. *
  63. * <p><B>Properties</B>
  64. *
  65. * <p>
  66. * The Tag interface specifies the setter and getter methods for the core
  67. * pageContext and parent properties.
  68. *
  69. * <p> The JSP page implementation object invokes setPageContext and
  70. * setParent, in that order, before invoking doStartTag() or doEndTag().
  71. *
  72. * <p><B>Methods</B>
  73. *
  74. * <p>
  75. * There are two main actions: doStartTag and doEndTag. Once all
  76. * appropriate properties have been initialized, the doStartTag and
  77. * doEndTag methods can be invoked on the tag handler. Between these
  78. * invocations, the tag handler is assumed to hold a state that must
  79. * be preserved. After the doEndTag invocation, the tag handler is
  80. * available for further invocations (and it is expected to have
  81. * retained its properties).
  82. *
  83. * <p><B>Lifecycle</B>
  84. * <p> Lifecycle details are described by the transition diagram below,
  85. * with the following comments:
  86. * <ul>
  87. * <li> [1] This transition is intended to be for releasing long-term data.
  88. * no guarantees are assumed on whether any properties have been retained
  89. * or not.
  90. * <li> [2] This transition happens if and only if the tag ends normally
  91. * without raising an exception
  92. * <li> [3] Note that since there are no guarantees on the state of the
  93. * properties, a tag handler that had some optional properties set can only be
  94. * reused if those properties are set to a new (known) value. This means
  95. * that tag handlers can only be reused within the same "AttSet" (set of
  96. * attributes that have been set).
  97. * <li> Check the TryCatchFinally interface for additional details related
  98. * to exception handling and resource management.
  99. * </ul>
  100. *
  101. * <IMG src="doc-files/TagProtocol.gif"/>
  102. *
  103. * <p> Once all invocations on the tag handler
  104. * are completed, the release method is invoked on it. Once a release
  105. * method is invoked <em>all</em> properties, including parent and
  106. * pageContext, are assumed to have been reset to an unspecified value.
  107. * The page compiler guarantees that release() will be invoked on the Tag
  108. * handler before the handler is released to the GC.
  109. *
  110. * <p><B>Empty and Non-Empty Action</B>
  111. * <p> If the TagLibraryDescriptor file indicates that the action must
  112. * always have an empty action, by an <body-content> entry of "empty",
  113. * then the doStartTag() method must return SKIP_BODY.
  114. *
  115. * Otherwise, the doStartTag() method may return SKIP_BODY or
  116. * EVAL_BODY_INCLUDE.
  117. *
  118. * <p>
  119. * If SKIP_BODY is returned the body, if present, is not evaluated.
  120. *
  121. * <p>
  122. * If EVAL_BODY_INCLUDE is returned, the body is evaluated and
  123. * "passed through" to the current out.
  124. */
  125. public interface Tag {
  126. /**
  127. * Skip body evaluation.
  128. * Valid return value for doStartTag and doAfterBody.
  129. */
  130. public final static int SKIP_BODY = 0;
  131. /**
  132. * Evaluate body into existing out stream.
  133. * Valid return value for doStartTag.
  134. */
  135. public final static int EVAL_BODY_INCLUDE = 1;
  136. /**
  137. * Skip the rest of the page.
  138. * Valid return value for doEndTag.
  139. */
  140. public final static int SKIP_PAGE = 5;
  141. /**
  142. * Continue evaluating the page.
  143. * Valid return value for doEndTag().
  144. */
  145. public final static int EVAL_PAGE = 6;
  146. // Setters for Tag handler data
  147. /**
  148. * Set the current page context.
  149. * This method is invoked by the JSP page implementation object
  150. * prior to doStartTag().
  151. * <p>
  152. * This value is *not* reset by doEndTag() and must be explicitly reset
  153. * by a page implementation if it changes between calls to doStartTag().
  154. *
  155. * @param pc The page context for this tag handler.
  156. */
  157. void setPageContext(PageContext pc);
  158. /**
  159. * Set the parent (closest enclosing tag handler) of this tag handler.
  160. * Invoked by the JSP page implementation object prior to doStartTag().
  161. * <p>
  162. * This value is *not* reset by doEndTag() and must be explicitly reset
  163. * by a page implementation.
  164. *
  165. * @param t The parent tag, or null.
  166. */
  167. void setParent(Tag t);
  168. /**
  169. * Get the parent (closest enclosing tag handler) for this tag handler.
  170. *
  171. * <p>
  172. * The getParent() method can be used to navigate the nested tag
  173. * handler structure at runtime for cooperation among custom actions;
  174. * for example, the findAncestorWithClass() method in TagSupport
  175. * provides a convenient way of doing this.
  176. *
  177. * <p>
  178. * The current version of the specification only provides one formal
  179. * way of indicating the observable type of a tag handler: its
  180. * tag handler implementation class, described in the tag-class
  181. * subelement of the tag element. This is extended in an
  182. * informal manner by allowing the tag library author to
  183. * indicate in the description subelement an observable type.
  184. * The type should be a subtype of the tag handler implementation
  185. * class or void.
  186. * This addititional constraint can be exploited by a
  187. * specialized container that knows about that specific tag library,
  188. * as in the case of the JSP standard tag library.
  189. *
  190. * @returns the current parent, or null if none.
  191. * @seealso TagSupport.findAncestorWithClass().
  192. */
  193. Tag getParent();
  194. // Actions for basic start/end processing.
  195. /**
  196. * Process the start tag for this instance.
  197. * This method is invoked by the JSP page implementation object.
  198. *
  199. * <p>
  200. * The doStartTag method assumes that the properties pageContext and
  201. * parent have been set. It also assumes that any properties exposed as
  202. * attributes have been set too. When this method is invoked, the body
  203. * has not yet been evaluated.
  204. *
  205. * <p>
  206. * This method returns Tag.EVAL_BODY_INCLUDE or
  207. * BodyTag.EVAL_BODY_BUFFERED to indicate
  208. * that the body of the action should be evaluated or SKIP_BODY to
  209. * indicate otherwise.
  210. *
  211. * <p>
  212. * When a Tag returns EVAL_BODY_INCLUDE the result of evaluating
  213. * the body (if any) is included into the current "out" JspWriter as it
  214. * happens and then doEndTag() is invoked.
  215. *
  216. * <p>
  217. * BodyTag.EVAL_BODY_BUFFERED is only valid if the tag handler
  218. * implements BodyTag.
  219. *
  220. * <p>
  221. * The JSP container will resynchronize
  222. * any variable values that are indicated as so in TagExtraInfo after the
  223. * invocation of doStartTag().
  224. *
  225. * @returns EVAL_BODY_INCLUDE if the tag wants to process body, SKIP_BODY if it
  226. * does not want to process it.
  227. * @throws JspException.
  228. * @see BodyTag
  229. */
  230. int doStartTag() throws JspException;
  231. /**
  232. * Process the end tag for this instance.
  233. * This method is invoked by the JSP page implementation object
  234. * on all Tag handlers.
  235. *
  236. * <p>
  237. * This method will be called after returning from doStartTag. The
  238. * body of the action may or not have been evaluated, depending on
  239. * the return value of doStartTag.
  240. *
  241. * <p>
  242. * If this method returns EVAL_PAGE, the rest of the page continues
  243. * to be evaluated. If this method returns SKIP_PAGE, the rest of
  244. * the page is not evaluated and the request is completed. If this
  245. * request was forwarded or included from another page (or Servlet),
  246. * only the current page evaluation is completed.
  247. *
  248. * <p>
  249. * The JSP container will resynchronize
  250. * any variable values that are indicated as so in TagExtraInfo after the
  251. * invocation of doEndTag().
  252. *
  253. * @returns indication of whether to continue evaluating the JSP page.
  254. * @throws JspException.
  255. */
  256. int doEndTag() throws JspException;
  257. /**
  258. * Called on a Tag handler to release state.
  259. * The page compiler guarantees that JSP page implementation
  260. * objects will invoke this method on all tag handlers,
  261. * but there may be multiple invocations on doStartTag and doEndTag in between.
  262. */
  263. void release();
  264. }