001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.hayabusa.taglib;
017
018import static org.opengion.fukurou.util.StringUtil.nval ;
019
020/**
021 * switch タグは、指定された条件を、case タグに伝えます。
022 *
023 * 各属性は、{@XXXX} 変数が使用できます。
024 * これは、ServletRequest から、XXXX をキーに値を取り出し,この変数に
025 * 割り当てます。つまり、このXXXXをキーにリクエストすれば、
026 * この変数に値をセットすることができます。
027 *
028 * @og.formSample
029 * ●形式:<og:switch key="・・・" />
030 *            <og:case match="A" /> ・・・ </og:case>
031 *            <og:case match="B" /> ・・・ </og:case>
032 *            <og:case match="C" /> ・・・ </og:case>
033 *            <og:case isDefault="true" /> ・・・ </og:case>
034 *         </og:switch>
035 * ●body:あり(EVAL_BODY_INCLUDE:BODYをインクルードし、{@XXXX} は解析しません)
036 *
037 * ●Tag定義:
038 *   <og:switch
039 *       key              ○【TAG】switch のマッチ判定用のキーを設定します(必須)。
040 *       debug              【TAG】デバッグ情報を出力するかどうか[true/false]を指定します(初期値:false)
041 *   >   ... Body ...
042 *   </og:switch>
043 *
044 * ●使用例
045 *         <og:switch key="{@PARAM}" />
046 *            <og:case match="A" /> 処理A </og:case>
047 *            <og:case match="B" /> 処理B </og:case>
048 *            <og:case match="C" /> 処理C </og:case>
049 *            <og:case isDefault="true" /> 処理X </og:case>
050 *         </og:switch>
051 *
052 *          ・switch の key に対して、case の match に指定された値が、マッチ(switch_key.match( case_match ))
053 *            した場合に、case の BODY 部分が処理されます。
054 *            マッチしなければ、BODY部は、スキップされます。
055 *          ・isDefault="true" の場合は、どれとも マッチしなかった場合に、実行されます。
056 *          ・Javaの switch-case 文は、最初に処理された case 以降を処理します。通常は、break を入れて
057 *            後続処理を実行されないようにしています。
058 *            この、switch-case タグは、caseタグの isBreak 属性で制御します。初期値が isBreak="true" に、
059 *            なっているため、通常は、どれかの case が実行された段階で、switchの処理は、終了されます。
060 *            isBreak="false" にすると、switchから抜けずに、継続して case との match を実行します。
061 *            この場合、Java等と異なるのは、直後のcase文が実行されるのではなく、あくまで match 作業が
062 *            継続されるということです。つまり、複数の case で処理を行いたい場合は、isBreak="false" に
063 *            すると同時に、match 条件もそれぞれで、マッチするように設定する必要があります。
064 *
065 *         <og:switch key="{@PARAM}" />
066 *            <og:case match="[1]"   isBreak="false" /> 処理A </og:case>
067 *            <og:case match="[12]"  isBreak="false" /> 処理B </og:case>
068 *            <og:case match="[123]" isBreak="false" /> 処理C </og:case>
069 *            <og:case isNull="true" /> 処理X </og:case>
070 *            <og:case isDefault="true" /> 処理Y </og:case>
071 *         </og:switch>
072 *
073 *          ・上記指定では、isBreak="false" が指定されているため、マッチした後も継続して判定処理が実施されます。
074 *          ・上記例で言うと、PARAM が "1" の場合、上記3つともにマッチします。
075 *          ・isNull="true" は、switch の key が null の場合に成立します。(null とは、ゼロ文字列も含む)
076 *
077 * @og.group 画面制御
078 * @og.rev 5.2.3.0 (2010/12/01) 新規追加
079 *
080 * @version  5.2.3.0 (2010/12/01)
081 * @author       Kazuhiko Hasegawa
082 * @since    JDK1.6,
083 */
084public class SwitchTag extends CommonTagSupport {
085        //* このプログラムのVERSION文字列を設定します。   {@value} */
086        private static final String VERSION = "5.2.3.0 (2010/12/01)" ;
087
088        private static final long serialVersionUID = 523020101201L ;
089
090        private String  switchKey       = null;
091        private boolean useMatch        = true;         // マッチ処理を継続して行う場合、true  (5.3.0.0 (2010/12/01) 変数名変更)
092
093        /**
094         * Taglibの開始タグが見つかったときに処理する doStartTag() を オーバーライドします。
095         *
096         * @return      後続処理の指示( EVAL_BODY_INCLUDE )
097         */
098        @Override
099        public int doStartTag() {
100                useMatch        = true; // 初期化
101
102                return EVAL_BODY_INCLUDE ;      // Body インクルード( extends TagSupport 時)
103        }
104
105        /**
106         * Taglibの終了タグが見つかったときに処理する doEndTag() を オーバーライドします。
107         *
108         * @return      後続処理の指示
109         */
110        @Override
111        public int doEndTag() {
112                debugPrint();
113
114                return EVAL_PAGE ;
115        }
116
117        /**
118         * タグリブオブジェクトをリリースします。
119         * キャッシュされて再利用されるので、フィールドの初期設定を行います。
120         *
121         */
122        @Override
123        protected void release2() {
124                super.release2();
125                switchKey       = null;
126                useMatch        = true;
127        }
128
129        /**
130         * 【TAG】switch のマッチ判定用のキーを設定します。
131         *
132         * @og.tag switch のマッチ判定用のキーを設定します。
133         *
134         * @param       key マッチ判定用のキー
135         * @see         #getKey()
136         */
137        public void setKey( final String key ) {
138                switchKey = nval( getRequestParameter( key ),switchKey );
139        }
140
141        /**
142         * switch のマッチ判定用のキーを取得します。
143         *
144         * case タグで、この値を取り出して、マッチ判定を行います。
145         *
146         * @return      マッチ判定用のキー
147         * @see         #setKey( String )
148         */
149        protected String getKey() {
150                return switchKey;
151        }
152
153        /**
154         * case タグが、ブレイクした場合に、このメソッドを呼び出します。
155         *
156         * これは、 case タグが isBreak="true" でマッチした場合、このメソッドを
157         * 呼び出し、isMatch フラグを false に設定します。
158         * 他の case は、このフラグを参照して、false であれば、スルーします。
159         *
160         * @see         #isMatch()
161         */
162        protected void setBreak() {
163                useMatch= false ;
164        }
165
166        /**
167         * すでにマッチしたかどうかを返します。
168         *
169         * これは、 case タグが 処理を継続するかどうかの判定に利用します。
170         * case タグが isBreak="true" でマッチした場合、isMatch フラグは、
171         * false が返りますので、継続処理しません。
172         *
173         * @return      マッチしたかどうか[true:継続判定/false:スルー]
174         * @see         #setBreak()
175         */
176        protected boolean isMatch() {
177                return useMatch ;
178        }
179
180        /**
181         * このオブジェクトの文字列表現を返します。
182         * 基本的にデバッグ目的に使用します。
183         *
184         * @return このクラスの文字列表現
185         */
186        @Override
187        public String toString() {
188                return org.opengion.fukurou.util.ToString.title( this.getClass().getName() )
189                                .println( "VERSION"                     ,VERSION        )
190                                .println( "switchKey"           ,switchKey      )
191                                .println( "Other..."    ,getAttributes().getAttribute() )
192                                .fixForm().toString() ;
193        }
194}