1 16 17 package org.apache.cocoon.taglib.core; 18 19 import java.sql.ResultSet ; 20 import java.util.Arrays ; 21 import java.util.Collection ; 22 import java.util.Enumeration ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.StringTokenizer ; 26 27 import org.apache.commons.lang.BooleanUtils; 28 import org.xml.sax.SAXException ; 29 30 51 public abstract class ForEachSupport extends LoopTagSupport { 52 53 56 71 72 75 91 protected static interface ForEachIterator { 92 public boolean hasNext() throws SAXException ; 93 public Object next() throws SAXException ; 94 } 95 96 101 protected static class SimpleForEachIterator implements ForEachIterator { 102 private Iterator i; 103 public SimpleForEachIterator(Iterator i) { 104 this.i = i; 105 } 106 public boolean hasNext() { 107 return i.hasNext(); 108 } 109 public Object next() { 110 return i.next(); 111 } 112 } 113 114 117 protected ForEachIterator items; protected Object rawItems; 120 123 125 protected boolean hasNext() throws SAXException { 126 return items.hasNext(); 127 } 128 129 protected Object next() throws SAXException { 130 return items.next(); 131 } 132 133 protected void prepare() throws SAXException { 134 if (rawItems != null) { 136 items = supportedTypeForEachIterator(rawItems); 138 } else { 139 items = beginEndForEachIterator(); 141 } 142 143 if (rawItems instanceof ResultSet && step != 1) 145 throw new SAXException ("FOREACH_STEP_NO_RESULTSET"); 146 } 147 148 151 public void recycle() { 153 super.recycle(); 154 items = null; 155 rawItems = null; 156 } 157 158 161 162 protected ForEachIterator supportedTypeForEachIterator(Object o) throws SAXException { 163 164 177 178 ForEachIterator items; 179 180 if (o instanceof Object []) 181 items = toForEachIterator((Object []) o); 182 else if (o instanceof boolean[]) 183 items = toForEachIterator((boolean[]) o); 184 else if (o instanceof byte[]) 185 items = toForEachIterator((byte[]) o); 186 else if (o instanceof char[]) 187 items = toForEachIterator((char[]) o); 188 else if (o instanceof short[]) 189 items = toForEachIterator((short[]) o); 190 else if (o instanceof int[]) 191 items = toForEachIterator((int[]) o); 192 else if (o instanceof long[]) 193 items = toForEachIterator((long[]) o); 194 else if (o instanceof float[]) 195 items = toForEachIterator((float[]) o); 196 else if (o instanceof double[]) 197 items = toForEachIterator((double[]) o); 198 else if (o instanceof Collection ) 199 items = toForEachIterator((Collection ) o); 200 else if (o instanceof Iterator ) 201 items = toForEachIterator((Iterator ) o); 202 else if (o instanceof Enumeration ) 203 items = toForEachIterator((Enumeration ) o); 204 else if (o instanceof Map ) 205 items = toForEachIterator((Map ) o); 206 else if (o instanceof ResultSet ) 207 items = toForEachIterator((ResultSet ) o); 208 else if (o instanceof String ) 209 items = toForEachIterator((String ) o); 210 else 211 items = toForEachIterator(o); 212 213 return (items); 214 } 215 216 221 private ForEachIterator beginEndForEachIterator() { 222 234 Integer [] ia = new Integer [end + 1]; 235 for (int i = 0; i <= end; i++) 236 ia[i] = new Integer (i); 237 return new SimpleForEachIterator(Arrays.asList(ia).iterator()); 238 } 239 240 243 protected ForEachIterator toForEachIterator(Object o) throws SAXException { 245 throw new SAXException ("FOREACH_BAD_ITEMS"); 246 } 247 248 protected ForEachIterator toForEachIterator(Object [] a) { 250 return new SimpleForEachIterator(Arrays.asList(a).iterator()); 251 } 252 253 protected ForEachIterator toForEachIterator(boolean[] a) { 255 Boolean [] wrapped = new Boolean [a.length]; 256 for (int i = 0; i < a.length; i++) 257 wrapped[i] = BooleanUtils.toBooleanObject(a[i]); 258 return new SimpleForEachIterator(Arrays.asList(wrapped).iterator()); 259 } 260 261 protected ForEachIterator toForEachIterator(byte[] a) { 263 Byte [] wrapped = new Byte [a.length]; 264 for (int i = 0; i < a.length; i++) 265 wrapped[i] = new Byte (a[i]); 266 return new SimpleForEachIterator(Arrays.asList(wrapped).iterator()); 267 } 268 269 protected ForEachIterator toForEachIterator(char[] a) { 271 Character [] wrapped = new Character [a.length]; 272 for (int i = 0; i < a.length; i++) 273 wrapped[i] = new Character (a[i]); 274 return new SimpleForEachIterator(Arrays.asList(wrapped).iterator()); 275 } 276 277 protected ForEachIterator toForEachIterator(short[] a) { 279 Short [] wrapped = new Short [a.length]; 280 for (int i = 0; i < a.length; i++) 281 wrapped[i] = new Short (a[i]); 282 return new SimpleForEachIterator(Arrays.asList(wrapped).iterator()); 283 } 284 285 protected ForEachIterator toForEachIterator(int[] a) { 287 Integer [] wrapped = new Integer [a.length]; 288 for (int i = 0; i < a.length; i++) 289 wrapped[i] = new Integer (a[i]); 290 return new SimpleForEachIterator(Arrays.asList(wrapped).iterator()); 291 } 292 293 protected ForEachIterator toForEachIterator(long[] a) { 295 Long [] wrapped = new Long [a.length]; 296 for (int i = 0; i < a.length; i++) 297 wrapped[i] = new Long (a[i]); 298 return new SimpleForEachIterator(Arrays.asList(wrapped).iterator()); 299 } 300 301 protected ForEachIterator toForEachIterator(float[] a) { 303 Float [] wrapped = new Float [a.length]; 304 for (int i = 0; i < a.length; i++) 305 wrapped[i] = new Float (a[i]); 306 return new SimpleForEachIterator(Arrays.asList(wrapped).iterator()); 307 } 308 309 protected ForEachIterator toForEachIterator(double[] a) { 311 Double [] wrapped = new Double [a.length]; 312 for (int i = 0; i < a.length; i++) 313 wrapped[i] = new Double (a[i]); 314 return new SimpleForEachIterator(Arrays.asList(wrapped).iterator()); 315 } 316 317 protected ForEachIterator toForEachIterator(Collection c) { 319 return new SimpleForEachIterator(c.iterator()); 320 } 321 322 protected ForEachIterator toForEachIterator(Iterator i) { 324 return new SimpleForEachIterator(i); 325 } 326 327 protected ForEachIterator toForEachIterator(Enumeration e) { 329 330 class EnumerationAdapter implements ForEachIterator { 332 private Enumeration e; 333 public EnumerationAdapter(Enumeration e) { 334 this.e = e; 335 } 336 public boolean hasNext() { 337 return e.hasMoreElements(); 338 } 339 public Object next() { 340 return e.nextElement(); 341 } 342 } 343 344 return new EnumerationAdapter(e); 345 } 346 347 protected ForEachIterator toForEachIterator(Map m) { 349 return new SimpleForEachIterator(m.entrySet().iterator()); 350 } 351 352 protected ForEachIterator toForEachIterator(ResultSet rs) throws SAXException { 354 355 class ResultSetAdapter implements ForEachIterator { 357 private ResultSet rs; 358 public ResultSetAdapter(ResultSet rs) { 359 this.rs = rs; 360 } 361 public boolean hasNext() throws SAXException { 362 try { 363 return !(rs.isLast()); } catch (java.sql.SQLException ex) { 365 throw new SAXException (ex.getMessage()); 366 } 367 } 368 public Object next() throws SAXException { 369 try { 370 rs.next(); 371 return rs; 372 } catch (java.sql.SQLException ex) { 373 throw new SAXException (ex.getMessage()); 374 } 375 } 376 } 377 378 return new ResultSetAdapter(rs); 379 } 380 381 protected ForEachIterator toForEachIterator(String s) { 383 StringTokenizer st = new StringTokenizer (s, ","); 384 return toForEachIterator(st); } 386 387 } 388 | Popular Tags |