00001 ' Attribute VB_Name = "Configuration"
00002 Option Explicit
00003 '! @brief 設定ファイルを読み込んで設定情報を得る為のクラス.
00004 '|
00005 '! <p>ロードされるファイル名は設定名称+".properties" <br />
00006 '! ファイルの検索順序は次のとおり.先に見つかったファイルがロードされる<br />
00007 '! 定数 CONFIG_FILE に設定されたディレクトリ.空文字の場合は無視<br />
00008 '! スクリプトが存在するディレクトリ<br />
00009 '! *common.vbs#CONFIG_PATH の定義が必要。<br /></p>
00010 '! @code
00011 '! Dim cfg
00012 '! Set cfg = New Configuration
00013 '| cfg.load "hoge"
00014 '! Dim val
00015 '! val = cfg.Item("key")
00016 '! @endcode
00017 '----------------------------------------------
00018 Class Configuration
00019 '' 最初に初期化ファイルを全部読み込んでコレクションに入れておく。
00020 Private mDictionary 'As Dictionary
00021
00022 Private mFileSystem
00023
00024 '* 設定ファイル要素のロード
00025 '* @param textFile 設定ファイルオブジェクト
00026 Public Sub loadElements(textFile)
00027 Dim charIndex
00028 Dim linebuf
00029 Dim key
00030 Dim val
00031
00032 Do While textFile.AtEndOfStream <> True
00033 linebuf =Trim( textFile.ReadLine())
00034 if(linebuf = "") Then
00035 Else
00036 If(left(linebuf,1) = "#") Then
00037 Else
00038 charIndex = InStr(linebuf,"=")
00039 If(charIndex < 1) Then ''みつかんねぇ(「設定されている」とみなす)
00040 key =Trim( linebuf,charIndex)
00041 val = ""
00042 Else
00043 key =Trim( Left(linebuf,charIndex - 1))
00044 val = Trim( Mid(linebuf,charIndex + 1))
00045 End If
00046 If mDictionary.Exists(key) Then
00047 mDictionary.Item(key) = val
00048 Else
00049 mDictionary.add key,val
00050 End If
00051 End If
00052 End If
00053 Loop
00054 End Sub
00055
00056 '* 設定ファイルのロード
00057 '* @param configname 設定の名称
00058 Public Sub load(configname)
00059 dim textFile
00060 Set mFileSystem = CreateObject("Scripting.FileSystemObject")
00061
00062 Set textFile = openConfigFile(configname)
00063 If Not textFile Is Nothing Then
00064 loadElements textFile
00065 textFile.Close
00066 End If
00067
00068 Set textFile = Nothing
00069 Set mFileSystem = Nothing
00070
00071 End Sub
00072
00073
00074 '* 設定ファイルを開く
00075 '* @param configname 設定名称
00076 '* @remarks CONFIG_PATH から設定名称 + ".properties"のファイルを探し、見つかればそのファイルを使う。見つからなければスクリプトが存在するディレクトリを探す
00077 Function openConfigFile(configname)
00078 On Error Resume Next
00079 dim textFile
00080 Dim filename
00081
00082 If(CONFIG_PATH <> "") Then
00083 '' TRY1:設定ファイルディレクトリ + configname + ".properties"
00084 filename = combinPath( CONFIG_PATH,configname & ".properties")
00085 If mFileSystem.FileExists(filename) Then
00086 Set textFile = mFileSystem.OpenTextFile(filename, ForReading, False)
00087 If(Err.Number = 0) Then
00088 Set openConfigFile = textFile
00089 Exit Function
00090 End If
00091 End If
00092 End If
00093 '' TRY2:"スクリプトのディレクトリ + configname + ".properties"
00094 filename = combinPath( getScriptPath,configname & ".properties")
00095 If mFileSystem.FileExists(filename) Then
00096 Set textFile = mFileSystem.OpenTextFile(filename, ForReading, False)
00097 If(Err.Number = 0) Then
00098 Set openConfigFile = textFile
00099 Exit Function
00100 End If
00101 End If
00102 Logger.Debug "config file not found " & Err.Description
00103 Set openConfigFile = Nothing
00104
00105 End Function
00106 '----------------------------------------------
00107 '* キーに対応する値の取得
00108 '----------------------------------------------
00109 Public Function getValue(key)
00110 Dim val
00111 val = mDictionary.Item(key)
00112
00113 getValue = val
00114 End Function
00115 '----------------------------------------------
00116 '* キーに対応する値の取得
00117 '* properties("key")を実現するため
00118 '----------------------------------------------
00119 Public Default Property Get Item(key)
00120 Dim val
00121 val = mDictionary.Item(key)
00122
00123 Item = val
00124 End Property
00125
00126
00127
00128 '----------------------------------------------
00129 '* オブジェクト初期化
00130 '----------------------------------------------
00131 Public Sub Class_Initialize()
00132 Set mDictionary = CreateObject("Scripting.Dictionary")
00133 End Sub
00134 '----------------------------------------------
00135 '* オブジェクト破棄
00136 '----------------------------------------------
00137 Public Sub Class_Terminate()
00138 Set mDictionary = Nothing
00139 End Sub
00140 End Class
00141
00142 '----------------------------------------------
00143 '* スクリプト本体のデフォルト設定
00144 '* common.propertiesと(スクリプト名).propertiesをマージして使う
00145 '----------------------------------------------
00146 Dim ScriptProperties
00147 Set ScriptProperties = new Configuration
00148 ScriptProperties.load "common"
00149 ScriptProperties.load getScriptBaseName
00150