01: package com.opensymphony.webwork.components;
02:
03: import com.opensymphony.xwork.util.OgnlValueStack;
04:
05: import java.io.Writer;
06:
07: /**
08: * <!-- START SNIPPET: javadoc -->
09: *
10: * <p>Perform basic condition flow. 'If' tag could be used by itself or with 'Else If' Tag and/or single/multiple 'Else'
11: * Tag.</p>
12: *
13: * <!-- END SNIPPET: javadoc -->
14: *
15: *
16: * <!-- START SNIPPET: params -->
17: *
18: * <ul>
19: *
20: * <li>test* (Boolean) - Logic to determined if body of tag is to be displayed</li>
21: *
22: * </ul>
23: *
24: * <!-- END SNIPPET: params -->
25: *
26: *
27: * <pre>
28: * <!-- START SNIPPET: example -->
29: * <ww:if test="%{false}">
30: * <div>Will Not Be Executed</div>
31: * </ww:if>
32: * <ww:elseif test="%{true}">
33: * <div>Will Be Executed</div>
34: * </ww:elseif>
35: * <ww:else>
36: * <div>Will Not Be Executed</div>
37: * </ww:else>
38: * <!-- END SNIPPET: example -->
39: * </pre>
40: *
41: * @author Rick Salsa (rsal@mb.sympatico.ca)
42: * @author tmjee
43: * @ww.tag name="elseif" tld-body-content="JSP" description="Elseif tag" tld-tag-class="com.opensymphony.webwork.views.jsp.ElseIfTag"
44: */
45: public class ElseIf extends Component {
46: public ElseIf(OgnlValueStack stack) {
47: super (stack);
48: }
49:
50: protected Boolean answer;
51: protected String test;
52:
53: public boolean start(Writer writer) {
54: Boolean ifResult = (Boolean) stack.getContext().get(If.ANSWER);
55:
56: if ((ifResult == null) || (ifResult.booleanValue())) {
57: return false;
58: }
59:
60: //make the comparision
61: answer = (Boolean) findValue(test, Boolean.class);
62:
63: if (answer == null) {
64: answer = Boolean.FALSE;
65: }
66:
67: if (answer.booleanValue()) {
68: stack.getContext().put(If.ANSWER, answer);
69: }
70:
71: return answer != null && answer.booleanValue();
72: }
73:
74: public boolean end(Writer writer, String body) {
75: if (answer == null) {
76: answer = Boolean.FALSE;
77: }
78:
79: if (answer.booleanValue()) {
80: stack.getContext().put(If.ANSWER, answer);
81: }
82: return super .end(writer, "");
83: }
84:
85: /**
86: * Expression to determine if body of tag is to be displayed
87: * @ww.tagattribute required="true" type="Boolean"
88: */
89: public void setTest(String test) {
90: this.test = test;
91: }
92: }
|