Source code: javax/servlet/jsp/tagext/BodyTag.java
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package javax.servlet.jsp.tagext;
17
18 import javax.servlet.jsp.*;
19
20 /**
21 * The BodyTag interface extends IterationTag by defining additional
22 * methods that let a tag handler manipulate the content of evaluating its body.
23 *
24 * <p>
25 * It is the responsibility of the tag handler to manipulate the body
26 * content. For example the tag handler may take the body content,
27 * convert it into a String using the bodyContent.getString
28 * method and then use it. Or the tag handler may take the body
29 * content and write it out into its enclosing JspWriter using
30 * the bodyContent.writeOut method.
31 *
32 * <p> A tag handler that implements BodyTag is treated as one that
33 * implements IterationTag, except that the doStartTag method can
34 * return SKIP_BODY, EVAL_BODY_INCLUDE or EVAL_BODY_BUFFERED.
35 *
36 * <p>
37 * If EVAL_BODY_INCLUDE is returned, then evaluation happens
38 * as in IterationTag.
39 *
40 * <p>
41 * If EVAL_BODY_BUFFERED is returned, then a BodyContent object will be
42 * created (by code generated by the JSP compiler) to capture the body
43 * evaluation.
44 * The code generated by the JSP compiler obtains the BodyContent object by
45 * calling the pushBody method of the current pageContext, which
46 * additionally has the effect of saving the previous out value.
47 * The page compiler returns this object by calling the popBody
48 * method of the PageContext class;
49 * the call also restores the value of out.
50 *
51 * <p>
52 * The interface provides one new property with a setter method and one
53 * new action method.
54 *
55 * <p><B>Properties</B>
56 * <p> There is a new property: bodyContent, to contain the BodyContent
57 * object, where the JSP Page implementation object will place the
58 * evaluation (and reevaluation, if appropriate) of the body. The setter
59 * method (setBodyContent) will only be invoked if doStartTag() returns
60 * EVAL_BODY_BUFFERED and the corresponding action element does not have
61 * an empty body.
62 *
63 * <p><B>Methods</B>
64 * <p> In addition to the setter method for the bodyContent property, there
65 * is a new action method: doInitBody(), which is invoked right after
66 * setBodyContent() and before the body evaluation. This method is only
67 * invoked if doStartTag() returns EVAL_BODY_BUFFERED.
68 *
69 * <p><B>Lifecycle</B>
70 * <p> Lifecycle details are described by the transition diagram below.
71 * Exceptions that are thrown during the computation of doStartTag(),
72 * setBodyContent(), doInitBody(), BODY, doAfterBody() interrupt the
73 * execution sequence and are propagated up the stack, unless the
74 * tag handler implements the TryCatchFinally interface; see that
75 * interface for details.
76 * <p>
77 * <IMG src="doc-files/BodyTagProtocol.gif"
78 * alt="Lifecycle Details Transition Diagram for BodyTag"/>
79 *
80 * <p><B>Empty and Non-Empty Action</B>
81 * <p> If the TagLibraryDescriptor file indicates that the action must
82 * always have an empty element body, by an <body-content> entry
83 * of "empty", then the doStartTag() method must return SKIP_BODY.
84 * Otherwise, the doStartTag() method may return SKIP_BODY,
85 * EVAL_BODY_INCLUDE, or EVAL_BODY_BUFFERED.
86 *
87 * <p>Note that which methods are invoked after the doStartTag() depends on
88 * both the return value and on if the custom action element is empty
89 * or not in the JSP page, not how it's declared in the TLD.
90 *
91 * <p>
92 * If SKIP_BODY is returned the body is not evaluated, and doEndTag() is
93 * invoked.
94 *
95 * <p>
96 * If EVAL_BODY_INCLUDE is returned, and the custom action element is not
97 * empty, setBodyContent() is not invoked,
98 * doInitBody() is not invoked, the body is evaluated and
99 * "passed through" to the current out, doAfterBody() is invoked
100 * and then, after zero or more iterations, doEndTag() is invoked.
101 * If the custom action element is empty, only doStart() and
102 * doEndTag() are invoked.
103 *
104 * <p>
105 * If EVAL_BODY_BUFFERED is returned, and the custom action element is not
106 * empty, setBodyContent() is invoked,
107 * doInitBody() is invoked, the body is evaluated, doAfterBody() is
108 * invoked, and then, after zero or more iterations, doEndTag() is invoked.
109 * If the custom action element is empty, only doStart() and doEndTag()
110 * are invoked.
111 */
112
113 public interface BodyTag extends IterationTag {
114
115 /**
116 * Deprecated constant that has the same value as EVAL_BODY_BUFFERED
117 * and EVAL_BODY_AGAIN. This name has been marked as deprecated
118 * to encourage the use of the two different terms, which are much
119 * more descriptive.
120 *
121 * @deprecated As of Java JSP API 1.2, use BodyTag.EVAL_BODY_BUFFERED
122 * or IterationTag.EVAL_BODY_AGAIN.
123 */
124
125 public final static int EVAL_BODY_TAG = 2;
126
127 /**
128 * Request the creation of new buffer, a BodyContent on which to
129 * evaluate the body of this tag.
130 *
131 * Returned from doStartTag when it implements BodyTag.
132 * This is an illegal return value for doStartTag when the class
133 * does not implement BodyTag.
134 */
135
136 public final static int EVAL_BODY_BUFFERED = 2;
137
138
139 /**
140 * Set the bodyContent property.
141 * This method is invoked by the JSP page implementation object at
142 * most once per action invocation.
143 * This method will be invoked before doInitBody.
144 * This method will not be invoked for empty tags or for non-empty
145 * tags whose doStartTag() method returns SKIP_BODY or EVAL_BODY_INCLUDE.
146 *
147 * <p>
148 * When setBodyContent is invoked, the value of the implicit object out
149 * has already been changed in the pageContext object. The BodyContent
150 * object passed will have not data on it but may have been reused
151 * (and cleared) from some previous invocation.
152 *
153 * <p>
154 * The BodyContent object is available and with the appropriate content
155 * until after the invocation of the doEndTag method, at which case it
156 * may be reused.
157 *
158 * @param b the BodyContent
159 * @see #doInitBody
160 * @see #doAfterBody
161 */
162
163 void setBodyContent(BodyContent b);
164
165
166 /**
167 * Prepare for evaluation of the body.
168 * This method is invoked by the JSP page implementation object
169 * after setBodyContent and before the first time
170 * the body is to be evaluated.
171 * This method will not be invoked for empty tags or for non-empty
172 * tags whose doStartTag() method returns SKIP_BODY or EVAL_BODY_INCLUDE.
173 *
174 * <p>
175 * The JSP container will resynchronize the values of any AT_BEGIN and
176 * NESTED variables (defined by the associated TagExtraInfo or TLD) after
177 * the invocation of doInitBody().
178 *
179 * @throws JspException if an error occurred while processing this tag
180 * @see #doAfterBody
181 */
182
183 void doInitBody() throws JspException;
184
185 }