<%
————————————————————————————
类名:registryobject 网页信息注册表类
作者:张少棠 (tonton)
邮箱:tonton@yeah.net
时间:2004年8月1日
说明:模仿windows中注册表的方式,结合xml与asp技术,给网站添加一个专用的“注册表”类。
网址:http://blog.csdn.net/tonton/archive/2004/08/01/58198.aspx
版权:读者可以把本程序使用于任何用途,如要刊登、转载,请保留以上版权信息!
————————————————————————————
const node_element = 1
const node_text = 3
class registryobject
private mdom
public path 注册表路径
public encoding 注册表编码
public defaultvalue 在读取键值时,如果子键不存在而返回的默认值
private sub class_initialize()
defaultvalue = empty
encoding = "gb2312"
set mdom = nothing
end sub
private sub class_terminate()
set mdom = nothing
end sub
public function newdom()
set newdom = server.createobject("microsoft.xmldom")
end function
private property get dom()
dim nde
if mdom is nothing then
set mdom = newdom()
mdom.async = false
mdom.load path
if mdom.parseerror.errorcode = &h800c0006 then 文件未找到
initial
elseif mdom.parseerror <> 0 then
exit property
end if
end if
set dom = mdom
end property
public function initial()
dim nde
set mdom = newdom()
with mdom
set nde = .createprocessinginstruction("xml", "version=1.0 encoding=" & encoding & "")
.appendchild (nde)
set nde = .createelement("registry")
.appendchild (nde)
end with
mdom.save path
end function
public function createchannel(channel)
with dom
set createchannel = .selectsinglenode("//" & channel)
if createchannel is nothing then
set createchannel = .createnode(node_element, channel, "")
.documentelement.appendchild (createchannel)
.save path
end if
end with
end function
public function getallchannels()
dim result
dim nde, i
with dom
with .documentelement.childnodes
if .length = 0 then
result = split("", 0)
else
redim result(.length – 1)
for i = 0 to .length – 1
result(i) = .item(i).nodename
next
end if
end with
end with
getallchannels = result
end function
public function deletechannel(channel)
dim selection
with dom
set selection = .selectnodes("//" & channel)
selection.removeall
.save path
end with
end function
public function createsection(channel, section)
dim nde
with dom
set nde = .selectsinglenode("//" & channel & "/" & section)
if nde is nothing then
set nde = .selectsinglenode("//" & channel)
if nde is nothing then
set nde = .createnode(node_element, channel, "")
.documentelement.appendchild (nde)
end if
set createsection = .createnode(node_element, section, "")
nde.appendchild (createsection)
.save path
end if
end with
end function
public function getallsections(channel)
dim result
dim nde, i
with dom
set nde = .selectsinglenode("//" & channel)
if not nde is nothing then
with nde.childnodes
if .length = 0 then
result = split("", 0)
else
redim result(.length – 1)
for i = 0 to .length – 1
result(i) = .item(i).nodename
next
end if
end with
else
result = split("", 0)
end if
end with
getallsections = result
end function
public function clearallsections(channel)
dim selection
with dom
set selection = .selectnodes("//" & channel)
selection.removeall
.save path
end with
end function
public function deletesection(channel, section)
dim selection
with dom
set selection = .selectnodes("//" & channel & "/" & section)
selection.removeall
.save path
end with
end function
public function getallkeys(channel, section)
dim result
dim nde, i
with dom
set nde = .selectsinglenode("//" & channel & "/" & section)
if not nde is nothing then
with nde.childnodes
if .length = 0 then
result = split("", 0)
else
redim result(.length – 1)
for i = 0 to .length – 1
result(i) = .item(i).nodename
next
end if
end with
else
result=split("", 0)
end if
end with
getallkeys = result
end function
public sub savevalue(channel, section, key, value)
dim nde, nde2, itemnode
with dom
set itemnode = .selectsinglenode("//" & channel & "/" & section & "/" & key)
if itemnode is nothing then
set nde = .selectsinglenode("//" & channel)
if nde is nothing then
set nde = .createnode(node_element, channel, "")
.documentelement.appendchild (nde)
end if
set nde2 = nde.selectsinglenode("//" & section)
if nde2 is nothing then
set nde2 = .createnode(node_element, section, "")
nde.appendchild (nde2)
end if
set itemnode = .createnode(node_element, key, "")
nde2.appendchild (itemnode)
end if
itemnode.text = value
.save path
end with
end sub
public function readvalue(channel, section, key)
dim nde
with dom
set nde = .selectsinglenode("//" & channel & "/" & section & "/" & key)
if nde is nothing then
readvalue = defaultvalue
else
readvalue = nde.text
end if
end with
end function
public function getallvalues(channel, section)
dim result
dim nde, i
with dom
set nde = .selectsinglenode("//" & channel & "/" & section)
if not nde is nothing then
with nde.childnodes
if .length = 0 then
result = split("", 0)
else
redim result(.length – 1)
for i = 0 to .length – 1
set result(i)=new registrykey
result(i).name=.item(i).nodename
result(i).value=.item(i).text
next
end if
end with
else
result = split("", 0)
end if
end with
getallvalues = result
end function
end class
class registrykey
public name
public value
end class
%>
