2
0

SyndicationItemFormatter.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //
  2. // SyndicationItemFormatter.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 SyndicationItemFormatter
  39. {
  40. #region static members
  41. // My assumption is that those static methods are defined to
  42. // be used in the derived classes and to dispatch the actual
  43. // processing to each syndication element. Considering that
  44. // they can be overriden in each element, I assume that these
  45. // methods depend on the elements, not in reverse order.
  46. protected internal static SyndicationCategory CreateCategory (SyndicationItem item)
  47. {
  48. if (item == null)
  49. throw new ArgumentNullException ("item");
  50. return item.CreateCategory ();
  51. }
  52. protected internal static SyndicationLink CreateLink (SyndicationItem item)
  53. {
  54. if (item == null)
  55. throw new ArgumentNullException ("item");
  56. return item.CreateLink ();
  57. }
  58. protected internal static SyndicationPerson CreatePerson (SyndicationItem item)
  59. {
  60. if (item == null)
  61. throw new ArgumentNullException ("item");
  62. return item.CreatePerson ();
  63. }
  64. [MonoTODO ("use maxExtensionsSize parameter")]
  65. protected internal static void LoadElementExtensions (XmlReader reader, SyndicationCategory category, int maxExtensionSize)
  66. {
  67. category.ElementExtensions.Add (reader);
  68. }
  69. [MonoTODO ("use maxExtensionsSize parameter")]
  70. protected internal static void LoadElementExtensions (XmlReader reader, SyndicationItem item, int maxExtensionSize)
  71. {
  72. item.ElementExtensions.Add (reader);
  73. }
  74. [MonoTODO ("use maxExtensionsSize parameter")]
  75. protected internal static void LoadElementExtensions (XmlReader reader, SyndicationLink link, int maxExtensionSize)
  76. {
  77. link.ElementExtensions.Add (reader);
  78. }
  79. [MonoTODO ("use maxExtensionsSize parameter")]
  80. protected internal static void LoadElementExtensions (XmlReader reader, SyndicationPerson person, int maxExtensionSize)
  81. {
  82. person.ElementExtensions.Add (reader);
  83. }
  84. protected internal static bool TryParseAttribute (string name, string ns, string value, SyndicationCategory category, string version)
  85. {
  86. if (category == null)
  87. throw new ArgumentNullException ("category");
  88. return category.TryParseAttribute (name, ns, value, version);
  89. }
  90. protected internal static bool TryParseAttribute (string name, string ns, string value, SyndicationItem item, string version)
  91. {
  92. if (item == null)
  93. throw new ArgumentNullException ("item");
  94. return item.TryParseAttribute (name, ns, value, version);
  95. }
  96. protected internal static bool TryParseAttribute (string name, string ns, string value, SyndicationLink link, string version)
  97. {
  98. if (link == null)
  99. throw new ArgumentNullException ("link");
  100. return link.TryParseAttribute (name, ns, value, version);
  101. }
  102. protected internal static bool TryParseAttribute (string name, string ns, string value, SyndicationPerson person, string version)
  103. {
  104. if (person == null)
  105. throw new ArgumentNullException ("person");
  106. return person.TryParseAttribute (name, ns, value, version);
  107. }
  108. protected internal static bool TryParseContent (XmlReader reader, SyndicationItem item, string contentType, string version, out SyndicationContent content)
  109. {
  110. if (item == null)
  111. throw new ArgumentNullException ("item");
  112. return item.TryParseContent (reader, contentType, version, out content);
  113. }
  114. protected internal static bool TryParseElement (XmlReader reader, SyndicationCategory category, string version)
  115. {
  116. if (category == null)
  117. throw new ArgumentNullException ("category");
  118. return category.TryParseElement (reader, version);
  119. }
  120. protected internal static bool TryParseElement (XmlReader reader, SyndicationItem item, string version)
  121. {
  122. if (item == null)
  123. throw new ArgumentNullException ("item");
  124. return item.TryParseElement (reader, version);
  125. }
  126. protected internal static bool TryParseElement (XmlReader reader, SyndicationLink link, string version)
  127. {
  128. if (link == null)
  129. throw new ArgumentNullException ("link");
  130. return link.TryParseElement (reader, version);
  131. }
  132. protected internal static bool TryParseElement (XmlReader reader, SyndicationPerson person, string version)
  133. {
  134. if (person == null)
  135. throw new ArgumentNullException ("person");
  136. return person.TryParseElement (reader, version);
  137. }
  138. protected internal static void WriteAttributeExtensions (XmlWriter writer, SyndicationCategory category, string version)
  139. {
  140. if (category == null)
  141. throw new ArgumentNullException ("category");
  142. category.WriteAttributeExtensions (writer, version);
  143. }
  144. protected internal static void WriteAttributeExtensions (XmlWriter writer, SyndicationItem item, string version)
  145. {
  146. if (item == null)
  147. throw new ArgumentNullException ("item");
  148. item.WriteAttributeExtensions (writer, version);
  149. }
  150. protected internal static void WriteAttributeExtensions (XmlWriter writer, SyndicationLink link, string version)
  151. {
  152. if (link == null)
  153. throw new ArgumentNullException ("link");
  154. link.WriteAttributeExtensions (writer, version);
  155. }
  156. protected internal static void WriteAttributeExtensions (XmlWriter writer, SyndicationPerson person, string version)
  157. {
  158. if (person == null)
  159. throw new ArgumentNullException ("person");
  160. person.WriteAttributeExtensions (writer, version);
  161. }
  162. protected internal void WriteElementExtensions (XmlWriter writer, SyndicationCategory category, string version)
  163. {
  164. if (category == null)
  165. throw new ArgumentNullException ("category");
  166. category.WriteElementExtensions (writer, version);
  167. }
  168. protected internal static void WriteElementExtensions (XmlWriter writer, SyndicationItem item, string version)
  169. {
  170. if (item == null)
  171. throw new ArgumentNullException ("item");
  172. item.WriteElementExtensions (writer, version);
  173. }
  174. protected internal void WriteElementExtensions (XmlWriter writer, SyndicationLink link, string version)
  175. {
  176. if (link == null)
  177. throw new ArgumentNullException ("link");
  178. link.WriteElementExtensions (writer, version);
  179. }
  180. protected internal void WriteElementExtensions (XmlWriter writer, SyndicationPerson person, string version)
  181. {
  182. if (person == null)
  183. throw new ArgumentNullException ("person");
  184. person.WriteElementExtensions (writer, version);
  185. }
  186. #endregion
  187. #region instance members
  188. SyndicationItem item;
  189. protected SyndicationItemFormatter ()
  190. {
  191. }
  192. protected SyndicationItemFormatter (SyndicationItem itemToWrite)
  193. {
  194. if (itemToWrite == null)
  195. throw new ArgumentNullException ("itemToWrite");
  196. SetItem (itemToWrite);
  197. }
  198. protected internal virtual void SetItem (SyndicationItem item)
  199. {
  200. if (item == null)
  201. throw new ArgumentNullException ("item");
  202. this.item = item;
  203. }
  204. public SyndicationItem Item {
  205. get { return item; }
  206. }
  207. public abstract string Version { get; }
  208. protected abstract SyndicationItem CreateItemInstance ();
  209. public abstract bool CanRead (XmlReader reader);
  210. public abstract void ReadFrom (XmlReader reader);
  211. public abstract void WriteTo (XmlWriter writer);
  212. public override string ToString ()
  213. {
  214. return String.Concat (GetType ().FullName, ", SyndicationVersion=", Version);
  215. }
  216. #endregion
  217. }
  218. }