asp的标签引擎类tagEngine.Class2009-11-04修改记录:1,增加扩展函数,2006-12-3 <% "****************************** "类名:tagEngine "名称:标签引擎 "日期:2006-11-29 "作者:西楼冷月 "网址:www.xilou.net | www.chinaCMS.org "描述:只有提取标签功能,没有解析标签功能 "版权:转载请注名出处,作者 "****************************** "最后修改:2006-12-3 "修改次数:3 "修改说明:修改正则,使匹配更精确 "目前版本:v1.1.3 "****************************** Class tagEngine Private regEx"正则对象 "定义标签规则 Private tagbegin Private tagend Private blockbegin_begin Private blockbegin_end Private blockend_begin Private blockend_end "//初始化 Private Sub Class_Initialize() "初始化标签规则 tagbegin="{" tagend="}" blockbegin_begin="<Block:" blockbegin_end =">" blockend_begin ="</Block:" blockend_end =">" "初始化正则对象 Set regEx=new RegExp regEx.IgnoreCase=True"不区分大小写 regEx.Global=True"全局匹配 End Sub Private Sub Class_Terminate() "释放对象 If IsObject(regEx) Then Set regEx=Nothing End Sub "方法:resetPattern() "参数: "返回:无返回值 "作用:重设标签规则 Public Sub resetPattern(tagbegin,tagend,_ blockbegin_begin,_ blockbegin_end,_ blockend_begin,_ blockend_end _ ) tagbegin=tagbegin tagend=tagend blockbegin_begin=blockbegin_begin blockbegin_end =blockbegin_end blockend_begin =blockend_begin blockend_end =blockend_end End Sub "方法:getBlocks(temp,blockname) "参数:temp,要匹配的内容;blockname,区块标志名称 "返回:返回集合对象(Matches) "作用:获取块标签集合 Public Function getBlocks(temp,blockname) Dim pattern pattern="("&blockbegin_begin&"[ ]*"&blockname&"[wW]*?"&blockbegin_end pattern=pattern&")([wW]*?)"&blockend_begin&"[
]*"&blockname&"[ ]*"&blockend_end "Response.Write pattern regEx.Pattern=pattern Set getBlocks=regEx.Execute(temp)"返回匹配集合 End Function "方法:getBlockByAtt(temp,attributename,attributevalue) "参数:temp,要匹配的内容;attributename,属性名称;attributevalue,属性值 "返回:返回集合对象(Matches) "作用:根据块标签里的某个属性的值取得符合的块集合 Public Function getBlockByAtt(temp,attributename,attributevalue) Dim pattern pattern="("&blockbegin_begin&"[wW]*?[
]+"&attributename pattern=pattern&"[ ]*=[ ]*"&Chr(34)&attributevalue&""&Chr(34)&"[
]*[wW]*?" pattern=pattern&blockbegin_end pattern=pattern&")([wW]*?)"&blockend_begin&"[wW]*?"&blockend_end "Response.Write pattern regEx.Pattern=pattern Set getBlockByAtt=regEx.Execute(temp)"返回匹配集合 End Function "方法:getAttValue(temp,attributename) "参数:temp,要匹配的内容;attributename,属性名称 "返回:返回集合对象(Matches) "作用:获取块标签内的属性值 Public Function getAttValue(temp,attributename) Dim pattern pattern="[
]+"&attributename&"[ ]*=[ ]*"&Chr(34)&"([^f
v"&Chr(34)&"]*?)"&Chr(34) "Response.Write pattern regEx.Pattern=pattern Set getAttValue=regEx.Execute(temp) End Function "方法:parseTag(temp,tagname,tagvalue) "参数:temp,要匹配的内容;attributename,属性名称;attributevalue,属性值 "返回:返回替换后的字符串 "作用:替换简单标签 Public Function parseTag(temp,tagname,tagvalue) Dim pattern "pattern=tagbegin&"[ ]*"&tagname&"[ ]*"&tagend pattern=tagbegin&tagname&tagend regEx.pattern=pattern parseTag=regEx.Replace(temp,tagvalue) End Function "方法:clearBlocks(temp) "参数:temp,要匹配的内容 "返回:返回清除后的字符串 "作用:清除所有块标签 Public Function clearBlocks(temp) Dim pattern pattern=blockbegin_begin&"[wW]*?"&blockbegin_end&"[wW]*?" pattern=pattern&blockend_begin&"[wW]*?"&blockend_end regEx.pattern=pattern clearBlocks=regEx.Replace(temp,"") End Function "方法:clearTags(temp) "参数:temp,要匹配的内容 "返回:返回清除后的字符串 "作用:清除所有的单标签 Public Function clearTags(temp) Dim pattern pattern=tagbegin&"[^f
v]*?"&tagend regEx.pattern=pattern clearTags=regEx.Replace(temp,"") End Function "方法:showError(errdes) "参数:errdes,错误描述 "返回:无 "作用:显示错误 Public Sub showError(errdes) Dim errinfo,cssstyle cssstyle="style="&Chr(34) cssstyle=cssstyle&"font:bold 12px 150%,"Arial";border:1px solid #CC3366;" cssstyle=cssstyle&"width:50%;color:#990066;padding:2px;"&Chr(34) errinfo=vbcrlf&"<ul "&cssstyle&"><li>"&errdes&"</li></ul>"&vbcrlf Response.Write errinfo End Sub "******************标准功能结束**************** "以下是自定义扩展功能 "方法:EXT_getSimpleBlocks(temp,blockname) "参数:temp,要匹配的内容;blockname,区块标志名称 "返回:返回集合对象(Matches) "作用:获取简单块标签集合 "例子:<Block:new id="" loop=""/> Public Function EXT_getSimpleBlocks(temp,blockname) Dim pattern Dim blockbegin,blockend "重新定义标签规则 blockbegin="<Block:" blockend ="/>" pattern=blockbegin&"[ ]*"&blockname&"[wW]*?"&blockend regEx.pattern=pattern Set EXT_getSimpleBlocks=regEx.Execute(temp) End Function "******************标准功能结束**************** "以下是自定义扩展功能 "方法:EXT_getSimpleBlocks(temp,blockname) "参数:temp,要匹配的内容;blockname,区块标志名称 "返回:返回集合对象(Matches) "作用:获取简单块标签集合 "例子:<Block:new id="" loop=""/> Public Function EXT_getSimpleBlocks(temp,blockname) Dim pattern Dim blockbegin,blockend "重新定义标签规则 blockbegin="<Block:" blockend ="/>" pattern=blockbegin&"[ ]*"&blockname&"[wW]*?"&blockend regEx.pattern=pattern Set EXT_getSimpleBlocks=regEx.Execute(temp) End Function "方法:EXT_getTEXT(path) "参数:path,要读取的文本相对或绝对路径 "返回:返回文本内容 "作用: 读取文件 "例子:c=EXT_getTEXT("tpl.htm") Public Function EXT_getTEXT(path) Dim fso,f,text On Error Resume Next Set fso = CreateObject("Scripting.FileSystemObject") Set f=Fso.OpenTextFile(path) text=f.ReadAll If Err Then Err.Clear showError "读取文件出错..." If IsObject(fso) Then Set fso=Nothing Exit Function End If If IsObject(fso) Then Set fso=Nothing EXT_getTEXT=text End Function "方法:EXT_getIncludeFile(temp) "参数:temp,要匹配的内容 "返回:返回集合对象(Matches) "作用: 解析<!--#include file="tpl.html"-->的区块 "例子:EXT_getIncludeFile(temp)(0).SubMatches(0),返回第一个匹配的文件名 Public Function EXT_getIncludeFile(temp) Dim pattern Dim blockbegin,blockend "重新定义标签规则 blockbegin="<!--#include" blockend ="-->" pattern=blockbegin&"[ ]*file[ ]*=[ ]*""([wW]*?)""[ ]*"&blockend regEx.pattern=pattern Set EXT_getIncludeFile=regEx.Execute(temp) End Function End Class %>