📄 baseatomwriter.cs
字号:
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
// Copyright (c) Telligent Systems Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Collections;
/**************
* This class needs to be re-evaluated
* 12/5/2004
***********************/
namespace CommunityServer.Components
{
/// <summary>
/// Base class for creating Atom feeds
/// </summary>
public abstract class BaseAtomWriter : BaseSyndicationWriter
{
//Guid we append to the link. (ScottW - Move this to base)
private string baseGuid = null;
/// <summary>
/// Constructs a writer for an RSS feed.
/// </summary>
/// <param name="posts">The entries to write as part of the feed.</param>
public BaseAtomWriter(ArrayList posts, Section s, string baseUrl) : base(posts,s, baseUrl)
{
baseGuid = CSContext.Current.SiteSettings.SiteKey.ToString();
}
/// <summary>
/// Usually used to fully qualify Urls
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
protected virtual string FormatUrl(string url)
{
return string.Concat(BaseUrl,url);
}
/// <summary>
/// Output the feed base element.
/// </summary>
protected virtual void StartDocument()
{
this.WriteStartElement("feed");
this.WriteAttributeString("version", "0.3");
}
/// <summary>
/// Default language posts are written in
/// </summary>
protected virtual string Language
{
get { return "en-US";}
}
/// <summary>
/// When was this feed last updated
/// </summary>
protected abstract DateTime Modified {get;}
/// <summary>
/// Link to the home page which corresponds to this Feed
/// </summary>
protected abstract string HomeLink {get;}
/// <summary>
/// Output the Atom namespaces used in the feed.
/// </summary>
protected virtual void SetNamespaces()
{
this.WriteAttributeString("xmlns:dc", "http://purl.org/dc/elements/1.1/");
this.WriteAttributeString("xmlns:wfw", "http://wellformedweb.org/CommentAPI/");
this.WriteAttributeString("xmlns:slash", "http://purl.org/rss/1.0/modules/slash/");
this.WriteAttributeString("xmlns", "http://purl.org/atom/ns#");
this.WriteAttributeString("xml:lang", Language);
}
/// <summary>
/// Writes the channel information usualling using BuildChannel
/// </summary>
protected abstract void WriteChannel();
/// <summary>
/// Called by the base class to create the feed once the
/// metadata has been retrieved
/// </summary>
protected override void Build()
{
StartDocument();
SetNamespaces();
WriteChannel();
foreach(Post post in this.posts)
{
StartEntry();
WriteEntry(post);
EndEntry();
}
EndDocument();
}
/// <summary>
/// Actually build the header contents of the feed.
/// </summary>
/// <param name="title">The title element of the feed.</param>
/// <param name="link">The link element of the feed.</param>
/// <param name="description">The description element of the feed.</param>
protected void BuildChannel(string title, string link, string description)
{
this.WriteElementString("title",title);
this.WriteStartElement("link");
this.WriteAttributeString("rel", "alternate");
this.WriteAttributeString("type", "text/html");
this.WriteAttributeString("href", link);
this.WriteEndElement();
this.WriteStartElement("tagline");
this.WriteAttributeString("type", "text/html");
this.WriteString(description);
this.WriteEndElement();
this.WriteElementString("id",link);
this.WriteStartElement("author");
//this.WriteElementString("name", config.Author);
this.WriteElementString("url", HomeLink);
this.WriteEndElement();
this.WriteStartElement("generator");
this.WriteAttributeString("url", "http://communityserver.org");
this.WriteAttributeString("version", SiteStatistics.CommunityServerVersion.ToString());
this.WriteString("Community Server");
this.WriteEndElement();
this.WriteElementString("modified", W3UTCZ(Modified));
}
/// <summary>
/// starts a new post
/// </summary>
protected virtual void StartEntry()
{
this.WriteStartElement("entry");
}
/// <summary>
/// Gets the link for an individual post. This will different by application
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
protected abstract string BuildLink(Post p);
/// <summary>
/// Different application may be able to support the modified and issued elements
/// </summary>
/// <param name="p"></param>
protected virtual void DateElements(Post p)
{
this.WriteElementString("created", W3UTCZ(p.PostDate.ToUniversalTime()));
}
/// <summary>
/// Different applications may want to filter/update/modify the data syndicated
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
protected virtual string PostBody(Post p)
{
return p.FormattedBody;
}
/// <summary>
/// Enables new elements to be created
/// </summary>
/// <param name="p"></param>
protected virtual void ExtendendElements(Post p)
{}
/// <summary>
/// Writes a single post out to the feed
/// </summary>
/// <param name="p"></param>
protected virtual void WriteEntry(Post p)
{
this.WriteElementString("title",p.Subject);
this.WriteStartElement("link");
this.WriteAttributeString("rel", "alternate");
this.WriteAttributeString("type", "text/html");
this.WriteAttributeString("href", BuildLink(p));
this.WriteEndElement();
this.WriteElementString("id",string.Format("{0}:{1}",baseGuid,p.PostID));
DateElements(p);
this.WriteStartElement("content");
this.WriteAttributeString("type","text/html");
this.WriteAttributeString("mode","escaped");
this.WriteString(PostBody(p));
this.WriteEndElement();
this.WriteElementString("slash:comments", p.Replies.ToString());
ExtendendElements(p);
}
/// <summary>
/// Output the end tag for the entry.
/// </summary>
protected void EndEntry()
{
this.WriteEndElement();
}
/// <summary>
/// Output the end tag for the feed.
/// </summary>
protected void EndDocument()
{
this.WriteEndElement();
}
#region TimeHelpers
/// <summary>
/// Format a timezone for the feed.
/// </summary>
/// <param name="tz">The timezone.</param>
/// <returns></returns>
private static string TimeZone(double tz)
{
if (tz < 0)
{
return tz.ToString("00") + ":00";
}
else
{
return "+" + tz.ToString("00") + ":00";
}
}
/// <summary>
/// Format a date & timezone pair.
/// </summary>
/// <param name="dt">The date/time.</param>
/// <param name="tz">The timezone.</param>
/// <returns></returns>
private static string W3UTC(DateTime dt, string tz)
{
return dt.ToString("yyyy-MM-ddTHH:mm:ss") + tz;
}
/// <summary>
/// Format a date.
/// </summary>
/// <param name="dt">The date/time.</param>
/// <returns></returns>
private static string W3UTCZ(DateTime dt)
{
return dt.ToString("yyyy-MM-ddTHH:mm:ssZ");
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -