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.db;
017
018import java.io.File;
019import java.io.FileFilter;
020import java.util.Arrays;
021import java.nio.file.FileSystems;
022import java.nio.file.PathMatcher;
023
024import org.opengion.fukurou.system.LogWriter;
025// import org.opengion.fukurou.util.StringUtil;
026// import static org.opengion.fukurou.system.HybsConst.CR ;
027import static org.opengion.fukurou.system.HybsConst.BUFFER_MIDDLE;
028
029/**
030 * ファイルのプルダウンリストの作成するクラスです。
031 *
032 * ファイルの一覧リストからプルダウンリストを作成します。
033 * オプションタグを作成したり、与えられたキーをもとに、チェック済みのオプションタグを
034 * 作成したりします。
035 *
036 * 編集パラメータ に属性を ";" で区切って指定します。
037 *  スキャンするフォルダ名(必須);
038 *  nameOnly        … nameOnlyキーワードがあれば、ファイル名のみ(拡張子なし)のリストを作成する。
039 *  match=選択条件  … WHERE条件となるファイル選択のマッチャー を指定します。
040 *
041 * 例)
042 *  /gg/jsp/backimage;nameOnly;match=*.png
043 *
044 * @og.rev 7.2.4.0 (2020/05/11) 新規追加
045 * @og.group 選択データ制御
046 *
047 * @version  7.1
048 * @author   Kazuhiko Hasegawa
049 * @since    JDK11.0,
050 */
051public class Selection_FILES extends Selection_NULL {
052        private final String    CACHE ;
053
054        /**
055         * コンストラクター
056         *
057         * @og.rev 7.2.4.0 (2020/05/11) 新規追加
058         * @og.rev 7.2.9.4 (2020/11/20) spotbugs:null になっている可能性があるメソッドの戻り値を利用している
059         * @og.rev 7.4.2.0 (2021/05/08) パラメータの判定方法変更
060         *
061         * @param       param   パラメータ文字列(;で条件を区切ります)
062         */
063        public Selection_FILES( final String param ) {
064                super();
065                // 7.4.2.0 (2021/05/08) パラメータの判定方法変更。
066                final int fst = param.indexOf( ';' );
067                final String from = fst < 0 ? param : param.substring( 0,fst ); // 開始フォルダは必ず指定する
068
069                final boolean nameOnly = param.contains( ";nameOnly" );
070        //      final boolean multi    = param.contains( ";multi" );
071
072                String matchStr = "*" ;
073                final int st = param.indexOf( ";match=" );
074                if( st > 0 ) {
075                        final int ed = param.indexOf( ";",st+7 );
076                        if( ed > 0 ) {
077                                matchStr = param.substring( st+7,ed );
078                        }
079                        else {
080                                matchStr = param.substring( st+7 );
081                        }
082                }
083                final PathMatcher match = FileSystems.getDefault().getPathMatcher( "glob:" + matchStr );
084                final FileFilter PATH_FLTR = file -> { return match.matches( file.toPath().getFileName() ); };
085
086//              final String[] prms = param.split(";");
087//              final String   from = prms[0];
088//              final boolean  nameOnly = prms.length > 1 && "nameOnly".equalsIgnoreCase( prms[1] ) ;
089
090//              final PathMatcher match = prms.length > 2 ? FileSystems.getDefault().getPathMatcher(prms[2]) : null;
091//              final PathMatcher match = prms.length > 2 ? FileSystems.getDefault().getPathMatcher(prms[2]) : null;
092//              final FileFilter PATH_FLTR = file -> {
093//                      return match == null || match.matches( file.toPath().getFileName() ) ;
094//              };
095
096                final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE );
097
098                final File[] list = new File( from ).listFiles( PATH_FLTR );
099                if( list != null ) {                                                                                            // 7.2.9.4 (2020/11/20)
100                        Arrays.sort( list );
101
102                        for( int i=0; i<list.length; i++ ) {
103                                String value = list[i].getName();
104                                if( nameOnly ) {
105                                        final int ad = value.lastIndexOf( '.' );
106                                        if( ad >= 0 ) { value = value.substring( 0,ad ); }
107                                }
108
109                                buf.append( "<option value=\"" ).append( value )
110                                        .append( "\">" ).append( value ).append( "</option>" );
111                        }
112                }
113
114                CACHE = buf.toString();
115        }
116
117        /**
118         * 初期値が選択済みの 選択肢(オプション)を返します。
119         * このオプションは、引数の値を初期値とするオプションタグを返します。
120         * このクラスでは、useShortLabel は、無視されます。(常に、false です)
121         *
122         * @og.rev 7.2.4.0 (2020/05/11) 新規追加
123         *
124         * @param   selectValue  選択されている値
125         * @param   seqFlag  シーケンスアクセス機能 [true:ON/false:OFF]
126         * @param   useShortLabel ラベル(短)をベースとしたオプション表示を行うかどうか(常にfalse)。
127         *
128         * @return  オプションタグ
129         * @og.rtnNotNull
130         */
131        @Override
132        public String getOption( final String selectValue,final boolean seqFlag, final boolean useShortLabel ) {
133                // マッチするアドレスを探す。キーの前後のダブルクオートを加味して検索
134                final String selVal = "\"" + selectValue + "\"" ;
135
136                final int indx = CACHE.indexOf( selVal );
137
138                if( indx < 0 ) {
139                        if( selectValue != null && selectValue.length() > 0 ) {
140                                final String errMsg = "コードに存在しない値が指定されました。"
141                                                        + " value=[" + selectValue + "]" ;
142                                LogWriter.log( errMsg );
143                        }
144                        return CACHE;
145                }
146                else {
147                        final int addIndx = indx + selVal.length() ;    // selected の挿入位置
148
149                        final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE );
150
151                        if( seqFlag ) {
152                                buf.append( "<option value=\"" ).append( selectValue ).append( '"' );           // 6.0.2.5 (2014/10/31) char を append する。
153                        }
154                        else {
155                                buf.append( CACHE.substring( 0,addIndx ) );
156                        }
157                        buf.append( " selected=\"selected\"" )
158                                .append( CACHE.substring( addIndx ) );
159                        return buf.toString() ;
160                }
161        }
162
163        /**
164         * 選択肢(value)に対するラベルを返します。
165         * 選択肢(value)が、存在しなかった場合は、選択肢そのものを返します。
166         * getValueLabel( XX,false ) は、getValueLabel( XX ) と同じです。
167         *
168         * @og.rev 7.2.4.0 (2020/05/11) 新規追加
169         *
170         * @param       selectValue     選択肢の値
171         * @param       isSLbl  短縮ラベルを [true:使用する/false:しない](常に false)
172         *
173         * @return  選択肢のラベル
174         * @see     #getValueLabel( String )
175         */
176        @Override
177        public String getValueLabel( final String selectValue,final boolean isSLbl ) {
178                // マッチするアドレスを探す。キーの前後のダブルクオートを加味して検索
179                final String selVal = "\"" + selectValue + "\"" ;
180
181                final int indx = CACHE.indexOf( selVal );
182
183                if( indx < 0 ) {
184                        // マッチしなければ、選択肢そのものを返す。
185                        return selectValue;
186                }
187                else {
188                        // マッチすれば、キー以下のBODY部の文字列を切り出して返す。
189                        final int stIdx = indx + selVal.length() + 1 ;                  // +1 は、">" の位置
190                        final int edIdx = CACHE.indexOf( '<',stIdx );                           // 終了アドレス
191
192                        return CACHE.substring( stIdx,edIdx );
193                }
194        }
195}