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.fukurou.util;
017
018import java.io.File;
019import java.util.Map;
020import java.util.HashMap;
021import java.util.Locale ;
022import 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 */
040public 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}