KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > StringListTag


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.bridge.jsp.taglib;
11
12 import java.io.IOException JavaDoc;
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 /**
22  * This class makes a tag which can list strings.
23  *
24  * @author Michiel Meeuwissen
25  * @version $Id: StringListTag.java,v 1.32 2006/07/28 11:31:22 michiel Exp $
26  * @since MMBase-1.7
27  */

28
29 public class StringListTag extends NodeReferrerTag implements ListProvider, Writer { // need to extend NodeRefferer becasue of AliasListTag, no MI in java.
30

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 JavaDoc getCurrent() {
56         return getWriterValue();
57     }
58
59     public void remove() {
60         iterator.remove();
61     }
62
63
64     public void setMax(String JavaDoc m) throws JspTagException {
65         max = getAttribute(m);
66     }
67
68     public void setAdd(String JavaDoc a) throws JspTagException {
69         add = getAttribute(a);
70     }
71
72     public void setRetain(String JavaDoc r) throws JspTagException {
73         retain = getAttribute(r);
74     }
75
76     public void setRemove(String JavaDoc 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 JavaDoc c) throws JspTagException {
85         comparator = getAttribute(c);
86     }
87
88     /**
89      * Lists do implement ContextProvider
90      */

91     private ContextCollector collector;
92
93
94
95     // ContextProvider implementation
96
public ContextContainer getContextContainer() {
97         return collector;
98     }
99
100
101     /**
102      * Creates the actual list of strings.
103      */

104     protected List getList() throws JspTagException {
105         throw new JspTagException("Should use 'referid' attribute on liststrings tag");
106     }
107
108     /**
109      * For use with referid
110      */

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     /**
121      *
122      *
123      */

124     public int doStartTag() throws JspTagException{
125
126         collector = new ContextCollector(getContextProvider());
127
128
129         helper.overrideWrite(false); // default behavior is not to write to page
130

131         if (getReferid() != null) {
132             Object JavaDoc 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())) { // in such a case, don't whine
143
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 JavaDoc 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 JavaDoc 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 JavaDoc 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; // reset index
184

185         ListSorter.sort(returnList, (String JavaDoc) comparator.getValue(this), this);
186         iterator = returnList.iterator();
187         // if we get a result from the query
188
// evaluate the body , else skip the body
189
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 JavaDoc 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         // dereference for gc
228
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 JavaDoc 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