/
ConfigurationElementCollection.cs
1302 lines (1115 loc) · 53.2 KB
/
ConfigurationElementCollection.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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;
using System.Diagnostics;
using System.Xml;
namespace System.Configuration
{
[DebuggerDisplay("Count = {Count}")]
public abstract class ConfigurationElementCollection : ConfigurationElement, ICollection
{
internal const string DefaultAddItemName = "add";
internal const string DefaultRemoveItemName = "remove";
internal const string DefaultClearItemsName = "clear";
private readonly IComparer _comparer;
private string _addElement = DefaultAddItemName;
private string _clearElement = DefaultClearItemsName;
private bool _collectionCleared;
private bool _emitClearTag;
private int _inheritedCount; // Total number of inherited items
private bool _modified;
private bool _readOnly;
private int _removedItemCount; // Number of items removed for this collection (not including parent)
private string _removeElement = DefaultRemoveItemName;
internal bool InternalAddToEnd;
internal string InternalElementTagName = string.Empty;
protected ConfigurationElementCollection() { }
protected ConfigurationElementCollection(IComparer comparer)
{
if (comparer is null)
{
throw new ArgumentNullException(nameof(comparer));
}
_comparer = comparer;
}
private ArrayList Items { get; } = new ArrayList();
protected internal string AddElementName
{
get { return _addElement; }
set
{
_addElement = value;
if (BaseConfigurationRecord.IsReservedAttributeName(value))
throw new ArgumentException(SR.Format(SR.Item_name_reserved, DefaultAddItemName, value));
}
}
protected internal string RemoveElementName
{
get { return _removeElement; }
set
{
if (BaseConfigurationRecord.IsReservedAttributeName(value))
throw new ArgumentException(SR.Format(SR.Item_name_reserved, DefaultRemoveItemName, value));
_removeElement = value;
}
}
protected internal string ClearElementName
{
get { return _clearElement; }
set
{
if (BaseConfigurationRecord.IsReservedAttributeName(value))
throw new ArgumentException(SR.Format(SR.Item_name_reserved, DefaultClearItemsName, value));
_clearElement = value;
}
}
public bool EmitClear
{
get { return _emitClearTag; }
set
{
if (IsReadOnly()) throw new ConfigurationErrorsException(SR.Config_base_read_only);
if (value)
{
CheckLockedElement(_clearElement, null); // has clear been locked?
CheckLockedElement(_removeElement, null); // has remove been locked? Clear implies remove
}
_modified = true;
_emitClearTag = value;
}
}
protected virtual string ElementName => "";
internal string LockableElements
{
get
{
if ((CollectionType == ConfigurationElementCollectionType.AddRemoveClearMap) ||
(CollectionType == ConfigurationElementCollectionType.AddRemoveClearMapAlternate))
{
string elementNames = "'" + AddElementName + "'"; // Must have an add
if (RemoveElementName.Length != 0)
elementNames += ", '" + RemoveElementName + "'";
if (ClearElementName.Length != 0)
elementNames += ", '" + ClearElementName + "'";
return elementNames;
}
if (!string.IsNullOrEmpty(ElementName)) return "'" + ElementName + "'";
return string.Empty;
}
}
protected virtual bool ThrowOnDuplicate
=> (CollectionType == ConfigurationElementCollectionType.AddRemoveClearMap) ||
(CollectionType == ConfigurationElementCollectionType.AddRemoveClearMapAlternate);
public virtual ConfigurationElementCollectionType CollectionType
=> ConfigurationElementCollectionType.AddRemoveClearMap;
public int Count => Items.Count - _removedItemCount;
public bool IsSynchronized => false;
public object SyncRoot => null;
void ICollection.CopyTo(Array arr, int index)
{
foreach (Entry entry in Items)
if (entry.EntryType != EntryType.Removed) arr.SetValue(entry.Value, index++);
}
public IEnumerator GetEnumerator()
{
return GetEnumeratorImpl();
}
internal override void AssociateContext(BaseConfigurationRecord configRecord)
{
base.AssociateContext(configRecord);
foreach (Entry entry in Items)
entry.Value?.AssociateContext(configRecord);
}
protected internal override bool IsModified()
{
if (_modified) return true;
if (base.IsModified()) return true;
foreach (Entry entry in Items)
{
if (entry.EntryType == EntryType.Removed) continue;
ConfigurationElement elem = entry.Value;
if (elem.IsModified()) return true;
}
return false;
}
protected internal override void ResetModified()
{
_modified = false;
base.ResetModified();
foreach (Entry entry in Items)
{
if (entry.EntryType == EntryType.Removed) continue;
ConfigurationElement elem = entry.Value;
elem.ResetModified();
}
}
public override bool IsReadOnly()
{
return _readOnly;
}
protected internal override void SetReadOnly()
{
_readOnly = true;
foreach (Entry entry in Items)
{
if (entry.EntryType == EntryType.Removed) continue;
ConfigurationElement elem = entry.Value;
elem.SetReadOnly();
}
}
internal virtual IEnumerator GetEnumeratorImpl()
{
return new Enumerator(Items, this);
}
internal IEnumerator GetElementsEnumerator()
{
// Return an enumerator over the collection's config elements.
// This is different then the std GetEnumerator because the second one
// can return different set of items if overridden in a derived class
return new Enumerator(Items, this);
}
public override bool Equals(object compareTo)
{
if (compareTo == null || compareTo.GetType() != GetType())
return false;
ConfigurationElementCollection compareToElem = (ConfigurationElementCollection)compareTo;
if (Count != compareToElem.Count)
return false;
foreach (Entry thisEntry in Items)
{
bool found = false;
foreach (Entry compareEntry in compareToElem.Items)
{
if (!Equals(thisEntry.Value, compareEntry.Value)) continue;
found = true;
break;
}
if (found == false)
{
// not in the collection must be different
return false;
}
}
return true;
}
public override int GetHashCode()
{
int hHashCode = 0;
foreach (Entry thisEntry in Items)
{
ConfigurationElement elem = thisEntry.Value;
hHashCode ^= elem.GetHashCode();
}
return hHashCode;
}
protected internal override void Unmerge(ConfigurationElement sourceElement,
ConfigurationElement parentElement,
ConfigurationSaveMode saveMode)
{
base.Unmerge(sourceElement, parentElement, saveMode);
if (sourceElement == null) return;
ConfigurationElementCollection parentCollection = parentElement as ConfigurationElementCollection;
ConfigurationElementCollection sourceCollection = sourceElement as ConfigurationElementCollection;
Hashtable inheritance = new Hashtable();
_lockedAllExceptAttributesList = sourceElement._lockedAllExceptAttributesList;
_lockedAllExceptElementsList = sourceElement._lockedAllExceptElementsList;
_itemLockedFlag = sourceElement._itemLockedFlag;
_lockedAttributesList = sourceElement._lockedAttributesList;
_lockedElementsList = sourceElement._lockedElementsList;
AssociateContext(sourceElement._configRecord);
if (parentElement != null)
{
if (parentElement._lockedAttributesList != null)
{
_lockedAttributesList = UnMergeLockList(sourceElement._lockedAttributesList,
parentElement._lockedAttributesList, saveMode);
}
if (parentElement._lockedElementsList != null)
{
_lockedElementsList = UnMergeLockList(sourceElement._lockedElementsList,
parentElement._lockedElementsList, saveMode);
}
if (parentElement._lockedAllExceptAttributesList != null)
{
_lockedAllExceptAttributesList = UnMergeLockList(sourceElement._lockedAllExceptAttributesList,
parentElement._lockedAllExceptAttributesList, saveMode);
}
if (parentElement._lockedAllExceptElementsList != null)
{
_lockedAllExceptElementsList = UnMergeLockList(sourceElement._lockedAllExceptElementsList,
parentElement._lockedAllExceptElementsList, saveMode);
}
}
if ((CollectionType == ConfigurationElementCollectionType.AddRemoveClearMap) ||
(CollectionType == ConfigurationElementCollectionType.AddRemoveClearMapAlternate))
{
// When writing out portable configurations the <clear/> tag should be written
_collectionCleared = sourceCollection._collectionCleared;
EmitClear = ((saveMode == ConfigurationSaveMode.Full) && (_clearElement.Length != 0)) ||
((saveMode == ConfigurationSaveMode.Modified) && _collectionCleared) || sourceCollection.EmitClear;
if ((parentCollection != null) && (EmitClear != true))
{
foreach (Entry entry in parentCollection.Items)
if (entry.EntryType != EntryType.Removed)
inheritance[entry.GetKey(this)] = InheritedType.InParent;
}
foreach (Entry entry in sourceCollection.Items)
{
if (entry.EntryType == EntryType.Removed) continue;
if (inheritance.Contains(entry.GetKey(this)))
{
Entry parentEntry =
(Entry)parentCollection.Items[parentCollection.RealIndexOf(entry.Value)];
ConfigurationElement elem = entry.Value;
if (elem.Equals(parentEntry.Value))
{
// in modified mode we consider any change to be different than the parent
inheritance[entry.GetKey(this)] = InheritedType.InBothSame;
if (saveMode != ConfigurationSaveMode.Modified) continue;
if (elem.IsModified())
inheritance[entry.GetKey(this)] = InheritedType.InBothDiff;
else
{
if (elem.ElementPresent)
{
// This is when the source file contained the entry but it was an
// exact copy. We don't want to emit a remove so we treat it as
// a special case.
inheritance[entry.GetKey(this)] = InheritedType.InBothCopyNoRemove;
}
}
}
else
{
inheritance[entry.GetKey(this)] = InheritedType.InBothDiff;
if ((CollectionType == ConfigurationElementCollectionType.AddRemoveClearMapAlternate)
&& (entry.EntryType == EntryType.Added))
{
// this is a special case for deailing with defect number 529517
// this code allow the config to write out the same xml when no remove was
// present during deserialization.
inheritance[entry.GetKey(this)] = InheritedType.InBothCopyNoRemove;
}
}
}
else
{
// not in parent
inheritance[entry.GetKey(this)] = InheritedType.InSelf;
}
}
if ((parentCollection != null) && (EmitClear != true))
{
foreach (Entry entry in parentCollection.Items)
{
if (entry.EntryType == EntryType.Removed) continue;
InheritedType tp = (InheritedType)inheritance[entry.GetKey(this)];
if ((tp != InheritedType.InParent) && (tp != InheritedType.InBothDiff)) continue;
ConfigurationElement elem = CallCreateNewElement(entry.GetKey(this).ToString());
elem.Reset(entry.Value); // copy this entry
BaseAdd(elem, ThrowOnDuplicate, true);
// Add it (so that is once existed in the temp
BaseRemove(entry.GetKey(this), false); // now remove it to for a remove instruction
}
}
foreach (Entry entry in sourceCollection.Items)
{
if (entry.EntryType == EntryType.Removed) continue;
InheritedType tp = (InheritedType)inheritance[entry.GetKey(this)];
if ((tp != InheritedType.InSelf) && (tp != InheritedType.InBothDiff) &&
(tp != InheritedType.InBothCopyNoRemove))
continue;
ConfigurationElement elem = CallCreateNewElement(entry.GetKey(this).ToString());
elem.Unmerge(entry.Value, null, saveMode);
if (tp == InheritedType.InSelf)
elem.RemoveAllInheritedLocks(); // If the key changed only local locks are kept
BaseAdd(elem, ThrowOnDuplicate, true); // Add it
}
}
else
{
if ((CollectionType != ConfigurationElementCollectionType.BasicMap) &&
(CollectionType != ConfigurationElementCollectionType.BasicMapAlternate))
return;
foreach (Entry entry in sourceCollection.Items)
{
bool foundKeyInParent = false;
Entry parentEntrySaved = null;
if ((entry.EntryType != EntryType.Added) && (entry.EntryType != EntryType.Replaced)) continue;
bool inParent = false;
if (parentCollection != null)
{
foreach (Entry parentEntry in parentCollection.Items)
{
if (Equals(entry.GetKey(this), parentEntry.GetKey(this)))
{
// for basic map collection where the key is actually an element
// we do not want the merging behavior or data will not get written
// out for the properties if they match the first element deamed to be a parent
// For example <allow verbs="NewVerb" users="*"/> will loose the users because
// an entry exists in the root element.
if (!IsElementName(entry.GetKey(this).ToString()))
{
// For elements which are not keyed by the element name
// need to be unmerged
foundKeyInParent = true;
parentEntrySaved = parentEntry;
}
}
if (!Equals(entry.Value, parentEntry.Value)) continue;
foundKeyInParent = true;
inParent = true; // in parent and the same exact values
parentEntrySaved = parentEntry;
break;
}
}
ConfigurationElement elem = CallCreateNewElement(entry.GetKey(this).ToString());
if (!foundKeyInParent)
{
// Unmerge is similar to a reset when used like this
// except that it handles the different update modes
// which Reset does not understand
elem.Unmerge(entry.Value, null, saveMode); // copy this entry
BaseAdd(-1, elem, true); // Add it
}
else
{
ConfigurationElement sourceItem = entry.Value;
if (inParent && ((saveMode != ConfigurationSaveMode.Modified) || !sourceItem.IsModified()) &&
(saveMode != ConfigurationSaveMode.Full))
continue;
elem.Unmerge(entry.Value, parentEntrySaved.Value, saveMode);
BaseAdd(-1, elem, true); // Add it
}
}
}
}
protected internal override void Reset(ConfigurationElement parentElement)
{
ConfigurationElementCollection parentCollection = parentElement as ConfigurationElementCollection;
ResetLockLists(parentElement);
if (parentCollection != null)
{
foreach (Entry entry in parentCollection.Items)
{
ConfigurationElement elem = CallCreateNewElement(entry.GetKey(this).ToString());
elem.Reset(entry.Value);
if (((CollectionType == ConfigurationElementCollectionType.AddRemoveClearMap) ||
(CollectionType == ConfigurationElementCollectionType.AddRemoveClearMapAlternate)) &&
((entry.EntryType == EntryType.Added) ||
(entry.EntryType == EntryType.Replaced)))
{
// do not add removed items from the parent
BaseAdd(elem, true, true); // This version combines dups and throws (unless overridden)
}
else
{
if ((CollectionType == ConfigurationElementCollectionType.BasicMap) ||
(CollectionType == ConfigurationElementCollectionType.BasicMapAlternate))
BaseAdd(-1, elem, true); // this version appends regardless of if it is a dup.
}
}
_inheritedCount = Count; // After reset the count is the number of items actually inherited.
}
}
public void CopyTo(ConfigurationElement[] array, int index)
{
((ICollection)this).CopyTo(array, index);
}
protected virtual void BaseAdd(ConfigurationElement element)
{
BaseAdd(element, ThrowOnDuplicate);
}
protected internal void BaseAdd(ConfigurationElement element, bool throwIfExists)
{
BaseAdd(element, throwIfExists, false);
}
private void BaseAdd(ConfigurationElement element, bool throwIfExists, bool ignoreLocks)
{
bool flagAsReplaced = false;
bool localAddToEnd = InternalAddToEnd;
if (IsReadOnly()) throw new ConfigurationErrorsException(SR.Config_base_read_only);
if (LockItem && (ignoreLocks == false))
throw new ConfigurationErrorsException(SR.Format(SR.Config_base_element_locked, _addElement));
object key = GetElementKeyInternal(element);
int iFoundItem = -1;
for (int index = 0; index < Items.Count; index++)
{
Entry entry = (Entry)Items[index];
if (!CompareKeys(key, entry.GetKey(this))) continue;
if ((entry.Value != null) && entry.Value.LockItem && (ignoreLocks == false))
throw new ConfigurationErrorsException(SR.Config_base_collection_item_locked);
if ((entry.EntryType != EntryType.Removed) && throwIfExists)
{
if (!element.Equals(entry.Value))
{
throw new ConfigurationErrorsException(
SR.Format(SR.Config_base_collection_entry_already_exists, key),
element.PropertyFileName(""), element.PropertyLineNumber(""));
}
entry.Value = element;
return;
}
if (entry.EntryType != EntryType.Added)
{
if (((CollectionType == ConfigurationElementCollectionType.AddRemoveClearMap) ||
(CollectionType == ConfigurationElementCollectionType.AddRemoveClearMapAlternate)) &&
(entry.EntryType == EntryType.Removed) &&
(_removedItemCount > 0))
_removedItemCount--; // account for the value
entry.EntryType = EntryType.Replaced;
flagAsReplaced = true;
}
if (localAddToEnd ||
(CollectionType == ConfigurationElementCollectionType.AddRemoveClearMapAlternate))
{
iFoundItem = index;
if (entry.EntryType == EntryType.Added)
{
// this is a special case for defect number 529517 to emulate Everett behavior
localAddToEnd = true;
}
break;
}
// check to see if the element is trying to set a locked property.
if (ignoreLocks == false)
{
element.HandleLockedAttributes(entry.Value);
// copy the lock from the removed element before setting the new element
element.MergeLocks(entry.Value);
}
entry.Value = element;
_modified = true;
return;
}
// Brand new item.
if (iFoundItem >= 0)
{
Items.RemoveAt(iFoundItem);
// if the item being removed was inherited adjust the cout
if ((CollectionType == ConfigurationElementCollectionType.AddRemoveClearMapAlternate) &&
(iFoundItem > Count + _removedItemCount - _inheritedCount))
_inheritedCount--;
}
BaseAddInternal(localAddToEnd ? -1 : iFoundItem, element, flagAsReplaced, ignoreLocks);
_modified = true;
}
protected int BaseIndexOf(ConfigurationElement element)
{
int index = 0;
object key = GetElementKeyInternal(element);
foreach (Entry entry in Items)
{
if (entry.EntryType == EntryType.Removed) continue;
if (CompareKeys(key, entry.GetKey(this))) return index;
index++;
}
return -1;
}
internal int RealIndexOf(ConfigurationElement element)
{
int index = 0;
object key = GetElementKeyInternal(element);
foreach (Entry entry in Items)
{
if (CompareKeys(key, entry.GetKey(this))) return index;
index++;
}
return -1;
}
private void BaseAddInternal(int index, ConfigurationElement element, bool flagAsReplaced, bool ignoreLocks)
{
// Allow the element to initialize itself after its
// constructor has been run so that it may access
// virtual methods.
element.AssociateContext(_configRecord);
element.CallInit();
if (IsReadOnly()) throw new ConfigurationErrorsException(SR.Config_base_read_only);
if (!ignoreLocks)
{
// during reset we ignore locks so we can copy the elements
if ((CollectionType == ConfigurationElementCollectionType.BasicMap) ||
(CollectionType == ConfigurationElementCollectionType.BasicMapAlternate))
{
if (BaseConfigurationRecord.IsReservedAttributeName(ElementName))
throw new ArgumentException(SR.Format(SR.Basicmap_item_name_reserved, ElementName));
CheckLockedElement(ElementName, null);
}
if ((CollectionType == ConfigurationElementCollectionType.AddRemoveClearMap) ||
(CollectionType == ConfigurationElementCollectionType.AddRemoveClearMapAlternate))
CheckLockedElement(_addElement, null);
}
if ((CollectionType == ConfigurationElementCollectionType.BasicMapAlternate) ||
(CollectionType == ConfigurationElementCollectionType.AddRemoveClearMapAlternate))
{
if (index == -1)
{
// insert before inherited, but after any removed
index = Count + _removedItemCount - _inheritedCount;
}
else
{
if ((index > Count + _removedItemCount - _inheritedCount) && (flagAsReplaced == false))
throw new ConfigurationErrorsException(SR.Config_base_cannot_add_items_below_inherited_items);
}
}
if ((CollectionType == ConfigurationElementCollectionType.BasicMap) &&
(index >= 0) &&
(index < _inheritedCount))
throw new ConfigurationErrorsException(SR.Config_base_cannot_add_items_above_inherited_items);
EntryType entryType = flagAsReplaced == false ? EntryType.Added : EntryType.Replaced;
object key = GetElementKeyInternal(element);
if (index >= 0)
{
if (index > Items.Count)
throw new ConfigurationErrorsException(SR.Format(SR.IndexOutOfRange, index));
Items.Insert(index, new Entry(entryType, key, element));
}
else
{
Items.Add(new Entry(entryType, key, element));
}
_modified = true;
}
protected virtual void BaseAdd(int index, ConfigurationElement element)
{
BaseAdd(index, element, false);
}
private void BaseAdd(int index, ConfigurationElement element, bool ignoreLocks)
{
if (IsReadOnly()) throw new ConfigurationErrorsException(SR.Config_base_read_only);
if (index < -1) throw new ConfigurationErrorsException(SR.Format(SR.IndexOutOfRange, index));
if ((index != -1) &&
((CollectionType == ConfigurationElementCollectionType.AddRemoveClearMap) ||
(CollectionType == ConfigurationElementCollectionType.AddRemoveClearMapAlternate)))
{
// If it's an AddRemoveClearMap*** collection, turn the index passed into a real internal index
int realIndex = 0;
if (index > 0)
{
foreach (Entry entryfound in Items)
{
if (entryfound.EntryType != EntryType.Removed) index--;
if (index == 0) break;
realIndex++;
}
index = ++realIndex;
}
// check for duplicates
object key = GetElementKeyInternal(element);
foreach (Entry entry in Items)
{
if (!CompareKeys(key, entry.GetKey(this))
|| (entry.EntryType == EntryType.Removed))
continue;
if (!element.Equals(entry.Value))
{
throw new ConfigurationErrorsException(
SR.Format(SR.Config_base_collection_entry_already_exists, key),
element.PropertyFileName(""), element.PropertyLineNumber(""));
}
return;
}
}
BaseAddInternal(index, element, false, ignoreLocks);
}
protected internal void BaseRemove(object key)
{
BaseRemove(key, false);
}
private void BaseRemove(object key, bool throwIfMissing)
{
if (IsReadOnly())
throw new ConfigurationErrorsException(SR.Config_base_read_only);
int index = 0;
foreach (Entry entry in Items)
{
if (CompareKeys(key, entry.GetKey(this)))
{
if (entry.Value == null) // A phoney delete is already present
{
if (throwIfMissing)
{
throw new ConfigurationErrorsException(
SR.Format(SR.Config_base_collection_entry_not_found, key));
}
return;
}
if (entry.Value.LockItem)
throw new ConfigurationErrorsException(SR.Format(SR.Config_base_attribute_locked, key));
if (entry.Value.ElementPresent == false)
CheckLockedElement(_removeElement, null); // has remove been locked?
switch (entry.EntryType)
{
case EntryType.Added:
if ((CollectionType != ConfigurationElementCollectionType.AddRemoveClearMap) &&
(CollectionType != ConfigurationElementCollectionType.AddRemoveClearMapAlternate))
{
if (CollectionType == ConfigurationElementCollectionType.BasicMapAlternate)
{
if (index >= Count - _inheritedCount)
{
throw new ConfigurationErrorsException(
SR.Config_base_cannot_remove_inherited_items);
}
}
if (CollectionType == ConfigurationElementCollectionType.BasicMap)
{
if (index < _inheritedCount)
{
throw new ConfigurationErrorsException(
SR.Config_base_cannot_remove_inherited_items);
}
}
Items.RemoveAt(index);
}
else
{
// don't really remove it from the collection just mark it removed
entry.EntryType = EntryType.Removed;
_removedItemCount++;
}
break;
case EntryType.Removed:
if (throwIfMissing)
throw new ConfigurationErrorsException(SR.Config_base_collection_entry_already_removed);
break;
default:
if ((CollectionType != ConfigurationElementCollectionType.AddRemoveClearMap) &&
(CollectionType != ConfigurationElementCollectionType.AddRemoveClearMapAlternate))
{
throw new ConfigurationErrorsException(
SR.Config_base_collection_elements_may_not_be_removed);
}
entry.EntryType = EntryType.Removed;
_removedItemCount++;
break;
}
_modified = true;
return;
}
index++;
}
// Note because it is possible for removes to get orphaned by the API they will
// not cause a throw from the base classes. The scenerio is:
// Add an item in a parent level
// remove the item in a child level
// remove the item at the parent level.
if (throwIfMissing)
throw new ConfigurationErrorsException(SR.Format(SR.Config_base_collection_entry_not_found, key));
if ((CollectionType != ConfigurationElementCollectionType.AddRemoveClearMap) &&
(CollectionType != ConfigurationElementCollectionType.AddRemoveClearMapAlternate))
return;
if (CollectionType == ConfigurationElementCollectionType.AddRemoveClearMapAlternate)
{
Items.Insert(Count + _removedItemCount - _inheritedCount,
new Entry(EntryType.Removed, key, null));
}
else
Items.Add(new Entry(EntryType.Removed, key, null));
_removedItemCount++;
}
protected internal ConfigurationElement BaseGet(object key)
{
foreach (Entry entry in Items)
if (entry.EntryType != EntryType.Removed)
if (CompareKeys(key, entry.GetKey(this))) return entry.Value;
return null;
}
protected internal bool BaseIsRemoved(object key)
{
foreach (Entry entry in Items)
if (CompareKeys(key, entry.GetKey(this)))
return entry.EntryType == EntryType.Removed;
return false;
}
protected internal ConfigurationElement BaseGet(int index)
{
if (index < 0) throw new ConfigurationErrorsException(SR.Format(SR.IndexOutOfRange, index));
int virtualIndex = 0;
Entry entry = null;
foreach (Entry entryfound in Items)
{
if ((virtualIndex == index) && (entryfound.EntryType != EntryType.Removed))
{
entry = entryfound;
break;
}
if (entryfound.EntryType != EntryType.Removed) virtualIndex++;
}
if (entry != null)
return entry.Value;
throw new ConfigurationErrorsException(SR.Format(SR.IndexOutOfRange, index));
}
protected internal object[] BaseGetAllKeys()
{
object[] keys = new object[Count];
int index = 0;
foreach (Entry entry in Items)
{
if (entry.EntryType == EntryType.Removed) continue;
keys[index] = entry.GetKey(this);
index++;
}
return keys;
}
protected internal object BaseGetKey(int index)
{
int virtualIndex = 0;
Entry entry = null;
if (index < 0) throw new ConfigurationErrorsException(SR.Format(SR.IndexOutOfRange, index));
foreach (Entry entryfound in Items)
{
if ((virtualIndex == index) && (entryfound.EntryType != EntryType.Removed))
{
entry = entryfound;
break;
}
if (entryfound.EntryType != EntryType.Removed) virtualIndex++;
}
// Entry entry = (Entry)_items[index];
if (entry == null) throw new ConfigurationErrorsException(SR.Format(SR.IndexOutOfRange, index));
return entry.GetKey(this);
}
protected internal void BaseClear()
{
if (IsReadOnly()) throw new ConfigurationErrorsException(SR.Config_base_read_only);
CheckLockedElement(_clearElement, null); // has clear been locked?
CheckLockedElement(_removeElement, null); // has remove been locked? Clear implies remove
_modified = true;
_collectionCleared = true;
if (((CollectionType == ConfigurationElementCollectionType.BasicMap) ||
(CollectionType == ConfigurationElementCollectionType.BasicMapAlternate))
&& (_inheritedCount > 0))
{
int removeIndex = 0;
if (CollectionType == ConfigurationElementCollectionType.BasicMapAlternate)
removeIndex = 0; // Inherited items are at the bottom and cannot be removed
if (CollectionType == ConfigurationElementCollectionType.BasicMap)
removeIndex = _inheritedCount; // inherited items are at the top and cannot be removed
while (Count - _inheritedCount > 0) Items.RemoveAt(removeIndex);
}
else
{
// do not clear any locked items
// _items.Clear();
int inheritedRemoved = 0;
int removedRemoved = 0;
int initialCount = Count;
// check for locks before removing any items from the collection
for (int checkIndex = 0; checkIndex < Items.Count; checkIndex++)
{
Entry entry = (Entry)Items[checkIndex];
if ((entry.Value != null) && entry.Value.LockItem)
throw new ConfigurationErrorsException(SR.Config_base_collection_item_locked_cannot_clear);
}
for (int removeIndex = Items.Count - 1; removeIndex >= 0; removeIndex--)
{
Entry entry = (Entry)Items[removeIndex];
if (((CollectionType == ConfigurationElementCollectionType.AddRemoveClearMap) &&
(removeIndex < _inheritedCount)) ||
((CollectionType == ConfigurationElementCollectionType.AddRemoveClearMapAlternate) &&
(removeIndex >= initialCount - _inheritedCount)))
inheritedRemoved++;
if (entry.EntryType == EntryType.Removed) removedRemoved++;
Items.RemoveAt(removeIndex);
}
_inheritedCount -= inheritedRemoved;
_removedItemCount -= removedRemoved;
}
}
protected internal void BaseRemoveAt(int index)
{
if (IsReadOnly()) throw new ConfigurationErrorsException(SR.Config_base_read_only);
int virtualIndex = 0;
Entry entry = null;
foreach (Entry entryfound in Items)
{
if ((virtualIndex == index) && (entryfound.EntryType != EntryType.Removed))
{
entry = entryfound;
break;
}
if (entryfound.EntryType != EntryType.Removed) virtualIndex++;
}
if (entry == null) throw new ConfigurationErrorsException(SR.Format(SR.IndexOutOfRange, index));
if (entry.Value.LockItem)
{
throw new ConfigurationErrorsException(SR.Format(SR.Config_base_attribute_locked,
entry.GetKey(this)));
}
if (entry.Value.ElementPresent == false)
CheckLockedElement(_removeElement, null); // has remove been locked?
switch (entry.EntryType)
{
case EntryType.Added:
if ((CollectionType != ConfigurationElementCollectionType.AddRemoveClearMap) &&
(CollectionType != ConfigurationElementCollectionType.AddRemoveClearMapAlternate))
{
if ((CollectionType == ConfigurationElementCollectionType.BasicMapAlternate) &&
(index >= Count - _inheritedCount))
throw new ConfigurationErrorsException(SR.Config_base_cannot_remove_inherited_items);
if ((CollectionType == ConfigurationElementCollectionType.BasicMap) && (index < _inheritedCount))
throw new ConfigurationErrorsException(SR.Config_base_cannot_remove_inherited_items);