ServiceDocumentFormatter.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // ServiceDocumentFormatter.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2009 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.IO;
  31. using System.Runtime.Serialization;
  32. using System.ServiceModel;
  33. using System.ServiceModel.Channels;
  34. using System.Xml;
  35. namespace System.ServiceModel.Syndication
  36. {
  37. [DataContract]
  38. public abstract class ServiceDocumentFormatter
  39. {
  40. protected static SyndicationCategory CreateCategory (InlineCategoriesDocument inlineCategories)
  41. {
  42. return inlineCategories.CreateCategory ();
  43. }
  44. protected static ResourceCollectionInfo CreateCollection (Workspace workspace)
  45. {
  46. return workspace.CreateResourceCollection ();
  47. }
  48. protected static InlineCategoriesDocument CreateInlineCategories (ResourceCollectionInfo collection)
  49. {
  50. return collection.CreateInlineCategoriesDocument ();
  51. }
  52. protected static ReferencedCategoriesDocument CreateReferencedCategories (ResourceCollectionInfo collection)
  53. {
  54. return collection.CreateReferencedCategoriesDocument ();
  55. }
  56. protected static Workspace CreateWorkspace (ServiceDocument document)
  57. {
  58. return document.CreateWorkspace ();
  59. }
  60. [MonoTODO ("Use maxExtensionSize somewhere")]
  61. protected static void LoadElementExtensions (XmlReader reader, CategoriesDocument categories, int maxExtensionSize)
  62. {
  63. categories.ElementExtensions.Add (reader);
  64. }
  65. [MonoTODO ("Use maxExtensionSize somewhere")]
  66. protected static void LoadElementExtensions (XmlReader reader,ResourceCollectionInfo collection, int maxExtensionSize)
  67. {
  68. collection.ElementExtensions.Add (reader);
  69. }
  70. [MonoTODO ("Use maxExtensionSize somewhere")]
  71. protected static void LoadElementExtensions (XmlReader reader, ServiceDocument document, int maxExtensionSize)
  72. {
  73. document.ElementExtensions.Add (reader);
  74. }
  75. [MonoTODO ("Use maxExtensionSize somewhere")]
  76. protected static void LoadElementExtensions (XmlReader reader, Workspace workspace, int maxExtensionSize)
  77. {
  78. workspace.ElementExtensions.Add (reader);
  79. }
  80. protected static bool TryParseAttribute (string name, string ns, string value, CategoriesDocument categories, string version)
  81. {
  82. return categories.TryParseAttribute (name, ns, value, version);
  83. }
  84. protected static bool TryParseAttribute (string name, string ns, string value, ResourceCollectionInfo collection, string version)
  85. {
  86. return collection.TryParseAttribute (name, ns, value, version);
  87. }
  88. protected static bool TryParseAttribute (string name, string ns, string value, ServiceDocument document, string version)
  89. {
  90. return document.TryParseAttribute (name, ns, value, version);
  91. }
  92. protected static bool TryParseAttribute (string name, string ns, string value, Workspace workspace, string version)
  93. {
  94. return workspace.TryParseAttribute (name, ns, value, version);
  95. }
  96. protected static bool TryParseElement (XmlReader reader, CategoriesDocument categories, string version)
  97. {
  98. return categories.TryParseElement (reader, version);
  99. }
  100. protected static bool TryParseElement (XmlReader reader, ResourceCollectionInfo collection, string version)
  101. {
  102. return collection.TryParseElement (reader, version);
  103. }
  104. protected static bool TryParseElement (XmlReader reader, ServiceDocument document, string version)
  105. {
  106. return document.TryParseElement (reader, version);
  107. }
  108. protected static bool TryParseElement (XmlReader reader, Workspace workspace, string version)
  109. {
  110. return workspace.TryParseElement (reader, version);
  111. }
  112. protected static void WriteAttributeExtensions (XmlWriter writer, CategoriesDocument categories, string version)
  113. {
  114. categories.WriteAttributeExtensions (writer, version);
  115. }
  116. protected static void WriteAttributeExtensions (XmlWriter writer, ResourceCollectionInfo collection, string version)
  117. {
  118. collection.WriteAttributeExtensions (writer, version);
  119. }
  120. protected static void WriteAttributeExtensions (XmlWriter writer, ServiceDocument document, string version)
  121. {
  122. document.WriteAttributeExtensions (writer, version);
  123. }
  124. protected static void WriteAttributeExtensions (XmlWriter writer, Workspace workspace, string version)
  125. {
  126. workspace.WriteAttributeExtensions (writer, version);
  127. }
  128. protected static void WriteElementExtensions (XmlWriter writer, CategoriesDocument categories, string version)
  129. {
  130. categories.WriteElementExtensions (writer, version);
  131. }
  132. protected static void WriteElementExtensions (XmlWriter writer, ResourceCollectionInfo collection, string version)
  133. {
  134. collection.WriteElementExtensions (writer, version);
  135. }
  136. protected static void WriteElementExtensions (XmlWriter writer, ServiceDocument document, string version)
  137. {
  138. document.WriteElementExtensions (writer, version);
  139. }
  140. protected static void WriteElementExtensions (XmlWriter writer, Workspace workspace, string version)
  141. {
  142. workspace.WriteElementExtensions (writer, version);
  143. }
  144. // instance members
  145. protected ServiceDocumentFormatter ()
  146. : this (new ServiceDocument ())
  147. {
  148. }
  149. protected ServiceDocumentFormatter (ServiceDocument documentToWrite)
  150. {
  151. SetDocument (documentToWrite);
  152. }
  153. public ServiceDocument Document { get; private set; }
  154. public abstract string Version { get; }
  155. public abstract bool CanRead (XmlReader reader);
  156. protected virtual ServiceDocument CreateDocumentInstance ()
  157. {
  158. return new ServiceDocument () { InternalFormatter = this };
  159. }
  160. public abstract void ReadFrom (XmlReader reader);
  161. protected virtual void SetDocument (ServiceDocument document)
  162. {
  163. if (document == null)
  164. throw new ArgumentNullException ("document");
  165. Document = document;
  166. }
  167. public abstract void WriteTo (XmlWriter writer);
  168. }
  169. }