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