/
SyndicationElementExtensionCollection.cs
267 lines (232 loc) · 9.07 KB
/
SyndicationElementExtensionCollection.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
266
267
// 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.ObjectModel;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Serialization;
using System.Diagnostics;
namespace System.ServiceModel.Syndication
{
// sealed because the ctor results in a call to the virtual InsertItem method
public sealed class SyndicationElementExtensionCollection : Collection<SyndicationElementExtension>
{
private XmlBuffer _buffer;
private readonly bool _initialized;
internal SyndicationElementExtensionCollection() : this((XmlBuffer)null)
{
}
internal SyndicationElementExtensionCollection(XmlBuffer buffer) : base()
{
_buffer = buffer;
if (_buffer != null)
{
PopulateElements();
}
_initialized = true;
}
internal SyndicationElementExtensionCollection(SyndicationElementExtensionCollection source) : base()
{
_buffer = source._buffer;
for (int i = 0; i < source.Items.Count; ++i)
{
base.Add(source.Items[i]);
}
_initialized = true;
}
public void Add(object extension)
{
if (extension is SyndicationElementExtension)
{
base.Add((SyndicationElementExtension)extension);
}
else
{
Add(extension, (DataContractSerializer)null);
}
}
public void Add(string outerName, string outerNamespace, object dataContractExtension)
{
Add(outerName, outerNamespace, dataContractExtension, null);
}
public void Add(object dataContractExtension, DataContractSerializer serializer)
{
Add(null, null, dataContractExtension, serializer);
}
public void Add(string outerName, string outerNamespace, object dataContractExtension, XmlObjectSerializer dataContractSerializer)
{
if (dataContractExtension is null)
{
throw new ArgumentNullException(nameof(dataContractExtension));
}
dataContractSerializer ??= new DataContractSerializer(dataContractExtension.GetType());
base.Add(new SyndicationElementExtension(outerName, outerNamespace, dataContractExtension, dataContractSerializer));
}
public void Add(object xmlSerializerExtension, XmlSerializer serializer)
{
if (xmlSerializerExtension is null)
{
throw new ArgumentNullException(nameof(xmlSerializerExtension));
}
serializer ??= new XmlSerializer(xmlSerializerExtension.GetType());
base.Add(new SyndicationElementExtension(xmlSerializerExtension, serializer));
}
public void Add(XmlReader xmlReader)
{
if (xmlReader is null)
{
throw new ArgumentNullException(nameof(xmlReader));
}
base.Add(new SyndicationElementExtension(xmlReader));
}
public XmlReader GetReaderAtElementExtensions()
{
XmlBuffer extensionsBuffer = GetOrCreateBufferOverExtensions();
XmlReader reader = extensionsBuffer.GetReader(0);
reader.ReadStartElement();
return reader;
}
public Collection<TExtension> ReadElementExtensions<TExtension>(string extensionName, string extensionNamespace)
{
return ReadElementExtensions<TExtension>(extensionName, extensionNamespace, new DataContractSerializer(typeof(TExtension)));
}
public Collection<TExtension> ReadElementExtensions<TExtension>(string extensionName, string extensionNamespace, XmlObjectSerializer serializer)
{
if (serializer is null)
{
throw new ArgumentNullException(nameof(serializer));
}
return ReadExtensions<TExtension>(extensionName, extensionNamespace, serializer, null);
}
public Collection<TExtension> ReadElementExtensions<TExtension>(string extensionName, string extensionNamespace, XmlSerializer serializer)
{
if (serializer is null)
{
throw new ArgumentNullException(nameof(serializer));
}
return ReadExtensions<TExtension>(extensionName, extensionNamespace, null, serializer);
}
internal void WriteTo(XmlWriter writer, Func<string, string, bool> shouldSkipElement)
{
if (_buffer != null)
{
using (XmlDictionaryReader reader = _buffer.GetReader(0))
{
reader.ReadStartElement();
while (reader.IsStartElement())
{
if (shouldSkipElement != null && shouldSkipElement(reader.LocalName, reader.NamespaceURI))
{
reader.Skip();
continue;
}
writer.WriteNode(reader, false);
}
}
}
else
{
for (int i = 0; i < Items.Count; ++i)
{
Items[i].WriteTo(writer);
}
}
}
protected override void ClearItems()
{
base.ClearItems();
Debug.Assert(_initialized, "The constructor should never clear the collection.");
_buffer = null;
}
protected override void InsertItem(int index, SyndicationElementExtension item)
{
if (item is null)
{
throw new ArgumentNullException(nameof(item));
}
base.InsertItem(index, item);
// clear the cached buffer if the operation is happening outside the constructor
if (_initialized)
{
_buffer = null;
}
}
protected override void RemoveItem(int index)
{
base.RemoveItem(index);
Debug.Assert(_initialized, "The constructor should never remove items from the collection.");
_buffer = null;
}
protected override void SetItem(int index, SyndicationElementExtension item)
{
if (item is null)
{
throw new ArgumentNullException(nameof(item));
}
base.SetItem(index, item);
Debug.Assert(_initialized, "The constructor should never set items in the collection.");
_buffer = null;
}
private XmlBuffer GetOrCreateBufferOverExtensions()
{
if (_buffer != null)
{
return _buffer;
}
XmlBuffer newBuffer = new XmlBuffer(int.MaxValue);
using (XmlWriter writer = newBuffer.OpenSection(XmlDictionaryReaderQuotas.Max))
{
writer.WriteStartElement(Rss20Constants.ExtensionWrapperTag);
for (int i = 0; i < Count; ++i)
{
this[i].WriteTo(writer);
}
writer.WriteEndElement();
}
newBuffer.CloseSection();
newBuffer.Close();
_buffer = newBuffer;
return newBuffer;
}
private void PopulateElements()
{
using (XmlDictionaryReader reader = _buffer.GetReader(0))
{
reader.ReadStartElement();
int index = 0;
while (reader.IsStartElement())
{
base.Add(new SyndicationElementExtension(_buffer, index, reader.LocalName, reader.NamespaceURI));
reader.Skip();
++index;
}
}
}
private Collection<TExtension> ReadExtensions<TExtension>(string extensionName, string extensionNamespace, XmlObjectSerializer dcSerializer, XmlSerializer xmlSerializer)
{
if (string.IsNullOrEmpty(extensionName))
{
throw new ArgumentException(SR.ExtensionNameNotSpecified);
}
Debug.Assert((dcSerializer == null) != (xmlSerializer == null), "exactly one serializer should be supplied");
// normalize the null and empty namespace
extensionNamespace ??= string.Empty;
Collection<TExtension> results = new Collection<TExtension>();
for (int i = 0; i < Count; ++i)
{
if (extensionName != this[i].OuterName || extensionNamespace != this[i].OuterNamespace)
{
continue;
}
if (dcSerializer != null)
{
results.Add(this[i].GetObject<TExtension>(dcSerializer));
}
else
{
results.Add(this[i].GetObject<TExtension>(xmlSerializer));
}
}
return results;
}
}
}