Atom10FeedFormatter.cs 16 KB

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