2 // Atom10ItemFormatter.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 // This class is not for outputting ATOM 1.0 conformant XML. For example
32 // it does not report errors with related to the following constraints:
34 // - atom:entry elements MUST NOT contain more than one atom:link
35 // element with a rel attribute value of "alternate" that has the
36 // same combination of type and hreflang attribute values.
37 // - atom:entry elements that contain no child atom:content element
38 // MUST contain at least one atom:link element with a rel attribute
39 // value of "alternate".
43 using System.Collections.Generic;
44 using System.Collections.ObjectModel;
45 using System.Globalization;
47 using System.Runtime.Serialization;
50 using System.Xml.Schema;
51 using System.Xml.Serialization;
53 namespace System.ServiceModel.Syndication
55 [XmlRoot ("entry", Namespace = "http://www.w3.org/2005/Atom")]
56 public class Atom10ItemFormatter : SyndicationItemFormatter, IXmlSerializable
58 const string AtomNamespace ="http://www.w3.org/2005/Atom";
60 bool preserve_att_ext = true, preserve_elem_ext = true;
63 public Atom10ItemFormatter ()
67 public Atom10ItemFormatter (SyndicationItem itemToWrite)
72 public Atom10ItemFormatter (Type itemTypeToCreate)
74 if (itemTypeToCreate == null)
75 throw new ArgumentNullException ("itemTypeToCreate");
76 item_type = itemTypeToCreate;
79 protected Type ItemType {
80 get { return item_type; }
83 public bool PreserveAttributeExtensions {
84 get { return preserve_att_ext; }
85 set { preserve_att_ext = value; }
88 public bool PreserveElementExtensions {
89 get { return preserve_elem_ext; }
90 set { preserve_elem_ext = value; }
93 public override string Version {
94 get { return "Atom10"; }
97 protected override SyndicationItem CreateItemInstance ()
99 return new SyndicationItem ();
102 public override bool CanRead (XmlReader reader)
105 throw new ArgumentNullException ("reader");
106 reader.MoveToContent ();
107 return reader.IsStartElement ("entry", AtomNamespace);
110 public override void ReadFrom (XmlReader reader)
112 if (!CanRead (reader))
113 throw new XmlException (String.Format ("Element '{0}' in namespace '{1}' is not accepted by this syndication formatter", reader.LocalName, reader.NamespaceURI));
114 ReadXml (reader, true);
117 public override void WriteTo (XmlWriter writer)
119 WriteXml (writer, true);
122 void IXmlSerializable.ReadXml (XmlReader reader)
124 ReadXml (reader, false);
127 void IXmlSerializable.WriteXml (XmlWriter writer)
129 WriteXml (writer, false);
132 XmlSchema IXmlSerializable.GetSchema ()
139 void ReadXml (XmlReader reader, bool fromSerializable)
142 throw new ArgumentNullException ("reader");
143 SetItem (CreateItemInstance ());
145 reader.MoveToContent ();
147 if (reader.MoveToFirstAttribute ()) {
149 if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
151 if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, Item, Version) && PreserveAttributeExtensions)
152 Item.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
153 } while (reader.MoveToNextAttribute ());
156 reader.ReadStartElement ();
158 for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
159 if (reader.NodeType != XmlNodeType.Element)
160 throw new XmlException ("Only element node is expected under 'entry' element");
161 if (reader.NamespaceURI == AtomNamespace)
162 switch (reader.LocalName) {
164 SyndicationPerson p = Item.CreatePerson ();
165 ReadPerson (reader, p);
166 Item.Authors.Add (p);
169 SyndicationCategory c = Item.CreateCategory ();
170 ReadCategory (reader, c);
171 Item.Categories.Add (c);
174 p = Item.CreatePerson ();
175 ReadPerson (reader, p);
176 Item.Contributors.Add (p);
179 Item.Id = reader.ReadElementContentAsString ();
182 SyndicationLink l = Item.CreateLink ();
183 ReadLink (reader, l);
187 Item.PublishDate = XmlConvert.ToDateTimeOffset (reader.ReadElementContentAsString ());
190 Item.Copyright = ReadTextSyndicationContent (reader);
193 Item.SourceFeed = ReadSourceFeed (reader);
196 Item.Summary = ReadTextSyndicationContent (reader);
199 Item.Title = ReadTextSyndicationContent (reader);
202 Item.LastUpdatedTime = XmlConvert.ToDateTimeOffset (reader.ReadElementContentAsString ());
205 // Atom 1.0 does not specify "content" element, but it is required to distinguish Content property from extension elements.
207 if (reader.GetAttribute ("src") != null) {
208 Item.Content = new UrlSyndicationContent (CreateUri (reader.GetAttribute ("src")), reader.GetAttribute ("type"));
212 switch (reader.GetAttribute ("type")) {
216 Item.Content = ReadTextSyndicationContent (reader);
219 SyndicationContent content;
220 if (!TryParseContent (reader, Item, reader.GetAttribute ("type"), Version, out content))
221 Item.Content = new XmlSyndicationContent (reader);
225 if (!TryParseElement (reader, Item, Version)) {
226 if (PreserveElementExtensions)
227 // FIXME: what to specify for maxExtensionSize
228 LoadElementExtensions (reader, Item, int.MaxValue);
234 reader.ReadEndElement (); // </item>
237 TextSyndicationContent ReadTextSyndicationContent (XmlReader reader)
239 TextSyndicationContentKind kind = TextSyndicationContentKind.Plaintext;
240 switch (reader.GetAttribute ("type")) {
242 kind = TextSyndicationContentKind.Html;
245 kind = TextSyndicationContentKind.XHtml;
248 string text = reader.ReadElementContentAsString ();
249 TextSyndicationContent t = new TextSyndicationContent (text, kind);
253 void ReadCategory (XmlReader reader, SyndicationCategory category)
255 if (reader.MoveToFirstAttribute ()) {
257 if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
259 if (reader.NamespaceURI == String.Empty) {
260 switch (reader.LocalName) {
262 category.Name = reader.Value;
265 category.Scheme = reader.Value;
268 category.Label = reader.Value;
272 if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, category, Version) && PreserveAttributeExtensions)
273 category.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
274 } while (reader.MoveToNextAttribute ());
275 reader.MoveToElement ();
278 if (!reader.IsEmptyElement) {
280 for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
281 if (!TryParseElement (reader, category, Version)) {
282 if (PreserveElementExtensions)
283 // FIXME: what should be used for maxExtenswionSize
284 LoadElementExtensions (reader, category, int.MaxValue);
290 reader.Read (); // </category> or <category ... />
293 void ReadLink (XmlReader reader, SyndicationLink link)
295 if (reader.MoveToFirstAttribute ()) {
297 if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
299 if (reader.NamespaceURI == String.Empty) {
300 switch (reader.LocalName) {
302 link.Uri = CreateUri (reader.Value);
305 link.RelationshipType = reader.Value;
308 link.MediaType = reader.Value;
311 link.Length = XmlConvert.ToInt64 (reader.Value);
314 link.Title = reader.Value;
318 if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, link, Version) && PreserveAttributeExtensions)
319 link.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
320 } while (reader.MoveToNextAttribute ());
321 reader.MoveToElement ();
324 if (!reader.IsEmptyElement) {
326 for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
327 if (!TryParseElement (reader, link, Version)) {
328 if (PreserveElementExtensions)
329 // FIXME: what should be used for maxExtenswionSize
330 LoadElementExtensions (reader, link, int.MaxValue);
336 reader.Read (); // </link> or <link ... />
339 void ReadPerson (XmlReader reader, SyndicationPerson person)
341 if (reader.MoveToFirstAttribute ()) {
343 if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
345 if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, person, Version) && PreserveAttributeExtensions)
346 person.AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
347 } while (reader.MoveToNextAttribute ());
348 reader.MoveToElement ();
351 if (!reader.IsEmptyElement) {
353 for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
354 if (reader.NodeType == XmlNodeType.Element && reader.NamespaceURI == AtomNamespace) {
355 switch (reader.LocalName) {
357 person.Name = reader.ReadElementContentAsString ();
360 person.Uri = reader.ReadElementContentAsString ();
363 person.Email = reader.ReadElementContentAsString ();
367 if (!TryParseElement (reader, person, Version)) {
368 if (PreserveElementExtensions)
369 // FIXME: what should be used for maxExtenswionSize
370 LoadElementExtensions (reader, person, int.MaxValue);
376 reader.Read (); // end element or empty element
379 SyndicationFeed ReadSourceFeed (XmlReader reader)
381 SyndicationFeed feed = null;
382 if (!reader.IsEmptyElement) {
383 Atom10FeedFormatter ff = new Atom10FeedFormatter ();
384 ((IXmlSerializable) ff).ReadXml (reader); // this does not check the QName of the wrapping element.
388 feed = new SyndicationFeed ();
389 reader.Read (); // </source> or <source ... />
393 Uri CreateUri (string uri)
395 return new Uri (uri, UriKind.RelativeOrAbsolute);
400 void WriteXml (XmlWriter writer, bool writeRoot)
403 throw new ArgumentNullException ("writer");
405 throw new InvalidOperationException ("Syndication item must be set before writing");
408 writer.WriteStartElement ("entry", AtomNamespace);
410 if (Item.BaseUri != null)
411 writer.WriteAttributeString ("xml", "base", null, Item.BaseUri.ToString ());
413 // atom:entry elements MUST contain exactly one atom:id element.
414 writer.WriteElementString ("id", AtomNamespace, Item.Id ?? new UniqueId ().ToString ());
416 // atom:entry elements MUST contain exactly one atom:title element.
417 (Item.Title ?? new TextSyndicationContent (String.Empty)).WriteTo (writer, "title", AtomNamespace);
419 if (Item.Summary != null)
420 Item.Summary.WriteTo (writer, "summary", AtomNamespace);
422 if (!Item.PublishDate.Equals (default (DateTimeOffset))) {
423 writer.WriteStartElement ("published");
424 // FIXME: use DateTimeOffset itself once it is implemented.
425 writer.WriteString (XmlConvert.ToString (Item.PublishDate.UtcDateTime, XmlDateTimeSerializationMode.RoundtripKind));
426 writer.WriteEndElement ();
429 // atom:entry elements MUST contain exactly one atom:updated element.
430 writer.WriteStartElement ("updated", AtomNamespace);
431 // FIXME: use DateTimeOffset itself once it is implemented.
432 writer.WriteString (XmlConvert.ToString (Item.LastUpdatedTime.UtcDateTime, XmlDateTimeSerializationMode.RoundtripKind));
433 writer.WriteEndElement ();
435 foreach (SyndicationPerson author in Item.Authors)
436 if (author != null) {
437 writer.WriteStartElement ("author", AtomNamespace);
438 WriteAttributeExtensions (writer, author, Version);
439 writer.WriteElementString ("name", AtomNamespace, author.Name);
440 writer.WriteElementString ("uri", AtomNamespace, author.Uri);
441 writer.WriteElementString ("email", AtomNamespace, author.Email);
442 WriteElementExtensions (writer, author, Version);
443 writer.WriteEndElement ();
446 foreach (SyndicationPerson contributor in Item.Contributors) {
447 if (contributor != null) {
448 writer.WriteStartElement ("contributor", AtomNamespace);
449 WriteAttributeExtensions (writer, contributor, Version);
450 writer.WriteElementString ("name", AtomNamespace, contributor.Name);
451 writer.WriteElementString ("uri", AtomNamespace, contributor.Uri);
452 writer.WriteElementString ("email", AtomNamespace, contributor.Email);
453 WriteElementExtensions (writer, contributor, Version);
454 writer.WriteEndElement ();
458 foreach (SyndicationCategory category in Item.Categories)
459 if (category != null) {
460 writer.WriteStartElement ("category", AtomNamespace);
461 if (category.Name != null)
462 writer.WriteAttributeString ("term", category.Name);
463 if (category.Label != null)
464 writer.WriteAttributeString ("label", category.Label);
465 if (category.Scheme != null)
466 writer.WriteAttributeString ("scheme", category.Scheme);
467 WriteAttributeExtensions (writer, category, Version);
468 WriteElementExtensions (writer, category, Version);
469 writer.WriteEndElement ();
472 foreach (SyndicationLink link in Item.Links)
474 writer.WriteStartElement ("link");
475 if (link.RelationshipType != null)
476 writer.WriteAttributeString ("rel", link.RelationshipType);
477 if (link.MediaType != null)
478 writer.WriteAttributeString ("type", link.MediaType);
479 if (link.Title != null)
480 writer.WriteAttributeString ("title", link.Title);
481 if (link.Length != 0)
482 writer.WriteAttributeString ("length", link.Length.ToString (CultureInfo.InvariantCulture));
483 writer.WriteAttributeString ("href", link.Uri != null ? link.Uri.ToString () : String.Empty);
484 WriteAttributeExtensions (writer, link, Version);
485 WriteElementExtensions (writer, link, Version);
486 writer.WriteEndElement ();
489 if (Item.Content != null)
490 Item.Content.WriteTo (writer, "content", AtomNamespace);
492 if (Item.Copyright != null)
493 Item.Copyright.WriteTo (writer, "rights", AtomNamespace);
495 if (Item.SourceFeed != null) {
496 writer.WriteStartElement ("source", AtomNamespace);
497 Item.SourceFeed.SaveAsAtom10 (writer);
498 writer.WriteEndElement ();
501 WriteElementExtensions (writer, Item, Version);
503 writer.WriteEndElement ();