1
/**//****************************************************************************
2
Modification History:
3
*****************************************************************************
4
Date Author Description
5
*****************************************************************************
6
08/29/2007 brian.kuhn Created SyndicationGenerator Class
7
****************************************************************************/
8
using System;
9
using System.Collections.Generic;
10
using System.Globalization;
11
using System.IO;
12
using System.Xml;
13
14
namespace LiveBlog.Core
15
...{
16
/**//// <summary>
17
/// Generates syndication feeds for blog entities.
18
/// </summary>
19
public class SyndicationGenerator
20
...{
21
//============================================================
22
// PUBLIC/PRIVATE/PROTECTED MEMBERS
23
//============================================================
24
PRIVATE/PROTECTED/PUBLIC MEMBERS#region PRIVATE/PROTECTED/PUBLIC MEMBERS
25
/**//// <summary>
26
/// Private member to hold the name of the syndication generation utility.
27
/// </summary>
28
private const string GENERATOR_NAME = "LiveBlog Syndication Generator";
29
/**//// <summary>
30
/// Private member to hold the URI of the syndication generation utility.
31
/// </summary>
32
private static readonly Uri GENERATOR_URI = new Uri("http://iusers.com.cn/");
33
/**//// <summary>
34
/// Private member to hold the version of the syndication generation utility.
35
/// </summary>
36
private static readonly Version GENERATOR_VERSION = new Version("1.0.0.0");
37
/**//// <summary>
38
/// Private member to hold the <see cref="BlogSettings"/> to use when generating syndication results.
39
/// </summary>
40
private BlogSettings blogSettings;
41
/**//// <summary>
42
/// Private member to hold a collection of <see cref="Category"/> objects used to categorize the web log content.
43
/// </summary>
44
private List<Category> blogCategories;
45
/**//// <summary>
46
/// Private member to hold a collection of the XML namespaces that define supported syndication extensions.
47
/// </summary>
48
private static Dictionary<string, string> xmlNamespaces;
49
#endregion
50
51
//============================================================
52
// CONSTRUCTORS
53
//============================================================
54
SyndicationGenerator(BlogSettings settings, List categories)#region SyndicationGenerator(BlogSettings settings, List<Category> categories)
55
/**//// <summary>
56
/// Initializes a new instance of the <see cref="SyndicationGenerator"/> class using the supplied <see cref="BlogSettings"/> and collection of <see cref="Category"/> objects.
57
/// </summary>
58
/// <param name="settings">The <see cref="BlogSettings"/> to use when generating syndication results.</param>
59
/// <param name="categories">A collection of <see cref="Category"/> objects used to categorize the web log content.</param>
60
public SyndicationGenerator(BlogSettings settings, List<Category> categories)
61
...{
62
if (settings == null)
63
...{
64
throw new ArgumentNullException("settings");
65
}
66
if (categories == null)
67
...{
68
throw new ArgumentNullException("categories");
69
}
70
71
//------------------------------------------------------------
72
// Initialize class state
73
//------------------------------------------------------------
74
this.Settings = settings;
75
76
if (categories.Count > 0)
77
...{
78
Category[] values = new Category[categories.Count];
79
categories.CopyTo(values);
80
81
foreach (Category category in values)
82
...{
83
this.Categories.Add(category);
84
}
85
}
86
}
87
#endregion
88
89
//============================================================
90
// PUBLIC PROPERTIES
91
//============================================================
92
Categories#region Categories
93
/**//// <summary>
94
/// Gets a collection of <see cref="Category"/> objects used to categorize the web log content.
95
/// </summary>
96
/// <value>A collection of <see cref="Category"/> objects used to categorize the web log content.</value>
97
public List<Category> Categories
98
...{
99
get
100
...{
101
if (blogCategories == null)
102
...{
103
blogCategories = new List<Category>();
104
}
105
106
return blogCategories;
107
}
108
}
109
#endregion
110
111
SupportedNamespaces#region SupportedNamespaces
112
/**//// <summary>
113
/// Gets a collection of the XML namespaces used to provide support for syndication extensions.
114
/// </summary>
115
/// <value>The collection of the XML namespaces, keyed by namespace prefix, that are used to provide support for syndication extensions.</value>
116
public static Dictionary<string, string> SupportedNamespaces
117
...{
118
get
119
...{
120
if (xmlNamespaces == null)
121
...{
122
xmlNamespaces = new Dictionary<string, string>();
123
124
xmlNamespaces.Add("blogChannel", "http://backend.userland.com/blogChannelModule");
125
xmlNamespaces.Add("dc", "http://purl.org/dc/elements/1.1/");
126
127
xmlNamespaces.Add("pingback", "http://madskills.com/public/xml/rss/module/pingback/");
128
xmlNamespaces.Add("trackback", "http://madskills.com/public/xml/rss/module/trackback/");
129
xmlNamespaces.Add("wfw", "http://wellformedweb.org/CommentAPI/");
130
xmlNamespaces.Add("slash", "http://purl.org/rss/1.0/modules/slash/");
131
xmlNamespaces.Add("geo", "http://www.w3.org/2003/01/geo/wgs84_pos#");
132
}
133
134
return xmlNamespaces;
135
}
136
}
137
#endregion
138
139
Settings#region Settings
140
/**//// <summary>
141
/// Gets or sets the <see cref="BlogSettings"/> used when generating syndication results.
142
/// </summary>
143
/// <value>The <see cref="BlogSettings"/> used when generating syndication results.</value>
144
/// <exception cref="ArgumentNullException">The <paramref name="value"/> is a null reference (Nothing in Visual Basic).</exception>
145
public BlogSettings Settings
146
...{
147
get
148
...{
149
return blogSettings;
150
}
151
152
protected set
153
...{
154
if (value == null)
155
...{
156
throw new ArgumentNullException("value");
157
}
158
else
159
...{
160
blogSettings = value;
161
}
162
}
163
}
164
#endregion
165
166
//============================================================
167
// STATIC UTILITY METHODS
168
//============================================================
169
FormatW3cOffset(TimeSpan offset, string separator)#region FormatW3cOffset(TimeSpan offset, string separator)
170
/**//// <summary>
171
/// Converts the value of the specified <see cref="TimeSpan"/> to its equivalent string representation.
172
/// </summary>
173
/// <param name="offset">The <see cref="TimeSpan"/> to convert.</param>
174
/// <param name="separator">Separator used to deliminate hours and minutes.</param>
175
/// <returns>A string representation of the TimeSpan.</returns>
176
private static string FormatW3cOffset(TimeSpan offset, string separator)
177
...{
178
string formattedOffset = String.Empty;
179
180
if (offset >= TimeSpan.Zero)
181
...{
182
formattedOffset = "+";
183
}
184
185
return String.Concat(formattedOffset, offset.Hours.ToString("00", CultureInfo.InvariantCulture), separator, offset.Minutes.ToString("00", CultureInfo.InvariantCulture));
186
}
187
#endregion
188
189
GetPermaLink(IPublishable publishable)#region GetPermaLink(IPublishable publishable)
190
/**//// <summary>
191
/// Creates a <see cref="Uri"/> that represents the peramlink for the supplied <see cref="IPublishable"/>.
192
/// </summary>
193
/// <param name="publishable">The <see cref="IPublishable"/> used to generate the permalink for.</param>
194
/// <returns>A <see cref="Uri"/> that represents the peramlink for the supplied <see cref="IPublishable"/>.</returns>
195
/// <exception cref="ArgumentNullException">The <paramref name="publishable"/> is a null reference (Nothing in Visual Basic).</exception>
196
private static Uri GetPermaLink(IPublishable publishable)
197
...{
198
Post post = publishable as Post;
199
if (post != null)
200
...{
201
return post.PermaLink;
202
}
203
204
return Utils.ConvertToAbsolute(publishable.RelativeLink);
205
}
206
#endregion
207
208
ToRfc822DateTime(DateTime dateTime)#region ToRfc822DateTime(DateTime dateTime)
209
/**//// <summary>
210
/// Converts the supplied <see cref="DateTime"/> to its equivalent <a href="http://asg.web.cmu.edu/rfc/rfc822.html">RFC-822 DateTime</a> string representation.
211
/// </summary>
212
/// <param name="dateTime">The <see cref="DateTime"/> to convert.</param>
213
/// <returns>The equivalent <a href="http://asg.web.cmu.edu/rfc/rfc822.html">RFC-822 DateTime</a> string representation.</returns>
214
public static string ToRfc822DateTime(DateTime dateTime)
215
...{
216
int offset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now.AddHours(BlogSettings.Instance.Timezone)).Hours;
217
string timeZone = "+" + offset.ToString(NumberFormatInfo.InvariantInfo).PadLeft(2, '0');
218
219
//------------------------------------------------------------
220
// Adjust time zone based on offset
221
//------------------------------------------------------------
222
if (offset < 0)
223
...{
224
int i = offset * -1;
225
timeZone = "-" + i.ToString(NumberFormatInfo.InvariantInfo).PadLeft(2, '0');
226
227
}
228
229
return dateTime.ToString("ddd, dd MMM yyyy HH:mm:ss " + timeZone.PadRight(5, '0'), DateTimeFormatInfo.InvariantInfo);
230
}
231
#endregion
232
233
ToW3CDateTime(DateTime utcDateTime)#region ToW3CDateTime(DateTime utcDateTime)
234
/**//// <summary>
235
/// Converts the supplied <see cref="DateTime"/> to its equivalent <a href="http://www.w3.org/TR/NOTE-datetime">W3C DateTime</a> string representation.
236
/// </summary>
237
/// <param name="utcDateTime">The Coordinated Universal Time (UTC) <see cref="DateTime"/> to convert.</param>
238
/// <returns>The equivalent <a href="http://www.w3.org/TR/NOTE-datetime">W3C DateTime</a> string representation.</returns>
239
private static string ToW3CDateTime(DateTime utcDateTime)
240
...{
241
TimeSpan utcOffset = TimeSpan.Zero;
242
return (utcDateTime + utcOffset).ToString("yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture) + SyndicationGenerator.FormatW3cOffset(utcOffset, ":");
243
}
244
#endregion
245
246
