1 26 27 package org.nextime.ion.frontoffice.taglib; 28 29 import java.io.IOException ; 30 import java.util.Collection ; 31 import java.util.Iterator ; 32 33 import javax.servlet.jsp.JspException ; 34 import javax.servlet.jsp.JspTagException ; 35 import javax.servlet.jsp.JspWriter ; 36 import javax.servlet.jsp.tagext.BodyContent ; 37 import javax.servlet.jsp.tagext.BodyTagSupport ; 38 import javax.servlet.jsp.tagext.Tag ; 39 40 public class IterateOverTag extends BodyTagSupport { 41 42 private String _var; 43 private Collection _collection; 44 private Iterator _iterator; 45 private IterateStatus _status; 46 47 public IterateOverTag() { 48 super(); 49 init(); 50 } 51 52 public void setVar(String var) { 53 _var = var; 54 } 55 56 public int doStartTag() throws JspException { 57 58 Tag t = findAncestorWithClass(this, SelectObjectsTag.class); 59 if (t == null) 60 throw new JspTagException ("Le tag iterateOver doit etre dans un tag selectObjects"); 61 62 _collection = ((SelectObjectsTag) t).getCollection(); 63 64 if (_collection == null) 65 return (SKIP_BODY); 66 67 _status = new IterateStatus(); 68 _status._size = _collection.size(); 69 _status._index = 0; 70 71 _iterator = _collection.iterator(); 72 73 if (_iterator.hasNext()) { 74 Object element = _iterator.next(); 75 if (element == null) 76 pageContext.removeAttribute(_var); 77 else 78 pageContext.setAttribute(_var, element); 79 _status._index++; 80 pageContext.setAttribute("iterateStatus", _status); 81 return (EVAL_BODY_TAG); 82 } else 83 return (SKIP_BODY); 84 } 85 86 public int doAfterBody() throws JspException { 87 88 if (bodyContent != null) { 89 JspWriter writer = pageContext.getOut(); 90 if (writer instanceof BodyContent ) 91 writer = ((BodyContent ) writer).getEnclosingWriter(); 92 try { 93 writer.print(bodyContent.getString()); 94 } catch (IOException e) { 95 throw new JspException (e); 96 } 97 bodyContent.clearBody(); 98 } 99 100 if (_iterator.hasNext()) { 101 Object element = _iterator.next(); 102 if (element == null) 103 pageContext.removeAttribute(_var); 104 else 105 pageContext.setAttribute(_var, element); 106 _status._index++; 107 pageContext.setAttribute("iterateStatus", _status); 108 return (EVAL_BODY_TAG); 109 } else 110 return (SKIP_BODY); 111 112 } 113 114 public int doEndTag() throws JspException { 115 return (EVAL_PAGE); 116 } 117 118 public void release() { 119 super.release(); 120 init(); 121 } 122 123 private void init() { 124 _var = null; 125 _status = null; 126 } 127 128 } | Popular Tags |