-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathConfigurationElementCollectionBase.cs
383 lines (324 loc) · 12 KB
/
ConfigurationElementCollectionBase.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Copyright (c) Lex Li. All rights reserved.
//
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Xml.Linq;
using System.IO;
using System.Runtime.InteropServices;
using System.Xml;
namespace Microsoft.Web.Administration
{
public class ConfigurationElementCollectionBase<T> : ConfigurationElement, ICollection, IEnumerable<T>
where T : ConfigurationElement
{
internal readonly Collection<T> Exposed = new Collection<T>();
internal readonly Collection<T> Real = new Collection<T>();
private object _syncRoot;
protected bool HasParent;
protected ConfigurationElementCollectionBase()
: this(null, null, null, null, null, null)
{ }
internal ConfigurationElementCollectionBase(ConfigurationElement element, string name, ConfigurationElementSchema schema, ConfigurationElement parent, XElement entity, FileContext core)
: base(element, name, schema, parent, entity, core)
{
AllowsAdd = Schema.CollectionSchema.AddSchemas.Count > 0;
AllowsClear = Schema.CollectionSchema.ClearSchema != null;
AllowsRemove = Schema.CollectionSchema.RemoveSchema != null;
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return Exposed.GetEnumerator();
}
public IEnumerator GetEnumerator()
{
return Exposed.GetEnumerator();
}
public void CopyTo(Array array, int index)
{
// TODO: how to fix real here.
Exposed.CopyTo(array.Cast<T>().ToArray(), index);
}
public int Count
{
get { return Exposed.Count; }
}
public bool IsSynchronized
{
get { return false; }
}
public object SyncRoot
{
get
{
if (_syncRoot == null)
{
Interlocked.CompareExchange(ref _syncRoot, new object(), null);
}
return _syncRoot;
}
}
protected virtual T CreateNewElement(string elementTagName)
{
var schema = Schema.CollectionSchema.GetElementSchema(elementTagName);
if (schema == null)
{
return null;
}
return (T)Activator.CreateInstance(typeof(T), null, elementTagName, schema, this, null);
}
internal void InternalAdd(T element)
{
SkipCheck = true;
Add(element);
SkipCheck = false;
}
public T Add(T element)
{
return AddAt(Count, element);
}
public T AddAt(int index, T element)
{
if (!SkipCheck)
{
FileContext.SetDirty();
}
element.Validate(false);
element.ForceCreateEntity();
CheckMatched(element);
if (HasParent)
{
if (Count > 0)
{
var left = index == 0 ? Exposed[0].IsLocallyStored : Exposed[index - 1].IsLocallyStored;
var right = index == Count
? Schema.Path != "system.webServer/defaultDocument/files"
: Exposed[index].IsLocallyStored;
if (!left && !right)
{
foreach (var item in Real.ToList())
{
if (Schema.CollectionSchema.RemoveElementName == item.ElementTagName)
{
Real.Remove(item);
item.Entity.Remove();
}
}
var clear1 = CreateElement(Schema.CollectionSchema.ClearElementName);
Real.Insert(0, clear1);
clear1.ForceCreateEntity();
clear1.AppendToParentElement(clear1.Entity, true);
foreach (var item in Exposed)
{
if (item.IsLocallyStored)
{
continue;
}
item.IsLocallyStored = true;
item.AppendToParentElement(item.Entity, false);
Real.Add(item);
}
}
}
element.IsLocallyStored = true;
}
if (Count == index)
{
Real.Add(element);
Exposed.Add(element);
element.AppendToParentElement(element.Entity, false);
return element;
}
Real.Insert(index, element);
var previous = Exposed[index];
Exposed.Insert(index, element);
if (previous.IsLocallyStored)
{
previous.InnerEntity.AddBeforeSelf(element.InnerEntity);
}
element.AppendToParentElement(element.Entity, false);
return element;
}
private void CheckMatched(T element)
{
foreach (var item in Exposed)
{
CheckItem(element, item);
}
}
private void CheckItem(T element, T item)
{
List<Tuple<string, object>> keys = new List<Tuple<string, object>>();
foreach (ConfigurationAttributeSchema attribute in element.Schema.AttributeSchemas)
{
if (!attribute.IsCombinedKey && !attribute.IsUniqueKey)
{
continue;
}
if (!item[attribute.Name].Equals(element[attribute.Name]))
{
return;
}
keys.Add(new Tuple<string, object>(attribute.Name, item[attribute.Name]));
}
if (keys.Count == 0)
{
return;
}
var line = (element.Entity as IXmlLineInfo).LineNumber;
if (keys.Count == 1)
{
throw new COMException(
$"Filename: \\\\?\\{FileContext.FileName}\r\nLine number: {line}\r\nError: Cannot add duplicate collection entry of type '{element.ElementTagName}' with unique key attribute '{keys[0].Item1}' set to '{keys[0].Item2}'\r\n\r\n");
}
var keyNames = new string[keys.Count];
var values = new object[keys.Count];
for (var index = 0; index < keys.Count; index++)
{
var key = keys[index];
keyNames[index] = key.Item1;
values[index] = key.Item2;
}
throw new COMException(
$"Filename: \\\\?\\{FileContext.FileName}\r\nLine number: {line}\r\nError: Cannot add duplicate collection entry of type '{element.ElementTagName}' with combined key attributes '{StringExtensions.Combine(keyNames, ", ")}' respectively set to '{StringExtensions.Combine(values.Select(value => value.ToString()), ", ")}'\r\n\r\n");
}
public void Clear()
{
FileContext.SetDirty();
foreach (T element in Exposed)
{
if (element.IsLocallyStored)
{
element.Delete();
}
}
Exposed.Clear();
Real.Clear();
if (HasParent && AllowsClear)
{
var clear1 = CreateElement(Schema.CollectionSchema.ClearElementName);
Real.Add(clear1);
clear1.ForceCreateEntity();
clear1.AppendToParentElement(clear1.Entity, true);
}
else
{
CleanEntity();
}
}
public T CreateElement()
{
return CreateElement(Schema.CollectionSchema?.AddSchemas[0].Name);
}
public T CreateElement(string elementTagName)
{
return CreateNewElement(elementTagName);
}
public int IndexOf(T element)
{
return Exposed.IndexOf(element);
}
public void Remove(T element)
{
if (FileContext.ReadOnly)
{
throw new FileLoadException(
$"Filename: \\\\?\\{FileContext.FileName}\r\nError: This configuration section cannot be modified because it has been opened for read only access\r\n\r\n");
}
var addSchema =
Schema.CollectionSchema.AddSchemas.FirstOrDefault(item => item.Name == element.ElementTagName);
if (addSchema == null)
{
throw new InvalidOperationException();
}
foreach (var item in Exposed.ToList())
{
var notMatched = false;
foreach (ConfigurationAttributeSchema child in addSchema.AttributeSchemas)
{
if (item[child.Name] != element[child.Name])
{
notMatched = true;
break;
}
}
if (notMatched)
{
continue;
}
if (item.IsLocked == "true")
{
throw new FileLoadException("Filename: \r\nError: Lock violation\r\n\r\n");
}
Exposed.Remove(item);
}
if (element.IsLocallyStored)
{
Real.Remove(element);
element.Delete();
if (Real.Count == 0)
{
CleanEntity();
}
}
else
{
var removal = CreateElement(Schema.CollectionSchema.RemoveElementName);
Real.Insert(0, removal);
removal.ForceCreateEntity();
removal.AppendToParentElement(removal.Entity, true);
foreach (ConfigurationAttributeSchema child in addSchema.AttributeSchemas)
{
if (child.IsUniqueKey || child.IsCombinedKey)
{
removal[child.Name] = element[child.Name];
}
}
}
}
public void RemoveAt(int index)
{
if (index < 0 || index >= Exposed.Count)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
var item = Exposed[index];
Remove(item);
}
public bool AllowsAdd { get; protected set; }
public bool AllowsClear { get; protected set; }
public bool AllowsRemove { get; protected set; }
public T this[int index]
{
get { return Exposed[index]; }
}
internal void Clone(ConfigurationElement item, ConfigurationElement newItem)
{
newItem.SkipCheck = true;
foreach (ConfigurationAttribute attribute in item.Attributes)
{
newItem[attribute.Name] = item[attribute.Name];
}
foreach (ConfigurationElement child in item.ChildElements)
{
var newChild = newItem.ChildElements[child.ElementTagName];
Clone(child, newChild);
}
if (item is ConfigurationElementCollection collection)
{
foreach (ConfigurationElement element in collection)
{
var newElement = ((ConfigurationElementCollection)newItem).CreateNewElement(element.ElementTagName);
Clone(element, newElement);
newItem.AddChild(newElement);
}
}
newItem.IsLocked = item.IsLocked;
newItem.SkipCheck = false;
}
}
}