Rss20ItemFormatter.cs 21 KB

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