//============================================================ // PUBLIC METHODS //============================================================ #region Fill(ISyndicationResource resource, SyndicationContentFormat format) /// <summary> /// Modifies the <see cref="ISyndicationResource"/> to match the data source. /// </summary> /// <param name="resource">The <see cref="ISyndicationResource"/> to be filled.</param> /// <param name="format">The <see cref="SyndicationContentFormat"/> enumeration value that indicates the type of syndication format that the <paramref name="resource"/> is expected to conform to.</param> /// <exception cref="ArgumentNullException">The <paramref name="resource"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentException">The <paramref name="format"/> is equal to <see cref="SyndicationContentFormat.None"/>.</exception> /// <exception cref="FormatException">The <paramref name="resource"/> data does not conform to the specified <paramref name="format"/>.</exception> public void Fill(ISyndicationResource resource, SyndicationContentFormat format) { //------------------------------------------------------------ // Validate parameters //------------------------------------------------------------ Guard.ArgumentNotNull(resource, "resource"); if (format == SyndicationContentFormat.None) { throw new ArgumentException(String.Format(null, "The specified syndication content format of {0} is invalid.", format), "format"); } //------------------------------------------------------------ // Extract syndication resource meta-data //------------------------------------------------------------ SyndicationResourceMetadata resourceMetadata = new SyndicationResourceMetadata(this.Navigator); //------------------------------------------------------------ // Verify resource conforms to specified format //------------------------------------------------------------ if (format != resourceMetadata.Format) { throw new FormatException(String.Format(null, "The supplied syndication resource has a content format of {0}, which does not match the expected content format of {1}.", resourceMetadata.Format, format)); } //------------------------------------------------------------ // Fill syndication resource using appropriate data adapter //------------------------------------------------------------ switch (format) { case SyndicationContentFormat.Atom: this.FillAtomResource(resource, resourceMetadata); break; } }
/// <summary> /// Retrieves a <see cref="ISyndicationResource"/> from the data store that has the specified key. /// </summary> /// <param name="resourceKey">A <see cref="Guid"/> that represents the globally unique identifier for the resource to be retrieved.</param> /// <returns> /// The <see cref="ISyndicationResource"/> that has the specified <paramref name="resourceKey"/>. /// If no resource exists for the specified <paramref name="resourceKey"/>, returns a <b>null</b> reference. /// </returns> private ISyndicationResource ResourceGet(Guid resourceKey) { ISyndicationResource resource = null; if (!this.ResourceKeyExists(resourceKey)) { return(null); } DirectoryInfo applicationDirectory = new DirectoryInfo(this.ApplicationPath); if (applicationDirectory.Exists) { FileInfo[] files = applicationDirectory.GetFiles(String.Format(null, "{0}.*", resourceKey.ToString())); if (files != null && files.Length == 1 && files[0].Exists) { using (FileStream stream = new FileStream(files[0].FullName, FileMode.Open, FileAccess.Read)) { SyndicationContentFormat format = XmlSyndicationResourceProvider.ContentFormatByFileExtension(files[0].Extension); if (format != SyndicationContentFormat.None) { resource = XmlSyndicationResourceProvider.BuildResource(format, stream); } } } } return(resource); }
/// <summary> /// Gets a collection of all the resources in the data source that conform to the specified <see cref="SyndicationContentFormat"/>. /// </summary> /// <param name="format">A <see cref="SyndicationContentFormat"/> enumeration value that indicates the content format to filter resources by.</param> /// <returns> /// A <see cref="Collection{T}"/> of <see cref="ISyndicationResource"/> objects that represent the syndication resources that conform to the specified <paramref name="format"/>. /// </returns> private Collection <ISyndicationResource> ResourcesGet(SyndicationContentFormat format) { Collection <ISyndicationResource> resources = new Collection <ISyndicationResource>(); DirectoryInfo applicationDirectory = new DirectoryInfo(this.ApplicationPath); if (applicationDirectory.Exists) { FileInfo[] files = applicationDirectory.GetFiles(); foreach (FileInfo file in files) { if (file.Exists) { SyndicationContentFormat contentFormat = XmlSyndicationResourceProvider.ContentFormatByFileExtension(file.Extension); if (contentFormat == format) { using (FileStream stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read)) { ISyndicationResource resource = XmlSyndicationResourceProvider.BuildResource(contentFormat, stream); if (resource != null) { resources.Add(resource); } } } } } } return(resources); }
//============================================================ // STATIC METHODS //============================================================ #region ContentFormatAsMimeType(SyndicationContentFormat format) /// <summary> /// Returns the MIME content type for the supplied <see cref="SyndicationContentFormat"/>. /// </summary> /// <param name="format">The <see cref="SyndicationContentFormat"/> to get the MIME content type for.</param> /// <returns>The MIME content type for the supplied <paramref name="format"/>, otherwise returns <b>text/xml</b>.</returns> public static string ContentFormatAsMimeType(SyndicationContentFormat format) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ string contentType = "text/xml"; //------------------------------------------------------------ // Return MIME type based on supplied format //------------------------------------------------------------ foreach (System.Reflection.FieldInfo fieldInfo in typeof(SyndicationContentFormat).GetFields()) { if (fieldInfo.FieldType == typeof(SyndicationContentFormat)) { SyndicationContentFormat contentFormat = (SyndicationContentFormat)Enum.Parse(fieldInfo.FieldType, fieldInfo.Name); if (contentFormat == format) { object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(MimeMediaTypeAttribute), false); if (customAttributes != null && customAttributes.Length > 0) { MimeMediaTypeAttribute mediaType = customAttributes[0] as MimeMediaTypeAttribute; string mimeType = String.Format(null, "{0}/{1}", mediaType.Name, mediaType.SubName); contentType = mimeType; break; } } } } return(contentType); }
/// <summary> /// Modifies the <see cref="ISyndicationResource"/> to match the data source. /// </summary> /// <param name="resource">The <see cref="ISyndicationResource"/> to be filled.</param> /// <param name="format">The <see cref="SyndicationContentFormat"/> enumeration value that indicates the type of syndication format that the <paramref name="resource"/> is expected to conform to.</param> /// <exception cref="ArgumentNullException">The <paramref name="resource"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentException">The <paramref name="format"/> is equal to <see cref="SyndicationContentFormat.None"/>.</exception> /// <exception cref="FormatException">The <paramref name="resource"/> data does not conform to the specified <paramref name="format"/>.</exception> public void Fill(ISyndicationResource resource, SyndicationContentFormat format) { Guard.ArgumentNotNull(resource, "resource"); if (format == SyndicationContentFormat.None) { throw new ArgumentException(String.Format(null, "The specified syndication content format of {0} is invalid.", format), "format"); } SyndicationResourceMetadata resourceMetadata = new SyndicationResourceMetadata(this.Navigator); if (format != resourceMetadata.Format) { throw new FormatException(String.Format(null, "The supplied syndication resource has a content format of {0}, which does not match the expected content format of {1}.", resourceMetadata.Format, format)); } switch (format) { case SyndicationContentFormat.Apml: this.FillApmlResource(resource, resourceMetadata); break; case SyndicationContentFormat.Atom: this.FillAtomResource(resource, resourceMetadata); break; case SyndicationContentFormat.AtomCategoryDocument: this.FillAtomPublishingResource(resource, resourceMetadata); break; case SyndicationContentFormat.AtomServiceDocument: this.FillAtomPublishingResource(resource, resourceMetadata); break; case SyndicationContentFormat.BlogML: this.FillBlogMLResource(resource, resourceMetadata); break; case SyndicationContentFormat.Opml: this.FillOpmlResource(resource, resourceMetadata); break; case SyndicationContentFormat.Rsd: this.FillRsdResource(resource, resourceMetadata); break; case SyndicationContentFormat.Rss: this.FillRssResource(resource, resourceMetadata); break; } }
/// <summary> /// Returns the content disposition header value for the supplied syndication format. /// </summary> /// <param name="format">The <see cref="SyndicationContentFormat"/> enumeration value to determine the content disposition for.</param> /// <returns> /// The content disposition header value that is mapped to the supplied <see cref="SyndicationContentFormat"/>. /// If no content disposition is mapped to the supplied <paramref name="format"/>, returns an empty string. /// </returns> private static string GetContentDisposition(SyndicationContentFormat format) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ string contentDisposition = String.Empty; //------------------------------------------------------------ // Map content disposition to supplied format //------------------------------------------------------------ switch (format) { case SyndicationContentFormat.Apml: contentDisposition = "inline; filename=apml.xml"; break; case SyndicationContentFormat.Atom: contentDisposition = "inline; filename=atom.xml"; break; case SyndicationContentFormat.BlogML: contentDisposition = "inline; filename=blogML.xml"; break; case SyndicationContentFormat.MicroSummaryGenerator: contentDisposition = "inline; filename=microSummary.xml"; break; case SyndicationContentFormat.NewsML: contentDisposition = "inline; filename=newsML.xml"; break; case SyndicationContentFormat.OpenSearchDescription: contentDisposition = "inline; filename=openSearch.xml"; break; case SyndicationContentFormat.Opml: contentDisposition = "inline; filename=opml.xml"; break; case SyndicationContentFormat.Rsd: contentDisposition = "inline; filename=rsd.xml"; break; case SyndicationContentFormat.Rss: contentDisposition = "inline; filename=rss.xml"; break; default: contentDisposition = String.Empty; break; } return(contentDisposition); }
/// <summary> /// Gets a collection of all the resources in the data source in pages of data. /// </summary> /// <param name="pageIndex">The index of the page of results to return. <paramref name="pageIndex"/> is zero-based.</param> /// <param name="pageSize">The size of the page of results to return.</param> /// <param name="totalRecords">The total number of syndication resources in the data store.</param> /// <returns> /// A <see cref="Collection{T}"/> that contains a page of <see cref="ISyndicationResource"/> objects /// with a size of <paramref name="pageSize"/>, beginning at the page specified by <paramref name="pageIndex"/>. /// </returns> private Collection <ISyndicationResource> ResourcesGet(int pageIndex, int pageSize, out int totalRecords) { Collection <ISyndicationResource> resources = new Collection <ISyndicationResource>(); Collection <ISyndicationResource> pagedResources = new Collection <ISyndicationResource>(); DirectoryInfo applicationDirectory = new DirectoryInfo(this.ApplicationPath); if (applicationDirectory.Exists) { FileInfo[] files = applicationDirectory.GetFiles(); foreach (FileInfo file in files) { if (file.Exists) { SyndicationContentFormat format = XmlSyndicationResourceProvider.ContentFormatByFileExtension(file.Extension); if (format != SyndicationContentFormat.None) { using (FileStream stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read)) { ISyndicationResource resource = XmlSyndicationResourceProvider.BuildResource(format, stream); if (resource != null) { resources.Add(resource); } } } } } } totalRecords = resources.Count; if (pageSize > resources.Count) { pagedResources = new Collection <ISyndicationResource>(resources); } else { ISyndicationResource[] array = new ISyndicationResource[resources.Count]; resources.CopyTo(array, 0); int startIndex = (pageIndex == 0 ? 0 : (pageSize + pageIndex - 1)); for (int i = startIndex; i < pageSize; i++) { if (i > array.Length - 1) { break; } pagedResources.Add(array[i]); } } return(pagedResources); }
/// <summary> /// Provides example code for the SyndicationDiscoveryUtility.SyndicationContentFormatGet(Uri) method /// </summary> public static void SyndicationContentFormatGetExample() { SyndicationContentFormat format = SyndicationContentFormat.None; Uri url = new Uri("http://feeds.feedburner.com/HanselminutesCompleteMP3?format=xml"); format = SyndicationDiscoveryUtility.SyndicationContentFormatGet(url); if (format != SyndicationContentFormat.None) { // Do something based on the determined content format } }
/// <summary> /// Gets a collection of all the resources in the data source that conform to the specified <see cref="SyndicationContentFormat"/>. /// </summary> /// <param name="format">A <see cref="SyndicationContentFormat"/> enumeration values that indicates the format of the resources to be returned.</param> /// <returns>A <see cref="Collection{T}"/> of all of the syndication resources contained in the data source that conform to the specified <see cref="SyndicationContentFormat"/>.</returns> /// <remarks> /// <para> /// <see cref="GetResources(SyndicationContentFormat)"/> returns a list of all of the resources from the data source for the configured <see cref="ApplicationName"/> property. /// Syndication resources are returned in order of last time they were updated in the data source. /// </para> /// </remarks> /// <exception cref="ArgumentException">The <paramref name="format"/> is invalid.</exception> public override Collection <ISyndicationResource> GetResources(SyndicationContentFormat format) { if (format == SyndicationContentFormat.None) { throw new ArgumentException(String.Format(null, "The the specified content format of {0} is invalid.", format), "format"); } lock (providerSyncObject) { return(this.ResourcesGet(format)); } }
//============================================================ // PUBLIC METHODS //============================================================ #region ProcessRequest(HttpContext context) /// <summary> /// Enables processing of HTTP Web requests for syndicated content. /// </summary> /// <param name="context"> /// An <see cref="HttpContext"/> object that provides references to the intrinsic server objects /// (for example, <b>Request</b>, <b>Response</b>, <b>Session</b>, and <b>Server</b>) used to service HTTP requests. /// </param> /// <exception cref="ArgumentNullException">The <paramref name="context"/> is a null reference (Nothing in Visual Basic).</exception> public void ProcessRequest(HttpContext context) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ ISyndicationResource syndicationResource = null; //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNull(context, "context"); //------------------------------------------------------------ // Initialize handler state using query string parameters //------------------------------------------------------------ if (context.Request.QueryString["format"] != null && !String.IsNullOrEmpty(context.Request.QueryString["format"])) { SyndicationContentFormat format = SyndicationDiscoveryUtility.SyndicationContentFormatByName(context.Request.QueryString["format"]); if (format != SyndicationContentFormat.None) { this.Format = format; } } if (context.Request.QueryString["id"] != null && !String.IsNullOrEmpty(context.Request.QueryString["id"])) { this.Id = context.Request.QueryString["id"]; } //------------------------------------------------------------ // Get syndication resource using provider model //------------------------------------------------------------ if (this.Id != null) { syndicationResource = SyndicationManager.GetResource(this.Id); } else if (this.Format != SyndicationContentFormat.None) { Collection <ISyndicationResource> resources = SyndicationManager.GetResources(this.Format); if (resources != null && resources.Count > 0) { syndicationResource = resources[0]; } } //------------------------------------------------------------ // Write syndication resource data and header details //------------------------------------------------------------ if (syndicationResource != null) { this.WriteResource(context, syndicationResource); } }
/// <summary> /// Builds the physical file path for a syndication resource XML data file using the supplied <see cref="Guid"/> and <see cref="SyndicationContentFormat"/>. /// </summary> /// <param name="resourceKey">The globally unique identifier for the syndication resource that is used to as the file name.</param> /// <param name="format">The <see cref="SyndicationContentFormat"/> enumeration value that determines the file extension that is used.</param> /// <returns>The physical file path for a syndication resource XML data file using the supplied <see cref="Guid"/> and <see cref="SyndicationContentFormat"/>.</returns> /// <remarks> /// If the physical file path for a syndication resource XML data file defines a directory path that does not exist, /// the <see cref="BuildResourcePath(Guid, SyndicationContentFormat)"/> method will attempt to create the directory. /// </remarks> private string BuildResourcePath(Guid resourceKey, SyndicationContentFormat format) { string applicationDirectory = this.ApplicationPath.TrimEnd("\\".ToCharArray()); string resourceFileExtension = XmlSyndicationResourceProvider.ContentFormatAsFileExtension(format); string path = String.Format(null, "{0}\\{1}{2}", applicationDirectory, resourceKey.ToString(), resourceFileExtension); FileInfo pathInfo = new FileInfo(path); if (!pathInfo.Directory.Exists) { pathInfo.Directory.Create(); } return(path); }
/// <summary> /// Returns the file extension for the supplied <see cref="SyndicationContentFormat"/>. /// </summary> /// <param name="format">The <see cref="SyndicationContentFormat"/> to get the file extension for.</param> /// <returns>The file extension for the supplied <paramref name="format"/>, otherwise returns the <b>.xml</b> file extension.</returns> private static string ContentFormatAsFileExtension(SyndicationContentFormat format) { string fileExtension = ".xml"; switch (format) { case SyndicationContentFormat.Apml: fileExtension = ".apml"; break; case SyndicationContentFormat.Atom: fileExtension = ".atom"; break; case SyndicationContentFormat.BlogML: fileExtension = ".blogML"; break; case SyndicationContentFormat.MicroSummaryGenerator: fileExtension = ".microSummary"; break; case SyndicationContentFormat.NewsML: fileExtension = ".newsML"; break; case SyndicationContentFormat.OpenSearchDescription: fileExtension = ".openSearch"; break; case SyndicationContentFormat.Opml: fileExtension = ".opml"; break; case SyndicationContentFormat.Rsd: fileExtension = ".rsd"; break; case SyndicationContentFormat.Rss: fileExtension = ".rss"; break; } return(fileExtension); }
/// <summary> /// Returns the <see cref="SyndicationContentFormat"/> enumeration value that corresponds to the specified file extension. /// </summary> /// <param name="fileExtension">The file extension of the content format.</param> /// <returns>A <see cref="SyndicationContentFormat"/> enumeration value that corresponds to the specified string, otherwise returns <b>SyndicationContentFormat.None</b>.</returns> /// <remarks>This method disregards case of specified file extension.</remarks> /// <exception cref="ArgumentNullException">The <paramref name="fileExtension"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="fileExtension"/> is an empty string.</exception> private static SyndicationContentFormat ContentFormatByFileExtension(string fileExtension) { SyndicationContentFormat format = SyndicationContentFormat.None; Guard.ArgumentNotNullOrEmptyString(fileExtension, "fileExtension"); if (String.Compare(fileExtension, ".apml", StringComparison.OrdinalIgnoreCase) == 0) { format = SyndicationContentFormat.Apml; } else if (String.Compare(fileExtension, ".atom", StringComparison.OrdinalIgnoreCase) == 0) { format = SyndicationContentFormat.Atom; } else if (String.Compare(fileExtension, ".blogML", StringComparison.OrdinalIgnoreCase) == 0) { format = SyndicationContentFormat.BlogML; } else if (String.Compare(fileExtension, ".microSummary", StringComparison.OrdinalIgnoreCase) == 0) { format = SyndicationContentFormat.MicroSummaryGenerator; } else if (String.Compare(fileExtension, ".newsML", StringComparison.OrdinalIgnoreCase) == 0) { format = SyndicationContentFormat.NewsML; } else if (String.Compare(fileExtension, ".openSearch", StringComparison.OrdinalIgnoreCase) == 0) { format = SyndicationContentFormat.OpenSearchDescription; } else if (String.Compare(fileExtension, ".opml", StringComparison.OrdinalIgnoreCase) == 0) { format = SyndicationContentFormat.Opml; } else if (String.Compare(fileExtension, ".rsd", StringComparison.OrdinalIgnoreCase) == 0) { format = SyndicationContentFormat.Rsd; } else if (String.Compare(fileExtension, ".rss", StringComparison.OrdinalIgnoreCase) == 0) { format = SyndicationContentFormat.Rss; } return(format); }
/// <summary> /// Returns the <see cref="SyndicationContentFormat"/> enumeration value that corresponds to the specified MIME content type. /// </summary> /// <param name="contentType">The MIME type of the syndication format.</param> /// <returns>A <see cref="SyndicationContentFormat"/> enumeration value that corresponds to the specified string, otherwise returns <b>SyndicationContentFormat.None</b>.</returns> /// <remarks>This method disregards case of specified MIME content type.</remarks> /// <exception cref="ArgumentNullException">The <paramref name="contentType"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="contentType"/> is an empty string.</exception> public static SyndicationContentFormat ContentFormatByMimeType(string contentType) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ SyndicationContentFormat contentFormat = SyndicationContentFormat.None; //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNullOrEmptyString(contentType, "contentType"); //------------------------------------------------------------ // Determine syndication content format based on supplied name //------------------------------------------------------------ foreach (System.Reflection.FieldInfo fieldInfo in typeof(SyndicationContentFormat).GetFields()) { if (fieldInfo.FieldType == typeof(SyndicationContentFormat)) { SyndicationContentFormat format = (SyndicationContentFormat)Enum.Parse(fieldInfo.FieldType, fieldInfo.Name); object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(MimeMediaTypeAttribute), false); if (customAttributes != null && customAttributes.Length > 0) { MimeMediaTypeAttribute mediaType = customAttributes[0] as MimeMediaTypeAttribute; string mimeType = String.Format(null, "{0}/{1}", mediaType.Name, mediaType.SubName); if (String.Compare(contentType, mimeType, StringComparison.OrdinalIgnoreCase) == 0) { contentFormat = format; break; } } } } return(contentFormat); }
/// <summary> /// Instantiates a <see cref="ISyndicationResource"/> that conforms to the specified <see cref="SyndicationContentFormat"/> using the supplied <see cref="Stream"/>. /// </summary> /// <param name="stream">The <see cref="Stream"/> used to load the syndication resource.</param> /// <param name="format">A <see cref="SyndicationContentFormat"/> enumeration value that indicates the type syndication resource the <paramref name="stream"/> represents.</param> /// <returns> /// An <see cref="ISyndicationResource"/> object that conforms to the specified <paramref name="format"/>, initialized using the supplied <paramref name="stream"/>. /// If the <paramref name="format"/> is not supported by the provider, returns a <b>null</b> reference. /// </returns> /// <exception cref="ArgumentNullException">The <paramref name="stream"/> is a null reference (Nothing in Visual Basic).</exception> private static ISyndicationResource BuildResource(SyndicationContentFormat format, Stream stream) { //------------------------------------------------------------ // Validate parameters //------------------------------------------------------------ Guard.ArgumentNotNull(stream, "stream"); //------------------------------------------------------------ // Create syndication resource based on content format //------------------------------------------------------------ if (format == SyndicationContentFormat.Apml) { ApmlDocument document = new ApmlDocument(); document.Load(stream); return document; } else if (format == SyndicationContentFormat.Atom) { XPathDocument document = new XPathDocument(stream); XPathNavigator navigator = document.CreateNavigator(); navigator.MoveToRoot(); navigator.MoveToChild(XPathNodeType.Element); if(String.Compare(navigator.LocalName, "entry", StringComparison.OrdinalIgnoreCase) == 0) { AtomEntry entry = new AtomEntry(); entry.Load(navigator); return entry; } else if (String.Compare(navigator.LocalName, "feed", StringComparison.OrdinalIgnoreCase) == 0) { AtomFeed feed = new AtomFeed(); feed.Load(navigator); return feed; } else { return null; } } else if (format == SyndicationContentFormat.BlogML) { BlogMLDocument document = new BlogMLDocument(); document.Load(stream); return document; } else if (format == SyndicationContentFormat.Opml) { OpmlDocument document = new OpmlDocument(); document.Load(stream); return document; } else if (format == SyndicationContentFormat.Rsd) { RsdDocument document = new RsdDocument(); document.Load(stream); return document; } else if (format == SyndicationContentFormat.Rss) { RssFeed feed = new RssFeed(); feed.Load(stream); return feed; } else { return null; } }
/// <summary> /// Instantiates a <see cref="ISyndicationResource"/> that conforms to the specified <see cref="SyndicationContentFormat"/> using the supplied <see cref="Stream"/>. /// </summary> /// <param name="stream">The <see cref="Stream"/> used to load the syndication resource.</param> /// <param name="format">A <see cref="SyndicationContentFormat"/> enumeration value that indicates the type syndication resource the <paramref name="stream"/> represents.</param> /// <returns> /// An <see cref="ISyndicationResource"/> object that conforms to the specified <paramref name="format"/>, initialized using the supplied <paramref name="stream"/>. /// If the <paramref name="format"/> is not supported by the provider, returns a <b>null</b> reference. /// </returns> /// <exception cref="ArgumentNullException">The <paramref name="stream"/> is a null reference (Nothing in Visual Basic).</exception> private static ISyndicationResource BuildResource(SyndicationContentFormat format, Stream stream) { Guard.ArgumentNotNull(stream, "stream"); if (format == SyndicationContentFormat.Apml) { ApmlDocument document = new ApmlDocument(); document.Load(stream); return(document); } else if (format == SyndicationContentFormat.Atom) { XPathDocument document = new XPathDocument(stream); XPathNavigator navigator = document.CreateNavigator(); navigator.MoveToRoot(); navigator.MoveToChild(XPathNodeType.Element); if (String.Compare(navigator.LocalName, "entry", StringComparison.OrdinalIgnoreCase) == 0) { AtomEntry entry = new AtomEntry(); entry.Load(navigator); return(entry); } else if (String.Compare(navigator.LocalName, "feed", StringComparison.OrdinalIgnoreCase) == 0) { AtomFeed feed = new AtomFeed(); feed.Load(navigator); return(feed); } else { return(null); } } else if (format == SyndicationContentFormat.BlogML) { BlogMLDocument document = new BlogMLDocument(); document.Load(stream); return(document); } else if (format == SyndicationContentFormat.Opml) { OpmlDocument document = new OpmlDocument(); document.Load(stream); return(document); } else if (format == SyndicationContentFormat.Rsd) { RsdDocument document = new RsdDocument(); document.Load(stream); return(document); } else if (format == SyndicationContentFormat.Rss) { RssFeed feed = new RssFeed(); feed.Load(stream); return(feed); } else { return(null); } }
/// <summary> /// Modifies the <see cref="ISyndicationResource"/> to match the data source. /// </summary> /// <param name="resource">The <see cref="ISyndicationResource"/> to be filled.</param> /// <param name="format">The <see cref="SyndicationContentFormat"/> enumeration value that indicates the type of syndication format that the <paramref name="resource"/> is expected to conform to.</param> /// <exception cref="ArgumentNullException">The <paramref name="resource"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentException">The <paramref name="format"/> is equal to <see cref="SyndicationContentFormat.None"/>.</exception> /// <exception cref="FormatException">The <paramref name="resource"/> data does not conform to the specified <paramref name="format"/>.</exception> public void Fill(ISyndicationResource resource, SyndicationContentFormat format) { //------------------------------------------------------------ // Validate parameters //------------------------------------------------------------ Guard.ArgumentNotNull(resource, "resource"); if (format == SyndicationContentFormat.None) { throw new ArgumentException(String.Format(null, "The specified syndication content format of {0} is invalid.", format), "format"); } //------------------------------------------------------------ // Extract syndication resource meta-data //------------------------------------------------------------ SyndicationResourceMetadata resourceMetadata = new SyndicationResourceMetadata(this.Navigator); //------------------------------------------------------------ // Verify resource conforms to specified format //------------------------------------------------------------ if (format != resourceMetadata.Format) { throw new FormatException(String.Format(null, "The supplied syndication resource has a content format of {0}, which does not match the expected content format of {1}.", resourceMetadata.Format, format)); } //------------------------------------------------------------ // Fill syndication resource using appropriate data adapter //------------------------------------------------------------ switch (format) { case SyndicationContentFormat.Atom: this.FillAtomResource(resource, resourceMetadata); break; } }
/// <summary> /// Gets a collection of all the resources in the data source that conform to the specified <see cref="SyndicationContentFormat"/>. /// </summary> /// <param name="format">A <see cref="SyndicationContentFormat"/> enumeration values that indicates the format of the resources to be returned.</param> /// <returns>A <see cref="Collection{T}"/> of all of the syndication resources contained in the data source that conform to the specified <see cref="SyndicationContentFormat"/>.</returns> /// <remarks> /// <para> /// <see cref="GetResources(SyndicationContentFormat)"/> returns a list of all of the resources from the data source for the configured <see cref="ApplicationName"/> property. /// Syndication resources are returned in order of last time they were updated in the data source. /// </para> /// </remarks> public static Collection<ISyndicationResource> GetResources(SyndicationContentFormat format) { return SyndicationManager.Provider.GetResources(format); }
/// <summary> /// Gets a collection of all the resources in the data source that conform to the specified <see cref="SyndicationContentFormat"/>. /// </summary> /// <param name="format">A <see cref="SyndicationContentFormat"/> enumeration values that indicates the format of the resources to be returned.</param> /// <returns>A <see cref="Collection{T}"/> of all of the syndication resources contained in the data source that conform to the specified <see cref="SyndicationContentFormat"/>.</returns> /// <remarks> /// <para> /// <see cref="GetResources(SyndicationContentFormat)"/> returns a list of all of the resources from the data source for the configured <see cref="ApplicationName"/> property. /// Syndication resources are returned in order of last time they were updated in the data source. /// </para> /// </remarks> public abstract Collection <ISyndicationResource> GetResources(SyndicationContentFormat format);
/// <summary> /// Extracts the content format, version, and XML namespaces for a syndication resource from the supplied <see cref="XPathNavigator"/>. /// </summary> /// <param name="resource">The <see cref="XPathNavigator"/> to extract the syndication resource meta-data from.</param> /// <exception cref="ArgumentNullException">The <paramref name="resource"/> is a null reference (Nothing in Visual Basic).</exception> private void Load(XPathNavigator resource) { XPathNavigator navigator = null; Version version = null; Guard.ArgumentNotNull(resource, "resource"); Dictionary <string, string> namespaces = (Dictionary <string, string>)resource.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml); foreach (string prefix in namespaces.Keys) { resourceNamespaces.Add(prefix, namespaces[prefix]); } resourceVersion = SyndicationResourceMetadata.GetVersionFromAttribute(resource, "version"); if (SyndicationResourceMetadata.TryParseApmlResource(resource, out navigator, out version)) { resourceFormat = SyndicationContentFormat.Apml; resourceRootNode = navigator; resourceVersion = version; } else if (SyndicationResourceMetadata.TryParseAtomResource(resource, out navigator, out version)) { resourceFormat = SyndicationContentFormat.Atom; resourceRootNode = navigator; resourceVersion = version; } else if (SyndicationResourceMetadata.TryParseAtomPublishingCategoriesResource(resource, out navigator, out version)) { resourceFormat = SyndicationContentFormat.AtomCategoryDocument; resourceRootNode = navigator; resourceVersion = version; } else if (SyndicationResourceMetadata.TryParseAtomPublishingServiceResource(resource, out navigator, out version)) { resourceFormat = SyndicationContentFormat.AtomServiceDocument; resourceRootNode = navigator; resourceVersion = version; } else if (SyndicationResourceMetadata.TryParseBlogMLResource(resource, out navigator, out version)) { resourceFormat = SyndicationContentFormat.BlogML; resourceRootNode = navigator; resourceVersion = version; } else if (SyndicationResourceMetadata.TryParseMicroSummaryGeneratorResource(resource, out navigator, out version)) { resourceFormat = SyndicationContentFormat.MicroSummaryGenerator; resourceRootNode = navigator; resourceVersion = version; } else if (SyndicationResourceMetadata.TryParseNewsMLResource(resource, out navigator, out version)) { resourceFormat = SyndicationContentFormat.NewsML; resourceRootNode = navigator; resourceVersion = version; } else if (SyndicationResourceMetadata.TryParseOpenSearchDescriptionResource(resource, out navigator, out version)) { resourceFormat = SyndicationContentFormat.OpenSearchDescription; resourceRootNode = navigator; resourceVersion = version; } else if (SyndicationResourceMetadata.TryParseOpmlResource(resource, out navigator, out version)) { resourceFormat = SyndicationContentFormat.Opml; resourceRootNode = navigator; resourceVersion = version; } else if (SyndicationResourceMetadata.TryParseRsdResource(resource, out navigator, out version)) { resourceFormat = SyndicationContentFormat.Rsd; resourceRootNode = navigator; resourceVersion = version; } else if (SyndicationResourceMetadata.TryParseRssResource(resource, out navigator, out version)) { resourceFormat = SyndicationContentFormat.Rss; resourceRootNode = navigator; resourceVersion = version; } else { resourceFormat = SyndicationContentFormat.None; resourceRootNode = null; resourceVersion = null; } }
/// <summary> /// Extracts the content format, version, and XML namespaces for a syndication resource from the supplied <see cref="XPathNavigator"/>. /// </summary> /// <param name="resource">The <see cref="XPathNavigator"/> to extract the syndication resource meta-data from.</param> /// <exception cref="ArgumentNullException">The <paramref name="resource"/> is a null reference (Nothing in Visual Basic).</exception> private void Load(XPathNavigator resource) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ XPathNavigator navigator = null; Version version = null; //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNull(resource, "resource"); //------------------------------------------------------------ // Get XML namespaces declared on syndication resource //------------------------------------------------------------ Dictionary<string, string> namespaces = (Dictionary<string, string>)resource.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml); foreach (string prefix in namespaces.Keys) { resourceNamespaces.Add(prefix, namespaces[prefix]); } //------------------------------------------------------------ // Get syndication resource format version using // version XML attribute //------------------------------------------------------------ resourceVersion = SyndicationResourceMetadata.GetVersionFromAttribute(resource, "version"); //------------------------------------------------------------ // Determine content format based on root element qualified name // and expected XML namespace(s) //------------------------------------------------------------ if (SyndicationResourceMetadata.TryParseApmlResource(resource, out navigator, out version)) { resourceFormat = SyndicationContentFormat.Apml; resourceRootNode = navigator; resourceVersion = version; } else if (SyndicationResourceMetadata.TryParseAtomResource(resource, out navigator, out version)) { resourceFormat = SyndicationContentFormat.Atom; resourceRootNode = navigator; resourceVersion = version; } else if (SyndicationResourceMetadata.TryParseAtomPublishingCategoriesResource(resource, out navigator, out version)) { resourceFormat = SyndicationContentFormat.AtomCategoryDocument; resourceRootNode = navigator; resourceVersion = version; } else if (SyndicationResourceMetadata.TryParseAtomPublishingServiceResource(resource, out navigator, out version)) { resourceFormat = SyndicationContentFormat.AtomServiceDocument; resourceRootNode = navigator; resourceVersion = version; } else if (SyndicationResourceMetadata.TryParseBlogMLResource(resource, out navigator, out version)) { resourceFormat = SyndicationContentFormat.BlogML; resourceRootNode = navigator; resourceVersion = version; } else if (SyndicationResourceMetadata.TryParseMicroSummaryGeneratorResource(resource, out navigator, out version)) { resourceFormat = SyndicationContentFormat.MicroSummaryGenerator; resourceRootNode = navigator; resourceVersion = version; } else if (SyndicationResourceMetadata.TryParseNewsMLResource(resource, out navigator, out version)) { resourceFormat = SyndicationContentFormat.NewsML; resourceRootNode = navigator; resourceVersion = version; } else if (SyndicationResourceMetadata.TryParseOpenSearchDescriptionResource(resource, out navigator, out version)) { resourceFormat = SyndicationContentFormat.OpenSearchDescription; resourceRootNode = navigator; resourceVersion = version; } else if (SyndicationResourceMetadata.TryParseOpmlResource(resource, out navigator, out version)) { resourceFormat = SyndicationContentFormat.Opml; resourceRootNode = navigator; resourceVersion = version; } else if (SyndicationResourceMetadata.TryParseRsdResource(resource, out navigator, out version)) { resourceFormat = SyndicationContentFormat.Rsd; resourceRootNode = navigator; resourceVersion = version; } else if (SyndicationResourceMetadata.TryParseRssResource(resource, out navigator, out version)) { resourceFormat = SyndicationContentFormat.Rss; resourceRootNode = navigator; resourceVersion = version; } else { resourceFormat = SyndicationContentFormat.None; resourceRootNode = null; resourceVersion = null; } }
/// <summary> /// Gets a collection of all the resources in the data source that conform to the specified <see cref="SyndicationContentFormat"/>. /// </summary> /// <param name="format">A <see cref="SyndicationContentFormat"/> enumeration values that indicates the format of the resources to be returned.</param> /// <returns>A <see cref="Collection{T}"/> of all of the syndication resources contained in the data source that conform to the specified <see cref="SyndicationContentFormat"/>.</returns> /// <remarks> /// <para> /// <see cref="GetResources(SyndicationContentFormat)"/> returns a list of all of the resources from the data source for the configured <see cref="ApplicationName"/> property. /// Syndication resources are returned in order of last time they were updated in the data source. /// </para> /// </remarks> public abstract Collection<ISyndicationResource> GetResources(SyndicationContentFormat format);
/// <summary> /// Builds the physical file path for a syndication resource XML data file using the supplied <see cref="Guid"/> and <see cref="SyndicationContentFormat"/>. /// </summary> /// <param name="resourceKey">The globally unique identifier for the syndication resource that is used to as the file name.</param> /// <param name="format">The <see cref="SyndicationContentFormat"/> enumeration value that determines the file extension that is used.</param> /// <returns>The physical file path for a syndication resource XML data file using the supplied <see cref="Guid"/> and <see cref="SyndicationContentFormat"/>.</returns> /// <remarks> /// If the physical file path for a syndication resource XML data file defines a directory path that does not exist, /// the <see cref="BuildResourcePath(Guid, SyndicationContentFormat)"/> method will attempt to create the directory. /// </remarks> private string BuildResourcePath(Guid resourceKey, SyndicationContentFormat format) { string applicationDirectory = this.ApplicationPath.TrimEnd("\\".ToCharArray()); string resourceFileExtension = XmlSyndicationResourceProvider.ContentFormatAsFileExtension(format); string path = String.Format(null, "{0}\\{1}{2}", applicationDirectory, resourceKey.ToString(), resourceFileExtension); FileInfo pathInfo = new FileInfo(path); if (!pathInfo.Directory.Exists) { pathInfo.Directory.Create(); } return path; }
/// <summary> /// Returns the file extension for the supplied <see cref="SyndicationContentFormat"/>. /// </summary> /// <param name="format">The <see cref="SyndicationContentFormat"/> to get the file extension for.</param> /// <returns>The file extension for the supplied <paramref name="format"/>, otherwise returns the <b>.xml</b> file extension.</returns> private static string ContentFormatAsFileExtension(SyndicationContentFormat format) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ string fileExtension = ".xml"; //------------------------------------------------------------ // Return file extension based on content format //------------------------------------------------------------ switch (format) { case SyndicationContentFormat.Apml: fileExtension = ".apml"; break; case SyndicationContentFormat.Atom: fileExtension = ".atom"; break; case SyndicationContentFormat.BlogML: fileExtension = ".blogML"; break; case SyndicationContentFormat.MicroSummaryGenerator: fileExtension = ".microSummary"; break; case SyndicationContentFormat.NewsML: fileExtension = ".newsML"; break; case SyndicationContentFormat.OpenSearchDescription: fileExtension = ".openSearch"; break; case SyndicationContentFormat.Opml: fileExtension = ".opml"; break; case SyndicationContentFormat.Rsd: fileExtension = ".rsd"; break; case SyndicationContentFormat.Rss: fileExtension = ".rss"; break; } return fileExtension; }
/// <summary> /// Initializes the generic syndication feed using the supplied <see cref="RssFeed"/>. /// </summary> /// <param name="feed">The <see cref="RssFeed"/> to build an abstraction against.</param> /// <exception cref="ArgumentNullException">The <paramref name="feed"/> is a null reference (Nothing in Visual Basic).</exception> public void Parse(RssFeed feed) { //------------------------------------------------------------ // Validate parameters //------------------------------------------------------------ Guard.ArgumentNotNull(feed, "feed"); //------------------------------------------------------------ // Initialize generic feed //------------------------------------------------------------ feedResource = feed; feedFormat = SyndicationContentFormat.Rss; if (!String.IsNullOrEmpty(feed.Channel.Title)) { feedTitle = feed.Channel.Title; } if (!String.IsNullOrEmpty(feed.Channel.Description)) { feedDescription = feed.Channel.Description; } if (feed.Channel.LastBuildDate != DateTime.MinValue) { feedLastUpdatedOn = feed.Channel.LastBuildDate; } if (feed.Channel.Language != null) { feedLanguage = feed.Channel.Language; } foreach (RssCategory category in feed.Channel.Categories) { GenericSyndicationCategory genericCategory = new GenericSyndicationCategory(category); feedCategories.Add(genericCategory); } foreach (RssItem item in feed.Channel.Items) { GenericSyndicationItem genericItem = new GenericSyndicationItem(item); ((Collection<GenericSyndicationItem>)feedItems).Add(genericItem); } }
/// <summary> /// Initializes the generic syndication feed using the supplied <see cref="OpmlDocument"/>. /// Since OmplDocument hasn't direct mappings to feeds in this method we simply initialize /// feedFormat to Opml and feedResource to omplDocument /// </summary> /// <param name="feed">The <see cref="RssFeed"/> to build an abstraction against.</param> /// <exception cref="ArgumentNullException">The <paramref name="feed"/> is a null reference (Nothing in Visual Basic).</exception> public void Parse(OpmlDocument opmlDocument) { //------------------------------------------------------------ // Validate parameters //------------------------------------------------------------ Guard.ArgumentNotNull(opmlDocument, "omplDocument"); //------------------------------------------------------------ // Initialize generic feed //------------------------------------------------------------ feedResource = opmlDocument; feedFormat = SyndicationContentFormat.Opml; }
/// <summary> /// Gets a collection of all the resources in the data source that conform to the specified <see cref="SyndicationContentFormat"/>. /// </summary> /// <param name="format">A <see cref="SyndicationContentFormat"/> enumeration values that indicates the format of the resources to be returned.</param> /// <returns>A <see cref="Collection{T}"/> of all of the syndication resources contained in the data source that conform to the specified <see cref="SyndicationContentFormat"/>.</returns> /// <remarks> /// <para> /// <see cref="GetResources(SyndicationContentFormat)"/> returns a list of all of the resources from the data source for the configured <see cref="ApplicationName"/> property. /// Syndication resources are returned in order of last time they were updated in the data source. /// </para> /// </remarks> /// <exception cref="ArgumentException">The <paramref name="format"/> is invalid.</exception> public override Collection<ISyndicationResource> GetResources(SyndicationContentFormat format) { //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ if (format == SyndicationContentFormat.None) { throw new ArgumentException(String.Format(null, "The the specified content format of {0} is invalid.", format), "format"); } //------------------------------------------------------------ // Get syndication resources from data store //------------------------------------------------------------ lock (providerSyncObject) { return this.ResourcesGet(format); } }
/// <summary> /// Initializes the generic syndication feed using the supplied <see cref="AtomFeed"/>. /// </summary> /// <param name="feed">The <see cref="AtomFeed"/> to build an abstraction against.</param> /// <exception cref="ArgumentNullException">The <paramref name="feed"/> is a null reference (Nothing in Visual Basic).</exception> public void Parse(AtomFeed feed) { //------------------------------------------------------------ // Validate parameters //------------------------------------------------------------ Guard.ArgumentNotNull(feed, "feed"); //------------------------------------------------------------ // Initialize generic feed //------------------------------------------------------------ feedResource = feed; feedFormat = SyndicationContentFormat.Atom; if (feed.Title != null && !String.IsNullOrEmpty(feed.Title.Content)) { feedTitle = feed.Title.Content; } if (feed.Subtitle != null && !String.IsNullOrEmpty(feed.Title.Content)) { feedDescription = feed.Subtitle.Content; } if (feed.UpdatedOn != DateTime.MinValue) { feedLastUpdatedOn = feed.UpdatedOn; } if(feed.Language != null) { feedLanguage = feed.Language; } foreach(AtomCategory category in feed.Categories) { GenericSyndicationCategory genericCategory = new GenericSyndicationCategory(category); feedCategories.Add(genericCategory); } foreach(AtomEntry entry in feed.Entries) { GenericSyndicationItem genericItem = new GenericSyndicationItem(entry); ((Collection<GenericSyndicationItem>)feedItems).Add(genericItem); } }
/// <summary> /// Gets a collection of all the resources in the data source that conform to the specified <see cref="SyndicationContentFormat"/>. /// </summary> /// <param name="format">A <see cref="SyndicationContentFormat"/> enumeration values that indicates the format of the resources to be returned.</param> /// <returns>A <see cref="Collection{T}"/> of all of the syndication resources contained in the data source that conform to the specified <see cref="SyndicationContentFormat"/>.</returns> /// <remarks> /// <para> /// <see cref="GetResources(SyndicationContentFormat)"/> returns a list of all of the resources from the data source for the configured <see cref="ApplicationName"/> property. /// Syndication resources are returned in order of last time they were updated in the data source. /// </para> /// </remarks> public static Collection <ISyndicationResource> GetResources(SyndicationContentFormat format) { return(SyndicationManager.Provider.GetResources(format)); }
/// <summary> /// Gets a collection of all the resources in the data source that conform to the specified <see cref="SyndicationContentFormat"/>. /// </summary> /// <param name="format">A <see cref="SyndicationContentFormat"/> enumeration value that indicates the content format to filter resources by.</param> /// <returns> /// A <see cref="Collection{T}"/> of <see cref="ISyndicationResource"/> objects that represent the syndication resources that conform to the specified <paramref name="format"/>. /// </returns> private Collection<ISyndicationResource> ResourcesGet(SyndicationContentFormat format) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ Collection<ISyndicationResource> resources = new Collection<ISyndicationResource>(); //------------------------------------------------------------ // Load filtered syndication resources from XML data store //------------------------------------------------------------ DirectoryInfo applicationDirectory = new DirectoryInfo(this.ApplicationPath); if (applicationDirectory.Exists) { FileInfo[] files = applicationDirectory.GetFiles(); foreach(FileInfo file in files) { if(file.Exists) { SyndicationContentFormat contentFormat = XmlSyndicationResourceProvider.ContentFormatByFileExtension(file.Extension); if (contentFormat == format) { using (FileStream stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read)) { ISyndicationResource resource = XmlSyndicationResourceProvider.BuildResource(contentFormat, stream); if (resource != null) { resources.Add(resource); } } } } } } return resources; }