KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > rt > CoreForEachTag


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jstl.rt;
30
31 import com.caucho.util.L10N;
32
33 import javax.servlet.jsp.JspTagException JavaDoc;
34 import javax.servlet.jsp.jstl.core.LoopTagSupport;
35 import java.util.Iterator JavaDoc;
36
37 public class CoreForEachTag extends LoopTagSupport {
38   private static L10N L = new L10N(CoreForEachTag.class);
39
40   protected int _begin;
41   protected int _end;
42   
43   protected Object JavaDoc _items;
44   protected boolean _hasItems;
45
46   // runtime values
47
protected Iterator JavaDoc _iterator;
48
49   /**
50    * Sets the collection expression.
51    */

52   public void setItems(Object JavaDoc items)
53   {
54     _items = items;
55     _hasItems = true;
56   }
57
58   /**
59    * Sets the beginning value
60    */

61   public void setBegin(int begin)
62   {
63     _begin = begin;
64     this.begin = begin;
65     this.beginSpecified = true;
66   }
67
68   /**
69    * Sets the ending value
70    */

71   public void setEnd(int end)
72   {
73     _end = end;
74     this.end = end;
75     this.endSpecified = true;
76   }
77
78   /**
79    * Sets the step value
80    */

81   public void setStep(int step)
82   {
83     this.step = step;
84     this.stepSpecified = true;
85   }
86
87   /**
88    * Prepares the iterator.
89    */

90   public void prepare()
91     throws JspTagException JavaDoc
92   {
93     if (_hasItems) {
94       _iterator = com.caucho.jstl.el.ForEachTag.getIterator(_items);
95     }
96     else {
97       this.beginSpecified = false;
98       this.begin = 0;
99       this.endSpecified = false;
100       this.end = -1;
101
102       _iterator = new RangeIterator(_begin, _end);
103     }
104   }
105
106   /**
107    * Returns true if there are more items.
108    */

109   public boolean hasNext()
110   {
111     return _iterator.hasNext();
112   }
113
114   /**
115    * Returns the next item
116    */

117   public Object JavaDoc next()
118   {
119     return _iterator.next();
120   }
121
122   public static class RangeIterator implements Iterator JavaDoc {
123     private int _end;
124     private int _i;
125
126     RangeIterator(int begin, int end)
127     {
128       _i = begin;
129       _end = end;
130     }
131     
132     public boolean hasNext()
133     {
134       return _i <= _end;
135     }
136     
137     public Object JavaDoc next()
138     {
139       if (_i <= _end)
140         return new Integer JavaDoc(_i++);
141       else
142         return null;
143     }
144     
145     public void remove()
146     {
147       throw new UnsupportedOperationException JavaDoc();
148     }
149   }
150 }
151
Popular Tags