001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Portions Copyright Apache Software Foundation.
007: *
008: * The contents of this file are subject to the terms of either the GNU
009: * General Public License Version 2 only ("GPL") or the Common Development
010: * and Distribution License("CDDL") (collectively, the "License"). You
011: * may not use this file except in compliance with the License. You can obtain
012: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
013: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
014: * language governing permissions and limitations under the License.
015: *
016: * When distributing the software, include this License Header Notice in each
017: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
018: * Sun designates this particular file as subject to the "Classpath" exception
019: * as provided by Sun in the GPL Version 2 section of the License file that
020: * accompanied this code. If applicable, add the following below the License
021: * Header, with the fields enclosed by brackets [] replaced by your own
022: * identifying information: "Portions Copyrighted [year]
023: * [name of copyright owner]"
024: *
025: * Contributor(s):
026: *
027: * If you wish your version of this file to be governed by only the CDDL or
028: * only the GPL Version 2, indicate your decision by adding "[Contributor]
029: * elects to include this software in this distribution under the [CDDL or GPL
030: * Version 2] license." If you don't indicate a single choice of license, a
031: * recipient has the option to distribute your version of this file under
032: * either the CDDL, the GPL Version 2 or to extend the choice of license to
033: * its licensees as provided above. However, if you add GPL Version 2 code
034: * and therefore, elected the GPL Version 2 license, then the option applies
035: * only if the new code is made subject to such option by the copyright
036: * holder.
037: */
038:
039: package javax.servlet.jsp.jstl.core;
040:
041: import javax.servlet.jsp.JspException;
042: import javax.servlet.jsp.JspTagException;
043: import javax.servlet.jsp.PageContext;
044: import javax.servlet.jsp.tagext.TagSupport;
045:
046: /**
047: * <p>Abstract class that facilitates implementation of conditional actions
048: * where the boolean result is exposed as a JSP scoped variable. The
049: * boolean result may then be used as the test condition in a <c:when>
050: * action.</p>
051: *
052: * <p>This base class provides support for:</p>
053: *
054: * <ul>
055: * <li> Conditional processing of the action's body based on the returned value
056: * of the abstract method <tt>condition()</tt>.</li>
057: * <li> Storing the result of <tt>condition()</tt> as a <tt>Boolean</tt> object
058: * into a JSP scoped variable identified by attributes <tt>var</tt> and
059: * <tt>scope</tt>.
060: * </ul>
061: *
062: * @author Shawn Bayern
063: */
064:
065: public abstract class ConditionalTagSupport extends TagSupport {
066: //*********************************************************************
067: // Abstract methods
068:
069: /**
070: * <p>Subclasses implement this method to compute the boolean result
071: * of the conditional action. This method is invoked once per tag invocation
072: * by <tt>doStartTag()</tt>.
073: *
074: * @return a boolean representing the condition that a particular subclass
075: * uses to drive its conditional logic.
076: */
077: protected abstract boolean condition() throws JspTagException;
078:
079: //*********************************************************************
080: // Constructor
081:
082: /**
083: * Base constructor to initialize local state. As with <tt>TagSupport</tt>,
084: * subclasses should not implement constructors with arguments, and
085: * no-argument constructors implemented by subclasses must call the
086: * superclass constructor.
087: */
088: public ConditionalTagSupport() {
089: super ();
090: init();
091: }
092:
093: //*********************************************************************
094: // Lifecycle management and implementation of conditional behavior
095:
096: /**
097: * Includes its body if <tt>condition()</tt> evaluates to true.
098: */
099: public int doStartTag() throws JspException {
100:
101: // execute our condition() method once per invocation
102: result = condition();
103:
104: // expose variables if appropriate
105: exposeVariables();
106:
107: // handle conditional behavior
108: if (result)
109: return EVAL_BODY_INCLUDE;
110: else
111: return SKIP_BODY;
112: }
113:
114: /**
115: * Releases any resources this ConditionalTagSupport may have (or inherit).
116: */
117: public void release() {
118: super .release();
119: init();
120: }
121:
122: //*********************************************************************
123: // Private state
124:
125: private boolean result; // the saved result of condition()
126: private String var; // scoped attribute name
127: private int scope; // scoped attribute scope
128:
129: //*********************************************************************
130: // Accessors
131:
132: /**
133: * Sets the 'var' attribute.
134: *
135: * @param var Name of the exported scoped variable storing the result of
136: * <tt>condition()</tt>.
137: */
138: public void setVar(String var) {
139: this .var = var;
140: }
141:
142: /**
143: * Sets the 'scope' attribute.
144: *
145: * @param scope Scope of the 'var' attribute
146: */
147: public void setScope(String scope) {
148: if (scope.equalsIgnoreCase("page"))
149: this .scope = PageContext.PAGE_SCOPE;
150: else if (scope.equalsIgnoreCase("request"))
151: this .scope = PageContext.REQUEST_SCOPE;
152: else if (scope.equalsIgnoreCase("session"))
153: this .scope = PageContext.SESSION_SCOPE;
154: else if (scope.equalsIgnoreCase("application"))
155: this .scope = PageContext.APPLICATION_SCOPE;
156: // TODO: Add error handling? Needs direction from spec.
157: }
158:
159: //*********************************************************************
160: // Utility methods
161:
162: // expose attributes if we have a non-null 'var'
163: private void exposeVariables() {
164: if (var != null)
165: pageContext.setAttribute(var, Boolean.valueOf(result),
166: scope);
167: }
168:
169: // initializes internal state
170: private void init() {
171: result = false; // not really necessary
172: var = null;
173: scope = PageContext.PAGE_SCOPE;
174: }
175: }
|