KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > common > core > ChooseTag


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 org.apache.taglibs.standard.tag.common.core;
18
19 import javax.servlet.jsp.JspException JavaDoc;
20 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
21
22 import org.apache.taglibs.standard.resources.Resources;
23
24 /**
25  * <p>Tag handler for &lt;choose&gt; in JSTL.</p>
26  *
27  * <p>&lt;choose&gt; is a very simple tag that acts primarily as a container;
28  * it always includes its body and allows exactly one of its child
29  * &lt;when&gt; tags to run. Since this tag handler doesn't have any
30  * attributes, it is common.core to both the rtexprvalue and expression-
31  * evaluating versions of the JSTL library.
32  *
33  * @author Shawn Bayern
34  */

35
36 public class ChooseTag extends TagSupport JavaDoc {
37
38     //*********************************************************************
39
// Constructor and lifecycle management
40

41     // initialize inherited and local state
42
public ChooseTag() {
43         super();
44         init();
45     }
46
47     // Releases any resources we may have (or inherit)
48
public void release() {
49         super.release();
50         init();
51     }
52
53
54     //*********************************************************************
55
// Private state
56

57     private boolean subtagGateClosed; // has one subtag already executed?
58

59
60     //*********************************************************************
61
// Public methods implementing exclusivity checks
62

63     /**
64      * Returns status indicating whether a subtag should run or not.
65      *
66      * @return <tt>true</tt> if the subtag should evaluate its condition
67      * and decide whether to run, <tt>false</tt> otherwise.
68      */

69     public synchronized boolean gainPermission() {
70         return (!subtagGateClosed);
71     }
72
73     /**
74      * Called by a subtag to indicate that it plans to evaluate its
75      * body.
76      */

77     public synchronized void subtagSucceeded() {
78         if (subtagGateClosed)
79             throw new IllegalStateException JavaDoc(
80         Resources.getMessage("CHOOSE_EXCLUSIVITY"));
81         subtagGateClosed = true;
82     }
83
84
85     //*********************************************************************
86
// Tag logic
87

88     // always include body
89
public int doStartTag() throws JspException JavaDoc {
90         subtagGateClosed = false; // when we start, no children have run
91
return EVAL_BODY_INCLUDE;
92     }
93
94
95     //*********************************************************************
96
// Private utility methods
97

98     private void init() {
99         subtagGateClosed = false; // reset flag
100
}
101 }
102
Popular Tags