View Javadoc

1   /*
2    * $Id: ElseIf.java 451544 2006-09-30 05:38:02Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.components;
19  
20  import java.io.Writer;
21  
22  import com.opensymphony.xwork2.util.ValueStack;
23  
24  /***
25   * <!-- START SNIPPET: javadoc -->
26   *
27   * <p>Perform basic condition flow. 'If' tag could be used by itself or with 'Else If' Tag and/or single/multiple 'Else'
28   * Tag.</p>
29   *
30   * <!-- END SNIPPET: javadoc -->
31   *
32   *
33   * <!-- START SNIPPET: params -->
34   *
35   * <ul>
36   *
37   * <li>test* (Boolean) - Logic to determined if body of tag is to be displayed</li>
38   *
39   * </ul>
40   *
41   * <!-- END SNIPPET: params -->
42   *
43   *
44   * <pre>
45   * <!-- START SNIPPET: example -->
46   *  &lt;s:if test="%{false}"&gt;
47   * 	    &lt;div&gt;Will Not Be Executed&lt;/div&gt;
48   *  &lt;/s:if&gt;
49   * 	&lt;s:elseif test="%{true}"&gt;
50   * 	    &lt;div&gt;Will Be Executed&lt;/div&gt;
51   *  &lt;/s:elseif&gt;
52   *  &lt;s:else&gt;
53   * 	    &lt;div&gt;Will Not Be Executed&lt;/div&gt;
54   *  &lt;/s:else&gt;
55   * <!-- END SNIPPET: example -->
56   * </pre>
57   *
58   * @s.tag name="elseif" tld-body-content="JSP" description="Elseif tag"  tld-tag-class="org.apache.struts2.views.jsp.ElseIfTag"
59   */
60  public class ElseIf extends Component {
61      public ElseIf(ValueStack stack) {
62          super(stack);
63      }
64  
65      protected Boolean answer;
66      protected String test;
67  
68      public boolean start(Writer writer) {
69          Boolean ifResult = (Boolean) stack.getContext().get(If.ANSWER);
70          
71          if ((ifResult == null) || (ifResult.booleanValue())) {
72              return false;
73          }
74  
75          //make the comparision
76          answer = (Boolean) findValue(test, Boolean.class);
77  
78          if (answer == null) {
79              answer = Boolean.FALSE;
80          }
81          if (answer.booleanValue()) {
82              stack.getContext().put(If.ANSWER, answer);
83          }
84          return answer != null && answer.booleanValue();
85      }
86  
87      public boolean end(Writer writer, String body) {
88          if (answer == null) {
89              answer = Boolean.FALSE;
90          }
91          if (answer.booleanValue()) {
92              stack.getContext().put(If.ANSWER, answer);
93          }
94          return super.end(writer, "");
95      }
96  
97      /***
98       * Expression to determine if body of tag is to be displayed
99       * @s.tagattribute required="true" type="Boolean"
100      */
101     public void setTest(String test) {
102         this.test = test;
103     }
104 }