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 * @og.rev 7.2.4.0 (2020/05/11) 新規追加
037 * @og.group 選択データ制御
038 *
039 * @version  7.1
040 * @author   Kazuhiko Hasegawa
041 * @since    JDK11.0,
042 */
043public class Selection_FILES extends Selection_NULL {
044        private final String    CACHE ;
045
046        /**
047         * コンストラクター
048         *
049         * @og.rev 7.2.4.0 (2020/05/11) 新規追加
050         * @og.rev 7.2.9.4 (2020/11/20) spotbugs:null になっている可能性があるメソッドの戻り値を利用している
051         *
052         * @param       param   パラメータ文字列(;で条件を区切ります)
053         */
054        public Selection_FILES( final String param ) {
055                super();
056
057                final String[] prms = param.split(";");
058                final String   from = prms[0];
059                final boolean  nameOnly = prms.length > 1 && "nameOnly".equalsIgnoreCase( prms[1] ) ;
060
061                final PathMatcher match = prms.length > 2 ? FileSystems.getDefault().getPathMatcher(prms[2]) : null;
062                final FileFilter PATH_FLTR = file -> {
063                        return match == null || match.matches( file.toPath().getFileName() ) ;
064                };
065
066                final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE );
067
068                final File[] list = new File( from ).listFiles( PATH_FLTR );
069                if( list != null ) {                                                                                            // 7.2.9.4 (2020/11/20)
070                        Arrays.sort( list );
071
072                        for( int i=0; i<list.length; i++ ) {
073                                String value = list[i].getName();
074                                if( nameOnly ) {
075                                        final int ad = value.lastIndexOf( '.' );
076                                        if( ad >= 0 ) { value = value.substring( 0,ad ); }
077                                }
078
079                                buf.append( "<option value=\"" ).append( value )
080                                        .append( "\">" ).append( value ).append( "</option>" );
081                        }
082                }
083
084                CACHE = buf.toString();
085        }
086
087        /**
088         * 初期値が選択済みの 選択肢(オプション)を返します。
089         * このオプションは、引数の値を初期値とするオプションタグを返します。
090         * このクラスでは、useShortLabel は、無視されます。(常に、false です)
091         *
092         * @og.rev 7.2.4.0 (2020/05/11) 新規追加
093         *
094         * @param   selectValue  選択されている値
095         * @param   seqFlag  シーケンスアクセス機能 [true:ON/false:OFF]
096         * @param   useShortLabel ラベル(短)をベースとしたオプション表示を行うかどうか(常にfalse)。
097         *
098         * @return  オプションタグ
099         * @og.rtnNotNull
100         */
101        @Override
102        public String getOption( final String selectValue,final boolean seqFlag, final boolean useShortLabel ) {
103                // マッチするアドレスを探す。キーの前後のダブルクオートを加味して検索
104                final String selVal = "\"" + selectValue + "\"" ;
105
106                final int indx = CACHE.indexOf( selVal );
107
108                if( indx < 0 ) {
109                        if( selectValue != null && selectValue.length() > 0 ) {
110                                final String errMsg = "コードに存在しない値が指定されました。"
111                                                        + " value=[" + selectValue + "]" ;
112                                LogWriter.log( errMsg );
113                        }
114                        return CACHE;
115                }
116                else {
117                        final int addIndx = indx + selVal.length() ;    // selected の挿入位置
118
119                        final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE );
120
121                        if( seqFlag ) {
122                                buf.append( "<option value=\"" ).append( selectValue ).append( '"' );           // 6.0.2.5 (2014/10/31) char を append する。
123                        }
124                        else {
125                                buf.append( CACHE.substring( 0,addIndx ) );
126                        }
127                        buf.append( " selected=\"selected\"" )
128                                .append( CACHE.substring( addIndx ) );
129                        return buf.toString() ;
130                }
131        }
132
133        /**
134         * 選択肢(value)に対するラベルを返します。
135         * 選択肢(value)が、存在しなかった場合は、選択肢そのものを返します。
136         * getValueLabel( XX,false ) は、getValueLabel( XX ) と同じです。
137         *
138         * @og.rev 7.2.4.0 (2020/05/11) 新規追加
139         *
140         * @param       selectValue     選択肢の値
141         * @param       isSLbl  短縮ラベルを [true:使用する/false:しない](常に false)
142         *
143         * @return  選択肢のラベル
144         * @see     #getValueLabel( String )
145         */
146        @Override
147        public String getValueLabel( final String selectValue,final boolean isSLbl ) {
148                // マッチするアドレスを探す。キーの前後のダブルクオートを加味して検索
149                final String selVal = "\"" + selectValue + "\"" ;
150
151                final int indx = CACHE.indexOf( selVal );
152
153                if( indx < 0 ) {
154                        // マッチしなければ、選択肢そのものを返す。
155                        return selectValue;
156                }
157                else {
158                        // マッチすれば、キー以下のBODY部の文字列を切り出して返す。
159                        final int stIdx = indx + selVal.length() + 1 ;                  // +1 は、">" の位置
160                        final int edIdx = CACHE.indexOf( '<',stIdx );                           // 終了アドレス
161
162                        return CACHE.substring( stIdx,edIdx );
163                }
164        }
165}