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