<%

Class Rss2

	private data_source_type '数据源类型, 1 数组, 2 RecordSet, 默认2
	private data_source      '数据源
	
	private s_encoding       'Xml 的Encoding, 默认UTF-8

	'---------------------------------
	' 以下是 channel 的标签
	'---------------------------------
    private s_title          '必需的, 定义频道的标题。
    private s_link           '必需的, 定义指向频道的超链接。
    private s_description    '必需的, 描述频道。
    private s_category       '可选的, 为 feed 定义所属的一个或多个种类。
    private s_cloud          '可选的, 注册进程，以获得 feed 更新的立即通知。
    private s_copyright      '可选的, 告知版权资料。
    private s_docs           '可选的, 规定指向当前 RSS 文件所用格式说明的 URL。
    private s_generator      '可选的, 指定用于生成 feed 的程序。
    private s_image          '可选的, 在聚合器呈现某个 feed 时，显示一个图像。
    private s_language       '可选的, 规定编写 feed 所用的语言。
    private s_lastBuildDate  '可选的, 定义 feed 内容的最后修改日期。
    private s_managingEditor '可选的, 定义 feed 内容编辑的电子邮件地址。
    private s_pubDate        '可选的, 为 feed 的内容定义最后发布日期。
    private s_rating         '可选的, feed 的 PICS 级别。
    private s_skipDays       '可选的, 规定忽略 feed 更新的天。
    private s_skipHours      '可选的, 规定忽略 feed 更新的小时。
    private s_textInput      '可选的, 规定应当与 feed 一同显示的文本输入域。
    private s_ttl            '可选的, 指定从 feed 源更新此 feed 之前，feed 可被缓存的分钟数。
    private s_webMaster      '可选的, 定义此 feed 的 web 管理员的电子邮件地址。
	
	'---------------------------------
	' 以下是 item 与数据源的关联
	'---------------------------------
    private item_title       '必需的, 定义此项目的标题。
    private item_link        '必需的, 定义指向此项目的超链接。
    private item_description '必需的, 描述此项目。
    private item_author      '可选的, 规定项目作者的电子邮件地址。
    private item_category    '可选的, 定义项目所属的一个或多个类别。
    private item_comments    '可选的, 允许项目连接到有关此项目的注释（文件）。
    private item_enclosure   '可选的, 允许将一个媒体文件导入一个项中。
    private item_guid        '可选的, 为项目定义一个唯一的标识符。
    private item_pubDate     '可选的, 定义此项目的最后发布日期。
    private item_source      '可选的, 为此项目指定一个第三方来源。
	
	'---------------------------------
	' 属性 RssVersion 只读 Rss的版本号
	'---------------------------------
	Public Property Get Version
		Version = "2.0"
	End Property
	
	'---------------------------------
	' 初始化
	'---------------------------------
	Private Sub class_initialize()
		data_source_type = 2
		data_source      = 0
		s_encoding       = "UTF-8"
		
		s_title          = ""
		s_link           = ""
		s_description    = ""
		s_category       = ""
		s_cloud          = ""
		s_copyright      = ""
		s_docs           = ""
		s_generator      = ""
		s_image          = ""
		s_language       = ""
		s_lastBuildDate  = ""
		s_managingEditor = ""
		s_pubDate        = ""
		s_rating         = ""
		s_skipDays       = ""
		s_skipHours      = ""
		s_textInput      = ""
		s_ttl            = ""
		s_webMaster      = ""
		
		item_title       = ""
		item_link        = ""
		item_description = ""
		item_author      = ""
		item_category    = ""
		item_comments    = ""
		item_enclosure   = ""
		item_guid        = ""
		item_pubDate     = ""
		item_source      = ""
	End Sub
	
	'---------------------------------
	' 析构
	'---------------------------------
	Private Sub class_terminate()
		if data_source_type = 2 then
			set data_source = Nothing
		end if
	End Sub
	
	'---------------------------------
	' 属性 Encoding 只写 XML字符编码
	'---------------------------------
	Public Property Let Encoding( Byval val )
		s_encoding = val
	End Property
	
	'---------------------------------
	' 属性 DataSourceType 只写 数据源类型
	'---------------------------------
	Public Property Let DataSourceType( Byval val )
		if val = 1 then
			data_source_type = 1
		else
			data_source_type = 2
		end if
	End Property
	
	'---------------------------------
	' 属性 DataSource 只写 数据源
	'---------------------------------
	Public Property Set DataSource( Byval val )
		set data_source = val
	End Property
	Public Property Let DataSource( Byval val )
		data_source = val
	End Property
	
	'---------------------------------
	' 属性 Title 只写 channel 的 title
	'---------------------------------
	Public Property Let Title( Byval val )
		s_title = val
	End Property
	
	'---------------------------------
	' 属性 Link 只写 channel 的 link
	'---------------------------------
	Public Property Let Link( Byval val )
		s_link = val
	End Property
	
	'---------------------------------
	' 属性 Description 只写 channel 的 description
	'---------------------------------
	Public Property Let Description( Byval val )
		s_description = val
	End Property
	
	'---------------------------------
	' 属性 ItemTitle 只写 关联 item 的 title 的数据源键名
	'---------------------------------
	Public Property Let ItemTitle( Byval val )
		item_title = val
	End Property
	
	'---------------------------------
	' 属性 ItemLink 只写 关联 item 的 link 的数据源键名
	'---------------------------------
	Public Property Let ItemLink( Byval val )
		item_link = val
	End Property
	
	'---------------------------------
	' 属性 ItemDescription 只写 关联 item 的 description 的数据源键名
	'---------------------------------
	Public Property Let ItemDescription( Byval val )
		item_description = val
	End Property

	
	
	'---------------------------------
	' 以下是 channel 的可选属性
	'---------------------------------
	Public Property Let Category( Byval val )
		s_category = val
	End Property
	Public Property Let Cloud( Byval val )
		s_cloud = val
	End Property
	Public Property Let Copyright( Byval val )
		s_copyright = val
	End Property
	Public Property Let Docs( Byval val )
		s_docs = val
	End Property
	Public Property Let Generator( Byval val )
		s_generator = val
	End Property
	Public Property Let Image( Byval val )
		s_image = val
	End Property
	Public Property Let Language( Byval val )
		s_language = val
	End Property
	Public Property Let LastBuildDate( Byval val )
		s_lastBuildDate = val
	End Property
	Public Property Let ManagingEditor( Byval val )
		s_managingEditor = val
	End Property
	Public Property Let PubDate( Byval val )
		s_pubDate = val
	End Property
	Public Property Let Rating( Byval val )
		s_rating = val
	End Property
	Public Property Let SkipDays( Byval val )
		s_skipDays = val
	End Property
	Public Property Let SkipHours( Byval val )
		s_skipHours = val
	End Property
	Public Property Let TextInput( Byval val )
		s_textInput = val
	End Property
	Public Property Let Ttl( Byval val )
		s_ttl = val
	End Property
	Public Property Let WebMaster( Byval val )
		s_webMaster = val
	End Property
	
	'---------------------------------
	' 以下是 关联 item 可选的数据源键名
	'---------------------------------
	Public Property Let ItemAuthor    ( Byval val )
		item_author = val
	End Property
	Public Property Let ItemCategory  ( Byval val )
		item_category = val
	End Property
	Public Property Let ItemComments  ( Byval val )
		item_comments = val
	End Property
	Public Property Let ItemEnclosure ( Byval val )
		item_enclosure = val
	End Property
	Public Property Let ItemGuid      ( Byval val )
		item_guid = val
	End Property
	Public Property Let ItemPubDate   ( Byval val )
		item_pubdate = val
	End Property
    Public Property Let ItemSource    ( Byval val )
		item_source = val
	End Property
	
	'---------------------------------
	' 方法 SaveAs
	'---------------------------------
	Public Function SaveAs( Byval path )
		Dim fso, f
		Set fso = Server.CreateObject( "Scripting.FileSystemObject" )
		set f   = fso.CreateTextFile( Server.MapPath( path ) )
		f.write( ToString() )
		f.close
		set f   = Nothing
		set fso = Nothing
		SaveAs = path
	End Function
	
	'---------------------------------
	' 方法 ToString
	'---------------------------------
	Public Function ToString()
		Dim result, items
		
		if data_source_type = 1 then
			items = ArrayToString()
		elseif data_source_type = 2 then
			items = RecordsetToString()
		else
			items = ""
		end if
		
		result = "<?xml version=""1.0"" encoding=""" & s_encoding & """ ?>"
		result = result & "<rss version=""2.0""><channel>"
		result = result & "<title>" & s_title & "</title>"
		result = result & "<link>" & s_link & "</link>"
		result = result & "<description>" & s_description & "</description>"
		if s_category       <> "" then result = result & "<category>"       & s_category       & "</category>"
		if s_cloud          <> "" then result = result & "<cloud>"          & s_cloud          & "</cloud>"
		if s_copyright      <> "" then result = result & "<copyright>"      & s_copyright      & "</copyright>"
		if s_docs           <> "" then result = result & "<docs>"           & s_docs           & "</docs>"
		if s_generator      <> "" then result = result & "<generator>"      & s_generator      & "</generator>"
		if s_image          <> "" then result = result & "<image>"          & s_image          & "</image>"
		if s_language       <> "" then result = result & "<language>"       & s_language       & "</language>"
		if s_lastBuildDate  <> "" then result = result & "<lastBuildDate>"  & s_lastBuildDate  & "</lastBuildDate>"
		if s_managingEditor <> "" then result = result & "<managingEditor>" & s_managingEditor & "</managingEditor>"
		if s_pubDate        <> "" then result = result & "<pubDate>"        & s_pubDate        & "</pubDate>"
		if s_rating         <> "" then result = result & "<rating>"         & s_rating         & "</rating>"
		if s_skipDays       <> "" then result = result & "<skipDays>"       & s_skipDays       & "</skipDays>"
		if s_skipHours      <> "" then result = result & "<skipHours>"      & s_skipHours      & "</skipHours>"
		if s_textInput      <> "" then result = result & "<textInput>"      & s_textInput      & "</textInput>"
		if s_ttl            <> "" then result = result & "<ttl>"            & s_ttl            & "</ttl>"
		if s_webMaster      <> "" then result = result & "<webMaster>"      & s_webMaster      & "</webMaster>"
		result = result & items & "</channel></rss>"
		
		ToString = result
	End Function
	
	Private Function ArrayToString()
		Dim result, iCount, i
		result = ""
		iCount = UBound( data_source, 1 )
		
		for i = 0 to iCount
			result = result & "<item>"
			result = result & "<title>" & data_source(i, item_title) & "</title>"
			result = result & "<link>"  & data_source(i, item_link) & "</link>"
			result = result & "<description>" & data_source(i, item_description) & "</description>"
			if item_author    <> "" then result = result & "<author>"    & data_source(i, item_author)    & "</author>"   
			if item_category  <> "" then result = result & "<category>"  & data_source(i, item_category)  & "</category>" 
			if item_comments  <> "" then result = result & "<comments>"  & data_source(i, item_comments)  & "</comments>" 
			if item_enclosure <> "" then result = result & "<enclosure>" & data_source(i, item_enclosure) & "</enclosure>"
			if item_guid      <> "" then result = result & "<guid>"      & data_source(i, item_guid)      & "</guid>"     
			if item_pubDate   <> "" then result = result & "<pubDate>"   & data_source(i, item_pubDate)   & "</pubDate>"  
			if item_source    <> "" then result = result & "<source>"    & data_source(i, item_source)    & "</source>"   
			result = result & "</item>"
		next
		
		ArrayToString = result
	End Function
	
	Private Function RecordsetToString()
		Dim result, linkfield, pos1, pos2
		result = ""
		linkfield = ""
		pos1 = instr( item_link, "{$" )
		pos2 = 0
		if pos1 > 0 then
			pos2 = instr( pos1, item_link, "}" )
			if pos2 - pos1 - 2 > 0 then
				linkfield = mid( item_link, pos1 + 2, pos2 - pos1 - 2 )
			else
				linkfield = item_link
				item_link = item_link & "}"
			end if
		end if
		if linkfield = "" then
			linkfield = item_link
			item_link = "{$" & item_link & "}"
		end if
		
		while not data_source.eof
			result = result & "<item>"
			result = result & "<title>" & data_source( item_title ) & "</title>"
			result = result & "<link>"  & replace( item_link, "{$" & linkfield & "}", data_source( linkfield ) ) & "</link>"
			result = result & "<description>" & data_source( item_description ) & "</description>"
			if item_author    <> "" then result = result & "<author>"    & data_source(item_author)    & "</author>"   
			if item_category  <> "" then result = result & "<category>"  & data_source(item_category)  & "</category>" 
			if item_comments  <> "" then result = result & "<comments>"  & data_source(item_comments)  & "</comments>" 
			if item_enclosure <> "" then result = result & "<enclosure>" & data_source(item_enclosure) & "</enclosure>"
			if item_guid      <> "" then result = result & "<guid>"      & data_source(item_guid)      & "</guid>"     
			if item_pubDate   <> "" then result = result & "<pubDate>"   & data_source(item_pubDate)   & "</pubDate>"  
			if item_source    <> "" then result = result & "<source>"    & data_source(item_source)    & "</source>"   
			result = result & "</item>"
			data_source.movenext
		wend
		
		RecordsetToString = result
	End Function
	
End Class

%>