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.fukurou.util;
017    
018    import java.io.File;
019    import java.util.Map;
020    import java.util.HashMap;
021    import java.util.Locale ;
022    import java.util.Set;
023    
024    /**
025     * FileMap は、ファイルを読み取って、キー??から、ファイルへのリンクを作?するための
026     * ??を返します?
027     * ファイルそ?も?は、指定??レクトリをすべて読み取り、拡張子以外?部?、キーとして
028     * 登録します?(キーは大?に統?れます?)
029     * 実際のファイルの拡張子?、リンク作?時?処?付与されます?
030     * 例えば、HELPファイルを?XXXX.html ??XXXX.htm 、XXXX.pdf など、色?形態で作?した
031     * 場合でも?キーとしては、XXXX で存在チェ?をかけることができるようになります?
032     *
033     * ファイルは、?すべて読み取ってメモリ上で管?れます?
034     * ?レクトリの再読取が?な場合?、オブジェクトを再作?する?があります?
035     *
036     * @version  4.0
037     * @author   Kazuhiko Hasegawa
038     * @since    JDK5.0,
039     */
040    public final class FileMap {
041    //      private final String directory;                         // 5.5.4.2 (2012/07/13) 初回しか使わな?め?
042            private final Map<String,String> map = new HashMap<String,String>();
043    
044            /**
045             * 読み取る?レクトリを指定して、ファイルマップを構築します?
046             *
047             * @og.rev 5.5.4.2 (2012/07/13) ? makeFileMap() を直接コンストラクターとして使用します?
048             *
049             * @param  dir ?レクトリ
050             */
051    //      public FileMap( final String dir ) {
052    //              directory = dir;
053    //
054    //              makeFileMap();
055    //      }
056    
057            /**
058             * すでに読み取っ?Set オブジェクトを?して、ファイルマップを構築します?
059             *
060             * これは、ServletContext を利用した、META-INF/resources からの読み取り対応になります?
061             * ?を取得する?は、ServletContext 関連の実??になるため?fukurou では
062             * java の??オブジェクトである Set を??るだけとします?
063             *
064             * ファイル名?、dir を削除した残りで構築します?フォル?層を含みます?
065             * Mapのキーは、フォル?層を含まな??ファイル名?みとします?
066             * つまり?フォル?層を持ってリソースを用意しておいても?キーとしては?
067             * ファイル名?みを使用します?
068             *
069             * @og.rev 5.5.4.2 (2012/07/13) 新規作?
070             *
071             * @param  dir ?レクトリ
072             * @param  resourcePaths リソースパス
073             * @throws IllegalArgumentException 引数の dir ??resourcePaths が?null の場?
074             */
075            public FileMap( final String dir , final Set<?> resourcePaths ) throws IllegalArgumentException {
076                    if( resourcePaths == null || dir == null ) {
077                            String errMsg = "??リソースは、存在しません?" + dir + "]";
078                            throw new IllegalArgumentException( errMsg );
079                    }
080    
081                    int len = dir.length() ;
082    
083                    for( Object path : resourcePaths ) {
084                            String fname  = String.valueOf( path ).substring( len );        // ファイル?
085                            String upkey  = fname.toUpperCase( Locale.JAPAN ) ;                     // 大?化されたファイル?Mapのキー)
086    
087                            int idx = fname.lastIndexOf( '.' );
088                            if( idx >= 0 ) {
089                                    map.put( upkey.substring( 0,idx ), fname );
090                            }
091                            else {
092                                    map.put( upkey, fname );
093                            }
094                    }
095            }
096    
097            /**
098             * 読み取る?レクトリを指定して、ファイルマップを構築します?
099             *
100             * こ??レクトリは、OSに対する物?ドレスになります?
101             *
102             * @og.rev 5.5.4.2 (2012/07/13) makeFileMap() を直接コンストラクターとして使用
103             *
104             * @param  dir ?レクトリ
105             * @throws IllegalArgumentException 引数の dir が存在しな?、ディレクトリ出な??合?
106             */
107            public FileMap( final String dir ) throws IllegalArgumentException {
108    //      private void makeFileMap() {
109                    File directory = new File( dir );
110                    if( ! directory.exists() ) {
111                            String errMsg = "???レクトリは、存在しません?" + directory + "]";
112                            throw new IllegalArgumentException( errMsg );
113                    }
114    
115                    if( ! directory.isDirectory() ) {
116                            String errMsg = "??キーは、ディレクトリではありません?" + directory + "]";
117                            throw new IllegalArgumentException( errMsg );
118                    }
119    
120                    for( String fname : directory.list() ) {
121                            String upkey  = fname.toUpperCase( Locale.JAPAN ) ;
122    
123                            int idx = upkey.lastIndexOf( '.' );
124                            if( idx >= 0 ) {
125                                    map.put( upkey.substring( 0,idx ), fname );
126                            }
127                            else {
128                                    map.put( upkey, fname );
129                            }
130                    }
131    
132    //              String[] files = directory.list();
133    //              for( int i=0; i<files.length; i++ ) {
134    //                      String key  = files[i].toUpperCase( Locale.JAPAN ) ;
135    //
136    //                      int idx = key.lastIndexOf( '.' );
137    //                      if( idx >= 0 ) {
138    //                              map.put( key.substring( 0,idx ), files[i] );
139    //                      }
140    //                      else {
141    //                              map.put( key, files[i] );
142    //                      }
143    //              }
144            }
145    
146            /**
147             * ??キーのファイルが存在して?かど?を返します?
148             * 存在して?場合?、true , 存在して???合?、false になります?
149             *
150             * @param   key ??キー
151             *
152             * @return      存在して?かど?(true:存在する/false:存在しな?
153             * @throws  IllegalArgumentException キーが指定されて?せん?
154             */
155            public boolean exists( final String key ) {
156                    if( key == null ) {
157                            String errMsg = "キーが指定されて?せん? ;
158                            throw new IllegalArgumentException( errMsg );
159                    }
160    
161                    return map.containsKey( key.toUpperCase( Locale.JAPAN ) );
162            }
163    
164            /**
165             * キーに対応したファイル名を返します?
166             * ??キーに対するファイル名が存在しな??合?、null を返します?
167             *
168             * @param   key ??キー
169             *
170             * @return      ファイル??レクトリパスは含ま?
171             */
172            public String getFilename( final String key ) {
173                    if( key == null ) {
174                            return null ;
175            //              String errMsg = "キーが指定されて?せん? ;
176            //              throw new IllegalArgumentException( errMsg );
177                    }
178    
179                    return map.get( key.toUpperCase( Locale.JAPAN ) );
180            }
181    }