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 org.opengion.fukurou.system.OgRuntimeException ;         // 6.4.2.0 (2016/01/29)
019import static org.opengion.fukurou.util.StringUtil.nval;
020
021import java.io.IOException;
022import java.io.ObjectInputStream;
023
024import org.opengion.fukurou.util.Options;
025import org.opengion.hayabusa.common.HybsSystem;
026import org.opengion.fukurou.util.ToString;                                              // 6.1.1.0 (2015/01/17)
027
028/**
029 * フォームの入力欄などで入力候補となるデータリストを定義するHTML拡張タグです。
030 * HTML5 から、新たに追加された要素です。
031 *
032 * データリスト内の選択肢は、optionタグ、queryOptionタグによって指定します。
033 * データリスト の id 属性は、フォームの list 属性と同じキーを指定する事で関連付けします。
034 *
035 * @og.formSample
036 * ●形式:<og:datalist id="…" />
037 * ●body:あり(EVAL_BODY_INCLUDE:BODYをインクルードし、{@XXXX} は解析しません)
038 *
039 * ●Tag定義:
040 *   <og:datalist
041 *       id               ○【TAG】入力候補を表示するフォームの list 属性に設定する id (必須)
042 *       caseKey            【TAG】このタグ自体を利用するかどうかの条件キーを指定します(初期値:null)
043 *       caseVal            【TAG】このタグ自体を利用するかどうかの条件値を指定します(初期値:null)
044 *       caseNN             【TAG】指定の値が、null/ゼロ文字列 でない場合(Not Null=NN)は、このタグは使用されます(初期値:判定しない)
045 *       caseNull           【TAG】指定の値が、null/ゼロ文字列 の場合は、このタグは使用されます(初期値:判定しない)
046 *       caseIf             【TAG】指定の値が、true/TRUE文字列の場合は、このタグは使用されます(初期値:判定しない)
047 *       debug              【TAG】デバッグ情報を出力するかどうか[true/false]を指定します(初期値:false)
048 *   >   ... Body ...
049 *   </og:datalist>
050 *
051 * ●使用例
052 * <og:input type="text" name="tokyo" autocomplete="on" list="tokyo.sel" />
053 *
054 *  <og:datalist id="tokyo.sel" >
055 *      <og:option value="渋谷" />
056 *      <og:option value="新宿" />
057 *      <og:option value="池袋" />
058 *  </og:datalist><
059 *
060 * @og.group 【HTML5】選択データ制御
061 * @og.rev 5.7.1.0 (2013/12/06) 新規追加
062 *
063 * @version  6.0
064 * @author       Kazuhiko Hasegawa
065 * @since    JDK5.0,
066 */
067public class DatalistTag extends CommonTagSupport implements OptionAncestorIF {
068        /** このプログラムのVERSION文字列を設定します。   {@value} */
069        private static final String VERSION = "6.8.0.0 (2017/06/02)" ;
070        private static final long serialVersionUID = 680020170602L ;
071
072        private transient Options option        = new Options();
073
074        private String id       ;               // フォームと関連付けるid
075
076        /**
077         * デフォルトコンストラクター
078         *
079         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
080         */
081        public DatalistTag() { super(); }               // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
082
083        /**
084         * Taglibの開始タグが見つかったときに処理する doStartTag() を オーバーライドします。
085         *
086         * @return      後続処理の指示( EVAL_BODY_INCLUDE )
087         */
088        @Override
089        public int doStartTag() {
090                // 6.4.1.1 (2016/01/16) PMD refactoring. A method should have only one exit point, and that should be the last statement in the method
091                return useTag()
092                                        ? EVAL_BODY_INCLUDE             // Body インクルード( extends TagSupport 時)
093                                        : SKIP_BODY ;                   // Body を評価しない
094        }
095
096        /**
097         * Taglibの終了タグが見つかったときに処理する doEndTag() を オーバーライドします。
098         *
099         * @og.rev 5.7.6.2 (2014/05/16) IEのHTML5機能が無効の場合、INDBMENU を作成します。
100         * @og.rev 6.3.9.0 (2015/11/06) コンストラクタで初期化されていないフィールドを null チェックなしで利用している(findbugs)
101         *
102         * @return      後続処理
103         */
104        @Override
105        public int doEndTag() {
106                debugPrint();           // 4.0.0 (2005/02/28)
107                // 5.2.2.0 (2010/11/01) caseKey 、caseVal 属性対応
108                if( useTag() ) {
109                        // 6.3.9.0 (2015/11/06) コンストラクタで初期化されていないフィールドを null チェックなしで利用している(findbugs)
110                        if( id == null ) {
111                                final String errMsg = "idは必須です。" ;
112                                throw new OgRuntimeException( errMsg );
113                        }
114
115                        final StringBuilder rtn = new StringBuilder( BUFFER_MIDDLE );
116
117                        // 5.7.6.2 (2014/05/16) IEのHTML5機能が無効の場合の処理
118                        final String ieHTML5 = (String)getSessionAttribute( HybsSystem.IE_HTML5_KEY );
119                        if( "FALSE".equalsIgnoreCase( ieHTML5 ) ) {
120                                final String inName = id.endsWith( ".sel" ) ? id.substring( 0,id.length()-4 ) : id ;
121
122                                rtn.append("<select id='").append( id )
123                                        .append( "' style='position:absolute;' onChange='selChanged(this);' >" )
124                                        .append( option.getOption() )
125                                        // 8.1.0.0 (2021/12/28) HTML5 準拠に見直し(<script> type属性削除)
126//                                      .append( "</select><script type='text/javascript'>makeInputMenu('" )
127                                        .append( "</select><script>makeInputMenu('" )
128                                        .append( inName ).append( "');</script>" );
129                        }
130                        else {
131                                // display:none は、datalist の optionのBODY部が、HTML5 以外では表示されてしまうため。
132                                rtn.append("<div style='display:none;'><datalist id='")
133                                        .append( id ).append( "' >" )
134                                        .append( option.getOption() )
135                                        .append( "</datalist></div>" );
136                        }
137
138                        jspPrint( rtn.toString() );
139                }
140                return EVAL_PAGE ;
141        }
142
143        /**
144         * タグリブオブジェクトをリリースします。
145         * キャッシュされて再利用されるので、フィールドの初期設定を行います。
146         *
147         */
148        @Override
149        protected void release2() {
150                super.release2();
151                option  = new Options();
152                id              = null;
153        }
154
155        /**
156         * データリストの選択項目を追加します。
157         *
158         * datalist タグのBODY要素の OptionTag よりアクセスされます。
159         *
160         * @param       opt      オプションタグ文字列
161         * @see         org.opengion.hayabusa.taglib.OptionAncestorIF#addOption( String )
162         */
163        @Override       // OptionAncestorIF
164        public void addOption( final String opt ) {
165                option.add( opt );
166        }
167
168        /**
169         * メニュー項目の最後の項目を削除します。
170         *
171         * select タグのBODY要素の OptionTag よりアクセスされます。
172         *
173         * @og.rev 6.8.0.0 (2017/06/02) メニュー項目の最後の項目を削除。
174         * @see         org.opengion.hayabusa.taglib.OptionAncestorIF#removeLast()
175         */
176        @Override       // OptionAncestorIF
177        public void removeLast() {
178                option.removeLast();
179        }
180
181        /**
182         * 【HTML】要素に対して固有の名前(id)をつける場合に設定します。
183         *
184         * @og.tag
185         * データリスト の id 属性は、フォームの list 属性と同じキーを指定する事で関連付けします。
186         *
187         * ※
188         * 内部事情で、inputタグ(columnタグ)の list属性 に設定するキーも、id属性に設定するキーも、
189         * inputタグ(columnタグ)の name属性+".sel" を標準的に使用してください。
190         *
191         * @param   id 固有の名前
192         */
193        @Override
194        public void setId( final String id ) {
195                this.id = nval( getRequestParameter( id ), null );
196        }
197
198        /**
199         * 値を外部から取り出します。
200         *
201         * OptionTag で、value を取り出して、内部の値と同じ場合は、選択状態にします。
202         *
203         * @og.rev 5.7.1.0 (2013/12/06) 新規追加
204         *
205         * @return      null固定
206         * @see         org.opengion.hayabusa.taglib.OptionAncestorIF#addOption( String )
207         */
208        @Override       // OptionAncestorIF
209        public String getValue() {
210                // ここでは、何もしません。
211                return null;
212        }
213
214        /**
215         * 複数選択可能時に全選択を設定するかどうかを返します。
216         *
217         * これは、上位入れ子のタグの OptionTag で、multipleAll を取り出して、
218         * true であれば、全選択に設定します。
219         *
220         * @og.rev 5.7.1.0 (2013/12/06) 新規追加
221         *
222         * @return      false固定
223         * @see         org.opengion.hayabusa.taglib.OptionAncestorIF#addOption( String )
224         */
225        @Override       // OptionAncestorIF
226        public boolean isMultipleAll() {
227                // ここでは、何もしません。
228                return false;
229        }
230
231        /**
232         * パラメーター変換({&#064;XXXX}の置き換えをしない状態のパラメーターをセットします。
233         *
234         * ※ ここでは、何もしません。
235         *
236         * @og.rev 5.7.1.0 (2013/12/06) 新規追加
237         *
238         * @param   param パラメーター
239         * @see         org.opengion.hayabusa.taglib.OptionAncestorIF#addOption( String )
240         */
241        @Override       // OptionAncestorIF
242        public void setRawParam( final String param ) {
243                // ここでは、何もしません。
244        }
245
246        /**
247         * セレクトメニューの場合、キー:ラベル形式で表示するかどうか[true/false/null]を返します。
248         *
249         * これは、上位入れ子のタグの OptionTag で、addKeyLabel を取り出して、
250         * true であれば、キー:ラベル形式 のオプションを、#addOption( String ) で
251         * 登録させます。
252         *
253         * @og.rev 6.0.4.0 (2014/11/28) キー:ラベル形式で表示するかどうか。新規追加
254         *
255         * @return      null固定
256         * @see         #addOption( String )
257         * @see         org.opengion.hayabusa.taglib.OptionAncestorIF#getAddKeyLabel()
258         */
259        @Override       // OptionAncestorIF
260        public String getAddKeyLabel() {
261                // ここでは、何もしません。
262                return null;
263        }
264
265        /**
266         * シリアライズ用のカスタムシリアライズ読み込みメソッド
267         *
268         * ここでは、transient 宣言された内部変数の内、初期化が必要なフィールドのみ設定します。
269         *
270         * @serialData 一部のオブジェクトは、シリアライズされません。
271         *
272         * @param       strm    ObjectInputStreamオブジェクト
273         * @see #release2()
274         * @throws IOException  シリアライズに関する入出力エラーが発生した場合
275         * @throws ClassNotFoundException       クラスを見つけることができなかった場合
276         */
277        private void readObject( final ObjectInputStream strm ) throws IOException , ClassNotFoundException {
278                strm.defaultReadObject();
279                option = new Options();
280        }
281
282        /**
283         * このオブジェクトの文字列表現を返します。
284         * 基本的にデバッグ目的に使用します。
285         *
286         * @return このクラスの文字列表現
287         * @og.rtnNotNull
288         */
289        @Override
290        public String toString() {
291                return ToString.title( this.getClass().getName() )
292                                .println( "VERSION"             ,VERSION                )
293                                .println( "id"                  ,id                             )
294                                .println( "Other..."    ,getAttributes().getAttribute() )
295                                .fixForm().toString() ;
296        }
297}