' open_jdk_apidoc.vbs ' ======================================================================== ' 選択範囲またはカーソル位置の単語でJava APIドキュメントを検索・表示する。 ' ======================================================================== ' 注意事項 ' 1. baseDir定数(★でコメントが書かれているパラメータ)は環境に合わせて ' 変更してください。 ' 2. このマクロは%TEMP%\jdk_api_temp_menu.htmlに一時ファイルを作成します。 ' また、このファイルは自動では削除されません。スクリプトを呼び出すたびに ' 上書きされますのでディスク容量を大幅に浪費することはないはずですが、 ' このスクリプトの使用が不要になったら削除してください。 ' Java APIドキュメントがインストールされたディレクトリへのフルパス。 ' allclasses-frame.htmlがあるディレクトリを指定します。 ' ★ 以下のパラメータは環境に合わせて修正してください。 ' ★ ファイルパスの定義の最後は\で終わるようにしてください。 Const baseDir = "C:\j2sdk1.4.2\j2sdk-1_4_0-doc-ja\docs\ja\api\" ' 選択範囲またはカーソル位置の単語 sWord = Editor.ExpandParameter("$C") ' FileSystemObjectを用いたファイル操作で使用する定数 Const ForReading = 1, ForWriting = 2 ' HTMLファイルを開くために使用するWScript.Shellオブジェクト Set wshShell = CreateObject("WScript.Shell") ' ファイルの読み書きに使用するScripting.FileSystemObject Set fileSystem = CreateObject("Scripting.FileSystemObject") ' 各HTMLファイルの配置検索に使用するインデックスファイルへのフルパス。 indexFilePath = baseDir & "allclasses-frame.html" ' 各HTMLファイルの配置検索に使用するインデックスファイル読込オブジェクト Set indexFile = fileSystem.OpenTextFile(indexFilePath,ForReading) ' --------------------------------------------- ' ファイルの読み込み ' --------------------------------------------- ' インデックスファイルから、検索単語を含む行を抽出する正規表現 Set lineRegExp = New RegExp lineRegExp.Pattern = ">" & sWord & "()?" lineRegExp.IgnoreCase = False lineRegExp.Global = True ' lineRegExpで抽出した行から、HREF部分を取り出す正規表現 Set pathRegExp = New RegExp pathRegExp.Pattern = "HREF=.*.html" pathRegExp.IgnoreCase = False pathRegExp.Global = True ' 抽出した結果を格納する配列. サイズは初期値0で検索結果格納の度に再定義. Dim resultArray() Redim resultArray(0) ' 1行づつインデックスファイルの最後まで読み込み Do Until indexFile.atEndOfStream line = indexFile.ReadLine Set lineMatches = lineRegExp.Execute(line) ' 検索単語を含む行であれば、抽出した相対ファイルパスをresultArrayに格納 ' 格納フォーマットはjava/io/File.htmlのような形式。 if lineMatches.Count <> 0 Then Set pathMatches = pathRegExp.Execute(line) resultIndex = UBound(resultArray) + 1 Redim Preserve resultArray(resultIndex) resultArray(resultIndex) = Mid(pathMatches.Item(0),7) End If Loop indexFile.Close ' --------------------------------------------- ' 結果の出力 ' --------------------------------------------- ' 区切り文字'/'を'\'や'.'に変換するために使用する正規表現 Set fromSlashRegExp = New RegExp fromSlashRegExp.Pattern = "/" fromSlashRegExp.Global = True ' 抽出された結果の数にしたがって分岐する。 ' 0個 : エラーダイアログを表示 ' 1個 : 該当するAPIドキュメントを表示 ' 2個以上 : 一時HTMLファイルに抽出された結果への簡易リンク集を ' 出力し、これを表示. たとえばTimerで検索した場合など。 If UBound(resultArray) = 0 Then wshShell.Popup "[" & sWord &"]は見つかりませんでした。" _ ,0 ,"エラー" ,0 ElseIf UBound(resultArray) = 1 Then filePath = baseDir & resultArray(1) filePath = fromSlashRegExp.Replace(filePath,"\") wshShell.Run(filePath) Else tempFilePath = wshShell.ExpandEnvironmentStrings("%TEMP%") _ & "\jdk_api_temp_menu.html" fileSystem.CreateTextFile(tempFilePath) Set tempFile = fileSystem.OpenTextFile(tempFilePath,ForWriting) tempFile.WriteLine "" tempFile.WriteLine "JDK API 検索結果 " _ & "キーワード:[" & sWord &"]" tempFile.WriteLine "[" & sWord &"]は" & UBound(resultArray) _ & "件見つかりました。
" For I=1 To UBound(resultArray) filePath = baseDir & resultArray(I) filePath = fromSlashRegExp.Replace(filePath,"\") classPath = Left(resultArray(I),Len(resultArray(I))-5) classPath = fromSlashRegExp.Replace(classPath,".") tempFile.WriteLine "" _ & classPath & "
" Next tempFile.WriteLine "" tempFile.Close wshShell.Run(tempFilePath) fileSystem.DeleteFile(tempFilePath) End If