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    }