2 // Rss20FeedFormatter.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.Generic;
30 using System.Collections.ObjectModel;
31 using System.Globalization;
33 using System.Runtime.Serialization;
36 using System.Xml.Schema;
37 using System.Xml.Serialization;
39 namespace System.ServiceModel.Syndication
41 [XmlRoot ("rss", Namespace = "")]
42 public class Rss20FeedFormatter : SyndicationFeedFormatter, IXmlSerializable
44 const string AtomNamespace ="http://www.w3.org/2005/Atom";
46 bool ext_atom_serialization, preserve_att_ext = true, preserve_elem_ext = true;
49 public Rss20FeedFormatter ()
51 ext_atom_serialization = true;
54 public Rss20FeedFormatter (SyndicationFeed feedToWrite)
55 : this (feedToWrite, true)
59 public Rss20FeedFormatter (SyndicationFeed feedToWrite, bool serializeExtensionsAsAtom)
62 ext_atom_serialization = serializeExtensionsAsAtom;
65 public Rss20FeedFormatter (Type feedTypeToCreate)
67 if (feedTypeToCreate == null)
68 throw new ArgumentNullException ("feedTypeToCreate");
69 feed_type = feedTypeToCreate;
72 public bool SerializeExtensionsAsAtom {
73 get { return ext_atom_serialization; }
74 set { ext_atom_serialization = value; }
77 protected Type FeedType {
78 get { return feed_type; }
81 public bool PreserveAttributeExtensions {
82 get { return preserve_att_ext; }
83 set { preserve_att_ext = value; }
86 public bool PreserveElementExtensions {
87 get { return preserve_elem_ext; }
88 set { preserve_elem_ext = value; }
91 public override string Version {
92 get { return "Rss20"; }
95 protected override SyndicationFeed CreateFeedInstance ()
97 return new SyndicationFeed ();
100 // hmm, why is it overriden? probably failed API cleanup.
101 protected internal override void SetFeed (SyndicationFeed feed)
106 public override bool CanRead (XmlReader reader)
109 throw new ArgumentNullException ("reader");
110 reader.MoveToContent ();
111 return reader.IsStartElement ("rss", String.Empty);
114 public override void ReadFrom (XmlReader reader)
116 if (!CanRead (reader))
117 throw new XmlException (String.Format ("Element '{0}' in namespace '{1}' is not accepted by this syndication formatter", reader.LocalName, reader.NamespaceURI));
118 ReadXml (reader, true);
121 protected virtual SyndicationItem ReadItem (XmlReader reader, SyndicationFeed feed)
123 Rss20ItemFormatter formatter = new Rss20ItemFormatter();
124 formatter.ReadFrom (reader);
125 return formatter.Item;
128 protected virtual IEnumerable<SyndicationItem> ReadItems (XmlReader reader, SyndicationFeed feed, out bool areAllItemsRead)
130 Collection<SyndicationItem> c = new Collection<SyndicationItem> ();
131 for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ())
132 if (reader.LocalName == "item" && reader.NamespaceURI == String.Empty)
133 c.Add (ReadItem (reader, feed));
134 areAllItemsRead = (reader.NodeType == XmlNodeType.EndElement);
138 [MonoTODO ("Find out how feedBaseUri is used")]
139 protected virtual void WriteItem (XmlWriter writer, SyndicationItem item, Uri feedBaseUri)
141 item.SaveAsRss20 (writer);
144 protected virtual void WriteItems (XmlWriter writer, IEnumerable<SyndicationItem> items, Uri feedBaseUri)
147 throw new ArgumentNullException ("items");
148 foreach (SyndicationItem item in items)
149 WriteItem (writer, item, feedBaseUri);
152 public override void WriteTo (XmlWriter writer)
154 WriteXml (writer, true);
157 void IXmlSerializable.ReadXml (XmlReader reader)
159 ReadXml (reader, false);
162 void IXmlSerializable.WriteXml (XmlWriter writer)
164 WriteXml (writer, false);
167 XmlSchema IXmlSerializable.GetSchema ()
174 void ReadXml (XmlReader reader, bool fromSerializable)
177 throw new ArgumentNullException ("reader");
178 SetFeed (CreateFeedInstance ());
180 reader.MoveToContent ();
182 string ver = reader.GetAttribute ("version");
184 throw new NotSupportedException (String.Format ("RSS Version '{0}' is not supported", ver));
186 if (PreserveAttributeExtensions && reader.MoveToFirstAttribute ()) {
188 if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
190 if (reader.NamespaceURI == String.Empty && reader.LocalName == "version")
192 if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, Feed, Version))
193 Feed.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
194 } while (reader.MoveToNextAttribute ());
197 reader.ReadStartElement (); // <rss> => <channel>
198 reader.MoveToContent ();
199 reader.ReadStartElement ("channel", String.Empty); // <channel> => *
201 Collection<SyndicationItem> items = null;
203 for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
204 if (reader.NodeType != XmlNodeType.Element)
205 throw new XmlException ("Only element node is expected under 'channel' element");
206 if (reader.NamespaceURI == String.Empty)
207 switch (reader.LocalName) {
209 Feed.Title = ReadTextSyndicationContent (reader);
212 SyndicationLink l = Feed.CreateLink ();
213 ReadLink (reader, l);
217 Feed.Description = ReadTextSyndicationContent (reader);
220 Feed.Language = reader.ReadElementContentAsString ();
223 Feed.Copyright = ReadTextSyndicationContent (reader);
225 case "managingEditor":
226 SyndicationPerson p = Feed.CreatePerson ();
227 ReadPerson (reader, p);
228 Feed.Authors.Add (p);
231 // FIXME: somehow DateTimeOffset causes the runtime crash.
232 reader.ReadElementContentAsString ();
233 // Feed.PublishDate = FromRFC822DateString (reader.ReadElementContentAsString ());
235 case "lastBuildDate":
236 // FIXME: somehow DateTimeOffset causes the runtime crash.
237 reader.ReadElementContentAsString ();
238 // Feed.LastUpdatedTime = FromRFC822DateString (reader.ReadElementContentAsString ());
241 SyndicationCategory c = Feed.CreateCategory ();
242 ReadCategory (reader, c);
243 Feed.Categories.Add (c);
246 Feed.Generator = reader.ReadElementContentAsString ();
248 // "webMaster" "docs" "cloud" "ttl" "image" "rating" "textInput" "skipHours" "skipDays" are not handled.
251 items = new Collection<SyndicationItem> ();
254 items.Add (ReadItem (reader, Feed));
257 if (!TryParseElement (reader, Feed, Version)) {
258 if (PreserveElementExtensions)
259 // FIXME: what to specify for maxExtensionSize
260 LoadElementExtensions (reader, Feed, int.MaxValue);
266 reader.ReadEndElement (); // </channel>
267 reader.ReadEndElement (); // </rss>
270 TextSyndicationContent ReadTextSyndicationContent (XmlReader reader)
272 TextSyndicationContentKind kind = TextSyndicationContentKind.Plaintext;
273 switch (reader.GetAttribute ("type")) {
275 kind = TextSyndicationContentKind.Html;
278 kind = TextSyndicationContentKind.XHtml;
281 string text = reader.ReadElementContentAsString ();
282 TextSyndicationContent t = new TextSyndicationContent (text, kind);
286 void ReadCategory (XmlReader reader, SyndicationCategory category)
288 if (reader.MoveToFirstAttribute ()) {
290 if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
292 if (reader.NamespaceURI == String.Empty) {
293 switch (reader.LocalName) {
295 category.Scheme = reader.Value;
299 if (PreserveAttributeExtensions)
300 if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, category, Version))
301 category.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
302 } while (reader.MoveToNextAttribute ());
303 reader.MoveToElement ();
306 if (!reader.IsEmptyElement) {
308 for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
309 if (reader.NodeType == XmlNodeType.Text)
310 category.Name += reader.Value;
311 else if (!TryParseElement (reader, category, Version)) {
312 if (PreserveElementExtensions)
313 // FIXME: what should be used for maxExtenswionSize
314 LoadElementExtensions (reader, category, int.MaxValue);
321 reader.Read (); // </category> or <category ... />
324 void ReadLink (XmlReader reader, SyndicationLink link)
326 if (PreserveAttributeExtensions && reader.MoveToFirstAttribute ()) {
328 if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
330 if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, link, Version))
331 link.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
332 } while (reader.MoveToNextAttribute ());
333 reader.MoveToElement ();
336 if (!reader.IsEmptyElement) {
339 for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
340 if (reader.NodeType == XmlNodeType.Text)
342 else if (!TryParseElement (reader, link, Version)) {
343 if (PreserveElementExtensions)
344 // FIXME: what should be used for maxExtenswionSize
345 LoadElementExtensions (reader, link, int.MaxValue);
351 link.Uri = CreateUri (url);
353 reader.Read (); // </link> or <link ... />
356 void ReadPerson (XmlReader reader, SyndicationPerson person)
358 if (PreserveAttributeExtensions && reader.MoveToFirstAttribute ()) {
360 if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
362 if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, person, Version))
363 person.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
364 } while (reader.MoveToNextAttribute ());
365 reader.MoveToElement ();
368 if (!reader.IsEmptyElement) {
370 for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
371 if (reader.NodeType == XmlNodeType.Text)
372 person.Email += reader.Value;
373 else if (!TryParseElement (reader, person, Version)) {
374 if (PreserveElementExtensions)
375 // FIXME: what should be used for maxExtenswionSize
376 LoadElementExtensions (reader, person, int.MaxValue);
383 reader.Read (); // end element or empty element
386 Uri CreateUri (string uri)
388 return new Uri (uri, UriKind.RelativeOrAbsolute);
393 void WriteXml (XmlWriter writer, bool writeRoot)
396 throw new ArgumentNullException ("writer");
398 throw new InvalidOperationException ("Syndication feed must be set before writing");
401 writer.WriteStartElement ("rss");
403 if (SerializeExtensionsAsAtom)
404 writer.WriteAttributeString ("xmlns", "a10", "http://www.w3.org/2000/xmlns/", AtomNamespace);
405 writer.WriteAttributeString ("version", "2.0");
407 writer.WriteStartElement ("channel");
409 if (Feed.BaseUri != null)
410 writer.WriteAttributeString ("xml:base", Feed.BaseUri.ToString ());
412 writer.WriteElementString ("title", String.Empty, Feed.Title != null ? Feed.Title.Text : String.Empty);
414 writer.WriteElementString ("description", String.Empty, Feed.Description != null ? Feed.Description.Text : String.Empty);
416 if (Feed.Copyright != null)
417 writer.WriteElementString ("copyright", String.Empty, Feed.Copyright.Text);
419 if (!Feed.LastUpdatedTime.Equals (default (DateTimeOffset))) {
420 writer.WriteStartElement ("lastBuildDate");
421 writer.WriteString (ToRFC822DateString (Feed.LastUpdatedTime));
422 writer.WriteEndElement ();
425 if (Feed.Generator != null)
426 writer.WriteElementString ("generator", String.Empty, Feed.Generator);
427 if (Feed.ImageUrl != null) {
428 writer.WriteStartElement ("image");
429 writer.WriteElementString ("url", String.Empty, Feed.ImageUrl.ToString ());
430 // FIXME: are they really empty?
431 writer.WriteElementString ("title", String.Empty, String.Empty);
432 writer.WriteElementString ("link", String.Empty, String.Empty);
433 writer.WriteEndElement ();
435 if (Feed.Language != null)
436 writer.WriteElementString ("language", String.Empty, Feed.Language);
438 foreach (SyndicationPerson author in Feed.Authors)
439 if (author != null) {
440 writer.WriteStartElement ("managingEditor");
441 WriteAttributeExtensions (writer, author, Version);
442 writer.WriteString (author.Email);
443 WriteElementExtensions (writer, author, Version);
444 writer.WriteEndElement ();
446 foreach (SyndicationCategory category in Feed.Categories)
447 if (category != null) {
448 writer.WriteStartElement ("category");
449 if (category.Scheme != null)
450 writer.WriteAttributeString ("domain", category.Scheme);
451 WriteAttributeExtensions (writer, category, Version);
452 writer.WriteString (category.Name);
453 WriteElementExtensions (writer, category, Version);
454 writer.WriteEndElement ();
457 foreach (SyndicationLink link in Feed.Links)
459 writer.WriteStartElement ("link");
460 WriteAttributeExtensions (writer, link, Version);
461 writer.WriteString (link.Uri != null ? link.Uri.ToString () : String.Empty);
462 WriteElementExtensions (writer, link, Version);
463 writer.WriteEndElement ();
466 WriteItems (writer, Feed.Items, Feed.BaseUri);
468 if (SerializeExtensionsAsAtom) {
470 if (Feed.Id != null) {
471 writer.WriteStartElement ("a10", "id", AtomNamespace);
472 writer.WriteString (Feed.Id);
473 writer.WriteEndElement ();
476 foreach (SyndicationPerson contributor in Feed.Contributors) {
477 if (contributor != null) {
478 writer.WriteStartElement ("a10", "contributor", AtomNamespace);
479 WriteAttributeExtensions (writer, contributor, Version);
480 writer.WriteElementString ("a10", "name", AtomNamespace, contributor.Name);
481 writer.WriteElementString ("a10", "uri", AtomNamespace, contributor.Uri);
482 writer.WriteElementString ("a10", "email", AtomNamespace, contributor.Email);
483 WriteElementExtensions (writer, contributor, Version);
484 writer.WriteEndElement ();
489 writer.WriteEndElement (); // </channel>
492 writer.WriteEndElement (); // </rss>
495 // FIXME: DateTimeOffset.ToString() needs another overload.
496 // When it is implemented, just remove ".DateTime" parts below.
497 string ToRFC822DateString (DateTimeOffset date)
499 switch (date.DateTime.Kind) {
500 case DateTimeKind.Utc:
501 return date.DateTime.ToString ("ddd, dd MMM yyyy HH:mm:ss 'Z'", DateTimeFormatInfo.InvariantInfo);
502 case DateTimeKind.Local:
503 StringBuilder sb = new StringBuilder (date.DateTime.ToString ("ddd, dd MMM yyyy HH:mm:ss zzz", DateTimeFormatInfo.InvariantInfo));
504 sb.Remove (sb.Length - 3, 1);
505 return sb.ToString (); // remove ':' from +hh:mm
507 return date.DateTime.ToString ("ddd, dd MMM yyyy HH:mm:ss", DateTimeFormatInfo.InvariantInfo);