KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jag > taglib > IteratorTag


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.jag.taglib;
19
20
21 import java.util.*;
22
23 import com.finalist.jag.*;
24 import com.finalist.jag.taglib.util.RequestUtil;
25
26
27 /**
28  * Class IteratorTag
29  *
30  *
31  * @author Wendel D. de Witte
32  * @version %I%, %G%
33  */

34 public class IteratorTag extends TagBodySupport {
35
36    /** Field id */
37    private String JavaDoc id = null;
38
39    /** Field name */
40    private String JavaDoc name = null;
41
42    /** Field property */
43    private String JavaDoc property = null;
44
45    /** Field seperator */
46    private String JavaDoc seperator = null;
47
48    /** Field iterator */
49    private Iterator iterator = null;
50
51    /** Field max */
52    protected String JavaDoc max = null;
53
54    /** Field counterId */
55    protected String JavaDoc counterId = null;
56
57
58    /////////////////////////////////
59

60    /** Field counter */
61    protected int counter = 0;
62
63    /** Field nMax */
64    protected int nMax = 0;
65
66    /**
67     * Method getId
68     *
69     *
70     * @return
71     *
72     */

73    public String JavaDoc getId() {
74       return (this.id);
75    }
76
77    /**
78     * Method setId
79     *
80     *
81     * @param id
82     *
83     */

84    public void setId(String JavaDoc id) {
85       this.id = id;
86    }
87
88    /**
89     * Method getName
90     *
91     *
92     * @return
93     *
94     */

95    public String JavaDoc getName() {
96       return (this.name);
97    }
98
99    /**
100     * Method setName
101     *
102     *
103     * @param name
104     *
105     */

106    public void setName(String JavaDoc name) {
107       this.name = name;
108    }
109
110    /**
111     * Method getValue
112     *
113     *
114     * @return
115     *
116     */

117    public String JavaDoc getProperty() {
118       return (this.property);
119    }
120
121    /**
122     * Method setValue
123     *
124     *
125     * @param property
126     *
127     */

128    public void setProperty(String JavaDoc property) {
129       this.property = property;
130    }
131
132    /**
133     * Method getSeperator
134     *
135     *
136     * @return
137     *
138     */

139    public String JavaDoc getSeperator() {
140       return (this.seperator);
141    }
142
143    /**
144     * Method setSeperator
145     *
146     *
147     * @param seperator
148     *
149     */

150    public void setSeperator(String JavaDoc seperator) {
151       this.seperator = seperator;
152    }
153
154    /**
155     * Method getMax
156     *
157     *
158     * @return
159     *
160     */

161    public String JavaDoc getMax() {
162       return (this.max);
163    }
164
165    /**
166     * Method setMax
167     *
168     *
169     * @param max
170     *
171     */

172    public void setMax(String JavaDoc max) {
173       this.max = max;
174    }
175
176    /**
177     * Method getCounter
178     *
179     *
180     * @return
181     *
182     */

183    public String JavaDoc getCounter() {
184       return (this.counterId);
185    }
186
187    /**
188     * Method setCounter
189     *
190     *
191     * @param counterId
192     *
193     */

194    public void setCounter(String JavaDoc counterId) {
195       this.counterId = counterId;
196    }
197
198    /**
199     * Method doStartTag
200     *
201     *
202     * @return
203     *
204     * @throws JagException
205     *
206     */

207    public int doStartTag() throws JagException {
208
209       Collection collection = RequestUtil.lookupCollection(getPageContext(),
210             name, property);
211
212       if (collection == null) {
213          throw new JagException("IteratorTag: Invalid attributes >" + name
214                + ", " + property + "<");
215       }
216       try {
217          nMax = (max == null) ? -1 : new Integer JavaDoc(max).intValue();
218       } catch (Exception JavaDoc e) {
219          throw new JagException(e.getMessage());
220       }
221       iterator = collection.iterator();
222
223       return (EVAL_PAGE);
224    }
225
226    /**
227     * Method doAfterBodyTag
228     *
229     *
230     * @return
231     *
232     * @throws JagException
233     *
234     */

235    public int doAfterBodyTag() throws JagException {
236       while ((iterator != null) && iterator.hasNext()) {
237          if (counterId != null) {
238             getPageContext().setAttribute(counterId, "" + counter);
239          }
240          if (nMax > -1 && counter >= nMax) break;
241
242          getPageContext().setAttribute(id, iterator.next());
243          if (seperator != null && counter > 0) {
244             getWriter().print(seperator);
245          }
246          counter++;
247          return (EVAL_BODY_TAG);
248       }
249
250       return (SKIP_BODY);
251    }
252
253    /**
254     * Method release
255     *
256     *
257     */

258    public void release() {
259
260       super.release();
261       getPageContext().removeAttribute(id);
262       if (counterId != null) {
263          getPageContext().removeAttribute(counterId);
264       }
265       iterator = null;
266    }
267 }
268
269
Popular Tags