1 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 PAGECONTEXT = "pageContext"; 46 public static final String ENVVAR = "_"; 47 public static final String HASNEXTCODEC = "hasNextCodec"; 48 public static final String NEXTCODEC = "nextCodec"; 49 50 51 52 public static final String COLLECTION = "collection"; 53 public static final String NEXT = "next"; 54 public static final String HASNEXT = "hasNext"; 55 public static final String INDEX = "index"; 56 57 protected PageContext _pageContext; 58 protected Iterator _iterator; 59 protected int _index; 60 61 public ELIterator(PageContext pageContext, Object source) throws Exception 62 { 63 super(source); 64 65 _pageContext = pageContext; 66 _iterator = null; 67 _index = 0; 68 69 Object 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 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 hasNextCodec = EL.replaceEvalEscape(getString(HASNEXTCODEC)); 89 90 Object 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 e) 104 { 105 e.printStackTrace(); 106 107 throw new RuntimeException (e); 108 } 109 finally 110 { 111 if (envVar != null) 112 { 113 EL.setPageAttribute(pageContext, ENVVAR, envVar); 114 } 115 } 116 } 117 118 public Object next() 119 { 120 Object 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 nextCodec = EL.replaceEvalEscape(getString(NEXTCODEC)); 130 131 Object 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 e) 147 { 148 e.printStackTrace(); 149 150 throw new RuntimeException (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 (); 164 } 165 166 public Object getObject(Object client, Object 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 (_index); 179 } 180 else 181 { 182 return super.getObject(client, key); 183 } 184 } 185 186 public Object putObject(Object client, Object key, Object 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 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 getNext() 215 { 216 Object 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 getHasNext() 232 { 233 Object 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 |