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 examples;
018    
019    import javax.servlet.jsp.*;
020    import java.io.IOException;
021    
022    /**
023     * Example1: the simplest tag
024     * Collect attributes and call into some actions
025     *
026     * <foo att1="..." att2="...." att3="...." />
027     */
028    
029    public class FooTag 
030        extends ExampleTagBase 
031    {
032        private String atts[] = new String[3];
033        int i = 0;
034        
035        private final void setAtt(int index, String value) {
036            atts[index] = value;
037        }
038        
039        public void setAtt1(String value) {
040            setAtt(0, value);
041        }
042        
043        public void setAtt2(String value) {
044            setAtt(1, value);
045        }
046    
047        public void setAtt3(String value) {
048            setAtt(2, value);
049        }
050        
051        /**
052         * Process start tag
053         *
054         * @return EVAL_BODY_INCLUDE
055         */
056        public int doStartTag() throws JspException {
057            i = 0;
058            return EVAL_BODY_TAG;
059        }
060    
061        public void doInitBody() throws JspException {
062            pageContext.setAttribute("member", atts[i]);
063            i++;
064        }
065        
066        public int doAfterBody() throws JspException {
067            try {
068                if (i == 3) {
069                    bodyOut.writeOut(bodyOut.getEnclosingWriter());
070                    return SKIP_BODY;
071                } else
072                    pageContext.setAttribute("member", atts[i]);
073                i++;
074                return EVAL_BODY_TAG;
075            } catch (IOException ex) {
076                throw new JspTagException(ex.toString());
077            }
078        }
079    }
080