KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > jsp > tag > ELIterator


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

34
35 package com.micronova.jsp.tag;
36
37 import com.micronova.util.*;
38 import java.util.*;
39 import javax.servlet.jsp.*;
40 import java.lang.reflect.*;
41 import java.util.regex.*;
42
43 public class ELIterator extends NestedMap implements Iterator
44 {
45     public static final String JavaDoc PAGECONTEXT = "pageContext";
46     public static final String JavaDoc ENVVAR = "_";
47     public static final String JavaDoc HASNEXTCODEC = "hasNextCodec";
48     public static final String JavaDoc NEXTCODEC = "nextCodec";
49
50     /** special keys */
51
52     public static final String JavaDoc COLLECTION = "collection";
53     public static final String JavaDoc NEXT = "next";
54     public static final String JavaDoc HASNEXT = "hasNext";
55     public static final String JavaDoc INDEX = "index";
56
57     protected PageContext _pageContext;
58     protected Iterator _iterator;
59     protected int _index;
60
61     public ELIterator(PageContext pageContext, Object JavaDoc source) throws Exception JavaDoc
62     {
63         super(source);
64
65         _pageContext = pageContext;
66         _iterator = null;
67         _index = 0;
68
69         Object JavaDoc indexSpec = this.getMapObject(INDEX);
70
71         if (indexSpec != null)
72         {
73             _index = TypeUtil.isInteger(indexSpec).intValue();
74         }
75     }
76
77     public boolean hasNext()
78     {
79         Object JavaDoc envVar = null;
80         PageContext pageContext = _pageContext;
81
82         try
83         {
84             envVar = EL.getPageAttribute(pageContext, ENVVAR);
85
86             EL.setPageAttribute(pageContext, ENVVAR, this);
87
88             String JavaDoc hasNextCodec = EL.replaceEvalEscape(getString(HASNEXTCODEC));
89
90             Object JavaDoc hasNextValue = null;
91
92             if (!TypeUtil.isEmptyString(hasNextCodec))
93             {
94                 hasNextValue = EL.applyCodec(pageContext, hasNextCodec, this);
95             }
96             else
97             {
98                 hasNextValue = getHasNext();
99             }
100
101             return TypeUtil.isTrue(hasNextValue);
102         }
103         catch (Exception JavaDoc e)
104         {
105             e.printStackTrace();
106
107             throw new RuntimeException JavaDoc(e);
108         }
109         finally
110         {
111             if (envVar != null)
112             {
113                 EL.setPageAttribute(pageContext, ENVVAR, envVar);
114             }
115         }
116     }
117
118     public Object JavaDoc next()
119     {
120         Object JavaDoc envVar = null;
121         PageContext pageContext = _pageContext;
122
123         try
124         {
125             envVar = EL.getPageAttribute(pageContext, ENVVAR);
126
127             EL.setPageAttribute(pageContext, ENVVAR, this);
128
129             String JavaDoc nextCodec = EL.replaceEvalEscape(getString(NEXTCODEC));
130
131             Object JavaDoc x = null;
132
133             if (!TypeUtil.isEmptyString(nextCodec))
134             {
135                 x = EL.applyCodec(pageContext, nextCodec, this);
136             }
137             else
138             {
139                 x = getNext();
140             }
141
142             _index ++;
143
144             return x;
145         }
146         catch (Exception JavaDoc e)
147         {
148             e.printStackTrace();
149
150             throw new RuntimeException JavaDoc(e);
151         }
152         finally
153         {
154             if (envVar != null)
155             {
156                 EL.setPageAttribute(pageContext, ENVVAR, envVar);
157             }
158         }
159     }
160
161     public void remove()
162     {
163         throw new UnsupportedOperationException JavaDoc();
164     }
165
166     public Object JavaDoc getObject(Object JavaDoc client, Object JavaDoc key)
167     {
168         if (NEXT.equals(key))
169         {
170             return getNext();
171         }
172         else if (HASNEXT.equals(key))
173         {
174             return getHasNext();
175         }
176         else if (INDEX.equals(key))
177         {
178             return new Integer JavaDoc(_index);
179         }
180         else
181         {
182             return super.getObject(client, key);
183         }
184     }
185
186     public Object JavaDoc putObject(Object JavaDoc client, Object JavaDoc key, Object JavaDoc value)
187     {
188         if (COLLECTION.equals(key))
189         {
190             _iterator = null;
191         }
192
193         return super.putObject(client, key, value);
194     }
195
196     public Iterator getIterator()
197     {
198         Iterator iterator = _iterator;
199         
200         if (iterator == null)
201         {
202             Object JavaDoc collection = get(COLLECTION);
203
204             if (collection != null)
205             {
206                 iterator = ((Collection)collection).iterator();
207                 _iterator = iterator;
208             }
209         }
210
211         return iterator;
212     }
213
214     public Object JavaDoc getNext()
215     {
216         Object JavaDoc next = getMapObject(NEXT);
217
218         if (next == null)
219         {
220             Iterator iterator = getIterator();
221
222             if (iterator != null)
223             {
224                 next = iterator.next();
225             }
226         }
227
228         return next;
229     }
230
231     public Object JavaDoc getHasNext()
232     {
233         Object JavaDoc hasNext = getMapObject(HASNEXT);
234
235         if (hasNext != null)
236         {
237             if (TypeUtil.isTrue(hasNext))
238             {
239                 hasNext = Boolean.TRUE;
240             }
241             else
242             {
243                 hasNext = null;
244             }
245         }
246         else
247         {
248             Iterator iterator = getIterator();
249
250             if (iterator != null)
251             {
252                 if (iterator.hasNext())
253                 {
254                     hasNext = Boolean.TRUE;
255                 }
256             }
257         }
258  
259         return hasNext;
260     }
261 }
262
263
Popular Tags