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 Map<String,String> map = new HashMap<String,String>(); 042 043 /** 044 * すでに読み取った Set オブジェクトを指定して、ファイルマップを構築します。 045 * 046 * これは、ServletContext を利用した、META-INF/resources からの読み取り対応になります。 047 * 一覧を取得するのは、ServletContext 関連の実装が必要になるため、fukurou では 048 * java の一般的なオブジェクトである Set を処理するだけとします。 049 * 050 * ファイル名は、dir を削除した残りで構築します。フォルダ階層を含みます。 051 * Mapのキーは、フォルダ階層を含まない、ファイル名のみとします。 052 * つまり、フォルダ階層を持ってリソースを用意しておいても、キーとしては、 053 * ファイル名のみを使用します。 054 * 055 * @og.rev 5.5.4.2 (2012/07/13) 新規作成 056 * 057 * @param dir ディレクトリ 058 * @param resourcePaths リソースパス 059 * @throws IllegalArgumentException 引数の dir や、resourcePaths が、null の場合 060 */ 061 public FileMap( final String dir , final Set<?> resourcePaths ) throws IllegalArgumentException { 062 if( resourcePaths == null || dir == null ) { 063 String errMsg = "指定のリソースは、存在しません。[" + dir + "]"; 064 throw new IllegalArgumentException( errMsg ); 065 } 066 067 int len = dir.length() ; 068 069 for( Object path : resourcePaths ) { 070 String fname = String.valueOf( path ).substring( len ); // ファイル名 071 String upkey = fname.toUpperCase( Locale.JAPAN ) ; // 大文字化されたファイル名(Mapのキー) 072 073 int idx = fname.lastIndexOf( '.' ); 074 if( idx >= 0 ) { 075 map.put( upkey.substring( 0,idx ), fname ); 076 } 077 else { 078 map.put( upkey, fname ); 079 } 080 } 081 } 082 083 /** 084 * 読み取るディレクトリを指定して、ファイルマップを構築します。 085 * 086 * このディレクトリは、OSに対する物理アドレスになります。 087 * 088 * @og.rev 5.5.4.2 (2012/07/13) makeFileMap() を直接コンストラクターとして使用 089 * 090 * @param dir ディレクトリ 091 * @throws IllegalArgumentException 引数の dir が存在しないか、ディレクトリ出ない場合。 092 */ 093 public FileMap( final String dir ) throws IllegalArgumentException { 094 File directory = new File( dir ); 095 if( ! directory.exists() ) { 096 String errMsg = "指定のディレクトリは、存在しません。[" + directory + "]"; 097 throw new IllegalArgumentException( errMsg ); 098 } 099 100 if( ! directory.isDirectory() ) { 101 String errMsg = "指定のキーは、ディレクトリではありません。[" + directory + "]"; 102 throw new IllegalArgumentException( errMsg ); 103 } 104 105 for( String fname : directory.list() ) { 106 String upkey = fname.toUpperCase( Locale.JAPAN ) ; 107 108 int idx = upkey.lastIndexOf( '.' ); 109 if( idx >= 0 ) { 110 map.put( upkey.substring( 0,idx ), fname ); 111 } 112 else { 113 map.put( upkey, fname ); 114 } 115 } 116 } 117 118 /** 119 * 指定のキーのファイルが存在しているかどうかを返します。 120 * 存在している場合は、true , 存在していない場合は、false になります。 121 * 122 * @param key 指定のキー 123 * 124 * @return 存在しているかどうか(true:存在する/false:存在しない) 125 * @throws IllegalArgumentException キーが指定されていません。 126 */ 127 public boolean exists( final String key ) { 128 if( key == null ) { 129 String errMsg = "キーが指定されていません。" ; 130 throw new IllegalArgumentException( errMsg ); 131 } 132 133 return map.containsKey( key.toUpperCase( Locale.JAPAN ) ); 134 } 135 136 /** 137 * キーに対応したファイル名を返します。 138 * 指定のキーに対するファイル名が存在しない場合は、null を返します。 139 * 140 * @param key 指定のキー 141 * 142 * @return ファイル名(ディレクトリパスは含まず) 143 */ 144 public String getFilename( final String key ) { 145 if( key == null ) { 146 return null ; 147 // String errMsg = "キーが指定されていません。" ; 148 // throw new IllegalArgumentException( errMsg ); 149 } 150 151 return map.get( key.toUpperCase( Locale.JAPAN ) ); 152 } 153}