PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/Utilities.FileFormats/FileFormats/RSSHelper/Channel.cs

#
C# | 328 lines | 227 code | 25 blank | 76 comment | 25 complexity | 56a900aba91b90124c155c7039ca8f3f MD5 | raw file
  1. /*
  2. Copyright (c) 2012 <a href="http://www.gutgames.com">James Craig</a>
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.*/
  18. #region Usings
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Diagnostics.Contracts;
  22. using System.Globalization;
  23. using System.Text;
  24. using System.Xml;
  25. #endregion
  26. namespace Utilities.FileFormats.RSSHelper
  27. {
  28. /// <summary>
  29. /// Channel item for RSS feeds
  30. /// </summary>
  31. public class Channel
  32. {
  33. #region Constructor
  34. /// <summary>
  35. /// Constructor
  36. /// </summary>
  37. public Channel()
  38. {
  39. }
  40. /// <summary>
  41. /// Constructor
  42. /// </summary>
  43. /// <param name="Element">XML representation of the channel</param>
  44. public Channel(XmlElement Element)
  45. {
  46. Contract.Requires<ArgumentNullException>(Element!=null,"Element");
  47. Contract.Requires<ArgumentException>(Element.Name.Equals("channel", StringComparison.CurrentCultureIgnoreCase), "Element is not a channel");
  48. XmlNamespaceManager NamespaceManager = new XmlNamespaceManager(Element.OwnerDocument.NameTable);
  49. XmlNode Node = Element.SelectSingleNode("./title", NamespaceManager);
  50. if (Node != null)
  51. {
  52. Title = Node.InnerText;
  53. }
  54. Node = Element.SelectSingleNode("./link", NamespaceManager);
  55. if (Node != null)
  56. {
  57. Link = Node.InnerText;
  58. }
  59. Node = Element.SelectSingleNode("./description", NamespaceManager);
  60. if (Node != null)
  61. {
  62. Description = Node.InnerText;
  63. }
  64. Node = Element.SelectSingleNode("./copyright", NamespaceManager);
  65. if (Node != null)
  66. {
  67. Copyright = Node.InnerText;
  68. }
  69. Node = Element.SelectSingleNode("./language", NamespaceManager);
  70. if (Node != null)
  71. {
  72. Language = Node.InnerText;
  73. }
  74. Node = Element.SelectSingleNode("./webmaster", NamespaceManager);
  75. if (Node != null)
  76. {
  77. WebMaster = Node.InnerText;
  78. }
  79. Node = Element.SelectSingleNode("./pubdate", NamespaceManager);
  80. if (Node != null)
  81. {
  82. PubDate = DateTime.Parse(Node.InnerText, CultureInfo.InvariantCulture);
  83. }
  84. XmlNodeList Nodes = Element.SelectNodes("./category", NamespaceManager);
  85. foreach (XmlNode TempNode in Nodes)
  86. {
  87. Categories.Add(RSS.StripIllegalCharacters(TempNode.InnerText));
  88. }
  89. Node = Element.SelectSingleNode("./docs", NamespaceManager);
  90. if (Node != null)
  91. {
  92. Docs = Node.InnerText;
  93. }
  94. Node = Element.SelectSingleNode("./ttl", NamespaceManager);
  95. if (Node != null)
  96. {
  97. TTL = int.Parse(Node.InnerText, CultureInfo.InvariantCulture);
  98. }
  99. Node = Element.SelectSingleNode("./image/url", NamespaceManager);
  100. if (Node != null)
  101. {
  102. ImageUrl = Node.InnerText;
  103. }
  104. Nodes = Element.SelectNodes("./item", NamespaceManager);
  105. foreach (XmlNode TempNode in Nodes)
  106. {
  107. Items.Add(new Item((XmlElement)TempNode));
  108. }
  109. }
  110. #endregion
  111. #region Private Variables
  112. private string _Title = string.Empty;
  113. private string _Link = string.Empty;
  114. private string _Description = string.Empty;
  115. private string _Copyright = "Copyright " + DateTime.Now.ToString("yyyy", CultureInfo.InvariantCulture) + ". All rights reserved.";
  116. private string _Language = "en-us";
  117. private string _webMaster = string.Empty;
  118. private DateTime _pubDate = DateTime.Now;
  119. private ICollection<string> _Categories = null;
  120. private string _Docs = "http://blogs.law.harvard.edu/tech/rss";
  121. private string _Cloud = string.Empty;
  122. private int _TTL = 5;
  123. private string _ImageUrl = string.Empty;
  124. private ICollection<Item> _Items = null;
  125. private bool _Explicit = false;
  126. #endregion
  127. #region Properties
  128. /// <summary>
  129. /// Determines if this is explicit or not
  130. /// </summary>
  131. public virtual bool Explicit
  132. {
  133. get
  134. {
  135. return _Explicit;
  136. }
  137. set { _Explicit = value; }
  138. }
  139. /// <summary>
  140. /// Items for this channel
  141. /// </summary>
  142. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
  143. public virtual ICollection<Item> Items
  144. {
  145. get
  146. {
  147. if (_Items == null)
  148. {
  149. _Items = new List<Item>();
  150. }
  151. return _Items;
  152. }
  153. set { _Items = value; }
  154. }
  155. /// <summary>
  156. /// Title of the channel
  157. /// </summary>
  158. public virtual string Title
  159. {
  160. get { return _Title; }
  161. set { _Title = RSS.StripIllegalCharacters(value); }
  162. }
  163. /// <summary>
  164. /// Link to the website
  165. /// </summary>
  166. public virtual string Link
  167. {
  168. get { return _Link; }
  169. set { _Link = value; }
  170. }
  171. /// <summary>
  172. /// Description of the channel
  173. /// </summary>
  174. public virtual string Description
  175. {
  176. get { return _Description; }
  177. set { _Description = RSS.StripIllegalCharacters(value); }
  178. }
  179. /// <summary>
  180. /// Copyright info
  181. /// </summary>
  182. public virtual string Copyright
  183. {
  184. get { return _Copyright; }
  185. set { _Copyright = value; }
  186. }
  187. /// <summary>
  188. /// Language it is in
  189. /// </summary>
  190. public virtual string Language
  191. {
  192. get { return _Language; }
  193. set { _Language = value; }
  194. }
  195. /// <summary>
  196. /// Web Master info
  197. /// </summary>
  198. public virtual string WebMaster
  199. {
  200. get { return _webMaster; }
  201. set { _webMaster = value; }
  202. }
  203. /// <summary>
  204. /// Date the channel was published
  205. /// </summary>
  206. public virtual DateTime PubDate
  207. {
  208. get { return _pubDate; }
  209. set { _pubDate = value; }
  210. }
  211. /// <summary>
  212. /// Categories associated with this channel
  213. /// </summary>
  214. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
  215. public virtual ICollection<string> Categories
  216. {
  217. get
  218. {
  219. if (_Categories == null)
  220. {
  221. _Categories = new List<string>();
  222. }
  223. return _Categories;
  224. }
  225. set { _Categories = value; }
  226. }
  227. /// <summary>
  228. /// Document describing the file format
  229. /// </summary>
  230. public virtual string Docs
  231. {
  232. get { return _Docs; }
  233. set { _Docs = value; }
  234. }
  235. /// <summary>
  236. /// Cloud information
  237. /// </summary>
  238. public virtual string Cloud
  239. {
  240. get { return _Cloud; }
  241. set { _Cloud = value; }
  242. }
  243. /// <summary>
  244. /// Time to live... Amount of time between updates.
  245. /// </summary>
  246. public virtual int TTL
  247. {
  248. get { return _TTL; }
  249. set { _TTL = value; }
  250. }
  251. /// <summary>
  252. /// Url pointing to the image/logo associated with the channel
  253. /// </summary>
  254. public virtual string ImageUrl
  255. {
  256. get { return _ImageUrl; }
  257. set { _ImageUrl = value; }
  258. }
  259. #endregion
  260. #region Overridden Functions
  261. /// <summary>
  262. /// Converts the channel to a string
  263. /// </summary>
  264. /// <returns>The channel as a string</returns>
  265. public override string ToString()
  266. {
  267. StringBuilder ChannelString = new StringBuilder();
  268. ChannelString.Append("<channel>");
  269. ChannelString.Append("<title>").Append(Title).Append("</title>\r\n");
  270. ChannelString.Append("<link>").Append(Link).Append("</link>\r\n");
  271. ChannelString.Append("<atom:link xmlns:atom=\"http://www.w3.org/2005/Atom\" rel=\"self\" href=\"").Append(Link).Append("\" type=\"application/rss+xml\" />");
  272. ChannelString.Append("<description><![CDATA[").Append(Description).Append("]]></description>\r\n");
  273. ChannelString.Append("<language>").Append(Language).Append("</language>\r\n");
  274. ChannelString.Append("<copyright>").Append(Copyright).Append("</copyright>\r\n");
  275. ChannelString.Append("<webMaster>").Append(WebMaster).Append("</webMaster>\r\n");
  276. ChannelString.Append("<pubDate>").Append(PubDate.ToString("Ddd, dd MMM yyyy HH':'mm':'ss", CultureInfo.InvariantCulture)).Append("</pubDate>\r\n");
  277. ChannelString.Append("<itunes:explicit>").Append((Explicit ? "yes" : "no")).Append("</itunes:explicit>");
  278. ChannelString.Append("<itunes:subtitle>").Append(Title).Append("</itunes:subtitle>");
  279. ChannelString.Append("<itunes:summary><![CDATA[").Append(Description).Append("]]></itunes:summary>");
  280. foreach (string Category in Categories)
  281. {
  282. ChannelString.Append("<category>").Append(Category).Append("</category>\r\n");
  283. ChannelString.Append("<itunes:category text=\"").Append(Category).Append("\" />\r\n");
  284. }
  285. ChannelString.Append("<docs>").Append(Docs).Append("</docs>\r\n");
  286. ChannelString.Append("<ttl>").Append(TTL.ToString(CultureInfo.InvariantCulture)).Append("</ttl>\r\n");
  287. if (!string.IsNullOrEmpty(ImageUrl))
  288. {
  289. ChannelString.Append("<image><url>").Append(ImageUrl).Append("</url>\r\n<title>").Append(Title).Append("</title>\r\n<link>").Append(Link).Append("</link>\r\n</image>\r\n");
  290. }
  291. foreach (Item CurrentItem in Items)
  292. {
  293. ChannelString.Append(CurrentItem.ToString());
  294. }
  295. ChannelString.Append("</channel>\r\n");
  296. return ChannelString.ToString();
  297. }
  298. #endregion
  299. }
  300. }