View Javadoc

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