/
SyndicationItem.cs
265 lines (215 loc) · 8.77 KB
/
SyndicationItem.cs
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Xml;
using System.Diagnostics.CodeAnalysis;
namespace System.ServiceModel.Syndication
{
// NOTE: This class implements Clone so if you add any members, please update the copy ctor
public class SyndicationItem : IExtensibleSyndicationObject
{
private Collection<SyndicationPerson> _authors;
private Collection<SyndicationCategory> _categories;
private Collection<SyndicationPerson> _contributors;
private ExtensibleSyndicationObject _extensions;
private DateTimeOffset _lastUpdatedTime;
private Collection<SyndicationLink> _links;
private DateTimeOffset _publishDate;
public SyndicationItem() : this(null, null, null)
{
}
public SyndicationItem(string title, string content, Uri itemAlternateLink)
: this(title, content, itemAlternateLink, null, DateTimeOffset.MinValue)
{
}
public SyndicationItem(string title, string content, Uri itemAlternateLink, string id, DateTimeOffset lastUpdatedTime)
: this(title, (content != null) ? new TextSyndicationContent(content) : null, itemAlternateLink, id, lastUpdatedTime)
{
}
public SyndicationItem(string title, SyndicationContent content, Uri itemAlternateLink, string id, DateTimeOffset lastUpdatedTime)
{
if (title != null)
{
Title = new TextSyndicationContent(title);
}
Content = content;
if (itemAlternateLink != null)
{
Links.Add(SyndicationLink.CreateAlternateLink(itemAlternateLink));
}
Id = id;
_lastUpdatedTime = lastUpdatedTime;
}
protected SyndicationItem(SyndicationItem source)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
_extensions = source._extensions.Clone();
_authors = FeedUtils.ClonePersons(source._authors);
_categories = FeedUtils.CloneCategories(source._categories);
Content = source.Content?.Clone();
_contributors = FeedUtils.ClonePersons(source._contributors);
Copyright = FeedUtils.CloneTextContent(source.Copyright);
Id = source.Id;
_lastUpdatedTime = source._lastUpdatedTime;
_links = FeedUtils.CloneLinks(source._links);
_publishDate = source._publishDate;
if (source.SourceFeed != null)
{
SourceFeed = source.SourceFeed.Clone(false);
SourceFeed.Items = new Collection<SyndicationItem>();
}
Summary = FeedUtils.CloneTextContent(source.Summary);
BaseUri = source.BaseUri;
Title = FeedUtils.CloneTextContent(source.Title);
}
public Dictionary<XmlQualifiedName, string> AttributeExtensions => _extensions.AttributeExtensions;
public Collection<SyndicationPerson> Authors
{
get => _authors ??= new NullNotAllowedCollection<SyndicationPerson>();
}
public Uri BaseUri { get; set; }
public Collection<SyndicationCategory> Categories
{
get => _categories ??= new NullNotAllowedCollection<SyndicationCategory>();
}
public SyndicationContent Content { get; set; }
public Collection<SyndicationPerson> Contributors
{
get => _contributors ??= new NullNotAllowedCollection<SyndicationPerson>();
}
public TextSyndicationContent Copyright { get; set; }
public SyndicationElementExtensionCollection ElementExtensions => _extensions.ElementExtensions;
public string Id { get; set; }
internal Exception LastUpdatedTimeException { get; set; }
public DateTimeOffset LastUpdatedTime
{
get
{
if (LastUpdatedTimeException != null)
{
throw LastUpdatedTimeException;
}
return _lastUpdatedTime;
}
set
{
LastUpdatedTimeException = null;
_lastUpdatedTime = value;
}
}
public Collection<SyndicationLink> Links
{
get => _links ??= new NullNotAllowedCollection<SyndicationLink>();
}
internal Exception PublishDateException { get; set; }
public DateTimeOffset PublishDate
{
get
{
if (PublishDateException != null)
{
throw PublishDateException;
}
return _publishDate;
}
set
{
PublishDateException = null;
_publishDate = value;
}
}
public SyndicationFeed SourceFeed { get; set; }
public TextSyndicationContent Summary { get; set; }
public TextSyndicationContent Title { get; set; }
public static SyndicationItem Load(XmlReader reader) => Load<SyndicationItem>(reader);
public static TSyndicationItem Load<TSyndicationItem>(XmlReader reader) where TSyndicationItem : SyndicationItem, new()
{
if (reader is null)
{
throw new ArgumentNullException(nameof(reader));
}
Atom10ItemFormatter<TSyndicationItem> atomSerializer = new Atom10ItemFormatter<TSyndicationItem>();
if (atomSerializer.CanRead(reader))
{
atomSerializer.ReadFrom(reader);
return atomSerializer.Item as TSyndicationItem;
}
Rss20ItemFormatter<TSyndicationItem> rssSerializer = new Rss20ItemFormatter<TSyndicationItem>();
if (rssSerializer.CanRead(reader))
{
rssSerializer.ReadFrom(reader);
return rssSerializer.Item as TSyndicationItem;
}
throw new XmlException(SR.Format(SR.UnknownItemXml, reader.LocalName, reader.NamespaceURI));
}
public void AddPermalink(Uri permalink)
{
if (permalink is null)
{
throw new ArgumentNullException(nameof(permalink));
}
Id = permalink.AbsoluteUri;
Links.Add(SyndicationLink.CreateAlternateLink(permalink));
}
public virtual SyndicationItem Clone() => new SyndicationItem(this);
public Atom10ItemFormatter GetAtom10Formatter() => new Atom10ItemFormatter(this);
public Rss20ItemFormatter GetRss20Formatter() => GetRss20Formatter(true);
public Rss20ItemFormatter GetRss20Formatter(bool serializeExtensionsAsAtom)
{
return new Rss20ItemFormatter(this, serializeExtensionsAsAtom);
}
public void SaveAsAtom10(XmlWriter writer)
{
GetAtom10Formatter().WriteTo(writer);
}
public void SaveAsRss20(XmlWriter writer)
{
GetRss20Formatter().WriteTo(writer);
}
protected internal virtual SyndicationCategory CreateCategory()
{
return new SyndicationCategory();
}
protected internal virtual SyndicationLink CreateLink()
{
return new SyndicationLink();
}
protected internal virtual SyndicationPerson CreatePerson()
{
return new SyndicationPerson();
}
protected internal virtual bool TryParseAttribute(string name, string ns, string value, string version)
{
return false;
}
protected internal virtual bool TryParseContent(XmlReader reader, string contentType, string version, out SyndicationContent content)
{
content = null;
return false;
}
protected internal virtual bool TryParseElement(XmlReader reader, string version)
{
return false;
}
protected internal virtual void WriteAttributeExtensions(XmlWriter writer, string version)
{
_extensions.WriteAttributeExtensions(writer);
}
protected internal virtual void WriteElementExtensions(XmlWriter writer, string version)
{
_extensions.WriteElementExtensions(writer);
}
internal void LoadElementExtensions(XmlReader readerOverUnparsedExtensions, int maxExtensionSize)
{
_extensions.LoadElementExtensions(readerOverUnparsedExtensions, maxExtensionSize);
}
internal void LoadElementExtensions(XmlBuffer buffer)
{
_extensions.LoadElementExtensions(buffer);
}
}
}