KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > IteratorLoopSupportTag


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.jsp;
30
31 import javax.servlet.jsp.jstl.core.LoopTag;
32 import javax.servlet.jsp.jstl.core.LoopTagStatus;
33 import javax.servlet.jsp.tagext.JspTag JavaDoc;
34 import javax.servlet.jsp.tagext.SimpleTag JavaDoc;
35 import javax.servlet.jsp.tagext.Tag JavaDoc;
36 import javax.servlet.jsp.tagext.TagAdapter JavaDoc;
37 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
38
39 public class IteratorLoopSupportTag extends TagSupport JavaDoc
40   implements LoopTag, LoopTagStatus {
41   private JspTag JavaDoc _parent;
42   
43   private int _begin;
44   private int _end;
45   private int _step;
46   
47   private int _index;
48   private int _count;
49
50   private Object JavaDoc _current;
51   private boolean _hasNext;
52
53   /**
54    * Sets the parent as a JspTag
55    */

56   public void setParent(JspTag JavaDoc parent)
57   {
58     _parent = parent;
59   }
60
61   /**
62    * Sets the parent as a JspTag
63    */

64   public void setParent(Tag parent)
65   {
66     _parent = parent;
67   }
68
69   /**
70    * Returns the parent.
71    */

72   public Tag getParent()
73   {
74     if (_parent == null || _parent instanceof Tag)
75       return (Tag) _parent;
76     else
77       return new TagAdapter JavaDoc((SimpleTag JavaDoc) _parent);
78   }
79   
80   /**
81    * Sets the initial values.
82    */

83   public void init(int begin, int end, int step)
84   {
85     _begin = begin;
86     _end = end;
87     _step = step;
88     _count = 0;
89     _index = _begin - _step;
90   }
91   
92   /**
93    * Sets the current value.
94    */

95   public void setCurrent(Object JavaDoc current, boolean hasNext)
96   {
97     _index += _step;
98     
99     _current = current;
100     _hasNext = hasNext;
101     
102     _count++;
103   }
104   
105   /**
106    * Returns the current value.
107    */

108   public Object JavaDoc getCurrent()
109   {
110     return _current;
111   }
112   
113   /**
114    * Returns the loop status
115    */

116   public LoopTagStatus getLoopStatus()
117   {
118     return this;
119   }
120
121   public int getIndex()
122   {
123     return _index;
124   }
125   
126   public int getCount()
127   {
128     return _count;
129   }
130   
131   public boolean isFirst()
132   {
133     return _count == 1;
134   }
135   
136   public boolean isLast()
137   {
138     return _end < _index + _step || ! _hasNext;
139   }
140   
141   public Integer JavaDoc getBegin()
142   {
143     return new Integer JavaDoc(_begin);
144   }
145   
146   public Integer JavaDoc getEnd()
147   {
148     return new Integer JavaDoc(_end);
149   }
150   
151   public Integer JavaDoc getStep()
152   {
153     return new Integer JavaDoc(_step);
154   }
155 }
156
Popular Tags