//============================================================ // ICOMPARABLE IMPLEMENTATION //============================================================ #region CompareTo(object obj) /// <summary> /// Compares the current instance with another object of the same type. /// </summary> /// <param name="obj">An object to compare with this instance.</param> /// <returns>A 32-bit signed integer that indicates the relative order of the objects being compared.</returns> /// <exception cref="ArgumentException">The <paramref name="obj"/> is not the expected <see cref="Type"/>.</exception> public int CompareTo(object obj) { //------------------------------------------------------------ // If target is a null reference, instance is greater //------------------------------------------------------------ if (obj == null) { return(1); } //------------------------------------------------------------ // Determine comparison result using property state of objects //------------------------------------------------------------ DiscoverableSyndicationEndpoint value = obj as DiscoverableSyndicationEndpoint; if (value != null) { int result = String.Compare(this.ContentType, value.ContentType, StringComparison.OrdinalIgnoreCase); result = result | Uri.Compare(this.Source, value.Source, UriComponents.AbsoluteUri, UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase); result = result | String.Compare(this.Title, value.Title, StringComparison.OrdinalIgnoreCase); return(result); } else { throw new ArgumentException(String.Format(null, "obj is not of type {0}, type was found to be '{1}'.", this.GetType().FullName, obj.GetType().FullName), "obj"); } }
/// <summary> /// Compares the current instance with another object of the same type. /// </summary> /// <param name="obj">An object to compare with this instance.</param> /// <returns>A 32-bit signed integer that indicates the relative order of the objects being compared.</returns> /// <exception cref="ArgumentException">The <paramref name="obj"/> is not the expected <see cref="Type"/>.</exception> public int CompareTo(object obj) { if (obj == null) { return(1); } DiscoverableSyndicationEndpoint value = obj as DiscoverableSyndicationEndpoint; if (value != null) { int result = String.Compare(this.ContentType, value.ContentType, StringComparison.OrdinalIgnoreCase); result = result | Uri.Compare(this.Source, value.Source, UriComponents.AbsoluteUri, UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase); result = result | String.Compare(this.Title, value.Title, StringComparison.OrdinalIgnoreCase); return(result); } else { throw new ArgumentException(String.Format(null, "obj is not of type {0}, type was found to be '{1}'.", this.GetType().FullName, obj.GetType().FullName), "obj"); } }
/// <summary> /// Extracts auto-discoverable syndication endpoints from the supplied HTML markup. /// </summary> /// <param name="content">The HTML markup to parse.</param> /// <returns> /// A collection of <see cref="DiscoverableSyndicationEndpoint"/> objects that represent auto-discoverable syndicated content endpoints contained within the <paramref name="content"/>. /// </returns> /// <remarks> /// See <a href="http://www.rssboard.org/rss-autodiscovery">http://www.rssboard.org/rss-autodiscovery</a> for /// further information about the auto-discovery of syndicated content. /// </remarks> /// <exception cref="ArgumentNullException">The <paramref name="content"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="content"/> is an empty string.</exception> public static Collection<DiscoverableSyndicationEndpoint> ExtractDiscoverableSyndicationEndpoints(string content) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ Collection<DiscoverableSyndicationEndpoint> results = new Collection<DiscoverableSyndicationEndpoint>(); Regex linkPattern = new Regex("<link[^>]+", RegexOptions.IgnoreCase); //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNullOrEmptyString(content, "content"); //------------------------------------------------------------ // Extract <link> elements using pattern matching //------------------------------------------------------------ MatchCollection links = linkPattern.Matches(content); foreach(Match link in links) { Hashtable linkAttributes = SyndicationDiscoveryUtility.ExtractHtmlAttributes(link.Value); if (linkAttributes.ContainsKey("HREF") && linkAttributes.ContainsKey("REL") && linkAttributes.ContainsKey("TYPE")) { string href = (string)linkAttributes["HREF"]; string rel = (string)linkAttributes["REL"]; string type = (string)linkAttributes["TYPE"]; //------------------------------------------------------------ // Determine if the rel attribute indicates an alternate link //------------------------------------------------------------ if (String.Compare(rel, "alternate", StringComparison.OrdinalIgnoreCase) == 0) { Uri url; if (Uri.TryCreate(href, UriKind.RelativeOrAbsolute, out url)) { //------------------------------------------------------------ // Create auto-discovery endpoint and add to results //------------------------------------------------------------ DiscoverableSyndicationEndpoint endpoint = new DiscoverableSyndicationEndpoint(); endpoint.Source = url; if (!String.IsNullOrEmpty(type)) { endpoint.ContentType = type; } if (linkAttributes.ContainsKey("TITLE")) { string title = (string)linkAttributes["TITLE"]; if (!String.IsNullOrEmpty(title)) { endpoint.Title = title; } } results.Add(endpoint); } } } } return results; }