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