KickJava   Java API By Example, From Geeks To Geeks.

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


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.jstl.core.*;
13 import org.mmbase.bridge.jsp.taglib.util.Attribute;
14 import javax.servlet.jsp.JspTagException JavaDoc;
15
16 import org.mmbase.util.logging.Logger;
17 import org.mmbase.util.logging.Logging;
18
19
20 /**
21  * This tag can be used inside the list tag. The body will be
22  * evaluated depending on the value of the index of the list.
23  *
24  *
25  * @author Michiel Meeuwissen
26  * @version $Id: ListConditionTag.java,v 1.22 2006/02/14 13:27:27 michiel Exp $
27  */

28
29 public class ListConditionTag extends ListReferrerTag implements Condition {
30
31     private static final Logger log = Logging.getLoggerInstance(ListConditionTag.class);
32
33     private Attribute value = Attribute.NULL;
34     private Attribute parentListId = Attribute.NULL;
35     private Attribute inverse = Attribute.NULL;
36
37     protected final static int CONDITION_FIRST = 1;
38     protected final static int CONDITION_LAST = 2;
39     protected final static int CONDITION_EVEN = 3;
40     protected final static int CONDITION_ODD = 4;
41     protected final static int CONDITION_CHANGED = 5;
42
43     public void setValue(String JavaDoc v) throws JspTagException JavaDoc {
44         value = getAttribute(v);
45     }
46
47     protected int getValue() throws JspTagException JavaDoc {
48         String JavaDoc v = value.getString(this).toLowerCase();
49         if (v.equals("first")) {
50             return CONDITION_FIRST;
51         } else if (v.equals("last")) {
52             return CONDITION_LAST;
53         } else if (v.equals("even")) {
54             return CONDITION_EVEN;
55         } else if (v.equals("odd")) {
56             return CONDITION_ODD;
57         } else if (v.equals("changed")) {
58             return CONDITION_CHANGED;
59         } else {
60             throw new JspTagException JavaDoc ("Unknown condiation value (" + v +")");
61         }
62     }
63
64     public void setInverse(String JavaDoc b) throws JspTagException JavaDoc {
65         inverse = getAttribute(b);
66     }
67
68     protected boolean getInverse() throws JspTagException JavaDoc {
69         return inverse.getBoolean(this, false);
70     }
71
72
73
74     public int doStartTag() throws JspTagException JavaDoc{
75         // find the parent list:
76
LoopTag list = getLoopTag();
77
78
79         boolean i = getInverse();
80         boolean result = false;
81         int j = getValue();
82         //
83
// One would expect a switch, but for some odd reason, that does not work in my resin 3.0.6
84
//
85
switch(j) {
86         case CONDITION_LAST: result = list.getLoopStatus().isLast() != i; break;
87         case CONDITION_FIRST: result = list.getLoopStatus().isFirst() != i; break;
88         case CONDITION_EVEN: result = ((list.getLoopStatus().getIndex() + 1) % 2 == 0) != i; break;
89         case CONDITION_ODD: result = ((list.getLoopStatus().getIndex() + 1) % 2 != 0) != i; break;
90         case CONDITION_CHANGED:
91             if (list instanceof ListProvider) {
92                 result = ((ListProvider) list).isChanged() != i;
93             } else {
94                 result = ! i;
95             }
96
97             break;
98         default:
99             throw new JspTagException JavaDoc("Don't know what to do (" + getValue() + ")");
100         }
101         if (log.isDebugEnabled()) {
102             log.debug("result " + (result ? "EVAL BODY" : "SKIP BODY"));
103         }
104         return (result ? EVAL_BODY : SKIP_BODY);
105     }
106
107
108     /**
109      *
110      **/

111     public int doAfterBody() throws JspTagException JavaDoc {
112         if (EVAL_BODY == EVAL_BODY_BUFFERED) { // not needed if EVAL_BODY_INCLUDE
113
try{
114                 if(bodyContent != null) {
115                     bodyContent.writeOut(bodyContent.getEnclosingWriter());
116                 }
117             } catch(java.io.IOException JavaDoc e){
118                 throw new TaglibException(e);
119             }
120             
121         }
122         return EVAL_PAGE;
123     }
124
125 }
126
Popular Tags