001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    //
021    // This source code implements specifications defined by the Java
022    // Community Process. In order to remain compliant with the specification
023    // DO NOT add / change / or delete method signatures!
024    //
025    
026    package javax.servlet.jsp.tagext;
027    
028    import javax.servlet.jsp.JspException;
029    import javax.servlet.jsp.JspWriter;
030    
031    /**
032     * A base class for defining tag handlers implementing BodyTag.
033     *
034     * <p>
035     * The BodyTagSupport class implements the BodyTag interface and adds
036     * additional convenience methods including getter methods for the
037     * bodyContent property and methods to get at the previous out JspWriter.
038     *
039     * <p>
040     * Many tag handlers will extend BodyTagSupport and only redefine a
041     * few methods.
042     */
043    
044    public class BodyTagSupport extends TagSupport implements BodyTag {
045    
046        /**
047         * Default constructor, all subclasses are required to only define
048         * a public constructor with the same signature, and to call the
049         * superclass constructor.
050         *
051         * This constructor is called by the code generated by the JSP
052         * translator.
053         */
054    
055        public BodyTagSupport() {
056            super();
057        }
058    
059        /**
060         * Default processing of the start tag returning EVAL_BODY_BUFFERED.
061         *
062         * @return EVAL_BODY_BUFFERED
063         * @throws JspException if an error occurred while processing this tag
064         * @see BodyTag#doStartTag
065         */
066     
067        public int doStartTag() throws JspException {
068            return EVAL_BODY_BUFFERED;
069        }
070    
071    
072        /**
073         * Default processing of the end tag returning EVAL_PAGE.
074         *
075         * @return EVAL_PAGE
076         * @throws JspException if an error occurred while processing this tag
077         * @see Tag#doEndTag
078         */
079    
080        public int doEndTag() throws JspException {
081            return super.doEndTag();
082        }
083    
084    
085        // Actions related to body evaluation
086    
087        /**
088         * Prepare for evaluation of the body: stash the bodyContent away.
089         *
090         * @param b the BodyContent
091         * @see #doAfterBody
092         * @see #doInitBody()
093         * @see BodyTag#setBodyContent
094         */
095    
096        public void setBodyContent(BodyContent b) {
097            this.bodyContent = b;
098        }
099    
100    
101        /**
102         * Prepare for evaluation of the body just before the first body evaluation:
103         * no action.
104         *
105         * @throws JspException if an error occurred while processing this tag
106         * @see #setBodyContent
107         * @see #doAfterBody
108         * @see BodyTag#doInitBody
109         */
110    
111        public void doInitBody() throws JspException {
112        }
113    
114    
115        /**
116         * After the body evaluation: do not reevaluate and continue with the page.
117         * By default nothing is done with the bodyContent data (if any).
118         *
119         * @return SKIP_BODY
120         * @throws JspException if an error occurred while processing this tag
121         * @see #doInitBody
122         * @see BodyTag#doAfterBody
123         */
124    
125        public int doAfterBody() throws JspException {
126            return SKIP_BODY;
127        }
128    
129    
130        /**
131         * Release state.
132         *
133         * @see Tag#release
134         */
135    
136        public void release() {
137            bodyContent = null;
138    
139            super.release();
140        }
141    
142        /**
143         * Get current bodyContent.
144         *
145         * @return the body content.
146         */
147        
148        public BodyContent getBodyContent() {
149            return bodyContent;
150        }
151    
152    
153        /**
154         * Get surrounding out JspWriter.
155         *
156         * @return the enclosing JspWriter, from the bodyContent.
157         */
158    
159        public JspWriter getPreviousOut() {
160            return bodyContent.getEnclosingWriter();
161        }
162    
163        // protected fields
164    
165        /**
166         * The current BodyContent for this BodyTag.
167         */
168        protected BodyContent   bodyContent;
169    }