PageRenderTime 62ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.ServiceModel.Web/System.ServiceModel.Syndication/Rss20ItemFormatter.cs

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C# | 664 lines | 551 code | 62 blank | 51 comment | 165 complexity | 1afbd975e7334f0e696969f911ab7c36 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1
  1. //
  2. // Rss20ItemFormatter.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.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. Item.AddPermalink (CreateUri (reader.ReadElementContentAsString ()));
  181. continue;
  182. case "pubDate":
  183. // FIXME: somehow DateTimeOffset causes the runtime crash.
  184. reader.ReadElementContentAsString ();
  185. // Item.PublishDate = FromRFC822DateString (reader.ReadElementContentAsString ());
  186. continue;
  187. case "source":
  188. Item.SourceFeed = new SyndicationFeed ();
  189. ReadSourceFeed (reader, Item.SourceFeed);
  190. continue;
  191. }
  192. else if (SerializeExtensionsAsAtom && reader.NamespaceURI == AtomNamespace) {
  193. switch (reader.LocalName) {
  194. case "contributor":
  195. SyndicationPerson p = Item.CreatePerson ();
  196. ReadPersonAtom10 (reader, p);
  197. Item.Contributors.Add (p);
  198. continue;
  199. case "updated":
  200. // FIXME: somehow DateTimeOffset causes the runtime crash.
  201. reader.ReadElementContentAsString ();
  202. // Item.LastUpdatedTime = XmlConvert.ToDateTimeOffset (reader.ReadElementContentAsString ());
  203. continue;
  204. case "rights":
  205. Item.Copyright = ReadTextSyndicationContent (reader);
  206. continue;
  207. case "content":
  208. if (reader.GetAttribute ("src") != null) {
  209. Item.Content = new UrlSyndicationContent (CreateUri (reader.GetAttribute ("src")), reader.GetAttribute ("type"));
  210. reader.Skip ();
  211. continue;
  212. }
  213. switch (reader.GetAttribute ("type")) {
  214. case "text":
  215. case "html":
  216. case "xhtml":
  217. Item.Content = ReadTextSyndicationContent (reader);
  218. continue;
  219. default:
  220. SyndicationContent content;
  221. if (!TryParseContent (reader, Item, reader.GetAttribute ("type"), Version, out content))
  222. Item.Content = new XmlSyndicationContent (reader);
  223. continue;
  224. }
  225. }
  226. }
  227. if (!TryParseElement (reader, Item, Version)) {
  228. if (PreserveElementExtensions)
  229. // FIXME: what to specify for maxExtensionSize
  230. LoadElementExtensions (reader, Item, int.MaxValue);
  231. else
  232. reader.Skip ();
  233. }
  234. }
  235. reader.ReadEndElement (); // </item>
  236. }
  237. TextSyndicationContent ReadTextSyndicationContent (XmlReader reader)
  238. {
  239. TextSyndicationContentKind kind = TextSyndicationContentKind.Plaintext;
  240. switch (reader.GetAttribute ("type")) {
  241. case "html":
  242. kind = TextSyndicationContentKind.Html;
  243. break;
  244. case "xhtml":
  245. kind = TextSyndicationContentKind.XHtml;
  246. break;
  247. }
  248. string text = reader.ReadElementContentAsString ();
  249. TextSyndicationContent t = new TextSyndicationContent (text, kind);
  250. return t;
  251. }
  252. void ReadCategory (XmlReader reader, SyndicationCategory category)
  253. {
  254. if (reader.MoveToFirstAttribute ()) {
  255. do {
  256. if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
  257. continue;
  258. if (reader.NamespaceURI == String.Empty) {
  259. switch (reader.LocalName) {
  260. case "domain":
  261. category.Scheme = reader.Value;
  262. continue;
  263. }
  264. }
  265. if (PreserveAttributeExtensions)
  266. if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, category, Version))
  267. category.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
  268. } while (reader.MoveToNextAttribute ());
  269. reader.MoveToElement ();
  270. }
  271. if (!reader.IsEmptyElement) {
  272. reader.Read ();
  273. for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
  274. if (reader.IsTextNode ())
  275. category.Name += reader.Value;
  276. else if (!TryParseElement (reader, category, Version)) {
  277. if (PreserveElementExtensions)
  278. // FIXME: what should be used for maxExtenswionSize
  279. LoadElementExtensions (reader, category, int.MaxValue);
  280. else
  281. reader.Skip ();
  282. }
  283. reader.Read ();
  284. }
  285. }
  286. reader.Read (); // </category> or <category ... />
  287. }
  288. // SyndicationLink.CreateMediaEnclosureLink() is almost
  289. // useless here since it cannot handle extension attributes
  290. // in straightforward way (it I use it, I have to iterate
  291. // attributes twice just to read extensions).
  292. void ReadEnclosure (XmlReader reader, SyndicationLink link)
  293. {
  294. link.RelationshipType = "enclosure";
  295. if (PreserveAttributeExtensions && reader.MoveToFirstAttribute ()) {
  296. do {
  297. if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
  298. continue;
  299. if (reader.NamespaceURI == String.Empty) {
  300. switch (reader.LocalName) {
  301. case "url":
  302. link.Uri = CreateUri (reader.Value);
  303. continue;
  304. case "type":
  305. link.MediaType = reader.Value;
  306. continue;
  307. case "length":
  308. link.Length = XmlConvert.ToInt64 (reader.Value);
  309. continue;
  310. }
  311. }
  312. if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, link, Version))
  313. link.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
  314. } while (reader.MoveToNextAttribute ());
  315. reader.MoveToElement ();
  316. }
  317. // Actually .NET fails to read extension here.
  318. if (!reader.IsEmptyElement) {
  319. reader.Read ();
  320. for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
  321. if (!TryParseElement (reader, link, Version)) {
  322. if (PreserveElementExtensions)
  323. // FIXME: what should be used for maxExtenswionSize
  324. LoadElementExtensions (reader, link, int.MaxValue);
  325. else
  326. reader.Skip ();
  327. }
  328. }
  329. }
  330. reader.Read (); // </enclosure> or <enclosure ... />
  331. }
  332. void ReadLink (XmlReader reader, SyndicationLink link)
  333. {
  334. if (PreserveAttributeExtensions && reader.MoveToFirstAttribute ()) {
  335. do {
  336. if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
  337. continue;
  338. if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, link, Version))
  339. link.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
  340. } while (reader.MoveToNextAttribute ());
  341. reader.MoveToElement ();
  342. }
  343. if (!reader.IsEmptyElement) {
  344. string url = null;
  345. reader.Read ();
  346. for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
  347. if (reader.IsTextNode ())
  348. url += reader.Value;
  349. else if (!TryParseElement (reader, link, Version)) {
  350. if (PreserveElementExtensions)
  351. // FIXME: what should be used for maxExtenswionSize
  352. LoadElementExtensions (reader, link, int.MaxValue);
  353. else
  354. reader.Skip ();
  355. }
  356. reader.Read ();
  357. }
  358. link.Uri = CreateUri (url);
  359. }
  360. reader.Read (); // </link> or <link ... />
  361. }
  362. void ReadPerson (XmlReader reader, SyndicationPerson person)
  363. {
  364. if (PreserveAttributeExtensions && reader.MoveToFirstAttribute ()) {
  365. do {
  366. if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
  367. continue;
  368. if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, person, Version))
  369. person.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
  370. } while (reader.MoveToNextAttribute ());
  371. reader.MoveToElement ();
  372. }
  373. if (!reader.IsEmptyElement) {
  374. reader.Read ();
  375. for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
  376. if (reader.IsTextNode ())
  377. person.Email += reader.Value;
  378. else if (!TryParseElement (reader, person, Version)) {
  379. if (PreserveElementExtensions)
  380. // FIXME: what should be used for maxExtenswionSize
  381. LoadElementExtensions (reader, person, int.MaxValue);
  382. else
  383. reader.Skip ();
  384. }
  385. reader.Read ();
  386. }
  387. }
  388. reader.Read (); // end element or empty element
  389. }
  390. // copied from Atom10ItemFormatter
  391. void ReadPersonAtom10 (XmlReader reader, SyndicationPerson person)
  392. {
  393. if (reader.MoveToFirstAttribute ()) {
  394. do {
  395. if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
  396. continue;
  397. if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, person, Version) && PreserveAttributeExtensions)
  398. person.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
  399. } while (reader.MoveToNextAttribute ());
  400. reader.MoveToElement ();
  401. }
  402. if (!reader.IsEmptyElement) {
  403. reader.Read ();
  404. for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
  405. if (reader.NodeType == XmlNodeType.Element && reader.NamespaceURI == AtomNamespace) {
  406. switch (reader.LocalName) {
  407. case "name":
  408. person.Name = reader.ReadElementContentAsString ();
  409. continue;
  410. case "uri":
  411. person.Uri = reader.ReadElementContentAsString ();
  412. continue;
  413. case "email":
  414. person.Email = reader.ReadElementContentAsString ();
  415. continue;
  416. }
  417. }
  418. if (!TryParseElement (reader, person, Version)) {
  419. if (PreserveElementExtensions)
  420. // FIXME: what should be used for maxExtenswionSize
  421. LoadElementExtensions (reader, person, int.MaxValue);
  422. else
  423. reader.Skip ();
  424. }
  425. }
  426. }
  427. reader.Read (); // end element or empty element
  428. }
  429. void ReadSourceFeed (XmlReader reader, SyndicationFeed feed)
  430. {
  431. if (reader.MoveToFirstAttribute ()) {
  432. do {
  433. if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
  434. continue;
  435. if (reader.NamespaceURI == String.Empty) {
  436. switch (reader.LocalName) {
  437. case "url":
  438. feed.Links.Add (new SyndicationLink (CreateUri (reader.Value)));
  439. continue;
  440. }
  441. }
  442. } while (reader.MoveToNextAttribute ());
  443. reader.MoveToElement ();
  444. }
  445. if (!reader.IsEmptyElement) {
  446. reader.Read ();
  447. string title = null;
  448. while (reader.NodeType != XmlNodeType.EndElement) {
  449. if (reader.IsTextNode ())
  450. title += reader.Value;
  451. reader.Skip ();
  452. reader.MoveToContent ();
  453. }
  454. feed.Title = new TextSyndicationContent (title);
  455. }
  456. reader.Read (); // </source> or <source ... />
  457. }
  458. Uri CreateUri (string uri)
  459. {
  460. return new Uri (uri, UriKind.RelativeOrAbsolute);
  461. }
  462. // write
  463. void WriteXml (XmlWriter writer, bool writeRoot)
  464. {
  465. if (writer == null)
  466. throw new ArgumentNullException ("writer");
  467. if (Item == null)
  468. throw new InvalidOperationException ("Syndication item must be set before writing");
  469. if (writeRoot)
  470. writer.WriteStartElement ("item");
  471. if (Item.BaseUri != null)
  472. writer.WriteAttributeString ("xml:base", Item.BaseUri.ToString ());
  473. WriteAttributeExtensions (writer, Item, Version);
  474. if (Item.Id != null) {
  475. writer.WriteStartElement ("guid");
  476. writer.WriteAttributeString ("isPermaLink", "false");
  477. writer.WriteString (Item.Id);
  478. writer.WriteEndElement ();
  479. }
  480. if (Item.Title != null) {
  481. writer.WriteStartElement ("title");
  482. writer.WriteString (Item.Title.Text);
  483. writer.WriteEndElement ();
  484. }
  485. foreach (SyndicationPerson author in Item.Authors)
  486. if (author != null) {
  487. writer.WriteStartElement ("author");
  488. WriteAttributeExtensions (writer, author, Version);
  489. writer.WriteString (author.Email);
  490. WriteElementExtensions (writer, author, Version);
  491. writer.WriteEndElement ();
  492. }
  493. foreach (SyndicationCategory category in Item.Categories)
  494. if (category != null) {
  495. writer.WriteStartElement ("category");
  496. if (category.Scheme != null)
  497. writer.WriteAttributeString ("domain", category.Scheme);
  498. WriteAttributeExtensions (writer, category, Version);
  499. writer.WriteString (category.Name);
  500. WriteElementExtensions (writer, category, Version);
  501. writer.WriteEndElement ();
  502. }
  503. if (Item.Content != null) {
  504. Item.Content.WriteTo (writer, "description", String.Empty);
  505. } else if (Item.Summary != null)
  506. Item.Summary.WriteTo (writer, "description", String.Empty);
  507. else if (Item.Title == null) { // according to the RSS 2.0 spec, either of title or description must exist.
  508. writer.WriteStartElement ("description");
  509. writer.WriteEndElement ();
  510. }
  511. foreach (SyndicationLink link in Item.Links)
  512. switch (link.RelationshipType) {
  513. case "enclosure":
  514. writer.WriteStartElement ("enclosure");
  515. if (link.Uri != null)
  516. writer.WriteAttributeString ("uri", link.Uri.ToString ());
  517. if (link.Length != 0)
  518. writer.WriteAttributeString ("length", XmlConvert.ToString (link.Length));
  519. if (link.MediaType != null)
  520. writer.WriteAttributeString ("type", link.MediaType);
  521. WriteAttributeExtensions (writer, link, Version);
  522. WriteElementExtensions (writer, link, Version);
  523. writer.WriteEndElement ();
  524. break;
  525. default:
  526. writer.WriteStartElement ("link");
  527. WriteAttributeExtensions (writer, link, Version);
  528. writer.WriteString (link.Uri != null ? link.Uri.ToString () : String.Empty);
  529. WriteElementExtensions (writer, link, Version);
  530. writer.WriteEndElement ();
  531. break;
  532. }
  533. if (Item.SourceFeed != null) {
  534. writer.WriteStartElement ("source");
  535. if (Item.SourceFeed.Links.Count > 0) {
  536. Uri u = Item.SourceFeed.Links [0].Uri;
  537. writer.WriteAttributeString ("url", u != null ? u.ToString () : String.Empty);
  538. }
  539. writer.WriteString (Item.SourceFeed.Title != null ? Item.SourceFeed.Title.Text : String.Empty);
  540. writer.WriteEndElement ();
  541. }
  542. if (!Item.PublishDate.Equals (default (DateTimeOffset))) {
  543. writer.WriteStartElement ("pubDate");
  544. writer.WriteString (ToRFC822DateString (Item.PublishDate));
  545. writer.WriteEndElement ();
  546. }
  547. if (SerializeExtensionsAsAtom) {
  548. foreach (SyndicationPerson contributor in Item.Contributors) {
  549. if (contributor != null) {
  550. writer.WriteStartElement ("contributor", AtomNamespace);
  551. WriteAttributeExtensions (writer, contributor, Version);
  552. writer.WriteElementString ("name", AtomNamespace, contributor.Name);
  553. writer.WriteElementString ("uri", AtomNamespace, contributor.Uri);
  554. writer.WriteElementString ("email", AtomNamespace, contributor.Email);
  555. WriteElementExtensions (writer, contributor, Version);
  556. writer.WriteEndElement ();
  557. }
  558. }
  559. if (!Item.LastUpdatedTime.Equals (default (DateTimeOffset))) {
  560. writer.WriteStartElement ("updated", AtomNamespace);
  561. // FIXME: how to handle offset part?
  562. writer.WriteString (XmlConvert.ToString (Item.LastUpdatedTime.DateTime, XmlDateTimeSerializationMode.Local));
  563. writer.WriteEndElement ();
  564. }
  565. if (Item.Copyright != null)
  566. Item.Copyright.WriteTo (writer, "rights", AtomNamespace);
  567. #if false
  568. if (Item.Content != null)
  569. Item.Content.WriteTo (writer, "content", AtomNamespace);
  570. #endif
  571. }
  572. WriteElementExtensions (writer, Item, Version);
  573. if (writeRoot)
  574. writer.WriteEndElement ();
  575. }
  576. // FIXME: DateTimeOffset.ToString() needs another overload.
  577. // When it is implemented, just remove ".DateTime" parts below.
  578. string ToRFC822DateString (DateTimeOffset date)
  579. {
  580. switch (date.DateTime.Kind) {
  581. case DateTimeKind.Utc:
  582. return date.DateTime.ToString ("ddd, dd MMM yyyy HH:mm:ss 'Z'", DateTimeFormatInfo.InvariantInfo);
  583. case DateTimeKind.Local:
  584. StringBuilder sb = new StringBuilder (date.DateTime.ToString ("ddd, dd MMM yyyy HH:mm:ss zzz", DateTimeFormatInfo.InvariantInfo));
  585. sb.Remove (sb.Length - 3, 1);
  586. return sb.ToString (); // remove ':' from +hh:mm
  587. default:
  588. return date.DateTime.ToString ("ddd, dd MMM yyyy HH:mm:ss", DateTimeFormatInfo.InvariantInfo);
  589. }
  590. }
  591. string [] rfc822formats = new string [] {
  592. "ddd, dd MMM yyyy HH:mm:ss 'Z'",
  593. "ddd, dd MMM yyyy HH:mm:ss zzz",
  594. "ddd, dd MMM yyyy HH:mm:ss"};
  595. // FIXME: DateTimeOffset is still incomplete. When it is done,
  596. // simplify the code.
  597. DateTimeOffset FromRFC822DateString (string s)
  598. {
  599. return XmlConvert.ToDateTimeOffset (s, rfc822formats);
  600. }
  601. }
  602. }