BaseReader.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. namespace FormulaOneApp.Rss.Readers
  2. {
  3. using System;
  4. using System.Linq;
  5. using System.Xml.Linq;
  6. using Exceptions;
  7. using Models;
  8. /// <summary>
  9. /// </summary>
  10. internal abstract class BaseReader
  11. {
  12. private static readonly XNamespace NsMedia = "http://search.yahoo.com/mrss/";
  13. private static readonly XNamespace NsItunes = "http://www.itunes.com/dtds/podcast-1.0.dtd";
  14. private static readonly XNamespace NsRdfNamespaceUri = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
  15. /// <summary>
  16. /// Return the RssType based on the XDocument passed
  17. /// </summary>
  18. /// <param name="doc"></param>
  19. /// <returns></returns>
  20. public static RssType GetFeedType(XDocument doc)
  21. {
  22. if (doc.Root == null) throw new NotSupportedFeedException("Not Supported");
  23. RssType result;
  24. XNamespace defaultNamespace = doc.Root.GetDefaultNamespace();
  25. if (defaultNamespace.NamespaceName.EndsWith("Atom"))
  26. {
  27. result = RssType.Atom;
  28. }
  29. else if (doc.Root.Name == (NsRdfNamespaceUri + "RDF"))
  30. {
  31. result = RssType.Rdf;
  32. }
  33. else
  34. {
  35. result = RssType.Rss;
  36. }
  37. return result;
  38. }
  39. /// <summary>
  40. /// Set the feed's image with the image extracted from the XDocument
  41. /// </summary>
  42. /// <param name="feed"></param>
  43. /// <param name="doc"></param>
  44. public void SetMediaImage(RssFeed feed, XDocument doc)
  45. {
  46. if (doc.Root != null)
  47. {
  48. XElement image = doc.Root.Descendants(NsItunes + "image").FirstOrDefault();
  49. if (image != null)
  50. {
  51. XAttribute xAttribute = image.Attribute("href");
  52. if (xAttribute != null) feed.ImageUri = new Uri(xAttribute.Value);
  53. return;
  54. }
  55. }
  56. XElement thumbnail = doc.Element(NsMedia + "thumbnail");
  57. if (thumbnail != null)
  58. {
  59. feed.ImageUri = new Uri(thumbnail.Value);
  60. return;
  61. }
  62. feed.ImageUri = new Uri("/Images/DefaultFeedIcon.jpg", UriKind.Relative);
  63. }
  64. /// <summary>
  65. /// Return a RssFeed object using the XDocument data
  66. /// </summary>
  67. /// <param name="doc"></param>
  68. /// <returns></returns>
  69. public abstract RssFeed LoadFeed(XDocument doc);
  70. }
  71. }