mono /mcs/class/System.Configuration/System.Configuration/ConfigurationElementCollection.cs

Language C# Lines 599
MD5 Hash 29258b9e0e2bf92970f3d7ae78c6373a Estimated Cost $12,200 (why?)
Repository https://bitbucket.org/danipen/mono.git View Raw File View Project SPDX
  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
//
// System.Configuration.ConfigurationElementCollection.cs
//
// Authors:
//	Tim Coleman (tim@timcoleman.com)
// 	Martin Baulig <martin.baulig@xamarin.com>
//
// Copyright (C) Tim Coleman, 2004
// Copyright (c) 2012 Xamarin Inc. (http://www.xamarin.com)

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//

#if NET_2_0

using System.Collections;
using System.Diagnostics;
using System.Xml;

namespace System.Configuration 
{
	[DebuggerDisplayAttribute ("Count = {Count}")]
	public abstract partial class ConfigurationElementCollection : ConfigurationElement, ICollection, IEnumerable
	{
		ArrayList list = new ArrayList ();
		ArrayList removed;
		ArrayList inherited;
		bool emitClear;
		bool modified;
		IComparer comparer;
		int inheritedLimitIndex;
		
		string addElementName = "add";
		string clearElementName = "clear";
		string removeElementName = "remove";
		
		#region Constructors

		protected ConfigurationElementCollection ()
		{
		}

		protected ConfigurationElementCollection (IComparer comparer)
		{
			this.comparer = comparer;
		}

		internal override void InitFromProperty (PropertyInformation propertyInfo)
		{
			ConfigurationCollectionAttribute colat = propertyInfo.Property.CollectionAttribute;
	
			if (colat == null)
				colat = Attribute.GetCustomAttribute (propertyInfo.Type, typeof (ConfigurationCollectionAttribute)) as ConfigurationCollectionAttribute;

			if (colat != null) {
				addElementName = colat.AddItemName;
				clearElementName = colat.ClearItemsName;
				removeElementName = colat.RemoveItemName;
			}
			base.InitFromProperty (propertyInfo);
		}
		
		#endregion // Constructors

		#region Properties
		
		public virtual ConfigurationElementCollectionType CollectionType {
			get { return ConfigurationElementCollectionType.AddRemoveClearMap; }
		}
		
		bool IsBasic {
			get {
				return CollectionType == ConfigurationElementCollectionType.BasicMap ||
						CollectionType == ConfigurationElementCollectionType.BasicMapAlternate;
			}
		}
		
		bool IsAlternate {
			get {
				return CollectionType == ConfigurationElementCollectionType.AddRemoveClearMapAlternate ||
						CollectionType == ConfigurationElementCollectionType.BasicMapAlternate;
			}
		}

		public int Count {
			get { return list.Count; }
		}

		protected virtual string ElementName {
			get { return string.Empty; }
		}

		public bool EmitClear {
			get { return emitClear; }
			set { emitClear = value; }
		}

		public bool IsSynchronized {
			get { return false; }
		}

		public object SyncRoot {
			get { return this; }
		}

		protected virtual bool ThrowOnDuplicate {
			get {
				if (CollectionType != ConfigurationElementCollectionType.AddRemoveClearMap &&
				    CollectionType != ConfigurationElementCollectionType.AddRemoveClearMapAlternate)
					return false;
				
				return true;
			}
		}
		
		protected internal string AddElementName {
			get { return addElementName; }
			set { addElementName = value; }
		}

		protected internal string ClearElementName {
			get { return clearElementName; }
			set { clearElementName = value; }
		}

		protected internal string RemoveElementName {
			get { return removeElementName; }
			set { removeElementName = value; }
		}

		#endregion // Properties

		#region Methods

		protected virtual void BaseAdd (ConfigurationElement element)
		{
			BaseAdd (element, ThrowOnDuplicate);
		}

		protected void BaseAdd (ConfigurationElement element, bool throwIfExists)
		{
			if (IsReadOnly ())
				throw new ConfigurationErrorsException ("Collection is read only.");
			
			if (IsAlternate) {
				list.Insert (inheritedLimitIndex, element);
				inheritedLimitIndex++;
			}
			else {
				int old_index = IndexOfKey (GetElementKey (element));
				if (old_index >= 0) {
					if (element.Equals (list [old_index]))
						return;
					if (throwIfExists)
						throw new ConfigurationErrorsException ("Duplicate element in collection");
					list.RemoveAt (old_index);
				}
				list.Add (element);
			}

			modified = true;
		}

		protected virtual void BaseAdd (int index, ConfigurationElement element)
		{
			if (ThrowOnDuplicate && BaseIndexOf (element) != -1)
				throw new ConfigurationErrorsException ("Duplicate element in collection");
			if (IsReadOnly ())
				throw new ConfigurationErrorsException ("Collection is read only.");
			
			if (IsAlternate && (index > inheritedLimitIndex))
				throw new ConfigurationErrorsException ("Can't insert new elements below the inherited elements.");
			if (!IsAlternate && (index <= inheritedLimitIndex))
				throw new ConfigurationErrorsException ("Can't insert new elements above the inherited elements.");
			
			list.Insert (index, element);
			modified = true;
		}

		protected internal void BaseClear ()
		{
			if (IsReadOnly ())
				throw new ConfigurationErrorsException ("Collection is read only.");
				
			list.Clear ();
			modified = true;
		}

		protected internal ConfigurationElement BaseGet (int index)
		{
			return (ConfigurationElement) list [index];
		}

		protected internal ConfigurationElement BaseGet (object key)
		{
			int index = IndexOfKey (key);
			if (index != -1) return (ConfigurationElement) list [index];
			else return null;
		}

		protected internal object[] BaseGetAllKeys ()
		{
			object[] keys = new object [list.Count];
			for (int n=0; n<list.Count; n++)
				keys [n] = BaseGetKey (n);
			return keys;
		}

		protected internal object BaseGetKey (int index)
		{
			if (index < 0 || index >= list.Count)
				throw new ConfigurationErrorsException (String.Format ("Index {0} is out of range", index));

			return GetElementKey ((ConfigurationElement) list[index]).ToString ();
		}

		protected int BaseIndexOf (ConfigurationElement element)
		{
			return list.IndexOf (element);
		}
		
		int IndexOfKey (object key)
		{
			for (int n=0; n<list.Count; n++) {
				if (CompareKeys (GetElementKey ((ConfigurationElement) list[n]), key))
					return n;
			}
			return -1;
		}

		protected internal bool BaseIsRemoved (object key)
		{
			if (removed == null)
				return false;
			foreach (ConfigurationElement elem in removed) {
				if (CompareKeys (GetElementKey (elem), key))
					return true;
			}
			return false;
		}

		protected internal void BaseRemove (object key)
		{
			if (IsReadOnly ())
				throw new ConfigurationErrorsException ("Collection is read only.");
				
			int index = IndexOfKey (key);
			if (index != -1) {
				BaseRemoveAt (index);
				modified = true;
			}
		}

		protected internal void BaseRemoveAt (int index)
		{
			if (IsReadOnly ())
				throw new ConfigurationErrorsException ("Collection is read only.");
				
			ConfigurationElement elem = (ConfigurationElement) list [index];
			if (!IsElementRemovable (elem))
				throw new ConfigurationErrorsException ("Element can't be removed from element collection.");
			
			if (inherited != null && inherited.Contains (elem))
				throw new ConfigurationErrorsException ("Inherited items can't be removed.");
			
			list.RemoveAt (index);
			
			if (IsAlternate) {
				if (inheritedLimitIndex > 0)
					inheritedLimitIndex--;
			}

			modified = true;
		}

		bool CompareKeys (object key1, object key2)
		{
			if (comparer != null)
				return comparer.Compare (key1, key2) == 0;
			else
				return object.Equals (key1, key2);
		}

		public void CopyTo (ConfigurationElement[] array, int index)
		{
			list.CopyTo (array, index);
		}
		
		protected abstract ConfigurationElement CreateNewElement ();

		protected virtual ConfigurationElement CreateNewElement (string elementName)
		{
			return CreateNewElement ();
		}
		
		ConfigurationElement CreateNewElementInternal (string elementName)
		{
			ConfigurationElement elem;
			if (elementName == null)
				elem = CreateNewElement ();
			else
				elem = CreateNewElement (elementName);
			elem.Init ();
			return elem;
		}
		
		public override bool Equals (object compareTo)
		{
			ConfigurationElementCollection other = compareTo as ConfigurationElementCollection;
			if (other == null) return false;
			if (GetType() != other.GetType()) return false;
			if (Count != other.Count) return false;
			
			for (int n=0; n<Count; n++) {
				if (!BaseGet (n).Equals (other.BaseGet (n)))
					return false;
			}
			return true;
		}

		protected abstract object GetElementKey (ConfigurationElement element);

		public override int GetHashCode ()
		{
			int code = 0;
			for (int n=0; n<Count; n++)
				code += BaseGet (n).GetHashCode ();
			return code;
		}
		
		void ICollection.CopyTo (Array arr, int index)
		{
			list.CopyTo (arr, index);
		}
		
		public IEnumerator GetEnumerator ()
		{
			return list.GetEnumerator ();
		}

		protected virtual bool IsElementName (string elementName)
		{
			return false;
		}

		protected virtual bool IsElementRemovable (ConfigurationElement element)
		{
			return !IsReadOnly ();
		}

		protected internal override bool IsModified ()
		{
			if (modified)
				return true;

			for (int n=0; n<list.Count; n++) {
				ConfigurationElement elem = (ConfigurationElement) list [n];
				if (!elem.IsModified ())
					continue;
				modified = true;
				break;
			}

			return modified;
		}

		[MonoTODO]
		public override bool IsReadOnly ()
		{
			return base.IsReadOnly ();
		}

		internal override void PrepareSave (ConfigurationElement parentElement, ConfigurationSaveMode mode)
		{
			var parent = (ConfigurationElementCollection)parentElement;
			base.PrepareSave (parentElement, mode);

			for (int n=0; n<list.Count; n++) {
				ConfigurationElement elem = (ConfigurationElement) list [n];
				object key = GetElementKey (elem);
				ConfigurationElement pitem = parent != null ? parent.BaseGet (key) as ConfigurationElement : null;

				elem.PrepareSave (pitem, mode);
			}
		}

		internal override bool HasValues (ConfigurationElement parentElement, ConfigurationSaveMode mode)
		{
			var parent = (ConfigurationElementCollection)parentElement;

			if (mode == ConfigurationSaveMode.Full)
				return list.Count > 0;

			for (int n=0; n<list.Count; n++) {
				ConfigurationElement elem = (ConfigurationElement) list [n];
				object key = GetElementKey (elem);
				ConfigurationElement pitem = parent != null ? parent.BaseGet (key) as ConfigurationElement : null;

				if (elem.HasValues (pitem, mode))
					return true;
			}

			return false;
		}

		protected internal override void Reset (ConfigurationElement parentElement)
		{
			bool basic = IsBasic;
				
			ConfigurationElementCollection parent = (ConfigurationElementCollection) parentElement;
			for (int n=0; n<parent.Count; n++)
			{
				ConfigurationElement parentItem = parent.BaseGet (n);
				ConfigurationElement item = CreateNewElementInternal (null);
				item.Reset (parentItem);
				BaseAdd (item);
				
				if (basic) {
					if (inherited == null)
						inherited = new ArrayList ();
					inherited.Add (item);
				}
			}
			if (IsAlternate)
				inheritedLimitIndex = 0;
			else
				inheritedLimitIndex = Count - 1;
			modified = false;
		}

		protected internal override void ResetModified ()
		{
			modified = false;
			for (int n=0; n<list.Count; n++) {
				ConfigurationElement elem = (ConfigurationElement) list [n];
				elem.ResetModified ();
			}
		}

		[MonoTODO]
		protected internal override void SetReadOnly ()
		{
			base.SetReadOnly ();
		}

		protected internal override bool SerializeElement (XmlWriter writer, bool serializeCollectionKey)
		{
			if (serializeCollectionKey) {
				return base.SerializeElement (writer, serializeCollectionKey);
			}
			
			bool wroteData = false;
			
			if (IsBasic)
			{
				for (int n=0; n<list.Count; n++) {
					ConfigurationElement elem = (ConfigurationElement) list [n];
					if (ElementName != string.Empty)
						wroteData = elem.SerializeToXmlElement (writer, ElementName) || wroteData;
					else
						wroteData = elem.SerializeElement (writer, false) || wroteData;
				}
			}
			else
			{
				if (emitClear) {
					writer.WriteElementString (clearElementName, "");
					wroteData = true;
				}
				
				if (removed != null) {
					for (int n=0; n<removed.Count; n++) {
						writer.WriteStartElement (removeElementName);
						((ConfigurationElement)removed[n]).SerializeElement (writer, true);
						writer.WriteEndElement ();
					}
					wroteData = wroteData || removed.Count > 0;
				}
				
				for (int n=0; n<list.Count; n++) {
					ConfigurationElement elem = (ConfigurationElement) list [n];
					elem.SerializeToXmlElement (writer, addElementName);
				}
				
				wroteData = wroteData || list.Count > 0;
			}
			return wroteData;
		}

		protected override bool OnDeserializeUnrecognizedElement (string elementName, XmlReader reader)
		{
			if (IsBasic)
			{
				ConfigurationElement elem = null;
				
				if (elementName == ElementName)
					elem = CreateNewElementInternal (null);
				if (IsElementName (elementName))
					elem = CreateNewElementInternal (elementName);

				if (elem != null) {
					elem.DeserializeElement (reader, false);
					BaseAdd (elem);
					modified = false;
					return true;
				}
			}
			else {
				if (elementName == clearElementName) {
					reader.MoveToContent ();
					if (reader.MoveToNextAttribute ())
						throw new ConfigurationErrorsException ("Unrecognized attribute '" + reader.LocalName + "'.");
					reader.MoveToElement ();
					reader.Skip ();
					BaseClear ();
					emitClear = true;
					modified = false;
					return true;
				}
				else if (elementName == removeElementName) {
					ConfigurationElement elem = CreateNewElementInternal (null);
					ConfigurationRemoveElement removeElem = new ConfigurationRemoveElement (elem, this);
					removeElem.DeserializeElement (reader, true);
					BaseRemove (removeElem.KeyValue);
					modified = false;
					return true;
				}
				else if (elementName == addElementName) {
					ConfigurationElement elem = CreateNewElementInternal (null);
					elem.DeserializeElement (reader, false);
					BaseAdd (elem);
					modified = false;
					return true;
				}
			}
			
			return false;
		}
		
		protected internal override void Unmerge (ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode updateMode)
		{
			ConfigurationElementCollection source = (ConfigurationElementCollection) sourceElement;
			ConfigurationElementCollection parent = (ConfigurationElementCollection) parentElement;
			
			for (int n=0; n<source.Count; n++) {
				ConfigurationElement sitem = source.BaseGet (n);
				object key = source.GetElementKey (sitem);
				ConfigurationElement pitem = parent != null ? parent.BaseGet (key) as ConfigurationElement : null;
				ConfigurationElement nitem = CreateNewElementInternal (null);
				if (pitem != null && updateMode != ConfigurationSaveMode.Full) {
					nitem.Unmerge (sitem, pitem, updateMode);
					if (nitem.HasValues (pitem, updateMode))
						BaseAdd (nitem);
				} else {
					nitem.Unmerge (sitem, null, ConfigurationSaveMode.Full);
					BaseAdd (nitem);
				}
			}
			
			if (updateMode == ConfigurationSaveMode.Full)
				EmitClear = true;
			else if (parent != null) {
				for (int n=0; n<parent.Count; n++) {
					ConfigurationElement pitem = parent.BaseGet (n);
					object key = parent.GetElementKey (pitem);
					if (source.IndexOfKey (key) == -1) {
						if (removed == null) removed = new ArrayList ();
						removed.Add (pitem);
					}
				}
			}
		}

		#endregion // Methods
        }
}

#endif
Back to Top