move to from olive to mcs
[mono.git] / mcs / class / System.ServiceModel.Web / System.ServiceModel.Syndication / Rss20FeedFormatter.cs
1 //
2 // Rss20FeedFormatter.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.Globalization;
32 using System.IO;
33 using System.Runtime.Serialization;
34 using System.Text;
35 using System.Xml;
36 using System.Xml.Schema;
37 using System.Xml.Serialization;
38
39 namespace System.ServiceModel.Syndication
40 {
41         [XmlRoot ("rss", Namespace = "")]
42         public class Rss20FeedFormatter : SyndicationFeedFormatter, IXmlSerializable
43         {
44                 const string AtomNamespace ="http://www.w3.org/2005/Atom";
45
46                 bool ext_atom_serialization, preserve_att_ext = true, preserve_elem_ext = true;
47                 Type feed_type;
48
49                 public Rss20FeedFormatter ()
50                 {
51                         ext_atom_serialization = true;
52                 }
53
54                 public Rss20FeedFormatter (SyndicationFeed feedToWrite)
55                         : this (feedToWrite, true)
56                 {
57                 }
58
59                 public Rss20FeedFormatter (SyndicationFeed feedToWrite, bool serializeExtensionsAsAtom)
60                         : base (feedToWrite)
61                 {
62                         ext_atom_serialization = serializeExtensionsAsAtom;
63                 }
64
65                 public Rss20FeedFormatter (Type feedTypeToCreate)
66                 {
67                         if (feedTypeToCreate == null)
68                                 throw new ArgumentNullException ("feedTypeToCreate");
69                         feed_type = feedTypeToCreate;
70                 }
71
72                 public bool SerializeExtensionsAsAtom {
73                         get { return ext_atom_serialization; }
74                         set { ext_atom_serialization = value; }
75                 }
76
77                 protected Type FeedType {
78                         get { return feed_type; }
79                 }
80
81                 public bool PreserveAttributeExtensions {
82                         get { return preserve_att_ext; }
83                         set { preserve_att_ext = value; }
84                 }
85
86                 public bool PreserveElementExtensions {
87                         get { return preserve_elem_ext; }
88                         set { preserve_elem_ext = value; }
89                 }
90
91                 public override string Version {
92                         get { return "Rss20"; }
93                 }
94
95                 protected override SyndicationFeed CreateFeedInstance ()
96                 {
97                         return new SyndicationFeed ();
98                 }
99
100                 // hmm, why is it overriden? probably failed API cleanup.
101                 protected internal override void SetFeed (SyndicationFeed feed)
102                 {
103                         base.SetFeed (feed);
104                 }
105
106                 public override bool CanRead (XmlReader reader)
107                 {
108                         if (reader == null)
109                                 throw new ArgumentNullException ("reader");
110                         reader.MoveToContent ();
111                         return reader.IsStartElement ("rss", String.Empty);
112                 }
113
114                 public override void ReadFrom (XmlReader reader)
115                 {
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);
119                 }
120
121                 protected virtual SyndicationItem ReadItem (XmlReader reader, SyndicationFeed feed)
122                 {
123                         Rss20ItemFormatter formatter = new Rss20ItemFormatter();
124                         formatter.ReadFrom (reader);
125                         return formatter.Item;
126                 }
127
128                 protected virtual IEnumerable<SyndicationItem> ReadItems (XmlReader reader, SyndicationFeed feed, out bool areAllItemsRead)
129                 {
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);
135                         return c;
136                 }
137
138                 [MonoTODO ("Find out how feedBaseUri is used")]
139                 protected virtual void WriteItem (XmlWriter writer, SyndicationItem item, Uri feedBaseUri)
140                 {
141                         item.SaveAsRss20 (writer);
142                 }
143
144                 protected virtual void WriteItems (XmlWriter writer, IEnumerable<SyndicationItem> items, Uri feedBaseUri)
145                 {
146                         if (items == null)
147                                 throw new ArgumentNullException ("items");
148                         foreach (SyndicationItem item in items)
149                                 WriteItem (writer, item, feedBaseUri);
150                 }
151
152                 public override void WriteTo (XmlWriter writer)
153                 {
154                         WriteXml (writer, true);
155                 }
156
157                 void IXmlSerializable.ReadXml (XmlReader reader)
158                 {
159                         ReadXml (reader, false);
160                 }
161
162                 void IXmlSerializable.WriteXml (XmlWriter writer)
163                 {
164                         WriteXml (writer, false);
165                 }
166
167                 XmlSchema IXmlSerializable.GetSchema ()
168                 {
169                         return null;
170                 }
171
172                 // read
173
174                 void ReadXml (XmlReader reader, bool fromSerializable)
175                 {
176                         if (reader == null)
177                                 throw new ArgumentNullException ("reader");
178                         SetFeed (CreateFeedInstance ());
179
180                         reader.MoveToContent ();
181
182                         string ver = reader.GetAttribute ("version");
183                         if (ver != "2.0")
184                                 throw new NotSupportedException (String.Format ("RSS Version '{0}' is not supported", ver));
185
186                         if (PreserveAttributeExtensions && reader.MoveToFirstAttribute ()) {
187                                 do {
188                                         if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
189                                                 continue;
190                                         if (reader.NamespaceURI == String.Empty && reader.LocalName == "version")
191                                                 continue;
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 ());
195                         }
196
197                         reader.ReadStartElement (); // <rss> => <channel>
198                         reader.MoveToContent ();
199                         reader.ReadStartElement ("channel", String.Empty); // <channel> => *
200
201                         Collection<SyndicationItem> items = null;
202
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) {
208                                         case "title":
209                                                 Feed.Title = ReadTextSyndicationContent (reader);
210                                                 continue;
211                                         case "link":
212                                                 SyndicationLink l = Feed.CreateLink ();
213                                                 ReadLink (reader, l);
214                                                 Feed.Links.Add (l);
215                                                 continue;
216                                         case "description":
217                                                 Feed.Description = ReadTextSyndicationContent (reader);
218                                                 continue;
219                                         case "language":
220                                                 Feed.Language = reader.ReadElementContentAsString ();
221                                                 continue;
222                                         case "copyright":
223                                                 Feed.Copyright = ReadTextSyndicationContent (reader);
224                                                 continue;
225                                         case "managingEditor":
226                                                 SyndicationPerson p = Feed.CreatePerson ();
227                                                 ReadPerson (reader, p);
228                                                 Feed.Authors.Add (p);
229                                                 continue;
230                                         case "pubDate":
231                                                 // FIXME: somehow DateTimeOffset causes the runtime crash.
232                                                 reader.ReadElementContentAsString ();
233                                                 // Feed.PublishDate = FromRFC822DateString (reader.ReadElementContentAsString ());
234                                                 continue;
235                                         case "lastBuildDate":
236                                                 // FIXME: somehow DateTimeOffset causes the runtime crash.
237                                                 reader.ReadElementContentAsString ();
238                                                 // Feed.LastUpdatedTime = FromRFC822DateString (reader.ReadElementContentAsString ());
239                                                 continue;
240                                         case "category":
241                                                 SyndicationCategory c = Feed.CreateCategory ();
242                                                 ReadCategory (reader, c);
243                                                 Feed.Categories.Add (c);
244                                                 continue;
245                                         case "generator":
246                                                 Feed.Generator = reader.ReadElementContentAsString ();
247                                                 continue;
248                                         //  "webMaster" "docs" "cloud" "ttl" "image" "rating" "textInput" "skipHours" "skipDays" are not handled.
249                                         case "item":
250                                                 if (items == null) {
251                                                         items = new Collection<SyndicationItem> ();
252                                                         Feed.Items = items;
253                                                 }
254                                                 items.Add (ReadItem (reader, Feed));
255                                                 continue;
256                                         }
257                                 if (!TryParseElement (reader, Feed, Version)) {
258                                         if (PreserveElementExtensions)
259                                                 // FIXME: what to specify for maxExtensionSize
260                                                 LoadElementExtensions (reader, Feed, int.MaxValue);
261                                         else
262                                                 reader.Skip ();
263                                 }
264                         }
265
266                         reader.ReadEndElement (); // </channel>
267                         reader.ReadEndElement (); // </rss>
268                 }
269
270                 TextSyndicationContent ReadTextSyndicationContent (XmlReader reader)
271                 {
272                         TextSyndicationContentKind kind = TextSyndicationContentKind.Plaintext;
273                         switch (reader.GetAttribute ("type")) {
274                         case "html":
275                                 kind = TextSyndicationContentKind.Html;
276                                 break;
277                         case "xhtml":
278                                 kind = TextSyndicationContentKind.XHtml;
279                                 break;
280                         }
281                         string text = reader.ReadElementContentAsString ();
282                         TextSyndicationContent t = new TextSyndicationContent (text, kind);
283                         return t;
284                 }
285
286                 void ReadCategory (XmlReader reader, SyndicationCategory category)
287                 {
288                         if (reader.MoveToFirstAttribute ()) {
289                                 do {
290                                         if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
291                                                 continue;
292                                         if (reader.NamespaceURI == String.Empty) {
293                                                 switch (reader.LocalName) {
294                                                 case "domain":
295                                                         category.Scheme = reader.Value;
296                                                         continue;
297                                                 }
298                                         }
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 ();
304                         }
305
306                         if (!reader.IsEmptyElement) {
307                                 reader.Read ();
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);
315                                                 else
316                                                         reader.Skip ();
317                                         }
318                                         reader.Read ();
319                                 }
320                         }
321                         reader.Read (); // </category> or <category ... />
322                 }
323
324                 void ReadLink (XmlReader reader, SyndicationLink link)
325                 {
326                         if (PreserveAttributeExtensions && reader.MoveToFirstAttribute ()) {
327                                 do {
328                                         if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
329                                                 continue;
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 ();
334                         }
335
336                         if (!reader.IsEmptyElement) {
337                                 string url = null;
338                                 reader.Read ();
339                                 for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
340                                         if (reader.NodeType == XmlNodeType.Text)
341                                                 url += reader.Value;
342                                         else if (!TryParseElement (reader, link, Version)) {
343                                                 if (PreserveElementExtensions)
344                                                         // FIXME: what should be used for maxExtenswionSize
345                                                         LoadElementExtensions (reader, link, int.MaxValue);
346                                                 else
347                                                         reader.Skip ();
348                                         }
349                                         reader.Read ();
350                                 }
351                                 link.Uri = CreateUri (url);
352                         }
353                         reader.Read (); // </link> or <link ... />
354                 }
355
356                 void ReadPerson (XmlReader reader, SyndicationPerson person)
357                 {
358                         if (PreserveAttributeExtensions && reader.MoveToFirstAttribute ()) {
359                                 do {
360                                         if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
361                                                 continue;
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 ();
366                         }
367
368                         if (!reader.IsEmptyElement) {
369                                 reader.Read ();
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);
377                                                 else
378                                                         reader.Skip ();
379                                         }
380                                         reader.Read ();
381                                 }
382                         }
383                         reader.Read (); // end element or empty element
384                 }
385
386                 Uri CreateUri (string uri)
387                 {
388                         return new Uri (uri, UriKind.RelativeOrAbsolute);
389                 }
390
391                 // write
392
393                 void WriteXml (XmlWriter writer, bool writeRoot)
394                 {
395                         if (writer == null)
396                                 throw new ArgumentNullException ("writer");
397                         if (Feed == null)
398                                 throw new InvalidOperationException ("Syndication feed must be set before writing");
399
400                         if (writeRoot)
401                                 writer.WriteStartElement ("rss");
402
403                         if (SerializeExtensionsAsAtom)
404                                 writer.WriteAttributeString ("xmlns", "a10", "http://www.w3.org/2000/xmlns/", AtomNamespace);
405                         writer.WriteAttributeString ("version", "2.0");
406
407                         writer.WriteStartElement ("channel");
408
409                         if (Feed.BaseUri != null)
410                                 writer.WriteAttributeString ("xml:base", Feed.BaseUri.ToString ());
411
412                         writer.WriteElementString ("title", String.Empty, Feed.Title != null ? Feed.Title.Text : String.Empty);
413
414                         writer.WriteElementString ("description", String.Empty, Feed.Description != null ? Feed.Description.Text : String.Empty);
415
416                         if (Feed.Copyright != null)
417                                 writer.WriteElementString ("copyright", String.Empty, Feed.Copyright.Text);
418
419                         if (!Feed.LastUpdatedTime.Equals (default (DateTimeOffset))) {
420                                 writer.WriteStartElement ("lastBuildDate");
421                                 writer.WriteString (ToRFC822DateString (Feed.LastUpdatedTime));
422                                 writer.WriteEndElement ();
423                         }
424
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 ();
434                         }
435                         if (Feed.Language != null)
436                                 writer.WriteElementString ("language", String.Empty, Feed.Language);
437
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 ();
445                                 }
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 ();
455                                 }
456
457                         foreach (SyndicationLink link in Feed.Links)
458                                 if (link != null) {
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 ();
464                                 }
465
466                         WriteItems (writer, Feed.Items, Feed.BaseUri);
467
468                         if (SerializeExtensionsAsAtom) {
469
470                                 if (Feed.Id != null) {
471                                         writer.WriteStartElement ("a10", "id", AtomNamespace);
472                                         writer.WriteString (Feed.Id);
473                                         writer.WriteEndElement ();
474                                 }
475
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 ();
485                                         }
486                                 }
487                         }
488
489                         writer.WriteEndElement (); // </channel>
490
491                         if (writeRoot)
492                                 writer.WriteEndElement (); // </rss>
493                 }
494
495                 // FIXME: DateTimeOffset.ToString() needs another overload.
496                 // When it is implemented, just remove ".DateTime" parts below.
497                 string ToRFC822DateString (DateTimeOffset date)
498                 {
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
506                         default:
507                                 return date.DateTime.ToString ("ddd, dd MMM yyyy HH:mm:ss", DateTimeFormatInfo.InvariantInfo);
508                         }
509                 }
510         }
511 }