SyndicationFeed.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // SyndicationFeed.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Collections.ObjectModel;
  31. using System.IO;
  32. using System.Runtime.Serialization;
  33. using System.Text;
  34. using System.Xml;
  35. namespace System.ServiceModel.Syndication
  36. {
  37. public class SyndicationFeed : ISyndicationElement
  38. {
  39. public static SyndicationFeed Load (XmlReader reader)
  40. {
  41. return Load<SyndicationFeed> (reader);
  42. }
  43. public static TSyndicationFeed Load<TSyndicationFeed> (XmlReader reader) where TSyndicationFeed : SyndicationFeed, new()
  44. {
  45. return SyndicationVersions.LoadFeed<TSyndicationFeed> (reader);
  46. }
  47. SyndicationExtensions extensions = new SyndicationExtensions ();
  48. Collection<SyndicationCategory> categories;
  49. Collection<SyndicationPerson> authors, contributors;
  50. Collection<SyndicationLink> links;
  51. Uri base_uri;
  52. TextSyndicationContent copyright, title;
  53. string id;
  54. DateTimeOffset last_updated_time;
  55. IEnumerable<SyndicationItem> items;
  56. TextSyndicationContent description;
  57. string generator, language;
  58. Uri image_url;
  59. public SyndicationFeed ()
  60. {
  61. }
  62. public SyndicationFeed (IEnumerable<SyndicationItem> items)
  63. {
  64. if (items != null)
  65. Items = items;
  66. }
  67. public SyndicationFeed (string title, string description, Uri feedAlternateLink)
  68. : this (title, description, feedAlternateLink, null)
  69. {
  70. }
  71. public SyndicationFeed (string title, string description, Uri feedAlternateLink,
  72. IEnumerable<SyndicationItem> items)
  73. : this (title, description, feedAlternateLink, null, default (DateTimeOffset), items)
  74. {
  75. }
  76. public SyndicationFeed (string title, string description, Uri feedAlternateLink, string id,
  77. DateTimeOffset lastUpdatedTime)
  78. : this (title, description, feedAlternateLink, id, lastUpdatedTime, null)
  79. {
  80. }
  81. public SyndicationFeed (string title, string description, Uri feedAlternateLink, string id,
  82. DateTimeOffset lastUpdatedTime, IEnumerable<SyndicationItem> items)
  83. {
  84. Title = title != null ? new TextSyndicationContent (title) : null;
  85. Description = description != null ? new TextSyndicationContent (description) : null;
  86. if (feedAlternateLink != null)
  87. Links.Add (SyndicationLink.CreateAlternateLink (feedAlternateLink));
  88. Id = id;
  89. LastUpdatedTime = lastUpdatedTime;
  90. if (items != null)
  91. Items = items;
  92. }
  93. protected SyndicationFeed (SyndicationFeed source, bool cloneItems)
  94. {
  95. extensions = source.extensions.Clone ();
  96. categories = source.categories == null ? null : new Collection<SyndicationCategory> (source.categories);
  97. authors = source.authors == null ? null : new Collection<SyndicationPerson> (source.authors);
  98. contributors = source.contributors == null ? null : new Collection<SyndicationPerson> (source.contributors);
  99. links = source.links == null ? null : new Collection<SyndicationLink> (source.links);
  100. base_uri = source.base_uri; // copy by reference !!
  101. copyright = source.copyright == null ? null : source.copyright.Clone () as TextSyndicationContent;
  102. title = source.title == null ? null : source.title.Clone () as TextSyndicationContent;
  103. id = source.id;
  104. last_updated_time = source.last_updated_time;
  105. description = source.description == null ? null : source.description.Clone () as TextSyndicationContent;
  106. generator = source.generator;
  107. image_url = source.image_url; // copy by reference !!
  108. language = source.language;
  109. if (cloneItems && source.items != null)
  110. items = new Collection<SyndicationItem> (new List<SyndicationItem> (source.items));
  111. }
  112. public Dictionary<XmlQualifiedName, string> AttributeExtensions {
  113. get { return extensions.Attributes; }
  114. }
  115. public SyndicationElementExtensionCollection ElementExtensions {
  116. get { return extensions.Elements; }
  117. }
  118. public Collection<SyndicationPerson> Authors {
  119. get {
  120. if (authors == null)
  121. authors = new Collection<SyndicationPerson> ();
  122. return authors;
  123. }
  124. }
  125. public Collection<SyndicationCategory> Categories {
  126. get {
  127. if (categories == null)
  128. categories = new Collection<SyndicationCategory> ();
  129. return categories;
  130. }
  131. }
  132. public Collection<SyndicationPerson> Contributors {
  133. get {
  134. if (contributors == null)
  135. contributors = new Collection<SyndicationPerson> ();
  136. return contributors;
  137. }
  138. }
  139. public Collection<SyndicationLink> Links {
  140. get {
  141. if (links == null)
  142. links = new Collection<SyndicationLink> ();
  143. return links;
  144. }
  145. }
  146. public Uri BaseUri {
  147. get { return base_uri; }
  148. set { base_uri = value; }
  149. }
  150. public TextSyndicationContent Copyright {
  151. get { return copyright; }
  152. set { copyright = value; }
  153. }
  154. public string Id {
  155. get { return id; }
  156. set { id = value; }
  157. }
  158. public DateTimeOffset LastUpdatedTime {
  159. get { return last_updated_time; }
  160. set { last_updated_time = value; }
  161. }
  162. public TextSyndicationContent Title {
  163. get { return title; }
  164. set { title = value; }
  165. }
  166. public IEnumerable<SyndicationItem> Items {
  167. get {
  168. if (items == null)
  169. items = new Collection<SyndicationItem> ();
  170. return items;
  171. }
  172. set {
  173. if (value == null)
  174. throw new ArgumentNullException ("value");
  175. items = value;
  176. }
  177. }
  178. public TextSyndicationContent Description {
  179. get { return description; }
  180. set { description = value; }
  181. }
  182. public string Generator {
  183. get { return generator; }
  184. set { generator = value; }
  185. }
  186. public Uri ImageUrl {
  187. get { return image_url; }
  188. set { image_url = value; }
  189. }
  190. public string Language {
  191. get { return language; }
  192. set { language = value; }
  193. }
  194. public virtual SyndicationFeed Clone (bool cloneItems)
  195. {
  196. return new SyndicationFeed (this, cloneItems);
  197. }
  198. protected internal virtual SyndicationCategory CreateCategory ()
  199. {
  200. return new SyndicationCategory ();
  201. }
  202. protected internal virtual SyndicationLink CreateLink ()
  203. {
  204. return new SyndicationLink ();
  205. }
  206. protected internal virtual SyndicationPerson CreatePerson ()
  207. {
  208. return new SyndicationPerson ();
  209. }
  210. protected internal virtual SyndicationItem CreateItem ()
  211. {
  212. return new SyndicationItem ();
  213. }
  214. public Atom10FeedFormatter GetAtom10Formatter ()
  215. {
  216. return new Atom10FeedFormatter (this);
  217. }
  218. public Rss20FeedFormatter GetRss20Formatter ()
  219. {
  220. return GetRss20Formatter (true);
  221. }
  222. public Rss20FeedFormatter GetRss20Formatter (bool serializeExtensionsAsAtom)
  223. {
  224. return new Rss20FeedFormatter (this, serializeExtensionsAsAtom);
  225. }
  226. public void SaveAsAtom10 (XmlWriter writer)
  227. {
  228. GetAtom10Formatter ().WriteTo (writer);
  229. }
  230. public void SaveAsRss20 (XmlWriter writer)
  231. {
  232. GetRss20Formatter ().WriteTo (writer);
  233. }
  234. protected internal virtual bool TryParseAttribute (string name, string ns, string value, string version)
  235. {
  236. return false;
  237. }
  238. protected internal virtual bool TryParseElement (XmlReader reader, string version)
  239. {
  240. return false;
  241. }
  242. protected internal virtual void WriteAttributeExtensions (XmlWriter writer, string version)
  243. {
  244. extensions.WriteAttributeExtensions (writer, version);
  245. }
  246. protected internal virtual void WriteElementExtensions (XmlWriter writer, string version)
  247. {
  248. extensions.WriteElementExtensions (writer, version);
  249. }
  250. }
  251. }