/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights 
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:  
 *       "This product includes software developed by the 
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written 
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */ 

package javax.servlet.jsp.jstl.core;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

/**
 * <p>Abstract class that facilitates implementation of conditional actions 
 * where the boolean result is exposed as a JSP scoped variable. The 
 * boolean result may then be used as the test condition in a &lt;c:when&gt;
 * action.</p>
 * <p>
 * 条件判定タグを実装しやすくするための抽象クラスです。
 * boolean結果値は、スコープ付きの変数として出力されます。
 * その後、booleanの結果値は、&lt;c:when&gt;タグのtest条件として使用して構いません。
 * </p>
 *
 * <p>This base class provides support for:</p>
 * 
 * <ul>
 *  <li> Conditional processing of the action's body based on the returned value
 *       of the abstract method <tt>condition()</tt>.</li>
 *  <li> Storing the result of <tt>condition()</tt> as a <tt>Boolean</tt> object
 *       into a JSP scoped variable identified by attributes <tt>var</tt> and
 *       <tt>scope</tt>.
 * </ul>
 * 
 * <p>この基本クラスは次の項目をサポートします:</p>
 * 
 * <ul>
 *   <li>
 *     <tt>condition()</tt>メソッドの戻り値をベースとしたアクションのボディの条件判定。
 *   </li>
 *   <li>
 *     <tt>var</tt>と<tt>scope</tt>属性によって決められたスコープ付きの変数に出力された
 *     <tt>Boolean</tt>オブジェクトを<tt>condition()</tt>の結果値として格納する。
 *   </li>
 * </ul>
 * 
 * @author Shawn Bayern
 */

public abstract class ConditionalTagSupport
    extends TagSupport
{
    //*********************************************************************
    // Abstract methods

    /**
     * <p>Subclasses implement this method to compute the boolean result
     * of the conditional action. This method is invoked once per tag invocation 
     * by <tt>doStartTag()</tt>.
     * </p>
     * <p>
     *   サブクラスにおいて、条件判定タグのboolean結果を演算する処理がこのメソッドに実装されます。
     *   このメソッドは、タグが<tt>doStartTag()</tt>を実行するたびに一度呼び出されます。
     * </p>
     *
     * @return a boolean representing the condition that a particular subclass
     *   uses to drive its conditional logic.<br>
     *         特定のサブクラスが使用する条件判定ロジックの実行状態を表すboolean値。
     */
    protected abstract boolean condition() throws JspTagException;


    //*********************************************************************
    // Constructor

    /**
     * <p>
     * Base constructor to initialize local state.  As with <tt>TagSupport</tt>,
     * subclasses should not implement constructors with arguments, and
     * no-argument constructors implemented by subclasses must call the 
     * superclass constructor.
     * </p>
     * <p>
     * ローカル状態を初期化する基本コンストラクタ。
     * <tt>TagSupport</tt>を含め、サブクラスに引数を伴うコンストラクタを実装しなくてもよいです。
     * そして、サブクラスに実装された引数のないコンストラクタでは、
     * スーパークラスのコンストラクタを呼び出す必要があります。
     * </p>
     */
    public ConditionalTagSupport() {
        super();
        init();
    }


    //*********************************************************************
    // Lifecycle management and implementation of conditional behavior

    /**
     * <p>Includes its body if <tt>condition()</tt> evaluates to true.</p>
     * <p>condition()の評価結果がtrueの場合にボディ部へ挿入します。</p>
     */
    public int doStartTag() throws JspException {

        // execute our condition() method once per invocation
        result = condition();

        // expose variables if appropriate
        exposeVariables();

        // handle conditional behavior
        if (result)
            return EVAL_BODY_INCLUDE;
        else
            return SKIP_BODY;
    }

    /**
     * <p>Releases any resources this ConditionalTagSupport may have (or inherit).</p>
     * <p>このConditionalTagSupportが持つ（継承する）リソースをすべて解放します。</p>
     */
    public void release() {
        super.release();
        init();
    }

    //*********************************************************************
    // Private state

    private boolean result;             // the saved result of condition()
    private String var;			// scoped attribute name
    private int scope;			// scoped attribute scope


    //*********************************************************************
    // Accessors

    /**
     * <p>Sets the 'var' attribute.</p>
     * <p>'var' 属性を設定します。</p>
     *
     * @param var Name of the exported scoped variable storing the result of
     * <tt>condition()</tt>.
     *            <br>condition()の結果を保持するスコープ付きの変数名
     */
    public void setVar(String var) {
	this.var = var;
    }

    /**
     * <p>Sets the 'scope' attribute.</p>
     * <p>'scope' 属性を設定します。</p>
     *
     * @param scope Scope of the 'var' attribute
     *              <br>'var' 属性のスコープ
     */
    public void setScope(String scope) {
	if (scope.equalsIgnoreCase("page"))
	    this.scope = PageContext.PAGE_SCOPE;
	else if (scope.equalsIgnoreCase("request"))
	    this.scope = PageContext.REQUEST_SCOPE;
	else if (scope.equalsIgnoreCase("session"))
	    this.scope = PageContext.SESSION_SCOPE;
	else if (scope.equalsIgnoreCase("application"))
	    this.scope = PageContext.APPLICATION_SCOPE;
	// TODO: Add error handling?  Needs direction from spec.
    }


    //*********************************************************************
    // Utility methods

    // expose attributes if we have a non-null 'var'
    private void exposeVariables() {
        if (var != null)
            pageContext.setAttribute(var, new Boolean(result), scope);
    }

    // initializes internal state
    private void init() {
        result = false;                 // not really necessary
	var = null;
	scope = PageContext.PAGE_SCOPE;
    }
}
