Merge pull request #757 from mlintner/master
[mono.git] / mcs / class / System.ServiceModel.Web / System.ServiceModel.Syndication / ServiceDocumentFormatter.cs
1 //
2 // ServiceDocumentFormatter.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
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
36 namespace System.ServiceModel.Syndication
37 {
38         [DataContract]
39         public abstract class ServiceDocumentFormatter
40         {
41                 protected static SyndicationCategory CreateCategory (InlineCategoriesDocument inlineCategories)
42                 {
43                         return inlineCategories.CreateCategory ();
44                 }
45
46                 protected static ResourceCollectionInfo CreateCollection (Workspace workspace)
47                 {
48                         return workspace.CreateResourceCollection ();
49                 }
50
51                 protected static InlineCategoriesDocument CreateInlineCategories (ResourceCollectionInfo collection)
52                 {
53                         return collection.CreateInlineCategoriesDocument ();
54                 }
55
56                 protected static ReferencedCategoriesDocument CreateReferencedCategories (ResourceCollectionInfo collection)
57                 {
58                         return collection.CreateReferencedCategoriesDocument ();
59                 }
60
61                 protected static Workspace CreateWorkspace (ServiceDocument document)
62                 {
63                         return document.CreateWorkspace ();
64                 }
65
66                 [MonoTODO ("Use maxExtensionSize somewhere")]
67                 protected static void LoadElementExtensions (XmlReader reader, CategoriesDocument categories, int maxExtensionSize)
68                 {
69                         categories.ElementExtensions.Add (reader);
70                 }
71
72                 [MonoTODO ("Use maxExtensionSize somewhere")]
73                 protected static void LoadElementExtensions (XmlReader reader,ResourceCollectionInfo collection, int maxExtensionSize)
74                 {
75                         collection.ElementExtensions.Add (reader);
76                 }
77
78                 [MonoTODO ("Use maxExtensionSize somewhere")]
79                 protected static void LoadElementExtensions (XmlReader reader, ServiceDocument document, int maxExtensionSize)
80                 {
81                         document.ElementExtensions.Add (reader);
82                 }
83
84                 [MonoTODO ("Use maxExtensionSize somewhere")]
85                 protected static void LoadElementExtensions (XmlReader reader, Workspace workspace, int maxExtensionSize)
86                 {
87                         workspace.ElementExtensions.Add (reader);
88                 }
89
90                 protected static bool TryParseAttribute (string name, string ns, string value, CategoriesDocument categories, string version)
91                 {
92                         return categories.TryParseAttribute (name, ns, value, version);
93                 }
94
95                 protected static bool TryParseAttribute (string name, string ns, string value, ResourceCollectionInfo collection, string version)
96                 {
97                         return collection.TryParseAttribute (name, ns, value, version);
98                 }
99
100                 protected static bool TryParseAttribute (string name, string ns, string value, ServiceDocument document, string version)
101                 {
102                         return document.TryParseAttribute (name, ns, value, version);
103                 }
104
105                 protected static bool TryParseAttribute (string name, string ns, string value, Workspace workspace, string version)
106                 {
107                         return workspace.TryParseAttribute (name, ns, value, version);
108                 }
109
110                 protected static bool TryParseElement (XmlReader reader, CategoriesDocument categories, string version)
111                 {
112                         return categories.TryParseElement (reader, version);
113                 }
114
115                 protected static bool TryParseElement (XmlReader reader, ResourceCollectionInfo collection, string version)
116                 {
117                         return collection.TryParseElement (reader, version);
118                 }
119
120                 protected static bool TryParseElement (XmlReader reader, ServiceDocument document, string version)
121                 {
122                         return document.TryParseElement (reader, version);
123                 }
124
125                 protected static bool TryParseElement (XmlReader reader, Workspace workspace, string version)
126                 {
127                         return workspace.TryParseElement (reader, version);
128                 }
129
130                 protected static void WriteAttributeExtensions (XmlWriter writer, CategoriesDocument categories, string version)
131                 {
132                         categories.WriteAttributeExtensions (writer, version);
133                 }
134
135                 protected static void WriteAttributeExtensions (XmlWriter writer, ResourceCollectionInfo collection, string version)
136                 {
137                         collection.WriteAttributeExtensions (writer, version);
138                 }
139
140                 protected static void WriteAttributeExtensions (XmlWriter writer, ServiceDocument document, string version)
141                 {
142                         document.WriteAttributeExtensions (writer, version);
143                 }
144
145                 protected static void WriteAttributeExtensions (XmlWriter writer, Workspace workspace, string version)
146                 {
147                         workspace.WriteAttributeExtensions (writer, version);
148                 }
149
150                 protected static void WriteElementExtensions (XmlWriter writer, CategoriesDocument categories, string version)
151                 {
152                         categories.WriteElementExtensions (writer, version);
153                 }
154
155                 protected static void WriteElementExtensions (XmlWriter writer, ResourceCollectionInfo collection, string version)
156                 {
157                         collection.WriteElementExtensions (writer, version);
158                 }
159
160                 protected static void WriteElementExtensions (XmlWriter writer, ServiceDocument document, string version)
161                 {
162                         document.WriteElementExtensions (writer, version);
163                 }
164
165                 protected static void WriteElementExtensions (XmlWriter writer, Workspace workspace, string version)
166                 {
167                         workspace.WriteElementExtensions (writer, version);
168                 }
169
170                 // instance members
171
172                 protected ServiceDocumentFormatter ()
173                         : this (new ServiceDocument ())
174                 {
175                 }
176
177                 protected ServiceDocumentFormatter (ServiceDocument documentToWrite)
178                 {
179                         SetDocument (documentToWrite);
180                 }
181
182                 public ServiceDocument Document { get; private set; }
183
184                 public abstract string Version { get; }
185
186
187                 public abstract bool CanRead (XmlReader reader);
188
189                 protected virtual ServiceDocument CreateDocumentInstance ()
190                 {
191                         return new ServiceDocument () { InternalFormatter = this };
192                 }
193
194                 public abstract void ReadFrom (XmlReader reader);
195
196                 protected virtual void SetDocument (ServiceDocument document)
197                 {
198                         if (document == null)
199                                 throw new ArgumentNullException ("document");
200                         Document = document;
201                 }
202
203                 public abstract void WriteTo (XmlWriter writer);
204         }
205 }