001: /*
002: * $Id: ElseIf.java 497654 2007-01-19 00:21:57Z rgielen $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.components;
022:
023: import java.io.Writer;
024:
025: import org.apache.struts2.views.annotations.StrutsTag;
026: import org.apache.struts2.views.annotations.StrutsTagAttribute;
027:
028: import com.opensymphony.xwork2.util.ValueStack;
029:
030: /**
031: * <!-- START SNIPPET: javadoc -->
032: *
033: * <p>Perform basic condition flow. 'If' tag could be used by itself or with 'Else If' Tag and/or single/multiple 'Else'
034: * Tag.</p>
035: *
036: * <!-- END SNIPPET: javadoc -->
037: *
038: *
039: * <!-- START SNIPPET: params -->
040: *
041: * <ul>
042: *
043: * <li>test* (Boolean) - Logic to determined if body of tag is to be displayed</li>
044: *
045: * </ul>
046: *
047: * <!-- END SNIPPET: params -->
048: *
049: *
050: * <pre>
051: * <!-- START SNIPPET: example -->
052: * <s:if test="%{false}">
053: * <div>Will Not Be Executed</div>
054: * </s:if>
055: * <s:elseif test="%{true}">
056: * <div>Will Be Executed</div>
057: * </s:elseif>
058: * <s:else>
059: * <div>Will Not Be Executed</div>
060: * </s:else>
061: * <!-- END SNIPPET: example -->
062: * </pre>
063: *
064: */
065: @StrutsTag(name="elseif",tldTagClass="org.apache.struts2.views.jsp.ElseIfTag",description="Elseif tag")
066: public class ElseIf extends Component {
067: public ElseIf(ValueStack stack) {
068: super (stack);
069: }
070:
071: protected Boolean answer;
072: protected String test;
073:
074: public boolean start(Writer writer) {
075: Boolean ifResult = (Boolean) stack.getContext().get(If.ANSWER);
076:
077: if ((ifResult == null) || (ifResult.booleanValue())) {
078: return false;
079: }
080:
081: //make the comparision
082: answer = (Boolean) findValue(test, Boolean.class);
083:
084: if (answer == null) {
085: answer = Boolean.FALSE;
086: }
087: if (answer.booleanValue()) {
088: stack.getContext().put(If.ANSWER, answer);
089: }
090: return answer != null && answer.booleanValue();
091: }
092:
093: public boolean end(Writer writer, String body) {
094: if (answer == null) {
095: answer = Boolean.FALSE;
096: }
097: if (answer.booleanValue()) {
098: stack.getContext().put(If.ANSWER, answer);
099: }
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: }
|