1 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 ; 15 16 import org.mmbase.util.logging.Logger; 17 import org.mmbase.util.logging.Logging; 18 19 20 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 v) throws JspTagException { 44 value = getAttribute(v); 45 } 46 47 protected int getValue() throws JspTagException { 48 String 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 ("Unknown condiation value (" + v +")"); 61 } 62 } 63 64 public void setInverse(String b) throws JspTagException { 65 inverse = getAttribute(b); 66 } 67 68 protected boolean getInverse() throws JspTagException { 69 return inverse.getBoolean(this, false); 70 } 71 72 73 74 public int doStartTag() throws JspTagException { 75 LoopTag list = getLoopTag(); 77 78 79 boolean i = getInverse(); 80 boolean result = false; 81 int j = getValue(); 82 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 ("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 111 public int doAfterBody() throws JspTagException { 112 if (EVAL_BODY == EVAL_BODY_BUFFERED) { try{ 114 if(bodyContent != null) { 115 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 116 } 117 } catch(java.io.IOException e){ 118 throw new TaglibException(e); 119 } 120 121 } 122 return EVAL_PAGE; 123 } 124 125 } 126 | Popular Tags |