SyndicationItem.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //
  2. // SyndicationItem.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 SyndicationItem : ISyndicationElement
  38. {
  39. public static SyndicationItem Load (XmlReader reader)
  40. {
  41. return Load<SyndicationItem> (reader);
  42. }
  43. public static TSyndicationItem Load<TSyndicationItem> (XmlReader reader) where TSyndicationItem : SyndicationItem, new()
  44. {
  45. return SyndicationVersions.LoadItem<TSyndicationItem> (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, summary, title;
  53. SyndicationContent content;
  54. string id;
  55. DateTimeOffset last_updated_time, published_date;
  56. SyndicationFeed source_feed;
  57. public SyndicationItem ()
  58. {
  59. }
  60. public SyndicationItem (string title, string content, Uri feedAlternateLink)
  61. : this (title, content, feedAlternateLink, null, default (DateTimeOffset))
  62. {
  63. }
  64. public SyndicationItem (string title, string content, Uri feedAlternateLink, string id,
  65. DateTimeOffset lastUpdatedTime)
  66. : this (title, content != null ? SyndicationContent.CreatePlaintextContent (content) : null, feedAlternateLink, id, lastUpdatedTime)
  67. {
  68. }
  69. public SyndicationItem (string title, SyndicationContent content, Uri feedAlternateLink, string id,
  70. DateTimeOffset lastUpdatedTime)
  71. {
  72. Title = title != null ? new TextSyndicationContent (title) : null;
  73. Content = content;
  74. if (feedAlternateLink != null)
  75. AddPermalink (feedAlternateLink);
  76. Id = id;
  77. LastUpdatedTime = lastUpdatedTime;
  78. }
  79. protected SyndicationItem (SyndicationItem source)
  80. {
  81. extensions = source.extensions.Clone ();
  82. categories = Copy<SyndicationCategory> (source.categories);
  83. authors = Copy<SyndicationPerson> (source.authors);
  84. contributors = Copy<SyndicationPerson> (source.contributors);
  85. links = Copy<SyndicationLink> (source.links);
  86. base_uri = source.base_uri; // copy by reference !!
  87. copyright = source.copyright == null ? null : source.copyright.Clone () as TextSyndicationContent;
  88. summary = source.summary == null ? null : source.summary.Clone () as TextSyndicationContent;
  89. title = source.title == null ? null : source.title.Clone () as TextSyndicationContent;
  90. content = source.content == null ? null : source.content.Clone ();
  91. id = source.id;
  92. last_updated_time = source.last_updated_time;
  93. published_date = source.published_date;
  94. source_feed = source.source_feed == null ? null : source.source_feed.Clone (false);
  95. }
  96. Collection<T> Copy<T> (Collection<T> source)
  97. {
  98. if (source == null)
  99. return source;
  100. Collection<T> ret = new Collection<T> ();
  101. foreach (object item in source)
  102. ret.Add ((T) CreateCopy (item));
  103. return ret;
  104. }
  105. object CreateCopy (object source)
  106. {
  107. if (source == null)
  108. return null;
  109. if (source is SyndicationPerson)
  110. return ((SyndicationPerson) source).Clone ();
  111. if (source is SyndicationLink)
  112. return ((SyndicationLink) source).Clone ();
  113. if (source is SyndicationCategory)
  114. return ((SyndicationCategory) source).Clone ();
  115. throw new InvalidOperationException ();
  116. }
  117. public Dictionary<XmlQualifiedName, string> AttributeExtensions {
  118. get { return extensions.Attributes; }
  119. }
  120. public SyndicationElementExtensionCollection ElementExtensions {
  121. get { return extensions.Elements; }
  122. }
  123. public Collection<SyndicationPerson> Authors {
  124. get {
  125. if (authors == null)
  126. authors = new Collection<SyndicationPerson> ();
  127. return authors;
  128. }
  129. }
  130. public Collection<SyndicationCategory> Categories {
  131. get {
  132. if (categories == null)
  133. categories = new Collection<SyndicationCategory> ();
  134. return categories;
  135. }
  136. }
  137. public Collection<SyndicationPerson> Contributors {
  138. get {
  139. if (contributors == null)
  140. contributors = new Collection<SyndicationPerson> ();
  141. return contributors;
  142. }
  143. }
  144. public Collection<SyndicationLink> Links {
  145. get {
  146. if (links == null)
  147. links = new Collection<SyndicationLink> ();
  148. return links;
  149. }
  150. }
  151. public Uri BaseUri {
  152. get { return base_uri; }
  153. set { base_uri = value; }
  154. }
  155. public TextSyndicationContent Copyright {
  156. get { return copyright; }
  157. set { copyright = value; }
  158. }
  159. public SyndicationContent Content {
  160. get { return content; }
  161. set { content = value; }
  162. }
  163. public string Id {
  164. get { return id; }
  165. set { id = value; }
  166. }
  167. public DateTimeOffset LastUpdatedTime {
  168. get { return last_updated_time; }
  169. set { last_updated_time = value; }
  170. }
  171. public DateTimeOffset PublishDate {
  172. get { return published_date; }
  173. set { published_date = value; }
  174. }
  175. public SyndicationFeed SourceFeed {
  176. get { return source_feed; }
  177. set { source_feed = value; }
  178. }
  179. public TextSyndicationContent Summary {
  180. get { return summary; }
  181. set { summary = value; }
  182. }
  183. public TextSyndicationContent Title {
  184. get { return title; }
  185. set { title = value; }
  186. }
  187. public void AddPermalink (Uri link)
  188. {
  189. if (link == null)
  190. throw new ArgumentNullException ("link");
  191. Links.Add (SyndicationLink.CreateAlternateLink (link));
  192. }
  193. public virtual SyndicationItem Clone ()
  194. {
  195. return new SyndicationItem (this);
  196. }
  197. protected internal virtual SyndicationCategory CreateCategory ()
  198. {
  199. return new SyndicationCategory ();
  200. }
  201. protected internal virtual SyndicationLink CreateLink ()
  202. {
  203. return new SyndicationLink ();
  204. }
  205. protected internal virtual SyndicationPerson CreatePerson ()
  206. {
  207. return new SyndicationPerson ();
  208. }
  209. public Atom10ItemFormatter GetAtom10Formatter ()
  210. {
  211. return new Atom10ItemFormatter (this);
  212. }
  213. public Rss20ItemFormatter GetRss20Formatter ()
  214. {
  215. return GetRss20Formatter (true);
  216. }
  217. public Rss20ItemFormatter GetRss20Formatter (bool serializeExtensionsAsAtom)
  218. {
  219. return new Rss20ItemFormatter (this, serializeExtensionsAsAtom);
  220. }
  221. public void SaveAsAtom10 (XmlWriter writer)
  222. {
  223. if (writer == null)
  224. throw new ArgumentNullException ("writer");
  225. GetAtom10Formatter ().WriteTo (writer);
  226. }
  227. public void SaveAsRss20 (XmlWriter writer)
  228. {
  229. if (writer == null)
  230. throw new ArgumentNullException ("writer");
  231. GetRss20Formatter ().WriteTo (writer);
  232. }
  233. protected internal virtual bool TryParseContent (XmlReader reader, string contentType, string version, out SyndicationContent content)
  234. {
  235. content = null;
  236. return false;
  237. }
  238. protected internal virtual bool TryParseAttribute (string name, string ns, string value, string version)
  239. {
  240. return false;
  241. }
  242. protected internal virtual bool TryParseElement (XmlReader reader, string version)
  243. {
  244. return false;
  245. }
  246. protected internal virtual void WriteAttributeExtensions (XmlWriter writer, string version)
  247. {
  248. extensions.WriteAttributeExtensions (writer, version);
  249. }
  250. protected internal virtual void WriteElementExtensions (XmlWriter writer, string version)
  251. {
  252. extensions.WriteElementExtensions (writer, version);
  253. }
  254. }
  255. }