KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.jsp.*;
13 import javax.servlet.jsp.tagext.*;
14 import javax.servlet.jsp.jstl.core.*;
15
16 import org.mmbase.bridge.Query;
17 import org.mmbase.bridge.jsp.taglib.containers.*;
18 import org.mmbase.bridge.jsp.taglib.tree.TreeContainerTag;
19 import org.mmbase.bridge.jsp.taglib.util.Attribute;
20 import org.mmbase.bridge.util.Queries;
21
22 /**
23  * The size of a list or of a nodelistcontainer (then the query is consulted).
24  *
25  * @author Michiel Meeuwissen
26  * @version $Id: SizeTag.java,v 1.26 2006/06/22 19:00:29 johannes Exp $
27  */

28 public class SizeTag extends ListReferrerTag implements Writer, QueryContainerReferrer {
29
30     private Attribute container = Attribute.NULL;
31
32     /**
33      * @since MMBase-1.7
34      */

35     public void setContainer(String JavaDoc c) throws JspTagException {
36         container = getAttribute(c);
37     }
38
39     /**
40      * When in a list-container only, the size can be predicted by altering the query with "count()".
41      * @since MMBase-1.7
42      */

43     protected void nodeListContainerSize(QueryContainer c) throws JspTagException {
44         Query query = c.getQuery();
45         int res = Queries.count(query) - query.getOffset();
46         int max = query.getMaxNumber();
47         if (max > -1 && res > max) { res = max; }
48         helper.setValue(new Integer JavaDoc(res));
49     }
50
51     /**
52      * When in a looptag, the size can simply be asked from the List
53      * @since MMBase-1.7
54      */

55     protected void listProviderSize(LoopTag list) throws JspTagException {
56         helper.setValue(new Integer JavaDoc(list.getLoopStatus().getCount()));
57     }
58
59
60     public int doStartTag() throws JspTagException{
61         if (container != Attribute.NULL) {
62             if (parentListId != Attribute.NULL) {
63                 throw new JspTagException("Cannot specify both 'container' and 'list' attributes");
64             }
65             QueryContainer c = (QueryContainer) findParentTag(QueryContainer.class, (String JavaDoc) container.getValue(this));
66             if (c instanceof TreeContainerTag) {
67                 helper.setValue(new Integer JavaDoc(((TreeContainerTag)c).getTree().size()));
68             } else {
69                 nodeListContainerSize(c);
70             }
71         } else if (parentListId != Attribute.NULL) {
72             listProviderSize(getList());
73         } else {
74             Tag tag = findLoopOrQuery(null, true);
75             if (tag instanceof TreeContainerTag) {
76                 helper.setValue(new Integer JavaDoc(((TreeContainerTag)tag).getTree().size()));
77             } else if (tag instanceof QueryContainer) {
78                 nodeListContainerSize((QueryContainer) tag);
79             } else {
80                 listProviderSize((LoopTag) tag);
81             }
82         }
83
84         if (getId() != null) {
85             getContextProvider().getContextContainer().register(getId(), helper.getValue());
86         }
87         return EVAL_BODY_BUFFERED;
88     }
89
90     public int doAfterBody() throws JspException {
91         return helper.doAfterBody();
92     }
93
94     /**
95      *
96      **/

97     public int doEndTag() throws JspTagException {
98         helper.doEndTag();
99         return super.doEndTag();
100     }
101 }
102
Popular Tags