Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RssChannel.cs
1using System;
2using System.Collections.Generic;
3using System.Xml;
5
6namespace Waher.Content.Rss
7{
11 public class RssChannel
12 {
18 public RssChannel(XmlElement Xml, Uri BaseUri)
19 {
20 if (Xml is null)
21 throw new ArgumentNullException(nameof(Xml));
22
23 List<RssWarning> Warnings = new List<RssWarning>();
24 List<RssItem> Items = new List<RssItem>();
25 List<RssCategory> Categories = new List<RssCategory>();
26 List<IRssExtension> Extensions = new List<IRssExtension>();
27
28 foreach (XmlNode N in Xml.ChildNodes)
29 {
30 if (!(N is XmlElement E))
31 continue;
32
33 if (E.NamespaceURI == Xml.NamespaceURI)
34 {
35 switch (E.LocalName)
36 {
37 case "title":
38 this.Title = E.InnerText;
39 break;
40
41 case "link":
42 if (Uri.TryCreate(BaseUri, E.InnerText, out Uri Link))
43 this.Link = Link;
44 else
45 Warnings.Add(new RssWarning(E, "Invalid URI: " + E.InnerText));
46 break;
47
48 case "description":
49 this.Description = E.InnerText;
50 break;
51
52 case "language":
53 this.Language = E.InnerText;
54 break;
55
56 case "copyright":
57 this.Copyright = E.InnerText;
58 break;
59
60 case "managingEditor":
61 this.ManagingEditor = E.InnerText;
62 break;
63
64 case "webMaster":
65 this.WebMaster = E.InnerText;
66 break;
67
68 case "pubDate":
69 if (CommonTypes.TryParseRfc822(E.InnerText, out DateTimeOffset PubDate))
70 this.PublicationDate = PubDate;
71 else
72 Warnings.Add(new RssWarning(E, "Unable to parse publication date: " + E.InnerText));
73 break;
74
75 case "lastBuildDate":
76 if (CommonTypes.TryParseRfc822(E.InnerText, out DateTimeOffset LastBuildDate))
77 this.LastBuildDate = LastBuildDate;
78 else
79 Warnings.Add(new RssWarning(E, "Unable to parse last build date: " + E.InnerText));
80 break;
81
82 case "category":
83 RssCategory Category = new RssCategory(E, BaseUri);
84 Warnings.AddRange(Category.Warnings);
85 Categories.Add(Category);
86 break;
87
88 case "generator":
89 this.Generator = E.InnerText;
90 break;
91
92 case "docs":
93 if (Uri.TryCreate(BaseUri, E.InnerText, out Uri Documentation))
94 this.Documentation = Documentation;
95 else
96 Warnings.Add(new RssWarning(E, "Invalid URI: " + E.InnerText));
97 break;
98
99 case "ttl":
100 if (int.TryParse(E.InnerText, out int Ttl))
101 this.TimeToLive = TimeSpan.FromMinutes(Ttl);
102 else
103 Warnings.Add(new RssWarning(E, "Unable to parse TTL: " + E.InnerText));
104 break;
105
106 case "image":
107 if (!(this.Image is null))
108 Warnings.Add(new RssWarning(E, "Image already defined for channel."));
109 else
110 {
111 this.Image = new RssImage(E, BaseUri);
112 Warnings.AddRange(this.Image.Warnings);
113 }
114 break;
115
116 case "item":
117 RssItem Item = new RssItem(E, BaseUri);
118 Warnings.AddRange(Item.Warnings);
119 Items.Add(Item);
120 break;
121
122 case "cloud":
123 if (!(this.Cloud is null))
124 Warnings.Add(new RssWarning(E, "Cloud service already defined for channel."));
125 else
126 this.Cloud = new RssCloud(E, BaseUri);
127 break;
128
129 case "rating": // TODO
130 case "textInput": // TODO
131 case "skipHours": // TODO
132 case "skipDays": // TODO
133 default:
134 Warnings.Add(new RssWarning(E));
135 break;
136 }
137 }
138 else
139 {
140 IRssExtension Extension = Types.FindBest<IRssExtension, XmlElement>(E);
141 if (Extension is null)
142 Warnings.Add(new RssWarning(E));
143 else
144 Extensions.Add(Extension.Create(E, BaseUri));
145 }
146 }
147
148 this.Categories = Categories.ToArray();
149 this.Items = Items.ToArray();
150 this.Warnings = Warnings.ToArray();
151 this.Extensions = Extensions.ToArray();
152 }
153
157 public RssWarning[] Warnings { get; } = null;
158
164 public string Title { get; } = null;
165
169 public Uri Link { get; } = null;
170
174 public string Description { get; } = null;
175
182 public string Language { get; } = null;
183
187 public string Copyright { get; } = null;
188
192 public string ManagingEditor { get; } = null;
193
197 public string WebMaster { get; } = null;
198
206 public DateTimeOffset? PublicationDate { get; } = null;
207
211 public DateTimeOffset? LastBuildDate { get; } = null;
212
217 public RssCategory[] Categories { get; } = null;
218
222 public string Generator { get; } = null;
223
229 public Uri Documentation { get; } = null;
230
234 public RssCloud Cloud { get; } = null;
235
240 public TimeSpan TimeToLive { get; } = TimeSpan.FromMinutes(60);
241
245 public RssImage Image { get; } = null;
246
250 public RssItem[] Items { get; } = null;
251
255 public IRssExtension[] Extensions { get; } = null;
256 }
257}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static bool TryParseRfc822(string s, out DateTimeOffset Value)
Parses a date and time value encoded according to RFC 822, §5.
Definition: CommonTypes.cs:170
Identifies a categorization taxonomy.
Definition: RssCategory.cs:12
RssWarning[] Warnings
Any warning messages created during parsing.
Definition: RssCategory.cs:44
Contains information about an RSS Channel.
Definition: RssChannel.cs:12
RssImage Image
Channel image.
Definition: RssChannel.cs:245
string Copyright
Copyright notice for content in the channel.
Definition: RssChannel.cs:187
string Title
The name of the channel. It's how people refer to your service. If you have an HTML website that cont...
Definition: RssChannel.cs:164
RssCloud Cloud
Reference to web service that supports the rssCloud interface.
Definition: RssChannel.cs:234
RssChannel(XmlElement Xml, Uri BaseUri)
Contains information about an RSS Channel.
Definition: RssChannel.cs:18
TimeSpan TimeToLive
ttl stands for time to live. It's a number of minutes that indicates how long a channel can be cached...
Definition: RssChannel.cs:240
DateTimeOffset? LastBuildDate
The last time the content of the channel changed.
Definition: RssChannel.cs:211
Uri Link
The URL to the HTML website corresponding to the channel.
Definition: RssChannel.cs:169
string ManagingEditor
Email address for person responsible for editorial content.
Definition: RssChannel.cs:192
string Language
The language the channel is written in. This allows aggregators to group all Italian language sites,...
Definition: RssChannel.cs:182
Uri Documentation
A URL that points to the documentation for the format used in the RSS file. It's probably a pointer t...
Definition: RssChannel.cs:229
string WebMaster
Email address for person responsible for technical issues relating to channel.
Definition: RssChannel.cs:197
IRssExtension[] Extensions
Extensions
Definition: RssChannel.cs:255
RssWarning[] Warnings
Any warning messages created during parsing.
Definition: RssChannel.cs:157
RssCategory[] Categories
Specify one or more categories that the channel belongs to. Follows the same rules as the <item>-leve...
Definition: RssChannel.cs:217
string Description
Phrase or sentence describing the channel.
Definition: RssChannel.cs:174
string Generator
A string indicating the program used to generate the channel.
Definition: RssChannel.cs:222
DateTimeOffset? PublicationDate
The publication date for the content in the channel. For example, the New York Times publishes on a d...
Definition: RssChannel.cs:206
Web service that supports the rssCloud interface.
Definition: RssCloud.cs:14
Specifies a GIF, JPEG or PNG image that can be displayed with the channel.
Definition: RssImage.cs:11
RssWarning[] Warnings
Any warning messages created during parsing.
Definition: RssImage.cs:84
A channel may contain any number of <item>s. An item may represent a "story" – much like a story in a...
Definition: RssItem.cs:17
RssWarning[] Warnings
Any warning messages created during parsing.
Definition: RssItem.cs:132
Contains a warning message.
Definition: RssWarning.cs:9
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
Interface for RSS extensions
IRssExtension Create(XmlElement Xml, Uri BaseUri)
Creates a new instance of the RSS extension.