KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > functions > ListFunctionTag


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.functions;
11
12 import java.io.IOException JavaDoc;
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 /**
24  * A function tag for functions returning a collection.
25  * The result is iterated. If a function does not return collection, or if the result requires sorting,
26  * the result value is transformed into a List (using {@link org.mmbase.util.Casting#toCollection}).
27  *
28  * @author Michiel Meeuwissen
29  * @since MMBase-1.7
30  * @version $Id: ListFunctionTag.java,v 1.12 2006/07/26 09:07:50 michiel Exp $
31  */

32 public class ListFunctionTag extends AbstractFunctionTag implements ListProvider, FunctionContainerReferrer, Writer {
33
34     private static final Logger log = Logging.getLoggerInstance(ListFunctionTag.class);
35     // implementation of ListProvider
36

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 JavaDoc 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 JavaDoc value = getFunctionValue(false); // registration is done in doInitBody
73
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); // default behavior is not to write to page
84
currentItemIndex = -1; // reset index
85
if (!comparator.equals(Attribute.NULL)) {
86             ListSorter.sort((List)returnCollection, (String JavaDoc) 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 JavaDoc 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         // dereference for gc.
122
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