View Javadoc

1   /*
2    * $Id: ElseIf.java 497654 2007-01-19 00:21:57Z rgielen $
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  package org.apache.struts2.components;
22  
23  import java.io.Writer;
24  
25  import org.apache.struts2.views.annotations.StrutsTag;
26  import org.apache.struts2.views.annotations.StrutsTagAttribute;
27  
28  import com.opensymphony.xwork2.util.ValueStack;
29  
30  /***
31   * <!-- START SNIPPET: javadoc -->
32   *
33   * <p>Perform basic condition flow. 'If' tag could be used by itself or with 'Else If' Tag and/or single/multiple 'Else'
34   * Tag.</p>
35   *
36   * <!-- END SNIPPET: javadoc -->
37   *
38   *
39   * <!-- START SNIPPET: params -->
40   *
41   * <ul>
42   *
43   * <li>test* (Boolean) - Logic to determined if body of tag is to be displayed</li>
44   *
45   * </ul>
46   *
47   * <!-- END SNIPPET: params -->
48   *
49   *
50   * <pre>
51   * <!-- START SNIPPET: example -->
52   *  &lt;s:if test="%{false}"&gt;
53   *      &lt;div&gt;Will Not Be Executed&lt;/div&gt;
54   *  &lt;/s:if&gt;
55   *  &lt;s:elseif test="%{true}"&gt;
56   *      &lt;div&gt;Will Be Executed&lt;/div&gt;
57   *  &lt;/s:elseif&gt;
58   *  &lt;s:else&gt;
59   *      &lt;div&gt;Will Not Be Executed&lt;/div&gt;
60   *  &lt;/s:else&gt;
61   * <!-- END SNIPPET: example -->
62   * </pre>
63   *
64   */
65  @StrutsTag(name="elseif", tldTagClass="org.apache.struts2.views.jsp.ElseIfTag", description="Elseif tag")
66  public class ElseIf extends Component {
67      public ElseIf(ValueStack stack) {
68          super(stack);
69      }
70  
71      protected Boolean answer;
72      protected String test;
73  
74      public boolean start(Writer writer) {
75          Boolean ifResult = (Boolean) stack.getContext().get(If.ANSWER);
76  
77          if ((ifResult == null) || (ifResult.booleanValue())) {
78              return false;
79          }
80  
81          //make the comparision
82          answer = (Boolean) findValue(test, Boolean.class);
83  
84          if (answer == null) {
85              answer = Boolean.FALSE;
86          }
87          if (answer.booleanValue()) {
88              stack.getContext().put(If.ANSWER, answer);
89          }
90          return answer != null && answer.booleanValue();
91      }
92  
93      public boolean end(Writer writer, String body) {
94          if (answer == null) {
95              answer = Boolean.FALSE;
96          }
97          if (answer.booleanValue()) {
98              stack.getContext().put(If.ANSWER, answer);
99          }
100         return super.end(writer, "");
101     }
102 
103     @StrutsTagAttribute(description="Expression to determine if body of tag is to be displayed", type="Boolean", required=true)
104     public void setTest(String test) {
105         this.test = test;
106     }
107 }