1 /*
2 * IfTag.java
3 *
4 * $Author: mballesteros $ $Date: 2004/03/10 14:22:13 $ $Revision: 1.4 $
5 */
6 package net.sf.ewc.taglib;
7
8 import java.util.Stack;
9
10 /***
11 * {@link IfTag}processes or skips its contents, depending on a condition
12 * test. The source parameter must point to a boolean value
13 *
14 * @author mballesteros
15 */
16 public class IfTag extends EWCTag {
17
18 static final String IF_BEGIN_TRUE = "IF_BEGIN_TRUE";
19
20 static final String IF_BEGIN_FALSE = "IF_BEGIN_FALSE";
21
22 static final String IF_END_TRUE = "IF_END_TRUE";
23
24 static final String IF_END_FALSE = "IF_END_FALSE";
25
26 /***
27 * Expression to the condition test
28 */
29 private String src;
30
31 /***
32 * Sets the expression to the boolean value that decides whether or not
33 * process contents
34 *
35 * @param src
36 * The expression
37 */
38 public void setSrc(String src) {
39 this.src = src;
40 }
41
42 /***
43 * Main constructor
44 */
45 public IfTag() {
46 super();
47 }
48
49 /***
50 * @return
51 */
52 public int doStartTag() {
53 boolean inverse = src.charAt(0) == '!';
54 Object value = inverse ? getExpressionValue(src.substring(1))
55 : getExpressionValue(src);
56 if (Boolean.TRUE.equals(value)) {
57 value = inverse ? IF_BEGIN_FALSE : IF_BEGIN_TRUE;
58 } else {
59 value = inverse ? IF_BEGIN_TRUE : IF_BEGIN_FALSE;
60 }
61
62 Stack ifStack = getStack("ifStack");
63 if (!ifStack.isEmpty()
64 && (ifStack.peek().equals(IF_END_TRUE) || ifStack.peek()
65 .equals(IF_END_FALSE))) {
66 ifStack.pop();
67 }
68 ifStack.push(value);
69 if (value.equals(IF_BEGIN_TRUE)) {
70 return EVAL_BODY_INCLUDE;
71 } else {
72 return SKIP_BODY;
73 }
74 }
75
76 /***
77 * @return
78 */
79 public int doEndTag() {
80 Stack ifStack = getStack("ifStack");
81
82 if (ifStack.peek().equals(IF_END_TRUE) || ifStack.peek()
83 .equals(IF_END_FALSE)) {
84 ifStack.pop();
85 }
86
87 if (ifStack.peek().equals(IF_BEGIN_TRUE)) {
88 ifStack.pop();
89 ifStack.push(IF_END_TRUE);
90 } else if (ifStack.peek().equals(IF_BEGIN_FALSE)) {
91 ifStack.pop();
92 ifStack.push(IF_END_FALSE);
93 }
94 return EVAL_PAGE;
95 }
96 }
This page was automatically generated by Maven