Rss20ItemFormatter.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. //
  2. // Rss20ItemFormatter.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 ("item", Namespace = "")]
  41. public class Rss20ItemFormatter : SyndicationItemFormatter, 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 item_type;
  46. public Rss20ItemFormatter ()
  47. {
  48. ext_atom_serialization = true;
  49. }
  50. public Rss20ItemFormatter (SyndicationItem itemToWrite)
  51. : this (itemToWrite, true)
  52. {
  53. }
  54. public Rss20ItemFormatter (SyndicationItem itemToWrite, bool serializeExtensionsAsAtom)
  55. : base (itemToWrite)
  56. {
  57. ext_atom_serialization = serializeExtensionsAsAtom;
  58. }
  59. public Rss20ItemFormatter (Type itemTypeToCreate)
  60. {
  61. if (itemTypeToCreate == null)
  62. throw new ArgumentNullException ("itemTypeToCreate");
  63. item_type = itemTypeToCreate;
  64. }
  65. public bool SerializeExtensionsAsAtom {
  66. get { return ext_atom_serialization; }
  67. set { ext_atom_serialization = value; }
  68. }
  69. protected Type ItemType {
  70. get { return item_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 SyndicationItem CreateItemInstance ()
  84. {
  85. return new SyndicationItem ();
  86. }
  87. public override bool CanRead (XmlReader reader)
  88. {
  89. if (reader == null)
  90. throw new ArgumentNullException ("reader");
  91. reader.MoveToContent ();
  92. return reader.IsStartElement ("item", String.Empty);
  93. }
  94. public override void ReadFrom (XmlReader reader)
  95. {
  96. if (!CanRead (reader))
  97. throw new XmlException (String.Format ("Element '{0}' in namespace '{1}' is not accepted by this syndication formatter", reader.LocalName, reader.NamespaceURI));
  98. ReadXml (reader, true);
  99. }
  100. public override void WriteTo (XmlWriter writer)
  101. {
  102. WriteXml (writer, true);
  103. }
  104. void IXmlSerializable.ReadXml (XmlReader reader)
  105. {
  106. ReadXml (reader, false);
  107. }
  108. void IXmlSerializable.WriteXml (XmlWriter writer)
  109. {
  110. WriteXml (writer, false);
  111. }
  112. XmlSchema IXmlSerializable.GetSchema ()
  113. {
  114. return null;
  115. }
  116. // read
  117. void ReadXml (XmlReader reader, bool fromSerializable)
  118. {
  119. if (reader == null)
  120. throw new ArgumentNullException ("reader");
  121. SetItem (CreateItemInstance ());
  122. reader.MoveToContent ();
  123. if (PreserveAttributeExtensions && reader.MoveToFirstAttribute ()) {
  124. do {
  125. if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
  126. continue;
  127. if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, Item, Version))
  128. Item.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
  129. } while (reader.MoveToNextAttribute ());
  130. }
  131. reader.ReadStartElement ();
  132. for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
  133. if (reader.NodeType != XmlNodeType.Element)
  134. throw new XmlException ("Only element node is expected under 'item' element");
  135. if (reader.NamespaceURI == String.Empty)
  136. switch (reader.LocalName) {
  137. case "title":
  138. Item.Title = ReadTextSyndicationContent (reader);
  139. continue;
  140. case "link":
  141. SyndicationLink l = Item.CreateLink ();
  142. ReadLink (reader, l);
  143. Item.Links.Add (l);
  144. continue;
  145. case "description":
  146. Item.Summary = ReadTextSyndicationContent (reader);
  147. continue;
  148. case "author":
  149. SyndicationPerson p = Item.CreatePerson ();
  150. ReadPerson (reader, p);
  151. Item.Authors.Add (p);
  152. continue;
  153. case "category":
  154. SyndicationCategory c = Item.CreateCategory ();
  155. ReadCategory (reader, c);
  156. Item.Categories.Add (c);
  157. continue;
  158. // case "comments": // treated as extension ...
  159. case "enclosure":
  160. l = Item.CreateLink ();
  161. ReadEnclosure (reader, l);
  162. Item.Links.Add (l);
  163. continue;
  164. case "guid":
  165. Item.AddPermalink (CreateUri (reader.ReadElementContentAsString ()));
  166. continue;
  167. case "pubDate":
  168. // FIXME: somehow DateTimeOffset causes the runtime crash.
  169. reader.ReadElementContentAsString ();
  170. // Item.PublishDate = FromRFC822DateString (reader.ReadElementContentAsString ());
  171. continue;
  172. case "source":
  173. Item.SourceFeed = new SyndicationFeed ();
  174. ReadSourceFeed (reader, Item.SourceFeed);
  175. continue;
  176. }
  177. else if (SerializeExtensionsAsAtom && reader.NamespaceURI == AtomNamespace) {
  178. switch (reader.LocalName) {
  179. case "contributor":
  180. SyndicationPerson p = Item.CreatePerson ();
  181. ReadPersonAtom10 (reader, p);
  182. Item.Contributors.Add (p);
  183. continue;
  184. case "updated":
  185. // FIXME: somehow DateTimeOffset causes the runtime crash.
  186. reader.ReadElementContentAsString ();
  187. // Item.LastUpdatedTime = XmlConvert.ToDateTimeOffset (reader.ReadElementContentAsString ());
  188. continue;
  189. case "rights":
  190. Item.Copyright = ReadTextSyndicationContent (reader);
  191. continue;
  192. case "content":
  193. if (reader.GetAttribute ("src") != null) {
  194. Item.Content = new UrlSyndicationContent (CreateUri (reader.GetAttribute ("src")), reader.GetAttribute ("type"));
  195. reader.Skip ();
  196. continue;
  197. }
  198. switch (reader.GetAttribute ("type")) {
  199. case "text":
  200. case "html":
  201. case "xhtml":
  202. Item.Content = ReadTextSyndicationContent (reader);
  203. continue;
  204. default:
  205. SyndicationContent content;
  206. if (!TryParseContent (reader, Item, reader.GetAttribute ("type"), Version, out content))
  207. Item.Content = new XmlSyndicationContent (reader);
  208. continue;
  209. }
  210. }
  211. }
  212. if (!TryParseElement (reader, Item, Version)) {
  213. if (PreserveElementExtensions)
  214. // FIXME: what to specify for maxExtensionSize
  215. LoadElementExtensions (reader, Item, int.MaxValue);
  216. else
  217. reader.Skip ();
  218. }
  219. }
  220. reader.ReadEndElement (); // </item>
  221. }
  222. TextSyndicationContent ReadTextSyndicationContent (XmlReader reader)
  223. {
  224. TextSyndicationContentKind kind = TextSyndicationContentKind.Plaintext;
  225. switch (reader.GetAttribute ("type")) {
  226. case "html":
  227. kind = TextSyndicationContentKind.Html;
  228. break;
  229. case "xhtml":
  230. kind = TextSyndicationContentKind.XHtml;
  231. break;
  232. }
  233. string text = reader.ReadElementContentAsString ();
  234. TextSyndicationContent t = new TextSyndicationContent (text, kind);
  235. return t;
  236. }
  237. void ReadCategory (XmlReader reader, SyndicationCategory category)
  238. {
  239. if (reader.MoveToFirstAttribute ()) {
  240. do {
  241. if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
  242. continue;
  243. if (reader.NamespaceURI == String.Empty) {
  244. switch (reader.LocalName) {
  245. case "domain":
  246. category.Scheme = reader.Value;
  247. continue;
  248. }
  249. }
  250. if (PreserveAttributeExtensions)
  251. if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, category, Version))
  252. category.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
  253. } while (reader.MoveToNextAttribute ());
  254. reader.MoveToElement ();
  255. }
  256. if (!reader.IsEmptyElement) {
  257. reader.Read ();
  258. for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
  259. if (reader.NodeType == XmlNodeType.Text)
  260. category.Name += reader.Value;
  261. else if (!TryParseElement (reader, category, Version)) {
  262. if (PreserveElementExtensions)
  263. // FIXME: what should be used for maxExtenswionSize
  264. LoadElementExtensions (reader, category, int.MaxValue);
  265. else
  266. reader.Skip ();
  267. }
  268. }
  269. }
  270. reader.Read (); // </category> or <category ... />
  271. }
  272. // SyndicationLink.CreateMediaEnclosureLink() is almost
  273. // useless here since it cannot handle extension attributes
  274. // in straightforward way (it I use it, I have to iterate
  275. // attributes twice just to read extensions).
  276. void ReadEnclosure (XmlReader reader, SyndicationLink link)
  277. {
  278. link.RelationshipType = "enclosure";
  279. if (PreserveAttributeExtensions && reader.MoveToFirstAttribute ()) {
  280. do {
  281. if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
  282. continue;
  283. if (reader.NamespaceURI == String.Empty) {
  284. switch (reader.LocalName) {
  285. case "url":
  286. link.Uri = CreateUri (reader.Value);
  287. continue;
  288. case "type":
  289. link.MediaType = reader.Value;
  290. continue;
  291. case "length":
  292. link.Length = XmlConvert.ToInt64 (reader.Value);
  293. continue;
  294. }
  295. }
  296. if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, link, Version))
  297. link.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
  298. } while (reader.MoveToNextAttribute ());
  299. reader.MoveToElement ();
  300. }
  301. // Actually .NET fails to read extension here.
  302. if (!reader.IsEmptyElement) {
  303. reader.Read ();
  304. for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
  305. if (!TryParseElement (reader, link, Version)) {
  306. if (PreserveElementExtensions)
  307. // FIXME: what should be used for maxExtenswionSize
  308. LoadElementExtensions (reader, link, int.MaxValue);
  309. else
  310. reader.Skip ();
  311. }
  312. }
  313. }
  314. reader.Read (); // </enclosure> or <enclosure ... />
  315. }
  316. void ReadLink (XmlReader reader, SyndicationLink link)
  317. {
  318. if (PreserveAttributeExtensions && reader.MoveToFirstAttribute ()) {
  319. do {
  320. if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
  321. continue;
  322. if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, link, Version))
  323. link.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
  324. } while (reader.MoveToNextAttribute ());
  325. reader.MoveToElement ();
  326. }
  327. if (!reader.IsEmptyElement) {
  328. string url = null;
  329. reader.Read ();
  330. for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
  331. if (reader.NodeType == XmlNodeType.Text)
  332. url += reader.Value;
  333. else if (!TryParseElement (reader, link, Version)) {
  334. if (PreserveElementExtensions)
  335. // FIXME: what should be used for maxExtenswionSize
  336. LoadElementExtensions (reader, link, int.MaxValue);
  337. else
  338. reader.Skip ();
  339. }
  340. reader.Read ();
  341. }
  342. link.Uri = CreateUri (url);
  343. }
  344. reader.Read (); // </link> or <link ... />
  345. }
  346. void ReadPerson (XmlReader reader, SyndicationPerson person)
  347. {
  348. if (PreserveAttributeExtensions && reader.MoveToFirstAttribute ()) {
  349. do {
  350. if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
  351. continue;
  352. if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, person, Version))
  353. person.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
  354. } while (reader.MoveToNextAttribute ());
  355. reader.MoveToElement ();
  356. }
  357. if (!reader.IsEmptyElement) {
  358. reader.Read ();
  359. for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
  360. if (reader.NodeType == XmlNodeType.Text)
  361. person.Email += reader.Value;
  362. else if (!TryParseElement (reader, person, Version)) {
  363. if (PreserveElementExtensions)
  364. // FIXME: what should be used for maxExtenswionSize
  365. LoadElementExtensions (reader, person, int.MaxValue);
  366. else
  367. reader.Skip ();
  368. }
  369. }
  370. }
  371. reader.Read (); // end element or empty element
  372. }
  373. // copied from Atom10ItemFormatter
  374. void ReadPersonAtom10 (XmlReader reader, SyndicationPerson person)
  375. {
  376. if (reader.MoveToFirstAttribute ()) {
  377. do {
  378. if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
  379. continue;
  380. if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, person, Version) && PreserveAttributeExtensions)
  381. person.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
  382. } while (reader.MoveToNextAttribute ());
  383. reader.MoveToElement ();
  384. }
  385. if (!reader.IsEmptyElement) {
  386. reader.Read ();
  387. for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
  388. if (reader.NodeType == XmlNodeType.Element && reader.NamespaceURI == AtomNamespace) {
  389. switch (reader.LocalName) {
  390. case "name":
  391. person.Name = reader.ReadElementContentAsString ();
  392. continue;
  393. case "uri":
  394. person.Uri = reader.ReadElementContentAsString ();
  395. continue;
  396. case "email":
  397. person.Email = reader.ReadElementContentAsString ();
  398. continue;
  399. }
  400. }
  401. if (!TryParseElement (reader, person, Version)) {
  402. if (PreserveElementExtensions)
  403. // FIXME: what should be used for maxExtenswionSize
  404. LoadElementExtensions (reader, person, int.MaxValue);
  405. else
  406. reader.Skip ();
  407. }
  408. }
  409. }
  410. reader.Read (); // end element or empty element
  411. }
  412. void ReadSourceFeed (XmlReader reader, SyndicationFeed feed)
  413. {
  414. if (reader.MoveToFirstAttribute ()) {
  415. do {
  416. if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
  417. continue;
  418. if (reader.NamespaceURI == String.Empty) {
  419. switch (reader.LocalName) {
  420. case "url":
  421. feed.Links.Add (new SyndicationLink (CreateUri (reader.Value)));
  422. continue;
  423. }
  424. }
  425. } while (reader.MoveToNextAttribute ());
  426. reader.MoveToElement ();
  427. }
  428. if (!reader.IsEmptyElement) {
  429. reader.Read ();
  430. string title = null;
  431. while (reader.NodeType != XmlNodeType.EndElement)
  432. if (reader.NodeType == XmlNodeType.Text)
  433. title += reader.Value;
  434. feed.Title = new TextSyndicationContent (title);
  435. }
  436. reader.Read (); // </source> or <source ... />
  437. }
  438. Uri CreateUri (string uri)
  439. {
  440. return new Uri (uri, UriKind.RelativeOrAbsolute);
  441. }
  442. // write
  443. void WriteXml (XmlWriter writer, bool writeRoot)
  444. {
  445. if (writer == null)
  446. throw new ArgumentNullException ("writer");
  447. if (Item == null)
  448. throw new InvalidOperationException ("Syndication item must be set before writing");
  449. if (writeRoot)
  450. writer.WriteStartElement ("item");
  451. if (Item.BaseUri != null)
  452. writer.WriteAttributeString ("xml:base", Item.BaseUri.ToString ());
  453. WriteAttributeExtensions (writer, Item, Version);
  454. if (Item.Id != null) {
  455. writer.WriteStartElement ("guid");
  456. writer.WriteAttributeString ("isPermaLink", "false");
  457. writer.WriteString (Item.Id);
  458. writer.WriteEndElement ();
  459. }
  460. if (Item.Title != null) {
  461. writer.WriteStartElement ("title");
  462. writer.WriteString (Item.Title.Text);
  463. writer.WriteEndElement ();
  464. }
  465. foreach (SyndicationPerson author in Item.Authors)
  466. if (author != null) {
  467. writer.WriteStartElement ("author");
  468. WriteAttributeExtensions (writer, author, Version);
  469. writer.WriteString (author.Email);
  470. WriteElementExtensions (writer, author, Version);
  471. writer.WriteEndElement ();
  472. }
  473. foreach (SyndicationCategory category in Item.Categories)
  474. if (category != null) {
  475. writer.WriteStartElement ("category");
  476. if (category.Scheme != null)
  477. writer.WriteAttributeString ("domain", category.Scheme);
  478. WriteAttributeExtensions (writer, category, Version);
  479. writer.WriteString (category.Name);
  480. WriteElementExtensions (writer, category, Version);
  481. writer.WriteEndElement ();
  482. }
  483. if (Item.Content != null) {
  484. Item.Content.WriteTo (writer, "description", String.Empty);
  485. } else if (Item.Summary != null)
  486. Item.Summary.WriteTo (writer, "description", String.Empty);
  487. else if (Item.Title == null) { // according to the RSS 2.0 spec, either of title or description must exist.
  488. writer.WriteStartElement ("description");
  489. writer.WriteEndElement ();
  490. }
  491. foreach (SyndicationLink link in Item.Links)
  492. switch (link.RelationshipType) {
  493. case "enclosure":
  494. writer.WriteStartElement ("enclosure");
  495. if (link.Uri != null)
  496. writer.WriteAttributeString ("uri", link.Uri.ToString ());
  497. if (link.Length != 0)
  498. writer.WriteAttributeString ("length", XmlConvert.ToString (link.Length));
  499. if (link.MediaType != null)
  500. writer.WriteAttributeString ("type", link.MediaType);
  501. WriteAttributeExtensions (writer, link, Version);
  502. WriteElementExtensions (writer, link, Version);
  503. writer.WriteEndElement ();
  504. break;
  505. default:
  506. writer.WriteStartElement ("link");
  507. WriteAttributeExtensions (writer, link, Version);
  508. writer.WriteString (link.Uri != null ? link.Uri.ToString () : String.Empty);
  509. WriteElementExtensions (writer, link, Version);
  510. writer.WriteEndElement ();
  511. break;
  512. }
  513. if (Item.SourceFeed != null) {
  514. writer.WriteStartElement ("source");
  515. if (Item.SourceFeed.Links.Count > 0) {
  516. Uri u = Item.SourceFeed.Links [0].Uri;
  517. writer.WriteAttributeString ("url", u != null ? u.ToString () : String.Empty);
  518. }
  519. writer.WriteString (Item.SourceFeed.Title != null ? Item.SourceFeed.Title.Text : String.Empty);
  520. writer.WriteEndElement ();
  521. }
  522. if (!Item.PublishDate.Equals (default (DateTimeOffset))) {
  523. writer.WriteStartElement ("pubDate");
  524. writer.WriteString (ToRFC822DateString (Item.PublishDate));
  525. writer.WriteEndElement ();
  526. }
  527. if (SerializeExtensionsAsAtom) {
  528. foreach (SyndicationPerson contributor in Item.Contributors) {
  529. if (contributor != null) {
  530. writer.WriteStartElement ("contributor", AtomNamespace);
  531. WriteAttributeExtensions (writer, contributor, Version);
  532. writer.WriteElementString ("name", AtomNamespace, contributor.Name);
  533. writer.WriteElementString ("uri", AtomNamespace, contributor.Uri);
  534. writer.WriteElementString ("email", AtomNamespace, contributor.Email);
  535. WriteElementExtensions (writer, contributor, Version);
  536. writer.WriteEndElement ();
  537. }
  538. }
  539. if (!Item.LastUpdatedTime.Equals (default (DateTimeOffset))) {
  540. writer.WriteStartElement ("updated", AtomNamespace);
  541. // FIXME: how to handle offset part?
  542. writer.WriteString (XmlConvert.ToString (Item.LastUpdatedTime.DateTime, XmlDateTimeSerializationMode.Local));
  543. writer.WriteEndElement ();
  544. }
  545. if (Item.Copyright != null)
  546. Item.Copyright.WriteTo (writer, "rights", AtomNamespace);
  547. #if false
  548. if (Item.Content != null)
  549. Item.Content.WriteTo (writer, "content", AtomNamespace);
  550. #endif
  551. }
  552. WriteElementExtensions (writer, Item, Version);
  553. if (writeRoot)
  554. writer.WriteEndElement ();
  555. }
  556. // FIXME: DateTimeOffset.ToString() needs another overload.
  557. // When it is implemented, just remove ".DateTime" parts below.
  558. string ToRFC822DateString (DateTimeOffset date)
  559. {
  560. switch (date.DateTime.Kind) {
  561. case DateTimeKind.Utc:
  562. return date.DateTime.ToString ("ddd, dd MMM yyyy HH:mm:ss 'Z'", DateTimeFormatInfo.InvariantInfo);
  563. case DateTimeKind.Local:
  564. StringBuilder sb = new StringBuilder (date.DateTime.ToString ("ddd, dd MMM yyyy HH:mm:ss zzz", DateTimeFormatInfo.InvariantInfo));
  565. sb.Remove (sb.Length - 3, 1);
  566. return sb.ToString (); // remove ':' from +hh:mm
  567. default:
  568. return date.DateTime.ToString ("ddd, dd MMM yyyy HH:mm:ss", DateTimeFormatInfo.InvariantInfo);
  569. }
  570. }
  571. string [] rfc822formats = new string [] {
  572. "ddd, dd MMM yyyy HH:mm:ss 'Z'",
  573. "ddd, dd MMM yyyy HH:mm:ss zzz",
  574. "ddd, dd MMM yyyy HH:mm:ss"};
  575. // FIXME: DateTimeOffset is still incomplete. When it is done,
  576. // simplify the code.
  577. DateTimeOffset FromRFC822DateString (string s)
  578. {
  579. return XmlConvert.ToDateTimeOffset (s, rfc822formats);
  580. }
  581. }
  582. }