| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 |
- //
- // Rss20FeedFormatter.cs
- //
- // Author:
- // Atsushi Enomoto <[email protected]>
- //
- // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Globalization;
- using System.IO;
- using System.Runtime.Serialization;
- using System.Text;
- using System.Xml;
- using System.Xml.Schema;
- using System.Xml.Serialization;
- namespace System.ServiceModel.Syndication
- {
- [XmlRoot ("rss", Namespace = "")]
- public class Rss20FeedFormatter : SyndicationFeedFormatter, IXmlSerializable
- {
- const string AtomNamespace ="http://www.w3.org/2005/Atom";
- bool ext_atom_serialization, preserve_att_ext = true, preserve_elem_ext = true;
- Type feed_type;
- public Rss20FeedFormatter ()
- {
- ext_atom_serialization = true;
- }
- public Rss20FeedFormatter (SyndicationFeed feedToWrite)
- : this (feedToWrite, true)
- {
- }
- public Rss20FeedFormatter (SyndicationFeed feedToWrite, bool serializeExtensionsAsAtom)
- : base (feedToWrite)
- {
- ext_atom_serialization = serializeExtensionsAsAtom;
- }
- public Rss20FeedFormatter (Type feedTypeToCreate)
- {
- if (feedTypeToCreate == null)
- throw new ArgumentNullException ("feedTypeToCreate");
- feed_type = feedTypeToCreate;
- }
- public bool SerializeExtensionsAsAtom {
- get { return ext_atom_serialization; }
- set { ext_atom_serialization = value; }
- }
- protected Type FeedType {
- get { return feed_type; }
- }
- public bool PreserveAttributeExtensions {
- get { return preserve_att_ext; }
- set { preserve_att_ext = value; }
- }
- public bool PreserveElementExtensions {
- get { return preserve_elem_ext; }
- set { preserve_elem_ext = value; }
- }
- public override string Version {
- get { return "Rss20"; }
- }
- protected override SyndicationFeed CreateFeedInstance ()
- {
- return new SyndicationFeed ();
- }
- // hmm, why is it overriden? probably failed API cleanup.
- protected internal override void SetFeed (SyndicationFeed feed)
- {
- base.SetFeed (feed);
- }
- public override bool CanRead (XmlReader reader)
- {
- if (reader == null)
- throw new ArgumentNullException ("reader");
- reader.MoveToContent ();
- return reader.IsStartElement ("rss", String.Empty);
- }
- public override void ReadFrom (XmlReader reader)
- {
- if (!CanRead (reader))
- throw new XmlException (String.Format ("Element '{0}' in namespace '{1}' is not accepted by this syndication formatter", reader.LocalName, reader.NamespaceURI));
- ReadXml (reader, true);
- }
- protected virtual SyndicationItem ReadItem (XmlReader reader, SyndicationFeed feed)
- {
- Rss20ItemFormatter formatter = new Rss20ItemFormatter();
- formatter.ReadFrom (reader);
- return formatter.Item;
- }
- protected virtual IEnumerable<SyndicationItem> ReadItems (XmlReader reader, SyndicationFeed feed, out bool areAllItemsRead)
- {
- Collection<SyndicationItem> c = new Collection<SyndicationItem> ();
- for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ())
- if (reader.LocalName == "item" && reader.NamespaceURI == String.Empty)
- c.Add (ReadItem (reader, feed));
- areAllItemsRead = (reader.NodeType == XmlNodeType.EndElement);
- return c;
- }
- protected virtual void WriteItem (XmlWriter writer, SyndicationItem item, Uri feedBaseUri)
- {
- item.SaveAsRss20 (writer);
- }
- protected virtual void WriteItems (XmlWriter writer, IEnumerable<SyndicationItem> items, Uri feedBaseUri)
- {
- if (items == null)
- throw new ArgumentNullException ("items");
- foreach (SyndicationItem item in items)
- WriteItem (writer, item, feedBaseUri);
- }
- public override void WriteTo (XmlWriter writer)
- {
- WriteXml (writer, true);
- }
- void IXmlSerializable.ReadXml (XmlReader reader)
- {
- ReadXml (reader, false);
- }
- void IXmlSerializable.WriteXml (XmlWriter writer)
- {
- WriteXml (writer, false);
- }
- XmlSchema IXmlSerializable.GetSchema ()
- {
- return null;
- }
- // read
- void ReadXml (XmlReader reader, bool fromSerializable)
- {
- if (reader == null)
- throw new ArgumentNullException ("reader");
- SetFeed (CreateFeedInstance ());
- reader.MoveToContent ();
- string ver = reader.GetAttribute ("version");
- if (ver != "2.0")
- throw new NotSupportedException (String.Format ("RSS Version '{0}' is not supported", ver));
- if (PreserveAttributeExtensions && reader.MoveToFirstAttribute ()) {
- do {
- if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
- continue;
- if (reader.NamespaceURI == String.Empty && reader.LocalName == "version")
- continue;
- if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, Feed, Version))
- Feed.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
- } while (reader.MoveToNextAttribute ());
- }
- reader.ReadStartElement (); // <rss> => <channel>
- reader.MoveToContent ();
- reader.ReadStartElement ("channel", String.Empty); // <channel> => *
- Collection<SyndicationItem> items = null;
- for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
- if (reader.NodeType != XmlNodeType.Element)
- throw new XmlException ("Only element node is expected under 'channel' element");
- if (reader.NamespaceURI == String.Empty)
- switch (reader.LocalName) {
- case "title":
- Feed.Title = ReadTextSyndicationContent (reader);
- continue;
- case "link":
- SyndicationLink l = Feed.CreateLink ();
- ReadLink (reader, l);
- Feed.Links.Add (l);
- continue;
- case "description":
- Feed.Description = ReadTextSyndicationContent (reader);
- continue;
- case "language":
- Feed.Language = reader.ReadElementContentAsString ();
- continue;
- case "copyright":
- Feed.Copyright = ReadTextSyndicationContent (reader);
- continue;
- case "managingEditor":
- SyndicationPerson p = Feed.CreatePerson ();
- ReadPerson (reader, p);
- Feed.Authors.Add (p);
- continue;
- case "pubDate":
- // FIXME: somehow DateTimeOffset causes the runtime crash.
- reader.ReadElementContentAsString ();
- // Feed.PublishDate = FromRFC822DateString (reader.ReadElementContentAsString ());
- continue;
- case "lastBuildDate":
- // FIXME: somehow DateTimeOffset causes the runtime crash.
- reader.ReadElementContentAsString ();
- // Feed.LastUpdatedTime = FromRFC822DateString (reader.ReadElementContentAsString ());
- continue;
- case "category":
- SyndicationCategory c = Feed.CreateCategory ();
- ReadCategory (reader, c);
- Feed.Categories.Add (c);
- continue;
- case "generator":
- Feed.Generator = reader.ReadElementContentAsString ();
- continue;
- // "webMaster" "docs" "cloud" "ttl" "image" "rating" "textInput" "skipHours" "skipDays" are not handled.
- case "item":
- if (items == null) {
- items = new Collection<SyndicationItem> ();
- Feed.Items = items;
- }
- items.Add (ReadItem (reader, Feed));
- continue;
- }
- if (!TryParseElement (reader, Feed, Version)) {
- if (PreserveElementExtensions)
- // FIXME: what to specify for maxExtensionSize
- LoadElementExtensions (reader, Feed, int.MaxValue);
- else
- reader.Skip ();
- }
- }
- reader.ReadEndElement (); // </channel>
- reader.ReadEndElement (); // </rss>
- }
- TextSyndicationContent ReadTextSyndicationContent (XmlReader reader)
- {
- TextSyndicationContentKind kind = TextSyndicationContentKind.Plaintext;
- switch (reader.GetAttribute ("type")) {
- case "html":
- kind = TextSyndicationContentKind.Html;
- break;
- case "xhtml":
- kind = TextSyndicationContentKind.XHtml;
- break;
- }
- string text = reader.ReadElementContentAsString ();
- TextSyndicationContent t = new TextSyndicationContent (text, kind);
- return t;
- }
- void ReadCategory (XmlReader reader, SyndicationCategory category)
- {
- if (reader.MoveToFirstAttribute ()) {
- do {
- if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
- continue;
- if (reader.NamespaceURI == String.Empty) {
- switch (reader.LocalName) {
- case "domain":
- category.Scheme = reader.Value;
- continue;
- }
- }
- if (PreserveAttributeExtensions)
- if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, category, Version))
- category.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
- } while (reader.MoveToNextAttribute ());
- reader.MoveToElement ();
- }
- if (!reader.IsEmptyElement) {
- reader.Read ();
- for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
- if (reader.IsTextNode ())
- category.Name += reader.Value;
- else if (!TryParseElement (reader, category, Version)) {
- if (PreserveElementExtensions)
- // FIXME: what should be used for maxExtenswionSize
- LoadElementExtensions (reader, category, int.MaxValue);
- else
- reader.Skip ();
- }
- reader.Read ();
- }
- }
- reader.Read (); // </category> or <category ... />
- }
- void ReadLink (XmlReader reader, SyndicationLink link)
- {
- if (PreserveAttributeExtensions && reader.MoveToFirstAttribute ()) {
- do {
- if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
- continue;
- if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, link, Version))
- link.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
- } while (reader.MoveToNextAttribute ());
- reader.MoveToElement ();
- }
- if (!reader.IsEmptyElement) {
- string url = null;
- reader.Read ();
- for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
- if (reader.IsTextNode ())
- url += reader.Value;
- else if (!TryParseElement (reader, link, Version)) {
- if (PreserveElementExtensions)
- // FIXME: what should be used for maxExtenswionSize
- LoadElementExtensions (reader, link, int.MaxValue);
- else
- reader.Skip ();
- }
- reader.Read ();
- }
- link.Uri = CreateUri (url);
- }
- reader.Read (); // </link> or <link ... />
- }
- void ReadPerson (XmlReader reader, SyndicationPerson person)
- {
- if (PreserveAttributeExtensions && reader.MoveToFirstAttribute ()) {
- do {
- if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
- continue;
- if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, person, Version))
- person.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
- } while (reader.MoveToNextAttribute ());
- reader.MoveToElement ();
- }
- if (!reader.IsEmptyElement) {
- reader.Read ();
- for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
- if (reader.IsTextNode ())
- person.Email += reader.Value;
- else if (!TryParseElement (reader, person, Version)) {
- if (PreserveElementExtensions)
- // FIXME: what should be used for maxExtenswionSize
- LoadElementExtensions (reader, person, int.MaxValue);
- else
- reader.Skip ();
- }
- reader.Read ();
- }
- }
- reader.Read (); // end element or empty element
- }
- Uri CreateUri (string uri)
- {
- return new Uri (uri, UriKind.RelativeOrAbsolute);
- }
- // write
- void WriteXml (XmlWriter writer, bool writeRoot)
- {
- if (writer == null)
- throw new ArgumentNullException ("writer");
- if (Feed == null)
- throw new InvalidOperationException ("Syndication feed must be set before writing");
- if (writeRoot)
- writer.WriteStartElement ("rss");
- if (SerializeExtensionsAsAtom)
- writer.WriteAttributeString ("xmlns", "a10", "http://www.w3.org/2000/xmlns/", AtomNamespace);
- writer.WriteAttributeString ("version", "2.0");
- writer.WriteStartElement ("channel");
- if (Feed.BaseUri != null)
- writer.WriteAttributeString ("xml:base", Feed.BaseUri.ToString ());
- writer.WriteElementString ("title", String.Empty, Feed.Title != null ? Feed.Title.Text : String.Empty);
- writer.WriteElementString ("description", String.Empty, Feed.Description != null ? Feed.Description.Text : String.Empty);
- if (Feed.Copyright != null)
- writer.WriteElementString ("copyright", String.Empty, Feed.Copyright.Text);
- if (!Feed.LastUpdatedTime.Equals (default (DateTimeOffset))) {
- writer.WriteStartElement ("lastBuildDate");
- writer.WriteString (ToRFC822DateString (Feed.LastUpdatedTime));
- writer.WriteEndElement ();
- }
- if (Feed.Generator != null)
- writer.WriteElementString ("generator", String.Empty, Feed.Generator);
- if (Feed.ImageUrl != null) {
- writer.WriteStartElement ("image");
- writer.WriteElementString ("url", String.Empty, Feed.ImageUrl.ToString ());
- // FIXME: are they really empty?
- writer.WriteElementString ("title", String.Empty, String.Empty);
- writer.WriteElementString ("link", String.Empty, String.Empty);
- writer.WriteEndElement ();
- }
- if (Feed.Language != null)
- writer.WriteElementString ("language", String.Empty, Feed.Language);
- foreach (SyndicationPerson author in Feed.Authors)
- if (author != null) {
- writer.WriteStartElement ("managingEditor");
- WriteAttributeExtensions (writer, author, Version);
- writer.WriteString (author.Email);
- WriteElementExtensions (writer, author, Version);
- writer.WriteEndElement ();
- }
- foreach (SyndicationCategory category in Feed.Categories)
- if (category != null) {
- writer.WriteStartElement ("category");
- if (category.Scheme != null)
- writer.WriteAttributeString ("domain", category.Scheme);
- WriteAttributeExtensions (writer, category, Version);
- writer.WriteString (category.Name);
- WriteElementExtensions (writer, category, Version);
- writer.WriteEndElement ();
- }
- foreach (SyndicationLink link in Feed.Links)
- if (link != null) {
- writer.WriteStartElement ("link");
- WriteAttributeExtensions (writer, link, Version);
- writer.WriteString (link.Uri != null ? link.Uri.ToString () : String.Empty);
- WriteElementExtensions (writer, link, Version);
- writer.WriteEndElement ();
- }
- WriteItems (writer, Feed.Items, Feed.BaseUri);
- if (SerializeExtensionsAsAtom) {
- if (Feed.Id != null) {
- writer.WriteStartElement ("a10", "id", AtomNamespace);
- writer.WriteString (Feed.Id);
- writer.WriteEndElement ();
- }
- foreach (SyndicationPerson contributor in Feed.Contributors) {
- if (contributor != null) {
- writer.WriteStartElement ("a10", "contributor", AtomNamespace);
- WriteAttributeExtensions (writer, contributor, Version);
- writer.WriteElementString ("a10", "name", AtomNamespace, contributor.Name);
- writer.WriteElementString ("a10", "uri", AtomNamespace, contributor.Uri);
- writer.WriteElementString ("a10", "email", AtomNamespace, contributor.Email);
- WriteElementExtensions (writer, contributor, Version);
- writer.WriteEndElement ();
- }
- }
- }
- writer.WriteEndElement (); // </channel>
- if (writeRoot)
- writer.WriteEndElement (); // </rss>
- }
- // FIXME: DateTimeOffset.ToString() needs another overload.
- // When it is implemented, just remove ".DateTime" parts below.
- string ToRFC822DateString (DateTimeOffset date)
- {
- switch (date.DateTime.Kind) {
- case DateTimeKind.Utc:
- return date.DateTime.ToString ("ddd, dd MMM yyyy HH:mm:ss 'Z'", DateTimeFormatInfo.InvariantInfo);
- case DateTimeKind.Local:
- StringBuilder sb = new StringBuilder (date.DateTime.ToString ("ddd, dd MMM yyyy HH:mm:ss zzz", DateTimeFormatInfo.InvariantInfo));
- sb.Remove (sb.Length - 3, 1);
- return sb.ToString (); // remove ':' from +hh:mm
- default:
- return date.DateTime.ToString ("ddd, dd MMM yyyy HH:mm:ss", DateTimeFormatInfo.InvariantInfo);
- }
- }
- }
- }
|