«
对ASP 动态包含文件方法的改进

时间:2008-5-31    作者:Deri    分类: 分享


   <p>  ASP 本身不支持动态包含文件,现在的动态包含是通过 FSO 把被包含的文件合并到主文件里再运行。以下也有把形如 <!--#include file="filename.asp" --> 的普通包含文件方式称作“传统引用”,用函数实现的动态包含文件称作“动态引用”。常见的程序如下:</p><code>Function include(filename)<br />Dim re,content,fso,f,aspStart,aspEnd<br />set fso=CreateObject("Scripting.FileSystemObject")<br />set f=fso.OpenTextFile(server.mappath(filename))<br />content=f.ReadAll<br />f.close<br />set f=nothing<br />set fso=nothing<br />set re=new RegExp<br />re.pattern="^s*="<br />aspEnd=1<br />aspStart=inStr(aspEnd,content,"<%")+2<br />do while aspStart>aspEnd+1<br />Response.write Mid(content,aspEnd,aspStart-aspEnd-2)<br />aspEnd=inStr(aspStart,content,"%>")+2<br />Execute(re.replace(Mid(content,aspStart,aspEnd-aspStart-2),"Response.Write "))<br />aspStart=inStr(aspEnd,content,"<%")+2<br />loop<br />Response.write Mid(content,aspEnd)<br />set re=nothing<br />End Function</code></p><p>  使用范例:include("youinc.asp")</p><p>  以上范例引自 http://www.blueidea.com/tech/program/2003/101.asp</p><p>  但这处函数在处理补包含的文件中还有包含文件时就不灵了。我在以上函数的基础上改进出来如下函数,在被包含文件中还有普通的包含文件 <!--#include file="filename.asp" --> 也可正常运行。</p><code>Function includeconvert(oRegExp, strFilename, strBlock)<br />Dim incStart, incEnd, match, oMatches, str, code<br />'用提取ASP代码的相同方式提取出include 部分的文件名,其余部分原样输出<br />code = ""<br />incEnd = 1<br />incStart = InStr(incEnd,strBlock,"<!--#include ") + 13 '要找个目标字符串<!--#include 正好是13个字符,所以要+13<br />Do While incStart>incEnd+12 '两个引用间距最小就是连续的--><--#,incStart是从<!--#include起数13个字符,所以要比前一个incEnd要至少多 13-1 得到的>incEnd+12的条件<br />str = Mid(strBlock,incEnd,incStart-incEnd-13)<br />str = Replace(str, """", """""") '把单个双引号换成两个双引号<br />str = Replace(str, VbCr, "")<br />str = Replace(str, VbLf, "")<br />str = Replace(str, VbCrLf, "")<br />code = code & VbCrLf & "Response.Write """ & str & """"<br />incEnd=InStr(incStart,strBlock,"-->")+3<br />oRegExp.pattern="(w+)=""([^""]+)""" '匹配 file="filename.ext" 或 virtual="virtualname.ext",捕捉类型及文件名两个子串<br />Set oMatches = oRegExp.Execute(Mid(strBlock,incStart,incEnd-incStart-3))<br />Set match = oMatches(0) '确定只有一组捕捉时,要得到这一组匹配的子串,可以这样做,省去用 For Each match In oMatches …… Next<br />code = code & include(Mid(strFilename, 1, InStrRev(strFilename, "/")) & match.SubMatches(1)) 'Mid(filename, 1, InStrRev(filename, "/")) 是在被引用的子文件名有路径时,把路径提取出来,加在子文件中传统引用的文件名前面,以找到正确的打开文件路径,因为动态引用时的文件路径是相对主文件而言的。要第二个匹配子串用SubMatches(1)<br />incStart = InStr(incEnd,strBlock,"<!--#include ")+13<br />Loop<br />str = Mid(strBlock,incEnd)<br />str = Replace(str, """", """""") '把单个双引号换成两个双引号<br />str = Replace(str, VbCr, "")<br />str = Replace(str, VbLf, "")<br />str = Replace(str, VbCrLf, "")<br />code = code & VbCrLf & "Response.Write """ & str & """"<br />includeconvert = code<br />End Function<br />Function include(filename)<br />Dim re, content, fso, f, aspStart, aspEnd, code<br />Set fso=CreateObject("scripting.FileSystemObject")<br />Set f=fso.OpenTextFile(Server.MapPath(filename))<br />content=f.ReadAll<br />f.close<br />Set f=nothing<br />Set fso=nothing<br />code = ""<br />aspEnd=1<br />aspStart=InStr(aspEnd,content,"<%")+2<br />Set re=new RegExp<br />Do While aspStart>aspEnd+1<br />'传统引用<!--#inclde 肯定是在ASP代码段以外的,所以先转。<br />code = code & includeconvert (re, filename, Mid(content,aspEnd,aspStart-aspEnd-2))<br />aspEnd=InStr(aspStart,content,"%>")+2<br />re.pattern="^s*=" '这段正则替换原来是把 <% = str % > 换回成标准的 <%Response.Write str % ><br />code = code & VbCrLf & re.replace(Mid(content,aspStart,aspEnd-aspStart-2),"Response.Write ") 'ASP块前面再加回车换行,以避免连接块之间多个 Response.Write在同一行的错误<br />aspStart=InStr(aspEnd,content,"<%")+2<br />Loop<br />code = code & includeconvert (re, filename, Mid(content,aspEnd))<br />Set re=nothing<br />include = code<br />End Function</code></p>
<p> </p>

   <p>  方便起见,以上函数最终返回的是整合了包含文件的整个 ASP 代码,使用时还要再用 Execute 执行之,即使用时需要:Execute(include("file.asp"))。</p><p>  以上函数对被包含文件与主文件同一路径时测试通过,未对被包含文件与主文件路径不同的情况做进一步容错,时间有限,欢迎有兴趣的朋友提出意见和改进。</p></p>