KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > utility > lang > ForTag


1 /*
2  * Copyright 1999,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 org.apache.taglibs.utility.lang;
17
18 import java.io.IOException JavaDoc;
19 import javax.servlet.jsp.*;
20 import javax.servlet.jsp.tagext.*;
21
22 /**
23  * This tag will iterate over its body
24  * for a number of times (as specified
25  * in the "numIter" attribute.
26  *
27  * Author Mandar Raje.
28  */

29
30 public class ForTag extends BodyTagSupport {
31     
32     private int iterations;
33     private String JavaDoc varName = "_count";
34     private int count = 0;
35     
36     public String JavaDoc getVarName() {
37     return this.varName;
38     }
39
40     public void setVarName(String JavaDoc val) {
41     this.varName = val;
42     }
43
44     public void setBegin(int begin) {
45     this.count = begin;
46     }
47     
48     public int getIterations() {
49     return iterations;
50     }
51
52     public void setIterations(int val) {
53     this.iterations = val;
54     }
55     
56     public int doStartTag() {
57         count = 0;
58     return EVAL_BODY_TAG;
59     }
60
61     public void doInitBody() throws JspException {
62     pageContext.setAttribute(getVarName(),new Integer JavaDoc(count));
63     }
64
65     public int doAfterBody() throws JspException {
66     try {
67         count++;
68         if (count < getIterations()) {
69         pageContext.setAttribute(getVarName(),new Integer JavaDoc(count));
70         return EVAL_BODY_TAG;
71         }
72         bodyContent.writeOut(bodyContent.getEnclosingWriter());
73         return SKIP_BODY;
74     } catch (IOException JavaDoc ex) {
75         throw new JspException(ex.getMessage());
76     }
77     }
78
79 }
80
Popular Tags