1 17 18 package com.finalist.jag.taglib; 19 20 21 import java.util.*; 22 23 import com.finalist.jag.*; 24 import com.finalist.jag.taglib.util.RequestUtil; 25 26 27 34 public class IteratorTag extends TagBodySupport { 35 36 37 private String id = null; 38 39 40 private String name = null; 41 42 43 private String property = null; 44 45 46 private String seperator = null; 47 48 49 private Iterator iterator = null; 50 51 52 protected String max = null; 53 54 55 protected String counterId = null; 56 57 58 60 61 protected int counter = 0; 62 63 64 protected int nMax = 0; 65 66 73 public String getId() { 74 return (this.id); 75 } 76 77 84 public void setId(String id) { 85 this.id = id; 86 } 87 88 95 public String getName() { 96 return (this.name); 97 } 98 99 106 public void setName(String name) { 107 this.name = name; 108 } 109 110 117 public String getProperty() { 118 return (this.property); 119 } 120 121 128 public void setProperty(String property) { 129 this.property = property; 130 } 131 132 139 public String getSeperator() { 140 return (this.seperator); 141 } 142 143 150 public void setSeperator(String seperator) { 151 this.seperator = seperator; 152 } 153 154 161 public String getMax() { 162 return (this.max); 163 } 164 165 172 public void setMax(String max) { 173 this.max = max; 174 } 175 176 183 public String getCounter() { 184 return (this.counterId); 185 } 186 187 194 public void setCounter(String counterId) { 195 this.counterId = counterId; 196 } 197 198 207 public int doStartTag() throws JagException { 208 209 Collection collection = RequestUtil.lookupCollection(getPageContext(), 210 name, property); 211 212 if (collection == null) { 213 throw new JagException("IteratorTag: Invalid attributes >" + name 214 + ", " + property + "<"); 215 } 216 try { 217 nMax = (max == null) ? -1 : new Integer (max).intValue(); 218 } catch (Exception e) { 219 throw new JagException(e.getMessage()); 220 } 221 iterator = collection.iterator(); 222 223 return (EVAL_PAGE); 224 } 225 226 235 public int doAfterBodyTag() throws JagException { 236 while ((iterator != null) && iterator.hasNext()) { 237 if (counterId != null) { 238 getPageContext().setAttribute(counterId, "" + counter); 239 } 240 if (nMax > -1 && counter >= nMax) break; 241 242 getPageContext().setAttribute(id, iterator.next()); 243 if (seperator != null && counter > 0) { 244 getWriter().print(seperator); 245 } 246 counter++; 247 return (EVAL_BODY_TAG); 248 } 249 250 return (SKIP_BODY); 251 } 252 253 258 public void release() { 259 260 super.release(); 261 getPageContext().removeAttribute(id); 262 if (counterId != null) { 263 getPageContext().removeAttribute(counterId); 264 } 265 iterator = null; 266 } 267 } 268 269 | Popular Tags |