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    package javax.servlet.jsp.tagext;
018    
019    import javax.servlet.jsp.JspException;
020    import javax.servlet.jsp.JspWriter;
021    
022    /**
023     * A base class for defining tag handlers implementing BodyTag.
024     *
025     * <p>
026     * The BodyTagSupport class implements the BodyTag interface and adds
027     * additional convenience methods including getter methods for the
028     * bodyContent property and methods to get at the previous out JspWriter.
029     *
030     * <p>
031     * Many tag handlers will extend BodyTagSupport and only redefine a
032     * few methods.
033     */
034    
035    public class BodyTagSupport extends TagSupport implements BodyTag {
036    
037        /**
038         * Default constructor, all subclasses are required to only define
039         * a public constructor with the same signature, and to call the
040         * superclass constructor.
041         *
042         * This constructor is called by the code generated by the JSP
043         * translator.
044         */
045    
046        public BodyTagSupport() {
047            super();
048        }
049    
050        /**
051         * Default processing of the start tag returning EVAL_BODY_BUFFERED.
052         *
053         * @return EVAL_BODY_BUFFERED
054         * @throws JspException if an error occurred while processing this tag
055         * @see BodyTag#doStartTag
056         */
057     
058        public int doStartTag() throws JspException {
059            return EVAL_BODY_BUFFERED;
060        }
061    
062    
063        /**
064         * Default processing of the end tag returning EVAL_PAGE.
065         *
066         * @return EVAL_PAGE
067         * @throws JspException if an error occurred while processing this tag
068         * @see Tag#doEndTag
069         */
070    
071        public int doEndTag() throws JspException {
072            return super.doEndTag();
073        }
074    
075    
076        // Actions related to body evaluation
077    
078        /**
079         * Prepare for evaluation of the body: stash the bodyContent away.
080         *
081         * @param b the BodyContent
082         * @see #doAfterBody
083         * @see #doInitBody()
084         * @see BodyTag#setBodyContent
085         */
086    
087        public void setBodyContent(BodyContent b) {
088            this.bodyContent = b;
089        }
090    
091    
092        /**
093         * Prepare for evaluation of the body just before the first body evaluation:
094         * no action.
095         *
096         * @throws JspException if an error occurred while processing this tag
097         * @see #setBodyContent
098         * @see #doAfterBody
099         * @see BodyTag#doInitBody
100         */
101    
102        public void doInitBody() throws JspException {
103        }
104    
105    
106        /**
107         * After the body evaluation: do not reevaluate and continue with the page.
108         * By default nothing is done with the bodyContent data (if any).
109         *
110         * @return SKIP_BODY
111         * @throws JspException if an error occurred while processing this tag
112         * @see #doInitBody
113         * @see BodyTag#doAfterBody
114         */
115    
116        public int doAfterBody() throws JspException {
117            return SKIP_BODY;
118        }
119    
120    
121        /**
122         * Release state.
123         *
124         * @see Tag#release
125         */
126    
127        public void release() {
128            bodyContent = null;
129    
130            super.release();
131        }
132    
133        /**
134         * Get current bodyContent.
135         *
136         * @return the body content.
137         */
138        
139        public BodyContent getBodyContent() {
140            return bodyContent;
141        }
142    
143    
144        /**
145         * Get surrounding out JspWriter.
146         *
147         * @return the enclosing JspWriter, from the bodyContent.
148         */
149    
150        public JspWriter getPreviousOut() {
151            return bodyContent.getEnclosingWriter();
152        }
153    
154        // protected fields
155    
156        /**
157         * The current BodyContent for this BodyTag.
158         */
159        protected BodyContent   bodyContent;
160    }