Rss20FeedFormatter.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. //
  2. // Rss20FeedFormatter.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  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. namespace System.ServiceModel.Syndication
  39. {
  40. [XmlRoot ("rss", Namespace = "")]
  41. public class Rss20FeedFormatter : SyndicationFeedFormatter, IXmlSerializable
  42. {
  43. const string AtomNamespace ="http://www.w3.org/2005/Atom";
  44. bool ext_atom_serialization, preserve_att_ext = true, preserve_elem_ext = true;
  45. Type feed_type;
  46. public Rss20FeedFormatter ()
  47. {
  48. ext_atom_serialization = true;
  49. }
  50. public Rss20FeedFormatter (SyndicationFeed feedToWrite)
  51. : this (feedToWrite, true)
  52. {
  53. }
  54. public Rss20FeedFormatter (SyndicationFeed feedToWrite, bool serializeExtensionsAsAtom)
  55. : base (feedToWrite)
  56. {
  57. ext_atom_serialization = serializeExtensionsAsAtom;
  58. }
  59. public Rss20FeedFormatter (Type feedTypeToCreate)
  60. {
  61. if (feedTypeToCreate == null)
  62. throw new ArgumentNullException ("feedTypeToCreate");
  63. feed_type = feedTypeToCreate;
  64. }
  65. public bool SerializeExtensionsAsAtom {
  66. get { return ext_atom_serialization; }
  67. set { ext_atom_serialization = value; }
  68. }
  69. protected Type FeedType {
  70. get { return feed_type; }
  71. }
  72. public bool PreserveAttributeExtensions {
  73. get { return preserve_att_ext; }
  74. set { preserve_att_ext = value; }
  75. }
  76. public bool PreserveElementExtensions {
  77. get { return preserve_elem_ext; }
  78. set { preserve_elem_ext = value; }
  79. }
  80. public override string Version {
  81. get { return "Rss20"; }
  82. }
  83. protected override SyndicationFeed CreateFeedInstance ()
  84. {
  85. return new SyndicationFeed ();
  86. }
  87. // hmm, why is it overriden? probably failed API cleanup.
  88. protected internal override void SetFeed (SyndicationFeed feed)
  89. {
  90. base.SetFeed (feed);
  91. }
  92. public override bool CanRead (XmlReader reader)
  93. {
  94. if (reader == null)
  95. throw new ArgumentNullException ("reader");
  96. reader.MoveToContent ();
  97. return reader.IsStartElement ("rss", String.Empty);
  98. }
  99. public override void ReadFrom (XmlReader reader)
  100. {
  101. if (!CanRead (reader))
  102. throw new XmlException (String.Format ("Element '{0}' in namespace '{1}' is not accepted by this syndication formatter", reader.LocalName, reader.NamespaceURI));
  103. ReadXml (reader, true);
  104. }
  105. protected virtual SyndicationItem ReadItem (XmlReader reader, SyndicationFeed feed)
  106. {
  107. Rss20ItemFormatter formatter = new Rss20ItemFormatter();
  108. formatter.ReadFrom (reader);
  109. return formatter.Item;
  110. }
  111. protected virtual IEnumerable<SyndicationItem> ReadItems (XmlReader reader, SyndicationFeed feed, out bool areAllItemsRead)
  112. {
  113. Collection<SyndicationItem> c = new Collection<SyndicationItem> ();
  114. for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ())
  115. if (reader.LocalName == "item" && reader.NamespaceURI == String.Empty)
  116. c.Add (ReadItem (reader, feed));
  117. areAllItemsRead = (reader.NodeType == XmlNodeType.EndElement);
  118. return c;
  119. }
  120. protected virtual void WriteItem (XmlWriter writer, SyndicationItem item, Uri feedBaseUri)
  121. {
  122. item.SaveAsRss20 (writer);
  123. }
  124. protected virtual void WriteItems (XmlWriter writer, IEnumerable<SyndicationItem> items, Uri feedBaseUri)
  125. {
  126. if (items == null)
  127. throw new ArgumentNullException ("items");
  128. foreach (SyndicationItem item in items)
  129. WriteItem (writer, item, feedBaseUri);
  130. }
  131. public override void WriteTo (XmlWriter writer)
  132. {
  133. WriteXml (writer, true);
  134. }
  135. void IXmlSerializable.ReadXml (XmlReader reader)
  136. {
  137. ReadXml (reader, false);
  138. }
  139. void IXmlSerializable.WriteXml (XmlWriter writer)
  140. {
  141. WriteXml (writer, false);
  142. }
  143. XmlSchema IXmlSerializable.GetSchema ()
  144. {
  145. return null;
  146. }
  147. // read
  148. void ReadXml (XmlReader reader, bool fromSerializable)
  149. {
  150. if (reader == null)
  151. throw new ArgumentNullException ("reader");
  152. SetFeed (CreateFeedInstance ());
  153. reader.MoveToContent ();
  154. string ver = reader.GetAttribute ("version");
  155. if (ver != "2.0")
  156. throw new NotSupportedException (String.Format ("RSS Version '{0}' is not supported", ver));
  157. if (PreserveAttributeExtensions && reader.MoveToFirstAttribute ()) {
  158. do {
  159. if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
  160. continue;
  161. if (reader.NamespaceURI == String.Empty && reader.LocalName == "version")
  162. continue;
  163. if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, Feed, Version))
  164. Feed.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
  165. } while (reader.MoveToNextAttribute ());
  166. }
  167. reader.ReadStartElement (); // <rss> => <channel>
  168. reader.MoveToContent ();
  169. reader.ReadStartElement ("channel", String.Empty); // <channel> => *
  170. Collection<SyndicationItem> items = null;
  171. for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
  172. if (reader.NodeType != XmlNodeType.Element)
  173. throw new XmlException ("Only element node is expected under 'channel' element");
  174. if (reader.NamespaceURI == String.Empty)
  175. switch (reader.LocalName) {
  176. case "title":
  177. Feed.Title = ReadTextSyndicationContent (reader);
  178. continue;
  179. case "link":
  180. SyndicationLink l = Feed.CreateLink ();
  181. ReadLink (reader, l);
  182. Feed.Links.Add (l);
  183. continue;
  184. case "description":
  185. Feed.Description = ReadTextSyndicationContent (reader);
  186. continue;
  187. case "language":
  188. Feed.Language = reader.ReadElementContentAsString ();
  189. continue;
  190. case "copyright":
  191. Feed.Copyright = ReadTextSyndicationContent (reader);
  192. continue;
  193. case "managingEditor":
  194. SyndicationPerson p = Feed.CreatePerson ();
  195. ReadPerson (reader, p);
  196. Feed.Authors.Add (p);
  197. continue;
  198. case "pubDate":
  199. // FIXME: somehow DateTimeOffset causes the runtime crash.
  200. reader.ReadElementContentAsString ();
  201. // Feed.PublishDate = FromRFC822DateString (reader.ReadElementContentAsString ());
  202. continue;
  203. case "lastBuildDate":
  204. // FIXME: somehow DateTimeOffset causes the runtime crash.
  205. reader.ReadElementContentAsString ();
  206. // Feed.LastUpdatedTime = FromRFC822DateString (reader.ReadElementContentAsString ());
  207. continue;
  208. case "category":
  209. SyndicationCategory c = Feed.CreateCategory ();
  210. ReadCategory (reader, c);
  211. Feed.Categories.Add (c);
  212. continue;
  213. case "generator":
  214. Feed.Generator = reader.ReadElementContentAsString ();
  215. continue;
  216. // "webMaster" "docs" "cloud" "ttl" "image" "rating" "textInput" "skipHours" "skipDays" are not handled.
  217. case "item":
  218. if (items == null) {
  219. items = new Collection<SyndicationItem> ();
  220. Feed.Items = items;
  221. }
  222. items.Add (ReadItem (reader, Feed));
  223. continue;
  224. }
  225. if (!TryParseElement (reader, Feed, Version)) {
  226. if (PreserveElementExtensions)
  227. // FIXME: what to specify for maxExtensionSize
  228. LoadElementExtensions (reader, Feed, int.MaxValue);
  229. else
  230. reader.Skip ();
  231. }
  232. }
  233. reader.ReadEndElement (); // </channel>
  234. reader.ReadEndElement (); // </rss>
  235. }
  236. TextSyndicationContent ReadTextSyndicationContent (XmlReader reader)
  237. {
  238. TextSyndicationContentKind kind = TextSyndicationContentKind.Plaintext;
  239. switch (reader.GetAttribute ("type")) {
  240. case "html":
  241. kind = TextSyndicationContentKind.Html;
  242. break;
  243. case "xhtml":
  244. kind = TextSyndicationContentKind.XHtml;
  245. break;
  246. }
  247. string text = reader.ReadElementContentAsString ();
  248. TextSyndicationContent t = new TextSyndicationContent (text, kind);
  249. return t;
  250. }
  251. void ReadCategory (XmlReader reader, SyndicationCategory category)
  252. {
  253. if (reader.MoveToFirstAttribute ()) {
  254. do {
  255. if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
  256. continue;
  257. if (reader.NamespaceURI == String.Empty) {
  258. switch (reader.LocalName) {
  259. case "domain":
  260. category.Scheme = reader.Value;
  261. continue;
  262. }
  263. }
  264. if (PreserveAttributeExtensions)
  265. if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, category, Version))
  266. category.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
  267. } while (reader.MoveToNextAttribute ());
  268. reader.MoveToElement ();
  269. }
  270. if (!reader.IsEmptyElement) {
  271. reader.Read ();
  272. for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
  273. if (reader.IsTextNode ())
  274. category.Name += reader.Value;
  275. else if (!TryParseElement (reader, category, Version)) {
  276. if (PreserveElementExtensions)
  277. // FIXME: what should be used for maxExtenswionSize
  278. LoadElementExtensions (reader, category, int.MaxValue);
  279. else
  280. reader.Skip ();
  281. }
  282. reader.Read ();
  283. }
  284. }
  285. reader.Read (); // </category> or <category ... />
  286. }
  287. void ReadLink (XmlReader reader, SyndicationLink link)
  288. {
  289. if (PreserveAttributeExtensions && reader.MoveToFirstAttribute ()) {
  290. do {
  291. if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
  292. continue;
  293. if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, link, Version))
  294. link.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
  295. } while (reader.MoveToNextAttribute ());
  296. reader.MoveToElement ();
  297. }
  298. if (!reader.IsEmptyElement) {
  299. string url = null;
  300. reader.Read ();
  301. for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
  302. if (reader.IsTextNode ())
  303. url += reader.Value;
  304. else if (!TryParseElement (reader, link, Version)) {
  305. if (PreserveElementExtensions)
  306. // FIXME: what should be used for maxExtenswionSize
  307. LoadElementExtensions (reader, link, int.MaxValue);
  308. else
  309. reader.Skip ();
  310. }
  311. reader.Read ();
  312. }
  313. link.Uri = CreateUri (url);
  314. }
  315. reader.Read (); // </link> or <link ... />
  316. }
  317. void ReadPerson (XmlReader reader, SyndicationPerson person)
  318. {
  319. if (PreserveAttributeExtensions && reader.MoveToFirstAttribute ()) {
  320. do {
  321. if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
  322. continue;
  323. if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, person, Version))
  324. person.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
  325. } while (reader.MoveToNextAttribute ());
  326. reader.MoveToElement ();
  327. }
  328. if (!reader.IsEmptyElement) {
  329. reader.Read ();
  330. for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
  331. if (reader.IsTextNode ())
  332. person.Email += reader.Value;
  333. else if (!TryParseElement (reader, person, Version)) {
  334. if (PreserveElementExtensions)
  335. // FIXME: what should be used for maxExtenswionSize
  336. LoadElementExtensions (reader, person, int.MaxValue);
  337. else
  338. reader.Skip ();
  339. }
  340. reader.Read ();
  341. }
  342. }
  343. reader.Read (); // end element or empty element
  344. }
  345. Uri CreateUri (string uri)
  346. {
  347. return new Uri (uri, UriKind.RelativeOrAbsolute);
  348. }
  349. // write
  350. void WriteXml (XmlWriter writer, bool writeRoot)
  351. {
  352. if (writer == null)
  353. throw new ArgumentNullException ("writer");
  354. if (Feed == null)
  355. throw new InvalidOperationException ("Syndication feed must be set before writing");
  356. if (writeRoot)
  357. writer.WriteStartElement ("rss");
  358. if (SerializeExtensionsAsAtom)
  359. writer.WriteAttributeString ("xmlns", "a10", "http://www.w3.org/2000/xmlns/", AtomNamespace);
  360. writer.WriteAttributeString ("version", "2.0");
  361. writer.WriteStartElement ("channel");
  362. if (Feed.BaseUri != null)
  363. writer.WriteAttributeString ("xml:base", Feed.BaseUri.ToString ());
  364. writer.WriteElementString ("title", String.Empty, Feed.Title != null ? Feed.Title.Text : String.Empty);
  365. writer.WriteElementString ("description", String.Empty, Feed.Description != null ? Feed.Description.Text : String.Empty);
  366. if (Feed.Copyright != null)
  367. writer.WriteElementString ("copyright", String.Empty, Feed.Copyright.Text);
  368. if (!Feed.LastUpdatedTime.Equals (default (DateTimeOffset))) {
  369. writer.WriteStartElement ("lastBuildDate");
  370. writer.WriteString (ToRFC822DateString (Feed.LastUpdatedTime));
  371. writer.WriteEndElement ();
  372. }
  373. if (Feed.Generator != null)
  374. writer.WriteElementString ("generator", String.Empty, Feed.Generator);
  375. if (Feed.ImageUrl != null) {
  376. writer.WriteStartElement ("image");
  377. writer.WriteElementString ("url", String.Empty, Feed.ImageUrl.ToString ());
  378. // FIXME: are they really empty?
  379. writer.WriteElementString ("title", String.Empty, String.Empty);
  380. writer.WriteElementString ("link", String.Empty, String.Empty);
  381. writer.WriteEndElement ();
  382. }
  383. if (Feed.Language != null)
  384. writer.WriteElementString ("language", String.Empty, Feed.Language);
  385. foreach (SyndicationPerson author in Feed.Authors)
  386. if (author != null) {
  387. writer.WriteStartElement ("managingEditor");
  388. WriteAttributeExtensions (writer, author, Version);
  389. writer.WriteString (author.Email);
  390. WriteElementExtensions (writer, author, Version);
  391. writer.WriteEndElement ();
  392. }
  393. foreach (SyndicationCategory category in Feed.Categories)
  394. if (category != null) {
  395. writer.WriteStartElement ("category");
  396. if (category.Scheme != null)
  397. writer.WriteAttributeString ("domain", category.Scheme);
  398. WriteAttributeExtensions (writer, category, Version);
  399. writer.WriteString (category.Name);
  400. WriteElementExtensions (writer, category, Version);
  401. writer.WriteEndElement ();
  402. }
  403. foreach (SyndicationLink link in Feed.Links)
  404. if (link != null) {
  405. writer.WriteStartElement ("link");
  406. WriteAttributeExtensions (writer, link, Version);
  407. writer.WriteString (link.Uri != null ? link.Uri.ToString () : String.Empty);
  408. WriteElementExtensions (writer, link, Version);
  409. writer.WriteEndElement ();
  410. }
  411. WriteItems (writer, Feed.Items, Feed.BaseUri);
  412. if (SerializeExtensionsAsAtom) {
  413. if (Feed.Id != null) {
  414. writer.WriteStartElement ("a10", "id", AtomNamespace);
  415. writer.WriteString (Feed.Id);
  416. writer.WriteEndElement ();
  417. }
  418. foreach (SyndicationPerson contributor in Feed.Contributors) {
  419. if (contributor != null) {
  420. writer.WriteStartElement ("a10", "contributor", AtomNamespace);
  421. WriteAttributeExtensions (writer, contributor, Version);
  422. writer.WriteElementString ("a10", "name", AtomNamespace, contributor.Name);
  423. writer.WriteElementString ("a10", "uri", AtomNamespace, contributor.Uri);
  424. writer.WriteElementString ("a10", "email", AtomNamespace, contributor.Email);
  425. WriteElementExtensions (writer, contributor, Version);
  426. writer.WriteEndElement ();
  427. }
  428. }
  429. }
  430. writer.WriteEndElement (); // </channel>
  431. if (writeRoot)
  432. writer.WriteEndElement (); // </rss>
  433. }
  434. // FIXME: DateTimeOffset.ToString() needs another overload.
  435. // When it is implemented, just remove ".DateTime" parts below.
  436. string ToRFC822DateString (DateTimeOffset date)
  437. {
  438. switch (date.DateTime.Kind) {
  439. case DateTimeKind.Utc:
  440. return date.DateTime.ToString ("ddd, dd MMM yyyy HH:mm:ss 'Z'", DateTimeFormatInfo.InvariantInfo);
  441. case DateTimeKind.Local:
  442. StringBuilder sb = new StringBuilder (date.DateTime.ToString ("ddd, dd MMM yyyy HH:mm:ss zzz", DateTimeFormatInfo.InvariantInfo));
  443. sb.Remove (sb.Length - 3, 1);
  444. return sb.ToString (); // remove ':' from +hh:mm
  445. default:
  446. return date.DateTime.ToString ("ddd, dd MMM yyyy HH:mm:ss", DateTimeFormatInfo.InvariantInfo);
  447. }
  448. }
  449. }
  450. }