ServiceDocumentFormatter.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Syndication
  5. {
  6. using System.Collections.ObjectModel;
  7. using System.Runtime.Serialization;
  8. using System.Xml.Serialization;
  9. using System.Collections.Generic;
  10. using System.Xml;
  11. using System.Runtime.CompilerServices;
  12. [TypeForwardedFrom("System.ServiceModel.Web, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
  13. [DataContract]
  14. public abstract class ServiceDocumentFormatter
  15. {
  16. ServiceDocument document;
  17. protected ServiceDocumentFormatter()
  18. {
  19. }
  20. protected ServiceDocumentFormatter(ServiceDocument documentToWrite)
  21. {
  22. if (documentToWrite == null)
  23. {
  24. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("documentToWrite");
  25. }
  26. this.document = documentToWrite;
  27. }
  28. public ServiceDocument Document
  29. {
  30. get { return this.document; }
  31. }
  32. public abstract string Version
  33. { get; }
  34. public abstract bool CanRead(XmlReader reader);
  35. public abstract void ReadFrom(XmlReader reader);
  36. public abstract void WriteTo(XmlWriter writer);
  37. internal static void LoadElementExtensions(XmlBuffer buffer, XmlDictionaryWriter writer, CategoriesDocument categories)
  38. {
  39. if (categories == null)
  40. {
  41. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("categories");
  42. }
  43. Atom10FeedFormatter.CloseBuffer(buffer, writer);
  44. categories.LoadElementExtensions(buffer);
  45. }
  46. internal static void LoadElementExtensions(XmlBuffer buffer, XmlDictionaryWriter writer, ResourceCollectionInfo collection)
  47. {
  48. if (collection == null)
  49. {
  50. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("collection");
  51. }
  52. Atom10FeedFormatter.CloseBuffer(buffer, writer);
  53. collection.LoadElementExtensions(buffer);
  54. }
  55. internal static void LoadElementExtensions(XmlBuffer buffer, XmlDictionaryWriter writer, Workspace workspace)
  56. {
  57. if (workspace == null)
  58. {
  59. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("workspace");
  60. }
  61. Atom10FeedFormatter.CloseBuffer(buffer, writer);
  62. workspace.LoadElementExtensions(buffer);
  63. }
  64. internal static void LoadElementExtensions(XmlBuffer buffer, XmlDictionaryWriter writer, ServiceDocument document)
  65. {
  66. if (document == null)
  67. {
  68. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("document");
  69. }
  70. Atom10FeedFormatter.CloseBuffer(buffer, writer);
  71. document.LoadElementExtensions(buffer);
  72. }
  73. protected static SyndicationCategory CreateCategory(InlineCategoriesDocument inlineCategories)
  74. {
  75. if (inlineCategories == null)
  76. {
  77. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("inlineCategories");
  78. }
  79. return inlineCategories.CreateCategory();
  80. }
  81. protected static ResourceCollectionInfo CreateCollection(Workspace workspace)
  82. {
  83. if (workspace == null)
  84. {
  85. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("workspace");
  86. }
  87. return workspace.CreateResourceCollection();
  88. }
  89. protected static InlineCategoriesDocument CreateInlineCategories(ResourceCollectionInfo collection)
  90. {
  91. return collection.CreateInlineCategoriesDocument();
  92. }
  93. protected static ReferencedCategoriesDocument CreateReferencedCategories(ResourceCollectionInfo collection)
  94. {
  95. return collection.CreateReferencedCategoriesDocument();
  96. }
  97. protected static Workspace CreateWorkspace(ServiceDocument document)
  98. {
  99. if (document == null)
  100. {
  101. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("document");
  102. }
  103. return document.CreateWorkspace();
  104. }
  105. protected static void LoadElementExtensions(XmlReader reader, CategoriesDocument categories, int maxExtensionSize)
  106. {
  107. if (categories == null)
  108. {
  109. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("categories");
  110. }
  111. categories.LoadElementExtensions(reader, maxExtensionSize);
  112. }
  113. protected static void LoadElementExtensions(XmlReader reader, ResourceCollectionInfo collection, int maxExtensionSize)
  114. {
  115. if (collection == null)
  116. {
  117. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("collection");
  118. }
  119. collection.LoadElementExtensions(reader, maxExtensionSize);
  120. }
  121. protected static void LoadElementExtensions(XmlReader reader, Workspace workspace, int maxExtensionSize)
  122. {
  123. if (workspace == null)
  124. {
  125. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("workspace");
  126. }
  127. workspace.LoadElementExtensions(reader, maxExtensionSize);
  128. }
  129. protected static void LoadElementExtensions(XmlReader reader, ServiceDocument document, int maxExtensionSize)
  130. {
  131. if (document == null)
  132. {
  133. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("document");
  134. }
  135. document.LoadElementExtensions(reader, maxExtensionSize);
  136. }
  137. protected static bool TryParseAttribute(string name, string ns, string value, ServiceDocument document, string version)
  138. {
  139. if (document == null)
  140. {
  141. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("document");
  142. }
  143. return document.TryParseAttribute(name, ns, value, version);
  144. }
  145. protected static bool TryParseAttribute(string name, string ns, string value, ResourceCollectionInfo collection, string version)
  146. {
  147. if (collection == null)
  148. {
  149. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("collection");
  150. }
  151. return collection.TryParseAttribute(name, ns, value, version);
  152. }
  153. protected static bool TryParseAttribute(string name, string ns, string value, CategoriesDocument categories, string version)
  154. {
  155. if (categories == null)
  156. {
  157. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("categories");
  158. }
  159. return categories.TryParseAttribute(name, ns, value, version);
  160. }
  161. protected static bool TryParseAttribute(string name, string ns, string value, Workspace workspace, string version)
  162. {
  163. if (workspace == null)
  164. {
  165. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("workspace");
  166. }
  167. return workspace.TryParseAttribute(name, ns, value, version);
  168. }
  169. protected static bool TryParseElement(XmlReader reader, ResourceCollectionInfo collection, string version)
  170. {
  171. if (collection == null)
  172. {
  173. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("collection");
  174. }
  175. return collection.TryParseElement(reader, version);
  176. }
  177. protected static bool TryParseElement(XmlReader reader, ServiceDocument document, string version)
  178. {
  179. if (document == null)
  180. {
  181. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("document");
  182. }
  183. return document.TryParseElement(reader, version);
  184. }
  185. protected static bool TryParseElement(XmlReader reader, Workspace workspace, string version)
  186. {
  187. if (workspace == null)
  188. {
  189. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("workspace");
  190. }
  191. return workspace.TryParseElement(reader, version);
  192. }
  193. protected static bool TryParseElement(XmlReader reader, CategoriesDocument categories, string version)
  194. {
  195. if (categories == null)
  196. {
  197. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("categories");
  198. }
  199. return categories.TryParseElement(reader, version);
  200. }
  201. protected static void WriteAttributeExtensions(XmlWriter writer, ServiceDocument document, string version)
  202. {
  203. if (document == null)
  204. {
  205. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("document");
  206. }
  207. document.WriteAttributeExtensions(writer, version);
  208. }
  209. protected static void WriteAttributeExtensions(XmlWriter writer, Workspace workspace, string version)
  210. {
  211. if (workspace == null)
  212. {
  213. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("workspace");
  214. }
  215. workspace.WriteAttributeExtensions(writer, version);
  216. }
  217. protected static void WriteAttributeExtensions(XmlWriter writer, ResourceCollectionInfo collection, string version)
  218. {
  219. if (collection == null)
  220. {
  221. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("collection");
  222. }
  223. collection.WriteAttributeExtensions(writer, version);
  224. }
  225. protected static void WriteAttributeExtensions(XmlWriter writer, CategoriesDocument categories, string version)
  226. {
  227. if (categories == null)
  228. {
  229. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("categories");
  230. }
  231. categories.WriteAttributeExtensions(writer, version);
  232. }
  233. protected static void WriteElementExtensions(XmlWriter writer, ServiceDocument document, string version)
  234. {
  235. if (document == null)
  236. {
  237. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("document");
  238. }
  239. document.WriteElementExtensions(writer, version);
  240. }
  241. protected static void WriteElementExtensions(XmlWriter writer, Workspace workspace, string version)
  242. {
  243. if (workspace == null)
  244. {
  245. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("workspace");
  246. }
  247. workspace.WriteElementExtensions(writer, version);
  248. }
  249. protected static void WriteElementExtensions(XmlWriter writer, ResourceCollectionInfo collection, string version)
  250. {
  251. if (collection == null)
  252. {
  253. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("collection");
  254. }
  255. collection.WriteElementExtensions(writer, version);
  256. }
  257. protected static void WriteElementExtensions(XmlWriter writer, CategoriesDocument categories, string version)
  258. {
  259. if (categories == null)
  260. {
  261. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("categories");
  262. }
  263. categories.WriteElementExtensions(writer, version);
  264. }
  265. protected virtual ServiceDocument CreateDocumentInstance()
  266. {
  267. return new ServiceDocument();
  268. }
  269. protected virtual void SetDocument(ServiceDocument document)
  270. {
  271. this.document = document;
  272. }
  273. }
  274. }