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

/YUI/YUI.Core/Utilities/RssBase.cs

http://yuicore.googlecode.com/
C# | 356 lines | 297 code | 28 blank | 31 comment | 1 complexity | 3abcb75e12474d7794a11c302ce46fd0 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Xml;
  5. namespace aNd1coder.Common
  6. {
  7. /// <summary>
  8. /// Rss??
  9. /// </summary>
  10. public class RssBase
  11. {
  12. #region RssBase constructor
  13. public RssBase(XmlTextWriter rssWriter)
  14. {
  15. this.rssWriter = rssWriter;
  16. }
  17. public RssBase(XmlTextWriter rssWriter, bool hasMedia, IList<ItemInfo> items)
  18. {
  19. this.rssWriter = rssWriter;
  20. this.hasMedia = hasMedia;
  21. this.items = items;
  22. }
  23. #endregion
  24. [Serializable]
  25. public sealed class ItemInfo
  26. {
  27. #region ItemInfo constructor
  28. public ItemInfo() { }
  29. public ItemInfo(string itemTitle, string itemLink, string itemDescription, string itemPubDate, string itemAuthor, string itemGuid)
  30. {
  31. this.itemTitle = itemTitle;
  32. this.itemLink = itemLink;
  33. this.itemDescription = itemDescription;
  34. this.itemAuthor = itemAuthor;
  35. this.itemGuid = itemGuid;
  36. this.itemPubDate = itemPubDate;
  37. }
  38. public ItemInfo(string itemTitle, string itemLink, string itemDescription, string itemPubDate, string itemAuthor,
  39. string itemGuid, string mediaContentUrl, string mediaTitle, string mediaDescription,
  40. string mediaThumbUrl, string mediaThumbWidth, string mediaThumbHeight, string mediaAuthor)
  41. {
  42. this.itemTitle = itemTitle;
  43. this.itemLink = itemLink;
  44. this.itemDescription = itemDescription;
  45. this.itemPubDate = itemPubDate;
  46. this.itemAuthor = itemAuthor;
  47. this.itemGuid = itemGuid;
  48. this.mediaContentUrl = mediaContentUrl;
  49. this.mediaTitle = mediaTitle;
  50. this.mediaDescription = mediaDescription;
  51. this.mediaThumbUrl = mediaThumbUrl;
  52. this.mediaThumbWidth = mediaThumbWidth;
  53. this.mediaThumbHeight = mediaThumbHeight;
  54. this.mediaAuthor = mediaAuthor;
  55. }
  56. #endregion
  57. #region Internal item variables
  58. // Item variables
  59. private string itemTitle;
  60. private string itemLink;
  61. private string itemDescription;
  62. private string itemPubDate = DateTime.Now.ToString("r");
  63. private string itemDateTaken = DateTime.Now.ToString();
  64. private string itemAuthor;
  65. private string itemGuid;
  66. // Media variables
  67. private string mediaContentUrl;
  68. private string mediaTitle;
  69. private string mediaDescription;
  70. private string mediaThumbUrl;
  71. private string mediaThumbWidth;
  72. private string mediaThumbHeight;
  73. private string mediaAuthor;
  74. #endregion
  75. #region Item properties
  76. public string ItemTitle
  77. {
  78. set { itemTitle = value; }
  79. get { return itemTitle; }
  80. }
  81. public string ItemLink
  82. {
  83. set { itemLink = value; }
  84. get { return itemLink; }
  85. }
  86. public string ItemDescription
  87. {
  88. set { itemDescription = value; }
  89. get { return itemDescription; }
  90. }
  91. public string ItemPubDate
  92. {
  93. set { itemPubDate = value; }
  94. get { return itemPubDate; }
  95. }
  96. public string ItemDateTaken
  97. {
  98. set { itemDateTaken = value; }
  99. get { return itemDateTaken; }
  100. }
  101. public string ItemAuthor
  102. {
  103. set { itemAuthor = value; }
  104. get { return itemAuthor; }
  105. }
  106. public string ItemGuid
  107. {
  108. set { itemGuid = value; }
  109. get { return itemGuid; }
  110. }
  111. public string MediaContentUrl
  112. {
  113. set { mediaContentUrl = value; }
  114. get { return mediaContentUrl; }
  115. }
  116. public string MediaTitle
  117. {
  118. set { mediaTitle = value; }
  119. get { return mediaTitle; }
  120. }
  121. public string MediaDescription
  122. {
  123. set { mediaDescription = value; }
  124. get { return mediaDescription; }
  125. }
  126. public string MediaThumbUrl
  127. {
  128. set { mediaThumbUrl = value; }
  129. get { return mediaThumbUrl; }
  130. }
  131. public string MediaThumbWidth
  132. {
  133. set { mediaThumbWidth = value; }
  134. get { return mediaThumbWidth; }
  135. }
  136. public string MediaThumbHeight
  137. {
  138. set { mediaThumbHeight = value; }
  139. get { return mediaThumbHeight; }
  140. }
  141. public string MediaAuthor
  142. {
  143. set { mediaAuthor = value; }
  144. get { return mediaAuthor; }
  145. }
  146. #endregion
  147. }
  148. #region Const string
  149. private const string TITLE = "MyMedia RSS";
  150. private const string XMLNS_MEDIA = "http://www.MyWii.com.au";
  151. private const string XMLNS_DC = "http://purl.org/dc/elements/1.1/";
  152. private const string LINK = "http://www.MyWii.com.au/";
  153. private const string DESCRIPTION = "description";
  154. private const string LANGUAGE = "en-us";
  155. private const string COPYRIGHT = "Copyright 2006";
  156. private const string GENERATOR = "MyMedia RSS Generator";
  157. private const string IMAGE_URL = "http://www.MyWii.com.au/img/main/feed.gif";
  158. private const string IMAGE_TITLE = "MyWii";
  159. private const string IMAGE_LINK = "http://www.MyWii.com.au";
  160. private string PUB_DATE = DateTime.Now.ToString("r");
  161. private string LAST_BUILD_DATE = DateTime.Now.ToString("r");
  162. #endregion
  163. #region Internal member variables
  164. private XmlTextWriter rssWriter;
  165. private bool hasMedia;
  166. private IList<ItemInfo> items;
  167. #endregion
  168. #region Properties
  169. public XmlTextWriter RssWriter
  170. {
  171. set { rssWriter = value; }
  172. get { return rssWriter; }
  173. }
  174. public bool HasMedia
  175. {
  176. set { hasMedia = value; }
  177. get { return hasMedia; }
  178. }
  179. public IList<ItemInfo> Items
  180. {
  181. set { items = value; }
  182. get { return items; }
  183. }
  184. #endregion
  185. #region Private method
  186. private void WritePrologue()
  187. {
  188. rssWriter.WriteStartDocument();
  189. rssWriter.WriteStartElement("rss");
  190. rssWriter.WriteAttributeString("version", "2.0");
  191. rssWriter.WriteAttributeString("xmlns:media", XMLNS_MEDIA);
  192. rssWriter.WriteAttributeString("xmlns:dc", XMLNS_DC);
  193. rssWriter.WriteString("\n ");
  194. // Write in channel block
  195. rssWriter.WriteStartElement("channel");
  196. rssWriter.WriteString("\n ");
  197. rssWriter.WriteElementString("title", TITLE);
  198. rssWriter.WriteString("\n ");
  199. rssWriter.WriteElementString("link", LINK);
  200. rssWriter.WriteString("\n ");
  201. rssWriter.WriteElementString("description", DESCRIPTION);
  202. rssWriter.WriteString("\n ");
  203. rssWriter.WriteElementString("pubDate", PUB_DATE);
  204. rssWriter.WriteString("\n ");
  205. rssWriter.WriteElementString("lastBuildDate", LAST_BUILD_DATE);
  206. rssWriter.WriteString("\n ");
  207. rssWriter.WriteElementString("language", LANGUAGE);
  208. rssWriter.WriteString("\n ");
  209. rssWriter.WriteElementString("copyright", COPYRIGHT);
  210. rssWriter.WriteString("\n ");
  211. rssWriter.WriteElementString("generator", GENERATOR);
  212. rssWriter.WriteString("\n ");
  213. // Write in image block
  214. rssWriter.WriteStartElement("image");
  215. rssWriter.WriteString("\n ");
  216. rssWriter.WriteElementString("url", IMAGE_URL);
  217. rssWriter.WriteString("\n ");
  218. rssWriter.WriteElementString("title", IMAGE_TITLE);
  219. rssWriter.WriteString("\n ");
  220. rssWriter.WriteElementString("link", IMAGE_LINK);
  221. rssWriter.WriteString("\n ");
  222. rssWriter.WriteEndElement();
  223. rssWriter.WriteString("\n ");
  224. }
  225. /**/
  226. /// <summary>
  227. /// Write in items
  228. /// </summary>
  229. private void WriteItem(ItemInfo item)
  230. {
  231. // Write in item block
  232. rssWriter.WriteStartElement("item");
  233. rssWriter.WriteString("\n ");
  234. rssWriter.WriteElementString("title", item.ItemTitle);
  235. rssWriter.WriteString("\n ");
  236. rssWriter.WriteElementString("link", item.ItemLink);
  237. rssWriter.WriteString("\n ");
  238. rssWriter.WriteStartElement("description");
  239. rssWriter.WriteString("\n ");
  240. rssWriter.WriteCData(item.ItemDescription);
  241. rssWriter.WriteString("\n ");
  242. rssWriter.WriteEndElement();
  243. rssWriter.WriteString("\n ");
  244. rssWriter.WriteElementString("pubDate", item.ItemPubDate);
  245. rssWriter.WriteString("\n ");
  246. rssWriter.WriteElementString("dc:date.Taken", item.ItemDateTaken);
  247. rssWriter.WriteString("\n ");
  248. rssWriter.WriteElementString("author", item.ItemAuthor);
  249. rssWriter.WriteString("\n ");
  250. rssWriter.WriteStartElement("guid");
  251. rssWriter.WriteAttributeString("isPermaLink", "false");
  252. rssWriter.WriteString(item.ItemGuid);
  253. rssWriter.WriteEndElement();
  254. // Write item media here
  255. }
  256. /**/
  257. /// <summary>
  258. /// Write in item media
  259. /// </summary>
  260. /// <param name="item">ItemInfo item</param>
  261. private void WriteItemMedia(ItemInfo item)
  262. {
  263. // Write in media
  264. rssWriter.WriteString("\n ");
  265. rssWriter.WriteStartElement("media:content");
  266. rssWriter.WriteAttributeString("url", item.MediaContentUrl);
  267. rssWriter.WriteAttributeString("type", "image/jpeg");
  268. rssWriter.WriteEndElement();
  269. rssWriter.WriteString("\n ");
  270. rssWriter.WriteElementString("media:title", item.MediaTitle);
  271. rssWriter.WriteString("\n ");
  272. rssWriter.WriteStartElement("media:text");
  273. rssWriter.WriteAttributeString("type", "html");
  274. rssWriter.WriteString(item.MediaDescription);
  275. rssWriter.WriteEndElement();
  276. rssWriter.WriteString("\n ");
  277. rssWriter.WriteStartElement("media:thumbnail");
  278. rssWriter.WriteAttributeString("url", item.MediaThumbUrl);
  279. rssWriter.WriteAttributeString("height", item.MediaThumbHeight);
  280. rssWriter.WriteAttributeString("width", item.MediaThumbWidth);
  281. rssWriter.WriteEndElement();
  282. rssWriter.WriteString("\n ");
  283. rssWriter.WriteStartElement("media:credit");
  284. rssWriter.WriteAttributeString("role", "photographer");
  285. rssWriter.WriteString(item.MediaAuthor);
  286. rssWriter.WriteEndElement();
  287. rssWriter.WriteString("\n ");
  288. }
  289. /**/
  290. /// <summary>
  291. /// Close item element
  292. /// </summary>
  293. private void WriteItemEnd()
  294. {
  295. rssWriter.WriteString("\n ");
  296. rssWriter.WriteEndElement();
  297. rssWriter.WriteString("\n ");
  298. }
  299. /**/
  300. /// <summary>
  301. /// Close all the elements
  302. /// </summary>
  303. private void WriteEnd()
  304. {
  305. rssWriter.WriteEndElement();
  306. rssWriter.WriteString("\n");
  307. rssWriter.WriteEndElement();
  308. rssWriter.WriteEndDocument();
  309. }
  310. #endregion
  311. /**/
  312. /// <summary>
  313. /// Write RSS feed
  314. /// </summary>
  315. public void WriteRSS()
  316. {
  317. WritePrologue();
  318. foreach (ItemInfo info in items)
  319. {
  320. WriteItem(info);
  321. if (hasMedia)
  322. WriteItemMedia(info);
  323. WriteItemEnd();
  324. }
  325. WriteEnd();
  326. }
  327. }
  328. }