1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 package com.ivata.mask.web.tag.util;
73
74 import javax.servlet.jsp.JspException;
75 import javax.servlet.jsp.JspWriter;
76 import javax.servlet.jsp.tagext.BodyContent;
77 import javax.servlet.jsp.tagext.BodyTagSupport;
78
79 /***
80 * <p>Loop increasing a counter nCounter until it reaches to (inclusive).</p>
81 *
82 * @since ivata masks 0.5 (2001-12-12)
83 * @author Colin MacLeod
84 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
85 * @version $Revision: 1.2 $
86 * TODO: This class is a temporary workaround because we had problems with the
87 * JSTL class.
88 */
89 public class ForEachTag extends BodyTagSupport {
90
91 /***
92 * Refer to {@link #getBegin}.
93 */
94 private int begin = 0;
95
96 /***
97 * Refer to {@link #getEnd}.
98 */
99 private int end;
100
101 /***
102 * Refer to {@link #getStep}.
103 */
104 private int step = 1;
105
106 /***
107 * Refer to {@link #getVar}.
108 */
109 private int var = 0;
110
111 /***
112 * Name of an attribute to set the var value to.
113 */
114 private String varAttribute = "forEachTagVarVariable";
115
116 /***
117 * Refer to {@link IterationTag#doAfterBody}.
118 *
119 * @return Refer to {@link IterationTag#doAfterBody}.
120 * @throws JspException Refer to {@link IterationTag#doAfterBody}.
121 */
122 public int doAfterBody() throws JspException {
123 try {
124 JspWriter out = getPreviousOut();
125 BodyContent bodyContent = getBodyContent();
126
127 bodyContent.writeOut(out);
128
129 bodyContent.clearBody();
130 bodyContent.clearBuffer();
131 } catch (Exception ex) {
132 throw new JspException("error in ForEachTag: " + ex.getClass()
133 + ": " + ex);
134 }
135
136 var += step;
137 pageContext.setAttribute(varAttribute, new Integer(var));
138 if (var <= end) {
139 return EVAL_BODY_BUFFERED;
140 } else {
141 return SKIP_BODY;
142 }
143 }
144
145
146 /***
147 * Refer to {@link Tag#doStartTag}.
148 * @return Refer to {@link Tag#doStartTag}.
149 * @throws JspException Refer to {@link Tag#doStartTag}.
150 */
151 public int doStartTag() throws JspException {
152 pageContext.setAttribute(varAttribute, new Integer(var));
153 if (var <= end) {
154 return EVAL_BODY_BUFFERED;
155 } else {
156 return SKIP_BODY;
157 }
158 }
159
160 /***
161 * Get the value of the counter.
162 *
163 * @return current counter value.
164 */
165 public final String getVar() {
166 return varAttribute;
167 }
168
169 /***
170 * Sets the first value of the loop counter.
171 *
172 * @param value the value the counter should start at.
173 */
174 public final void setBegin(final int value) {
175 begin = value;
176 }
177
178 /***
179 * Sets the final value of the loop counter, inclusive.
180 *
181 * @param value final value of the loop counter, inclusive.
182 */
183 public final void setEnd(final int value) {
184 end = value;
185 }
186
187 /***
188 * Sets the increment by which the counter is increased after each loop.
189 *
190 * @param value increment by which the counter is increased after each loop.
191 */
192 public final void setStep(final int value) {
193 step = value;
194 }
195
196 /***
197 * Set the current value of the counter.
198 *
199 * @param value new value of counter.
200 */
201 public final void setVar(final String value) {
202 varAttribute = value;
203 }
204 }