1 10 package org.mmbase.bridge.jsp.taglib; 11 12 import java.io.IOException ; 13 import java.util.*; 14 15 import javax.servlet.jsp.*; 16 import javax.servlet.jsp.jstl.core.*; 17 18 import org.mmbase.util.Casting; 19 import org.mmbase.bridge.jsp.taglib.util.*; 20 21 28 29 public class StringListTag extends NodeReferrerTag implements ListProvider, Writer { 31 protected List returnList; 32 protected Iterator iterator; 33 protected int currentItemIndex= -1; 34 35 protected Attribute max = Attribute.NULL; 36 protected Attribute comparator = Attribute.NULL; 37 protected Attribute add= Attribute.NULL; 38 protected Attribute retain = Attribute.NULL; 39 protected Attribute remove = Attribute.NULL; 40 41 public int size(){ 42 return returnList.size(); 43 } 44 public int getIndex() { 45 return currentItemIndex; 46 } 47 48 public int getIndexOffset() { 49 return 1; 50 } 51 52 public boolean isChanged() { 53 return true; 54 } 55 public Object getCurrent() { 56 return getWriterValue(); 57 } 58 59 public void remove() { 60 iterator.remove(); 61 } 62 63 64 public void setMax(String m) throws JspTagException { 65 max = getAttribute(m); 66 } 67 68 public void setAdd(String a) throws JspTagException { 69 add = getAttribute(a); 70 } 71 72 public void setRetain(String r) throws JspTagException { 73 retain = getAttribute(r); 74 } 75 76 public void setRemove(String r) throws JspTagException { 77 remove = getAttribute(r); 78 } 79 80 protected int getMaxNumber() throws JspTagException { 81 return max.getInt(this, -1); 82 } 83 84 public void setComparator(String c) throws JspTagException { 85 comparator = getAttribute(c); 86 } 87 88 91 private ContextCollector collector; 92 93 94 95 public ContextContainer getContextContainer() { 97 return collector; 98 } 99 100 101 104 protected List getList() throws JspTagException { 105 throw new JspTagException("Should use 'referid' attribute on liststrings tag"); 106 } 107 108 111 protected void truncateList() throws JspTagException { 112 if (max != Attribute.NULL) { 113 int m = getMaxNumber(); 114 if (m > 0 && m < returnList.size()) { 115 returnList = returnList.subList(0, m); 116 } 117 } 118 } 119 120 124 public int doStartTag() throws JspTagException{ 125 126 collector = new ContextCollector(getContextProvider()); 127 128 129 helper.overrideWrite(false); 131 if (getReferid() != null) { 132 Object o = getObject(getReferid()); 133 if (! (o instanceof Collection)) { 134 throw new JspTagException("Context variable " + getReferid() + " is not a Collection"); 135 } 136 if (o instanceof List) { 137 returnList = (List) o; 138 } else { 139 returnList = new ArrayList((Collection) o); 140 } 141 truncateList(); 142 if (getReferid().equals(getId())) { getContextProvider().getContextContainer().unRegister(getId()); 144 } 145 } else { 146 returnList = getList(); 147 } 148 149 if (getId() != null) { 150 returnList = new ArrayList(returnList); 151 } 152 if (add != Attribute.NULL) { 153 Object addObject = getObjectConditional(add.getString(this)); 154 if (addObject != null) { 155 if (addObject instanceof Collection) { 156 returnList.addAll((Collection) addObject); 157 } else { 158 returnList.add(Casting.toString(addObject)); 159 } 160 } 161 } 162 if (retain != Attribute.NULL) { 163 Object retainObject = getObjectConditional(retain.getString(this)); 164 if (retainObject != null) { 165 if (retainObject instanceof Collection) { 166 returnList.retainAll((Collection) retainObject); 167 } else { 168 returnList.retainAll(Collections.singletonList(Casting.toString(retainObject))); 169 } 170 } 171 } 172 if (remove != Attribute.NULL) { 173 Object removeObject = getObjectConditional(remove.getString(this)); 174 if (removeObject != null) { 175 if (removeObject instanceof Collection) { 176 returnList.removeAll((Collection) removeObject); 177 } else { 178 returnList.remove(Casting.toString(removeObject)); 179 } 180 } 181 } 182 183 currentItemIndex = - 1; 185 ListSorter.sort(returnList, (String ) comparator.getValue(this), this); 186 iterator = returnList.iterator(); 187 if (iterator.hasNext()) { 190 setNext(); 191 return EVAL_BODY; 192 } else { 193 return SKIP_BODY; 194 } 195 } 196 197 public int doAfterBody() throws JspException { 198 if (getId() != null) { 199 getContextProvider().getContextContainer().unRegister(getId()); 200 } 201 202 203 helper.doAfterBody(); 204 collector.doAfterBody(); 205 206 if (iterator.hasNext()){ 207 setNext(); 208 return EVAL_BODY_AGAIN; 209 } else { 210 if (EVAL_BODY == EVAL_BODY_BUFFERED) { 211 if (bodyContent != null) { 212 try { 213 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 214 bodyContent.clearBody(); 215 } catch (IOException ioe){ 216 throw new TaglibException(ioe); 217 } 218 } 219 } 220 return SKIP_BODY; 221 } 222 } 223 public int doEndTag() throws JspTagException { 224 if (getId() != null) { 225 getContextProvider().getContextContainer().register(getId(), returnList, false); 226 } 227 returnList = null; 229 iterator = null; 230 if (collector != null) { 231 collector.release(pageContext, getContextProvider().getContextContainer()); 232 collector = null; 233 } 234 bodyContent = null; 235 helper.doEndTag(); 236 return super.doEndTag(); 237 } 238 239 public void doFinally() { 240 returnList = null; 241 iterator = null; 242 if (collector != null) { 243 try { 244 collector.release(pageContext, getContextProvider().getContextContainer()); 245 } catch (Exception e) { 246 } 247 collector = null; 248 } 249 super.doFinally(); 250 } 251 252 protected void setNext() throws JspTagException { 253 currentItemIndex++; 254 helper.setValue(iterator.next()); 255 if (getId() != null) { 256 getContextProvider().getContextContainer().register(getId(), helper.getValue()); 257 } 258 } 259 260 public LoopTagStatus getLoopStatus() { 261 return new ListProviderLoopTagStatus(this); 262 } 263 264 } 265 266 | Popular Tags |