1 64 65 package com.jcorporate.expresso.ext.struts.taglib.logic; 66 67 import com.jcorporate.expresso.core.controller.Block; 68 import com.jcorporate.expresso.core.controller.ControllerElement; 69 import com.jcorporate.expresso.core.misc.StringUtil; 70 import com.jcorporate.expresso.ext.struts.taglib.ControllerUtils; 71 import org.apache.log4j.Logger; 72 import org.apache.struts.taglib.logic.IterateTag; 73 import org.apache.struts.util.RequestUtils; 74 75 import javax.servlet.jsp.JspException ; 76 import java.util.ArrayList ; 77 import java.util.Vector ; 78 79 80 105 public class ExIterateTag 106 extends IterateTag { 107 private static Logger log = Logger.getLogger("expresso.ext.struts.taglib.logic.ExIterateTag"); 108 109 public ExIterateTag() { 110 super(); 111 } 112 113 120 public int doStartTag() 121 throws JspException { 122 Vector v = null; 123 124 if (property == null) { 125 if (name == null) { 126 throw new JspException ("Property and name may not both be null"); 127 } 128 129 ControllerElement ce = ControllerUtils.findElement(pageContext, 130 name, null); 131 collection = ce; 132 133 if (ce != null) { 134 iterator = ce.getNestedIterator(); 135 } else { 136 throw new JspException ("No such ControllerElement '" + 137 name + "' in session"); 138 } 139 } else if (property.equalsIgnoreCase("blocks")) { 140 if (name != null) { 141 ControllerElement ce = ControllerUtils.findElement(pageContext, name, null); 142 143 if (ce instanceof Block) { 144 Block b = (Block) ce; 145 collection = ce; 146 v = b.getBlocks(); 147 148 if (v != null) { 149 iterator = new ArrayList (v).iterator(); 150 } 151 } else { 152 throw new JspException ("You cannot request nested blocks from '" + 153 name + "', it is not a block"); 154 } 155 } else { 156 v = ControllerUtils.getResponse(pageContext).getBlocks(); 157 158 if (v != null) { 159 collection = v; 160 iterator = new ArrayList (v).iterator(); 161 } 162 } 163 } else if (property.equalsIgnoreCase("transitions")) { 164 if (name != null) { 165 ControllerElement ce = ControllerUtils.findElement(pageContext, name, null); 166 167 if (ce instanceof Block) { 168 Block b = (Block) ce; 169 collection = ce; 170 v = b.getTransitions(); 171 172 if (v != null) { 173 iterator = new ArrayList (v).iterator(); 174 } 175 } else { 176 throw new JspException ("You cannot request nested blocks from '" + 177 name + "', it is not a block"); 178 } 179 } else { 180 v = ControllerUtils.getResponse(pageContext).getTransitions(); 181 182 if (v != null) { 183 collection = v; 184 iterator = new ArrayList (v).iterator(); 185 } 186 } 187 } else if (property.equalsIgnoreCase("inputs")) { 188 if (name != null) { 189 ControllerElement ce = ControllerUtils.findElement(pageContext, name, null); 190 191 if (ce instanceof Block) { 192 Block b = (Block) ce; 193 collection = ce; 194 v = b.getInputs(); 195 196 if (v != null) { 197 iterator = new ArrayList (v).iterator(); 198 } 199 } else { 200 throw new JspException ("You cannot request nested blocks from '" + 201 name + "', it is not a block"); 202 } 203 } else { 204 v = ControllerUtils.getResponse(pageContext).getInputs(); 205 206 if (v != null) { 207 collection = v; 208 iterator = new ArrayList (v).iterator(); 209 } 210 } 211 212 } else if (property.equalsIgnoreCase("outputs")) { 213 if (log.isDebugEnabled()) { 214 log.debug("Iterating over all outputs"); 215 } 216 217 if (name != null) { 218 ControllerElement ce = ControllerUtils.findElement(pageContext, name, null); 219 220 if (ce instanceof Block) { 221 Block b = (Block) ce; 222 collection = ce; 223 v = b.getOutputs(); 224 225 if (v != null) { 226 iterator = new ArrayList (v).iterator(); 227 } 228 } else { 229 throw new JspException ("You cannot request nested blocks from '" + 230 name + "', it is not a block"); 231 } 232 } else { 233 v = ControllerUtils.getResponse(pageContext).getOutputs(); 234 235 if (v != null) { 236 collection = v; 237 iterator = new ArrayList (v).iterator(); 238 } else { 239 if (log.isDebugEnabled()) { 240 log.debug("Got no outputs!"); 241 } 242 } 243 } 244 245 246 } else if (property.equalsIgnoreCase("nested")) { 247 if (log.isDebugEnabled()) { 248 log.debug("Iterating over all nested controller elements generically"); 249 } 250 251 if (name != null) { 252 ControllerElement ce = ControllerUtils.findElement(pageContext, name, null); 253 254 v = ce.getNested(); 257 if (v != null) { 258 iterator = new ArrayList (v).iterator(); 259 } else { 260 throw new JspException ("The controller element '" + 261 name + "', does not contain any nested elements"); 262 } 263 } else { 264 269 v = new Vector (); 270 Vector tmp = ControllerUtils.getResponse(pageContext).getInputs(); 271 if (tmp != null) { 272 v.addAll(tmp); 273 } 274 tmp = ControllerUtils.getResponse(pageContext).getOutputs(); 275 if (tmp != null) { 276 v.addAll(tmp); 277 } 278 tmp = ControllerUtils.getResponse(pageContext).getTransitions(); 279 if (tmp != null) { 280 v.addAll(tmp); 281 } 282 tmp = ControllerUtils.getResponse(pageContext).getBlocks(); 283 if (tmp != null) { 284 v.addAll(tmp); 285 } 286 287 collection = v; 288 iterator = new ArrayList (v).iterator(); 289 if (v.size() < 0 && log.isDebugEnabled()) { 290 log.debug("MVC Controller did not generate any controller elements!"); 291 } 292 } 293 } else { 294 ControllerElement ce = ControllerUtils.findElement(pageContext, 295 name, 296 property); 297 collection = ce; 298 299 if (ce != null) { 300 iterator = ce.getNestedIterator(); 301 } 302 } 303 if (collection == null) { 304 JspException e = new JspException (messages.getMessage("iterate.collection")); 305 RequestUtils.saveException(pageContext, e); 306 throw e; 307 } 308 if (offset == null) { 310 offsetValue = 0; 311 } else { 312 try { 313 offsetValue = Integer.parseInt(offset); 314 } catch (NumberFormatException e) { 315 Integer offsetObject = (Integer ) pageContext.findAttribute(offset); 316 317 if (offsetObject == null) { 318 offsetValue = 0; 319 } else { 320 offsetValue = offsetObject.intValue(); 321 } 322 } 323 } 324 if (offsetValue < 0) { 325 offsetValue = 0; 326 } 327 if (length == null) { 329 lengthValue = 0; 330 } else { 331 try { 332 lengthValue = Integer.parseInt(length); 333 } catch (NumberFormatException e) { 334 Integer lengthObject = (Integer ) pageContext.findAttribute(length); 335 336 if (lengthObject == null) { 337 lengthValue = 0; 338 } else { 339 lengthValue = lengthObject.intValue(); 340 } 341 } 342 } 343 if (lengthValue < 0) { 344 lengthValue = 0; 345 } 346 347 lengthCount = 0; 348 349 for (int i = 0; i < offsetValue; i++) { 351 if (iterator.hasNext()) { 352 iterator.next(); 353 } 354 } 355 if (iterator == null) { 356 throw new JspException ("No nested elements for bean '" + 357 StringUtil.notNull(name) + "', property '" + 358 StringUtil.notNull(property) + "'"); 359 } 360 if (iterator.hasNext()) { 362 Object element = iterator.next(); 363 364 if (element == null) { 365 pageContext.removeAttribute(id); 366 } else { 367 pageContext.setAttribute(id, element); 368 } 369 370 lengthCount++; 371 started = true; 372 373 if (indexId != null) { 374 pageContext.setAttribute(indexId, new Integer (getIndex())); 375 } 376 377 return (EVAL_BODY_BUFFERED); 379 } else { 380 return (SKIP_BODY); 381 } 382 } 383 } 384 | Popular Tags |