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     */
016    package org.opengion.hayabusa.taglib;
017    
018    import 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     * ●形式ï¼?lt;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" /> 処ç�? </og:case>
047     *            <og:case match="B" /> 処ç�? </og:case>
048     *            <og:case match="C" /> 処ç�? </og:case>
049     *            <og:case isDefault="true" /> 処ç�? </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" /> 処ç�? </og:case>
067     *            <og:case match="[12]"  isBreak="false" /> 処ç�? </og:case>
068     *            <og:case match="[123]" isBreak="false" /> 処ç�? </og:case>
069     *            <og:case isNull="true" /> 処ç�? </og:case>
070     *            <og:case isDefault="true" /> 処ç�? </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     */
084    public 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 isMatch         = true;         // マッチå?ç�?‚’継続して行う場合ã?true
092            private boolean useMatch        = true;         // マッチå?ç�?‚’継続して行う場合ã?true (5.3.0.0 (2010/12/01) 変数名変更)
093    
094            /**
095             * Taglibの開始タグが見つかったときに処ç�?�™ã‚?doStartTag() ã‚?オーバã?ライドしますã?
096             *
097             * @return      後続å?ç�??æŒ?¤º( EVAL_BODY_INCLUDE )
098             */
099            @Override
100            public int doStartTag() {
101    //              isMatch         = true; // 初期åŒ?
102                    useMatch        = true; // 初期åŒ?
103    
104                    return( EVAL_BODY_INCLUDE );    // Body インクルーãƒ? extends TagSupport æ™?
105            }
106    
107            /**
108             * Taglibの終äº?‚¿グが見つかったときに処ç�?�™ã‚?doEndTag() ã‚?オーバã?ライドしますã?
109             *
110             * @return      後続å?ç�??æŒ?¤º
111             */
112            @Override
113            public int doEndTag() {
114                    debugPrint();
115    
116                    return(EVAL_PAGE);
117            }
118    
119            /**
120             * タグリブオブジェクトをリリースしますã?
121             * キャãƒ?‚·ュされて再利用されるã?で、フィールドã?初期設定を行いますã?
122             *
123             */
124            @Override
125            protected void release2() {
126                    super.release2();
127                    switchKey       = null;
128    //              isMatch         = true;
129                    useMatch        = true;
130            }
131    
132            /**
133             * 【TAG】switch のマッチ判定用のキーを設定しますã?
134             *
135             * @og.tag switch のマッチ判定用のキーを設定しますã?
136             *
137             * @param       key マッチ判定用のキー
138             * @see         #getKey()
139             */
140            public void setKey( final String key ) {
141                    switchKey = nval( getRequestParameter( key ),switchKey );
142            }
143    
144            /**
145             * switch のマッチ判定用のキーを取得しますã?
146             *
147             * case タグで、この値を取りå?して、ã?ãƒ?ƒ�判定を行いますã?
148             *
149             * @return      マッチ判定用のキー
150             * @see         #setKey( String )
151             */
152            protected String getKey() {
153                    return switchKey;
154            }
155    
156            /**
157             * case タグがã?ブレイクした場合に、このメソãƒ?ƒ‰を呼び出しますã?
158             *
159             * これはã€?case タグã�?isBreak="true" でマッチしたå?合ã?こã?メソãƒ?ƒ‰ã‚?
160             * 呼び出しã?isMatch フラグã‚?false に設定しますã?
161             * 他ã? case は、このフラグを参照して、false であれば、スルーしますã?
162             *
163             * @see         #isMatch()
164             */
165            protected void setBreak() {
166    //              isMatch = false ;
167                    useMatch= false ;
168            }
169    
170            /**
171             * すでにマッチしたかどã�?�‹を返しますã?
172             *
173             * これはã€?case タグã�?処ç�?‚’継続するかどã�?�‹の判定に利用しますã?
174             * case タグã�?isBreak="true" でマッチしたå?合ã?isMatch フラグはã€?
175             * false が返りますã?で、継続å?ç�?�—ませんã€?
176             *
177             * @return      マッチしたかどã�?�‹[true:継続判å®?false:スルー]
178             * @see         #setBreak()
179             */
180            protected boolean isMatch() {
181    //              return isMatch ;
182                    return useMatch ;
183            }
184    
185            /**
186             * こã?オブジェクトã?æ–?­—å?表現を返しますã?
187             * 基本çš?�«ãƒ?ƒ�ãƒ?‚°目çš?�«使用しますã?
188             *
189             * @return こã?クラスのæ–?­—å?表現
190             */
191            @Override
192            public String toString() {
193                    return org.opengion.fukurou.util.ToString.title( this.getClass().getName() )
194                                    .println( "VERSION"                     ,VERSION        )
195                                    .println( "switchKey"           ,switchKey      )
196                                    .println( "Other..."    ,getAttributes().getAttribute() )
197                                    .fixForm().toString() ;
198            }
199    }