SyndicationFeedFormatter.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. //
  2. // SyndicationFeedFormatter.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. [DataContract]
  38. public abstract class SyndicationFeedFormatter
  39. {
  40. #region static members
  41. protected internal static SyndicationCategory CreateCategory (SyndicationFeed feed)
  42. {
  43. if (feed == null)
  44. throw new ArgumentNullException ("feed");
  45. return feed.CreateCategory ();
  46. }
  47. protected internal static SyndicationCategory CreateCategory (SyndicationItem item)
  48. {
  49. if (item == null)
  50. throw new ArgumentNullException ("item");
  51. return item.CreateCategory ();
  52. }
  53. protected internal static SyndicationItem CreateItem (SyndicationFeed feed)
  54. {
  55. if (feed == null)
  56. throw new ArgumentNullException ("feed");
  57. return feed.CreateItem ();
  58. }
  59. protected internal static SyndicationLink CreateLink (SyndicationFeed feed)
  60. {
  61. if (feed == null)
  62. throw new ArgumentNullException ("feed");
  63. return feed.CreateLink ();
  64. }
  65. protected internal static SyndicationLink CreateLink (SyndicationItem item)
  66. {
  67. if (item == null)
  68. throw new ArgumentNullException ("item");
  69. return item.CreateLink ();
  70. }
  71. protected internal static SyndicationPerson CreatePerson (SyndicationFeed feed)
  72. {
  73. if (feed == null)
  74. throw new ArgumentNullException ("feed");
  75. return feed.CreatePerson ();
  76. }
  77. protected internal static SyndicationPerson CreatePerson (SyndicationItem item)
  78. {
  79. if (item == null)
  80. throw new ArgumentNullException ("item");
  81. return item.CreatePerson ();
  82. }
  83. [MonoTODO ("use maxExtensionsSize parameter")]
  84. protected internal static void LoadElementExtensions (XmlReader reader, SyndicationCategory category, int maxExtensionSize)
  85. {
  86. category.ElementExtensions.Add (reader);
  87. }
  88. [MonoTODO ("use maxExtensionsSize parameter")]
  89. protected internal static void LoadElementExtensions (XmlReader reader, SyndicationFeed feed, int maxExtensionSize)
  90. {
  91. feed.ElementExtensions.Add (reader);
  92. }
  93. [MonoTODO ("use maxExtensionsSize parameter")]
  94. protected internal static void LoadElementExtensions (XmlReader reader, SyndicationItem item, int maxExtensionSize)
  95. {
  96. item.ElementExtensions.Add (reader);
  97. }
  98. [MonoTODO ("use maxExtensionsSize parameter")]
  99. protected internal static void LoadElementExtensions (XmlReader reader, SyndicationLink link, int maxExtensionSize)
  100. {
  101. link.ElementExtensions.Add (reader);
  102. }
  103. [MonoTODO ("use maxExtensionsSize parameter")]
  104. protected internal static void LoadElementExtensions (XmlReader reader, SyndicationPerson person, int maxExtensionSize)
  105. {
  106. person.ElementExtensions.Add (reader);
  107. }
  108. protected internal static bool TryParseAttribute (string name, string ns, string value, SyndicationCategory category, string version)
  109. {
  110. if (category == null)
  111. throw new ArgumentNullException ("category");
  112. return category.TryParseAttribute (name, ns, value, version);
  113. }
  114. protected internal static bool TryParseAttribute (string name, string ns, string value, SyndicationFeed feed, string version)
  115. {
  116. if (feed == null)
  117. throw new ArgumentNullException ("feed");
  118. return feed.TryParseAttribute (name, ns, value, version);
  119. }
  120. protected internal static bool TryParseAttribute (string name, string ns, string value, SyndicationItem item, string version)
  121. {
  122. if (item == null)
  123. throw new ArgumentNullException ("item");
  124. return item.TryParseAttribute (name, ns, value, version);
  125. }
  126. protected internal static bool TryParseAttribute (string name, string ns, string value, SyndicationLink link, string version)
  127. {
  128. if (link == null)
  129. throw new ArgumentNullException ("link");
  130. return link.TryParseAttribute (name, ns, value, version);
  131. }
  132. protected internal static bool TryParseAttribute (string name, string ns, string value, SyndicationPerson person, string version)
  133. {
  134. if (person == null)
  135. throw new ArgumentNullException ("person");
  136. return person.TryParseAttribute (name, ns, value, version);
  137. }
  138. protected internal static bool TryParseContent (XmlReader reader, SyndicationItem item, string contentType, string version, out SyndicationContent content)
  139. {
  140. if (item == null)
  141. throw new ArgumentNullException ("item");
  142. return item.TryParseContent (reader, contentType, version, out content);
  143. }
  144. protected internal static bool TryParseElement (XmlReader reader, SyndicationCategory category, string version)
  145. {
  146. if (category == null)
  147. throw new ArgumentNullException ("category");
  148. return category.TryParseElement (reader, version);
  149. }
  150. protected internal static bool TryParseElement (XmlReader reader, SyndicationFeed feed, string version)
  151. {
  152. if (feed == null)
  153. throw new ArgumentNullException ("feed");
  154. return feed.TryParseElement (reader, version);
  155. }
  156. protected internal static bool TryParseElement (XmlReader reader, SyndicationItem item, string version)
  157. {
  158. if (item == null)
  159. throw new ArgumentNullException ("item");
  160. return item.TryParseElement (reader, version);
  161. }
  162. protected internal static bool TryParseElement (XmlReader reader, SyndicationLink link, string version)
  163. {
  164. if (link == null)
  165. throw new ArgumentNullException ("link");
  166. return link.TryParseElement (reader, version);
  167. }
  168. protected internal static bool TryParseElement (XmlReader reader, SyndicationPerson person, string version)
  169. {
  170. if (person == null)
  171. throw new ArgumentNullException ("person");
  172. return person.TryParseElement (reader, version);
  173. }
  174. protected internal static void WriteAttributeExtensions (XmlWriter writer, SyndicationCategory category, string version)
  175. {
  176. if (category == null)
  177. throw new ArgumentNullException ("category");
  178. category.WriteAttributeExtensions (writer, version);
  179. }
  180. protected internal static void WriteAttributeExtensions (XmlWriter writer, SyndicationItem item, string version)
  181. {
  182. if (item == null)
  183. throw new ArgumentNullException ("item");
  184. item.WriteAttributeExtensions (writer, version);
  185. }
  186. protected internal static void WriteAttributeExtensions (XmlWriter writer, SyndicationLink link, string version)
  187. {
  188. if (link == null)
  189. throw new ArgumentNullException ("link");
  190. link.WriteAttributeExtensions (writer, version);
  191. }
  192. protected internal static void WriteAttributeExtensions (XmlWriter writer, SyndicationPerson person, string version)
  193. {
  194. if (person == null)
  195. throw new ArgumentNullException ("person");
  196. person.WriteAttributeExtensions (writer, version);
  197. }
  198. protected internal static void WriteElementExtensions (XmlWriter writer, SyndicationCategory category, string version)
  199. {
  200. if (category == null)
  201. throw new ArgumentNullException ("category");
  202. category.WriteElementExtensions (writer, version);
  203. }
  204. protected internal static void WriteAttributeExtensions (XmlWriter writer, SyndicationFeed feed, string version)
  205. {
  206. if (feed == null)
  207. throw new ArgumentNullException ("feed");
  208. feed.WriteElementExtensions (writer, version);
  209. }
  210. protected internal static void WriteElementExtensions (XmlWriter writer, SyndicationFeed feed, string version)
  211. {
  212. if (feed == null)
  213. throw new ArgumentNullException ("feed");
  214. feed.WriteElementExtensions (writer, version);
  215. }
  216. protected internal static void WriteElementExtensions (XmlWriter writer, SyndicationItem item, string version)
  217. {
  218. if (item == null)
  219. throw new ArgumentNullException ("item");
  220. item.WriteElementExtensions (writer, version);
  221. }
  222. protected internal static void WriteElementExtensions (XmlWriter writer, SyndicationLink link, string version)
  223. {
  224. if (link == null)
  225. throw new ArgumentNullException ("link");
  226. link.WriteElementExtensions (writer, version);
  227. }
  228. protected internal static void WriteElementExtensions (XmlWriter writer, SyndicationPerson person, string version)
  229. {
  230. if (person == null)
  231. throw new ArgumentNullException ("person");
  232. person.WriteElementExtensions (writer, version);
  233. }
  234. #endregion
  235. #region instance members
  236. SyndicationFeed feed;
  237. protected SyndicationFeedFormatter ()
  238. {
  239. }
  240. protected SyndicationFeedFormatter (SyndicationFeed feedToWrite)
  241. {
  242. if (feedToWrite == null)
  243. throw new ArgumentNullException ("feedToWrite");
  244. SetFeed (feedToWrite);
  245. }
  246. protected internal virtual void SetFeed (SyndicationFeed feed)
  247. {
  248. if (feed == null)
  249. throw new ArgumentNullException ("feed");
  250. this.feed = feed;
  251. }
  252. public SyndicationFeed Feed {
  253. get { return feed; }
  254. }
  255. public abstract string Version { get; }
  256. protected abstract SyndicationFeed CreateFeedInstance ();
  257. public abstract bool CanRead (XmlReader reader);
  258. public abstract void ReadFrom (XmlReader reader);
  259. public abstract void WriteTo (XmlWriter writer);
  260. public override string ToString ()
  261. {
  262. return String.Concat (GetType ().FullName, ", SyndicationVersion=", Version);
  263. }
  264. #endregion
  265. }
  266. }