Atom10ItemFormatter.cs 17 KB

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