1 10 package org.mmbase.bridge.jsp.taglib.functions; 11 12 import java.io.IOException ; 13 import java.util.*; 14 15 import javax.servlet.jsp.*; 16 17 import org.mmbase.bridge.jsp.taglib.*; 18 import org.mmbase.bridge.jsp.taglib.containers.FunctionContainerReferrer; 19 import org.mmbase.bridge.jsp.taglib.util.*; 20 import org.mmbase.util.Casting; 21 import org.mmbase.util.logging.*; 22 23 32 public class ListFunctionTag extends AbstractFunctionTag implements ListProvider, FunctionContainerReferrer, Writer { 33 34 private static final Logger log = Logging.getLoggerInstance(ListFunctionTag.class); 35 37 protected Collection returnCollection; 38 protected Iterator iterator; 39 protected int currentItemIndex= -1; 40 41 private ContextCollector collector; 42 protected Attribute comparator = Attribute.NULL; 43 44 public int size(){ 45 return returnCollection.size(); 46 } 47 public int getIndex() { 48 return currentItemIndex; 49 } 50 51 public int getIndexOffset() { 52 return 1; 53 } 54 55 public boolean isChanged() { 56 return true; 57 } 58 public Object getCurrent() { 59 return getWriterValue(); 60 } 61 62 public void remove() { 63 iterator.remove(); 64 } 65 66 67 public ContextContainer getContextContainer() { 68 return collector.getContextContainer(); 69 } 70 71 public int doStartTag() throws JspTagException { 72 Object value = getFunctionValue(false); if (value instanceof Collection && comparator.equals(Attribute.NULL)) { 74 returnCollection = (Collection) value; 75 } else { 76 returnCollection = Casting.toCollection(value); 77 } 78 if (log.isDebugEnabled()) { 79 log.debug("Using " + returnCollection); 80 } 81 82 collector = new ContextCollector(getContextProvider()); 83 helper.overrideWrite(false); currentItemIndex = -1; if (!comparator.equals(Attribute.NULL)) { 86 ListSorter.sort((List)returnCollection, (String ) comparator.getValue(this), this); 87 } 88 iterator = returnCollection.iterator(); 89 if (iterator.hasNext()) { 90 return EVAL_BODY_BUFFERED; 91 } 92 return SKIP_BODY; 93 } 94 95 public int doAfterBody() throws JspException { 96 if (getId() != null) { 97 getContextProvider().getContextContainer().unRegister(getId()); 98 } 99 100 helper.doAfterBody(); 101 collector.doAfterBody(); 102 103 if (iterator.hasNext()){ 104 doInitBody(); 105 return EVAL_BODY_AGAIN; 106 } else { 107 if (bodyContent != null) { 108 try { 109 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 110 } catch (IOException ioe){ 111 throw new TaglibException(ioe); 112 } 113 } 114 return SKIP_BODY; 115 } 116 } 117 public int doEndTag() throws JspTagException { 118 if (getId() != null) { 119 getContextProvider().getContextContainer().register(getId(), returnCollection, false); 120 } 121 returnCollection = null; 123 iterator = null; 124 collector = null; 125 return super.doEndTag(); 126 } 127 128 129 public void doInitBody() throws JspTagException { 130 if (iterator.hasNext()){ 131 currentItemIndex ++; 132 helper.setValue(iterator.next()); 133 if (getId() != null) { 134 getContextProvider().getContextContainer().register(getId(), helper.getValue()); 135 } 136 137 } 138 } 139 140 public javax.servlet.jsp.jstl.core.LoopTagStatus getLoopStatus() { 141 return new ListProviderLoopTagStatus(this); 142 } 143 144 } 145 | Popular Tags |