-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathRssDocument.cs
More file actions
99 lines (84 loc) · 2.38 KB
/
RssDocument.cs
File metadata and controls
99 lines (84 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Xml;
using Waher.Content.Xml;
using Waher.Runtime.Collections;
using Waher.Runtime.Inventory;
namespace Waher.Content.Rss
{
/// <summary>
/// Contains information from an RSS feed.
/// </summary>
public class RssDocument
{
private readonly XmlDocument xml;
/// <summary>
/// Contains information from an RSS feed.
/// </summary>
/// <param name="Xml">RSS XML document.</param>
/// <param name="BaseUri">Base Uri</param>
public RssDocument(XmlDocument Xml, Uri BaseUri)
{
if (Xml is null)
throw new ArgumentNullException(nameof(Xml));
if (Xml.DocumentElement is null)
throw new ArgumentException("Missing XML.", nameof(Xml));
if (Xml.DocumentElement.LocalName != "rss")
throw new ArgumentException("Not an RSS document.", nameof(Xml));
this.xml = Xml;
this.Version = XML.Attribute(Xml.DocumentElement, "version", 0.0);
ChunkedList<RssChannel> Channels = new ChunkedList<RssChannel>();
ChunkedList<RssWarning> Warnings = new ChunkedList<RssWarning>();
ChunkedList<IRssExtension> Extensions = new ChunkedList<IRssExtension>();
foreach (XmlNode N in Xml.DocumentElement.ChildNodes)
{
if (!(N is XmlElement E))
continue;
if (E.NamespaceURI == Xml.NamespaceURI)
{
switch (E.LocalName)
{
case "channel":
RssChannel Channel = new RssChannel(E, BaseUri);
Channels.Add(Channel);
Warnings.AddRange(Channel.Warnings);
break;
default:
Warnings.Add(new RssWarning(E));
break;
}
}
else
{
IRssExtension Extension = Types.FindBest<IRssExtension, XmlElement>(E);
if (Extension is null)
Warnings.Add(new RssWarning(E));
else
Extensions.Add(Extension.Create(E, BaseUri));
}
}
this.Channels = Channels.ToArray();
this.Warnings = Warnings.ToArray();
this.Extensions = Extensions.ToArray();
}
/// <summary>
/// RSS XML document.
/// </summary>
public XmlDocument Xml => this.xml;
/// <summary>
/// Version of RSS document.
/// </summary>
public double Version { get; }
/// <summary>
/// Channels
/// </summary>
public RssChannel[] Channels { get; }
/// <summary>
/// Any warning messages created during parsing.
/// </summary>
public RssWarning[] Warnings { get; }
/// <summary>
/// Extensions
/// </summary>
public IRssExtension[] Extensions { get; } = null;
}
}