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