namespace FormulaOneApp.Rss.Readers
{
using System;
using System.Linq;
using System.Xml.Linq;
using Exceptions;
using Models;
///
///
internal abstract class BaseReader
{
private static readonly XNamespace NsMedia = "http://search.yahoo.com/mrss/";
private static readonly XNamespace NsItunes = "http://www.itunes.com/dtds/podcast-1.0.dtd";
private static readonly XNamespace NsRdfNamespaceUri = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
///
/// Return the RssType based on the XDocument passed
///
///
///
public static RssType GetFeedType(XDocument doc)
{
if (doc.Root == null) throw new NotSupportedFeedException("Not Supported");
RssType result;
XNamespace defaultNamespace = doc.Root.GetDefaultNamespace();
if (defaultNamespace.NamespaceName.EndsWith("Atom"))
{
result = RssType.Atom;
}
else if (doc.Root.Name == (NsRdfNamespaceUri + "RDF"))
{
result = RssType.Rdf;
}
else
{
result = RssType.Rss;
}
return result;
}
///
/// Set the feed's image with the image extracted from the XDocument
///
///
///
public void SetMediaImage(RssFeed feed, XDocument doc)
{
if (doc.Root != null)
{
XElement image = doc.Root.Descendants(NsItunes + "image").FirstOrDefault();
if (image != null)
{
XAttribute xAttribute = image.Attribute("href");
if (xAttribute != null) feed.ImageUri = new Uri(xAttribute.Value);
return;
}
}
XElement thumbnail = doc.Element(NsMedia + "thumbnail");
if (thumbnail != null)
{
feed.ImageUri = new Uri(thumbnail.Value);
return;
}
feed.ImageUri = new Uri("/Images/DefaultFeedIcon.jpg", UriKind.Relative);
}
///
/// Return a RssFeed object using the XDocument data
///
///
///
public abstract RssFeed LoadFeed(XDocument doc);
}
}