熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

對ASP 動態包含文件方法的改進

2022-06-13   來源: .NET編程 

  ASP 本身不支持動態包含文件現在的動態包含是通過 FSO 把被包含的文件合並到主文件裡再運行以下也有把形如 <!#include file="filenameasp" > 的普通包含文件方式稱作“傳統引用”用函數實現的動態包含文件稱作“動態引用”常見的程序如下

Function include(filename)
Dim recontentfsofaspStartaspEnd

set fso=CreateObject("ScriptingFileSystemObject")
set f=fsoOpenTextFile(servermappath(filename))
content=fReadAll
fclose
set f=nothing
set fso=nothing

set re=new RegExp
repattern="^s*="
aspEnd=
aspStart=inStr(aspEndcontent"<%")+
do while aspStart>aspEnd+
Responsewrite Mid(contentaspEndaspStartaspEnd)
aspEnd=inStr(aspStartcontent"%>")+
Execute(rereplace(Mid(contentaspStartaspEndaspStart)"ResponseWrite "))
aspStart=inStr(aspEndcontent"<%")+
loop
Responsewrite Mid(contentaspEnd)
set re=nothing
End Function
  使用范例include("youincasp")

  以上范例引自

  但這處函數在處理補包含的文件中還有包含文件時就不靈了我在以上函數的基礎上改進出來如下函數在被包含文件中還有普通的包含文件 <!#include file="filenameasp" > 也可正常運行

Function includeconvert(oRegExp strFilename strBlock)
Dim incStart incEnd match oMatches str code
用提取ASP代碼的相同方式提取出include 部分的文件名其余部分原樣輸出
code = ""
incEnd =
incStart = InStr(incEndstrBlock"<!#include ") + 要找個目標字符串<!#include 正好是個字符所以要+
Do While incStart>incEnd+ 兩個引用間距最小就是連續的><#incStart是從<!#include起數個字符所以要比前一個incEnd要至少多 得到的>incEnd+的條件
str = Mid(strBlockincEndincStartincEnd)
str = Replace(str """" """""") 把單個雙引號換成兩個雙引號
str = Replace(str VbCr "")
str = Replace(str VbLf "")
str = Replace(str VbCrLf "")
code = code & VbCrLf & "ResponseWrite """ & str & """"
incEnd=InStr(incStartstrBlock">")+
oRegExppattern="(w+)=""([^""]+)""" 匹配 file="filenameext" 或 virtual="virtualnameext"捕捉類型及文件名兩個子串
Set oMatches = oRegExpExecute(Mid(strBlockincStartincEndincStart))
Set match = oMatches() 確定只有一組捕捉時要得到這一組匹配的子串可以這樣做省去用 For Each match In oMatches …… Next
code = code & include(Mid(strFilename InStrRev(strFilename "/")) & matchSubMatches()) Mid(filename InStrRev(filename "/")) 是在被引用的子文件名有路徑時把路徑提取出來加在子文件中傳統引用的文件名前面以找到正確的打開文件路徑因為動態引用時的文件路徑是相對主文件而言的要第二個匹配子串用SubMatches()
incStart = InStr(incEndstrBlock"<!#include ")+
Loop
str = Mid(strBlockincEnd)
str = Replace(str """" """""") 把單個雙引號換成兩個雙引號
str = Replace(str VbCr "")
str = Replace(str VbLf "")
str = Replace(str VbCrLf "")
code = code & VbCrLf & "ResponseWrite """ & str & """"
includeconvert = code
End Function
Function include(filename)
Dim re content fso f aspStart aspEnd code
Set fso=CreateObject("scriptingFileSystemObject")
Set f=fsoOpenTextFile(ServerMapPath(filename))
content=fReadAll
fclose
Set f=nothing
Set fso=nothing

code = ""
aspEnd=
aspStart=InStr(aspEndcontent"<%")+
Set re=new RegExp
Do While aspStart>aspEnd+
傳統引用<!#inclde 肯定是在ASP代碼段以外的所以先轉
code = code & includeconvert (re filename Mid(contentaspEndaspStartaspEnd))
aspEnd=InStr(aspStartcontent"%>")+
repattern="^s*=" 這段正則替換原來是把 <% = str % > 換回成標准的 <%ResponseWrite str % >
code = code & VbCrLf & rereplace(Mid(contentaspStartaspEndaspStart)"ResponseWrite ") ASP塊前面再加回車換行以避免連接塊之間多個 ResponseWrite在同一行的錯誤
aspStart=InStr(aspEndcontent"<%")+
Loop
code = code & includeconvert (re filename Mid(contentaspEnd))
Set re=nothing
include = code
End Function
  方便起見以上函數最終返回的是整合了包含文件的整個 ASP 代碼使用時還要再用 Execute 執行之即使用時需要Execute(include("fileasp"))

  以上函數對被包含文件與主文件同一路徑時測試通過未對被包含文件與主文件路徑不同的情況做進一步容錯時間有限歡迎有興趣的朋友提出意見和改進


From:http://tw.wingwit.com/Article/program/net/201311/14391.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.