1 16 17 package org.apache.taglibs.standard.tag.common.core; 18 19 import java.util.Arrays ; 20 import java.util.Collection ; 21 import java.util.Enumeration ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.StringTokenizer ; 25 26 import javax.servlet.jsp.JspTagException ; 27 import javax.servlet.jsp.jstl.core.LoopTagSupport; 28 29 import org.apache.taglibs.standard.resources.Resources; 30 31 47 48 public abstract class ForEachSupport extends LoopTagSupport { 49 50 53 68 69 70 73 89 protected static interface ForEachIterator { 90 public boolean hasNext() throws JspTagException ; 91 public Object next() throws JspTagException ; 92 } 93 94 99 protected class SimpleForEachIterator implements ForEachIterator { 100 private Iterator i; 101 public SimpleForEachIterator(Iterator i) { 102 this.i = i; 103 } 104 public boolean hasNext() { 105 return i.hasNext(); 106 } 107 public Object next() { 108 return i.next(); 109 } 110 } 111 112 113 116 protected ForEachIterator items; protected Object rawItems; 119 120 123 125 protected boolean hasNext() throws JspTagException { 126 return items.hasNext(); 127 } 128 129 protected Object next() throws JspTagException { 130 return items.next(); 131 } 132 133 protected void prepare() throws JspTagException { 134 if (rawItems != null) { 136 items = supportedTypeForEachIterator(rawItems); 138 } else { 139 items = beginEndForEachIterator(); 141 } 142 143 149 } 150 151 152 155 public void release() { 157 super.release(); 158 items = null; 159 rawItems = null; 160 } 161 162 163 166 167 protected ForEachIterator supportedTypeForEachIterator(Object o) 168 throws JspTagException { 169 170 183 184 ForEachIterator items; 185 186 if (o instanceof Object []) 187 items = toForEachIterator((Object []) o); 188 else if (o instanceof boolean[]) 189 items = toForEachIterator((boolean[]) o); 190 else if (o instanceof byte[]) 191 items = toForEachIterator((byte[]) o); 192 else if (o instanceof char[]) 193 items = toForEachIterator((char[]) o); 194 else if (o instanceof short[]) 195 items = toForEachIterator((short[]) o); 196 else if (o instanceof int[]) 197 items = toForEachIterator((int[]) o); 198 else if (o instanceof long[]) 199 items = toForEachIterator((long[]) o); 200 else if (o instanceof float[]) 201 items = toForEachIterator((float[]) o); 202 else if (o instanceof double[]) 203 items = toForEachIterator((double[]) o); 204 else if (o instanceof Collection ) 205 items = toForEachIterator((Collection ) o); 206 else if (o instanceof Iterator ) 207 items = toForEachIterator((Iterator ) o); 208 else if (o instanceof Enumeration ) 209 items = toForEachIterator((Enumeration ) o); 210 else if (o instanceof Map ) 211 items = toForEachIterator((Map ) o); 212 216 else if (o instanceof String ) 217 items = toForEachIterator((String ) o); 218 else 219 items = toForEachIterator(o); 220 221 return (items); 222 } 223 224 229 private ForEachIterator beginEndForEachIterator() { 230 242 Integer [] ia = new Integer [end + 1]; 243 for (int i = 0; i <= end; i++) 244 ia[i] = new Integer (i); 245 return new SimpleForEachIterator(Arrays.asList(ia).iterator()); 246 } 247 248 249 252 protected ForEachIterator toForEachIterator(Object o) 254 throws JspTagException { 255 throw new JspTagException (Resources.getMessage("FOREACH_BAD_ITEMS")); 256 } 257 258 protected ForEachIterator toForEachIterator(Object [] a) { 260 return new SimpleForEachIterator(Arrays.asList(a).iterator()); 261 } 262 263 protected ForEachIterator toForEachIterator(boolean[] a) { 265 Boolean [] wrapped = new Boolean [a.length]; 266 for (int i = 0; i < a.length; i++) 267 wrapped[i] = new Boolean (a[i]); 268 return new SimpleForEachIterator(Arrays.asList(wrapped).iterator()); 269 } 270 271 protected ForEachIterator toForEachIterator(byte[] a) { 273 Byte [] wrapped = new Byte [a.length]; 274 for (int i = 0; i < a.length; i++) 275 wrapped[i] = new Byte (a[i]); 276 return new SimpleForEachIterator(Arrays.asList(wrapped).iterator()); 277 } 278 279 protected ForEachIterator toForEachIterator(char[] a) { 281 Character [] wrapped = new Character [a.length]; 282 for (int i = 0; i < a.length; i++) 283 wrapped[i] = new Character (a[i]); 284 return new SimpleForEachIterator(Arrays.asList(wrapped).iterator()); 285 } 286 287 protected ForEachIterator toForEachIterator(short[] a) { 289 Short [] wrapped = new Short [a.length]; 290 for (int i = 0; i < a.length; i++) 291 wrapped[i] = new Short (a[i]); 292 return new SimpleForEachIterator(Arrays.asList(wrapped).iterator()); 293 } 294 295 protected ForEachIterator toForEachIterator(int[] a) { 297 Integer [] wrapped = new Integer [a.length]; 298 for (int i = 0; i < a.length; i++) 299 wrapped[i] = new Integer (a[i]); 300 return new SimpleForEachIterator(Arrays.asList(wrapped).iterator()); 301 } 302 303 protected ForEachIterator toForEachIterator(long[] a) { 305 Long [] wrapped = new Long [a.length]; 306 for (int i = 0; i < a.length; i++) 307 wrapped[i] = new Long (a[i]); 308 return new SimpleForEachIterator(Arrays.asList(wrapped).iterator()); 309 } 310 311 protected ForEachIterator toForEachIterator(float[] a) { 313 Float [] wrapped = new Float [a.length]; 314 for (int i = 0; i < a.length; i++) 315 wrapped[i] = new Float (a[i]); 316 return new SimpleForEachIterator(Arrays.asList(wrapped).iterator()); 317 } 318 319 protected ForEachIterator toForEachIterator(double[] a) { 321 Double [] wrapped = new Double [a.length]; 322 for (int i = 0; i < a.length; i++) 323 wrapped[i] = new Double (a[i]); 324 return new SimpleForEachIterator(Arrays.asList(wrapped).iterator()); 325 } 326 327 protected ForEachIterator toForEachIterator(Collection c) { 329 return new SimpleForEachIterator(c.iterator()); 330 } 331 332 protected ForEachIterator toForEachIterator(Iterator i) { 334 return new SimpleForEachIterator(i); 335 } 336 337 protected ForEachIterator toForEachIterator(Enumeration e) { 339 340 class EnumerationAdapter implements ForEachIterator { 342 private Enumeration e; 343 public EnumerationAdapter(Enumeration e) { 344 this.e = e; 345 } 346 public boolean hasNext() { 347 return e.hasMoreElements(); 348 } 349 public Object next() { 350 return e.nextElement(); 351 } 352 } 353 354 return new EnumerationAdapter(e); 355 } 356 357 protected ForEachIterator toForEachIterator(Map m) { 359 return new SimpleForEachIterator(m.entrySet().iterator()); 360 } 361 362 393 394 protected ForEachIterator toForEachIterator(String s) { 396 StringTokenizer st = new StringTokenizer (s, ","); 397 return toForEachIterator(st); } 399 400 } 401 | Popular Tags |