KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > servlet > jsp > jstl > core > ConditionalTagSupport


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package javax.servlet.jsp.jstl.core;
18
19 import javax.servlet.jsp.JspException JavaDoc;
20 import javax.servlet.jsp.JspTagException JavaDoc;
21 import javax.servlet.jsp.PageContext JavaDoc;
22 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
23
24 /**
25  * <p>Abstract class that facilitates implementation of conditional actions
26  * where the boolean result is exposed as a JSP scoped variable. The
27  * boolean result may then be used as the test condition in a &lt;c:when&gt;
28  * action.</p>
29  *
30  * <p>This base class provides support for:</p>
31  *
32  * <ul>
33  * <li> Conditional processing of the action's body based on the returned value
34  * of the abstract method <tt>condition()</tt>.</li>
35  * <li> Storing the result of <tt>condition()</tt> as a <tt>Boolean</tt> object
36  * into a JSP scoped variable identified by attributes <tt>var</tt> and
37  * <tt>scope</tt>.
38  * </ul>
39  *
40  * @author Shawn Bayern
41  */

42
43 public abstract class ConditionalTagSupport
44     extends TagSupport JavaDoc
45 {
46     //*********************************************************************
47
// Abstract methods
48

49     /**
50      * <p>Subclasses implement this method to compute the boolean result
51      * of the conditional action. This method is invoked once per tag invocation
52      * by <tt>doStartTag()</tt>.
53      *
54      * @return a boolean representing the condition that a particular subclass
55      * uses to drive its conditional logic.
56      */

57     protected abstract boolean condition() throws JspTagException JavaDoc;
58
59
60     //*********************************************************************
61
// Constructor
62

63     /**
64      * Base constructor to initialize local state. As with <tt>TagSupport</tt>,
65      * subclasses should not implement constructors with arguments, and
66      * no-argument constructors implemented by subclasses must call the
67      * superclass constructor.
68      */

69     public ConditionalTagSupport() {
70         super();
71         init();
72     }
73
74
75     //*********************************************************************
76
// Lifecycle management and implementation of conditional behavior
77

78     /**
79      * Includes its body if <tt>condition()</tt> evaluates to true.
80      */

81     public int doStartTag() throws JspException JavaDoc {
82
83         // execute our condition() method once per invocation
84
result = condition();
85
86         // expose variables if appropriate
87
exposeVariables();
88
89         // handle conditional behavior
90
if (result)
91             return EVAL_BODY_INCLUDE;
92         else
93             return SKIP_BODY;
94     }
95
96     /**
97      * Releases any resources this ConditionalTagSupport may have (or inherit).
98      */

99     public void release() {
100         super.release();
101         init();
102     }
103
104     //*********************************************************************
105
// Private state
106

107     private boolean result; // the saved result of condition()
108
private String JavaDoc var; // scoped attribute name
109
private int scope; // scoped attribute scope
110

111
112     //*********************************************************************
113
// Accessors
114

115     /**
116      * Sets the 'var' attribute.
117      *
118      * @param var Name of the exported scoped variable storing the result of
119      * <tt>condition()</tt>.
120      */

121     public void setVar(String JavaDoc var) {
122     this.var = var;
123     }
124
125     /**
126      * Sets the 'scope' attribute.
127      *
128      * @param scope Scope of the 'var' attribute
129      */

130     public void setScope(String JavaDoc scope) {
131     if (scope.equalsIgnoreCase("page"))
132         this.scope = PageContext.PAGE_SCOPE;
133     else if (scope.equalsIgnoreCase("request"))
134         this.scope = PageContext.REQUEST_SCOPE;
135     else if (scope.equalsIgnoreCase("session"))
136         this.scope = PageContext.SESSION_SCOPE;
137     else if (scope.equalsIgnoreCase("application"))
138         this.scope = PageContext.APPLICATION_SCOPE;
139     // TODO: Add error handling? Needs direction from spec.
140
}
141
142
143     //*********************************************************************
144
// Utility methods
145

146     // expose attributes if we have a non-null 'var'
147
private void exposeVariables() {
148         if (var != null)
149             pageContext.setAttribute(var, new Boolean JavaDoc(result), scope);
150     }
151
152     // initializes internal state
153
private void init() {
154         result = false; // not really necessary
155
var = null;
156     scope = PageContext.PAGE_SCOPE;
157     }
158 }
159
Popular Tags