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
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
namespace WixToolset.Harvesters.Serialize
{
using System;
using System.Collections;
using System.Globalization;
using WixToolset.Harvesters.Extensibility.Serialize;
/// <summary>
/// Collection used in the CodeDOM for the children of a given element. Provides type-checking
/// on the allowed children to ensure that only allowed types are added.
/// </summary>
public class ElementCollection : ICollection, IEnumerable
{
private CollectionType collectionType;
private int totalContainedItems;
private int containersUsed;
private ArrayList items;
/// <summary>
/// Creates a new element collection.
/// </summary>
/// <param name="collectionType">Type of the collection to create.</param>
public ElementCollection(CollectionType collectionType)
{
this.collectionType = collectionType;
this.items = new ArrayList();
}
/// <summary>
/// Enum representing types of XML collections.
/// </summary>
public enum CollectionType
{
/// <summary>
/// A choice type, corresponding to the XSD choice element.
/// </summary>
Choice,
/// <summary>
/// A sequence type, corresponding to the XSD sequence element.
/// </summary>
Sequence
}
/// <summary>
/// Gets the type of collection.
/// </summary>
/// <value>The type of collection.</value>
public CollectionType Type
{
get { return this.collectionType; }
}
/// <summary>
/// Gets the count of child elements in this collection (counts ISchemaElements, not nested collections).
/// </summary>
/// <value>The count of child elements in this collection (counts ISchemaElements, not nested collections).</value>
public int Count
{
get { return this.totalContainedItems; }
}
/// <summary>
/// Gets the flag specifying whether this collection is synchronized. Always returns false.
/// </summary>
/// <value>The flag specifying whether this collection is synchronized. Always returns false.</value>
public bool IsSynchronized
{
get { return false; }
}
/// <summary>
/// Gets an object external callers can synchronize on.
/// </summary>
/// <value>An object external callers can synchronize on.</value>
public object SyncRoot
{
get { return this; }
}
/// <summary>
/// Adds a child element to this collection.
/// </summary>
/// <param name="element">The element to add.</param>
/// <exception cref="ArgumentException">Thrown if the child is not of an allowed type.</exception>
public void AddElement(ISchemaElement element)
{
foreach (object obj in this.items)
{
bool containerUsed;
CollectionItem collectionItem = obj as CollectionItem;
if (collectionItem != null)
{
containerUsed = collectionItem.Elements.Count != 0;
if (collectionItem.ElementType.IsAssignableFrom(element.GetType()))
{
collectionItem.AddElement(element);
if (!containerUsed)
{
this.containersUsed++;
}
this.totalContainedItems++;
return;
}
continue;
}
ElementCollection collection = obj as ElementCollection;
if (collection != null)
{
containerUsed = collection.Count != 0;
try
{
collection.AddElement(element);
if (!containerUsed)
{
this.containersUsed++;
}
this.totalContainedItems++;
return;
}
catch (ArgumentException)
{
// Eat the exception and keep looking. We'll throw our own if we can't find its home.
}
continue;
}
}
throw new ArgumentException(String.Format(
CultureInfo.InvariantCulture,
WixHarvesterStrings.EXP_ElementOfTypeIsNotValidForThisCollection,
element.GetType().Name));
}
/// <summary>
/// Removes a child element from this collection.
/// </summary>
/// <param name="element">The element to remove.</param>
/// <exception cref="ArgumentException">Thrown if the element is not of an allowed type.</exception>
public void RemoveElement(ISchemaElement element)
{
foreach (object obj in this.items)
{
CollectionItem collectionItem = obj as CollectionItem;
if (collectionItem != null)
{
if (collectionItem.ElementType.IsAssignableFrom(element.GetType()))
{
if (collectionItem.Elements.Count == 0)
{
return;
}
collectionItem.RemoveElement(element);
if (collectionItem.Elements.Count == 0)
{
this.containersUsed--;
}
this.totalContainedItems--;
return;
}
continue;
}
ElementCollection collection = obj as ElementCollection;
if (collection != null)
{
if (collection.Count == 0)
{
continue;
}
try
{
collection.RemoveElement(element);
if (collection.Count == 0)
{
this.containersUsed--;
}
this.totalContainedItems--;
return;
}
catch (ArgumentException)
{
// Eat the exception and keep looking. We'll throw our own if we can't find its home.
}
continue;
}
}
throw new ArgumentException(String.Format(
CultureInfo.InvariantCulture,
WixHarvesterStrings.EXP_ElementOfTypeIsNotValidForThisCollection,
element.GetType().Name));
}
/// <summary>
/// Copies this collection to an array.
/// </summary>
/// <param name="array">Array to copy to.</param>
/// <param name="index">Offset into the array.</param>
public void CopyTo(Array array, int index)
{
int item = 0;
foreach (ISchemaElement element in this)
{
array.SetValue(element, (long)(item + index));
item++;
}
}
/// <summary>
/// Creates an enumerator for walking the elements in this collection.
/// </summary>
/// <returns>A newly created enumerator.</returns>
public IEnumerator GetEnumerator()
{
return new ElementCollectionEnumerator(this);
}
/// <summary>
/// Gets an enumerable collection of children of a given type.
/// </summary>
/// <param name="childType">Type of children to get.</param>
/// <returns>A collection of children.</returns>
/// <exception cref="ArgumentException">Thrown if the type isn't a valid child type.</exception>
public IEnumerable Filter(Type childType)
{
foreach (object container in this.items)
{
CollectionItem collectionItem = container as CollectionItem;
if (collectionItem != null)
{
if (collectionItem.ElementType.IsAssignableFrom(childType))
{
return collectionItem.Elements;
}
continue;
}
ElementCollection elementCollection = container as ElementCollection;
if (elementCollection != null)
{
IEnumerable nestedFilter = elementCollection.Filter(childType);
if (nestedFilter != null)
{
return nestedFilter;
}
continue;
}
}
throw new ArgumentException(String.Format(
CultureInfo.InvariantCulture,
WixHarvesterStrings.EXP_TypeIsNotValidForThisCollection,
childType.Name));
}
/// <summary>
/// Adds a type to this collection.
/// </summary>
/// <param name="collectionItem">CollectionItem representing the type to add.</param>
public void AddItem(CollectionItem collectionItem)
{
this.items.Add(collectionItem);
}
/// <summary>
/// Adds a nested collection to this collection.
/// </summary>
/// <param name="collection">ElementCollection to add.</param>
public void AddCollection(ElementCollection collection)
{
this.items.Add(collection);
}
/// <summary>
/// Class used to represent a given type in the child collection of an element. Abstract,
/// has subclasses for choice and sequence (which can do cardinality checks).
/// </summary>
public abstract class CollectionItem
{
private Type elementType;
private ArrayList elements;
/// <summary>
/// Creates a new CollectionItem for the given element type.
/// </summary>
/// <param name="elementType">Type of the element for this collection item.</param>
protected CollectionItem(Type elementType)
{
this.elementType = elementType;
this.elements = new ArrayList();
}
/// <summary>
/// Gets the type of this collection's items.
/// </summary>
/// <value>The type of this collection's items.</value>
public Type ElementType
{
get { return this.elementType; }
}
/// <summary>
/// Gets the elements of this collection.
/// </summary>
/// <value>The elements of this collection.</value>
public ArrayList Elements
{
get { return this.elements; }
}
/// <summary>
/// Adds an element to this collection. Must be of an assignable type to the collection's
/// type.
/// </summary>
/// <param name="element">The element to add.</param>
/// <exception cref="ArgumentException">Thrown if the type isn't assignable to the collection's type.</exception>
public void AddElement(ISchemaElement element)
{
if (!this.elementType.IsAssignableFrom(element.GetType()))
{
throw new ArgumentException(
String.Format(
CultureInfo.InvariantCulture,
WixHarvesterStrings.EXP_ElementIsSubclassOfDifferentType,
this.elementType.Name,
element.GetType().Name),
"element");
}
this.elements.Add(element);
}
/// <summary>
/// Removes an element from this collection.
/// </summary>
/// <param name="element">The element to remove.</param>
/// <exception cref="ArgumentException">Thrown if the element's type isn't assignable to the collection's type.</exception>
public void RemoveElement(ISchemaElement element)
{
if (!this.elementType.IsAssignableFrom(element.GetType()))
{
throw new ArgumentException(
String.Format(
CultureInfo.InvariantCulture,
WixHarvesterStrings.EXP_ElementIsSubclassOfDifferentType,
this.elementType.Name,
element.GetType().Name),
"element");
}
this.elements.Remove(element);
}
}
/// <summary>
/// Class representing a choice item. Doesn't do cardinality checks.
/// </summary>
public class ChoiceItem : CollectionItem
{
/// <summary>
/// Creates a new choice item.
/// </summary>
/// <param name="elementType">Type of the created item.</param>
public ChoiceItem(Type elementType)
: base(elementType)
{
}
}
/// <summary>
/// Class representing a sequence item. Can do cardinality checks, if required.
/// </summary>
public class SequenceItem : CollectionItem
{
/// <summary>
/// Creates a new sequence item.
/// </summary>
/// <param name="elementType">Type of the created item.</param>
public SequenceItem(Type elementType)
: base(elementType)
{
}
}
/// <summary>
/// Enumerator for the ElementCollection.
/// </summary>
private class ElementCollectionEnumerator : IEnumerator
{
private ElementCollection collection;
private Stack collectionStack;
/// <summary>
/// Creates a new ElementCollectionEnumerator.
/// </summary>
/// <param name="collection">The collection to create an enumerator for.</param>
public ElementCollectionEnumerator(ElementCollection collection)
{
this.collection = collection;
}
/// <summary>
/// Gets the current object from the enumerator.
/// </summary>
public object Current
{
get
{
if (this.collectionStack != null && this.collectionStack.Count > 0)
{
CollectionSymbol symbol = (CollectionSymbol)this.collectionStack.Peek();
object container = symbol.Collection.items[symbol.ContainerIndex];
CollectionItem collectionItem = container as CollectionItem;
if (collectionItem != null)
{
return collectionItem.Elements[symbol.ItemIndex];
}
throw new InvalidOperationException(String.Format(
CultureInfo.InvariantCulture,
WixHarvesterStrings.EXP_ElementMustBeChoiceItemOrSequenceItem,
container.GetType().Name));
}
return null;
}
}
/// <summary>
/// Resets the enumerator to the beginning.
/// </summary>
public void Reset()
{
if (this.collectionStack != null)
{
this.collectionStack.Clear();
this.collectionStack = null;
}
}
/// <summary>
/// Moves the enumerator to the next item.
/// </summary>
/// <returns>True if there is a next item, false otherwise.</returns>
public bool MoveNext()
{
if (this.collectionStack == null)
{
if (this.collection.Count == 0)
{
return false;
}
this.collectionStack = new Stack();
this.collectionStack.Push(new CollectionSymbol(this.collection));
}
CollectionSymbol symbol = (CollectionSymbol)this.collectionStack.Peek();
if (this.FindNext(symbol))
{
return true;
}
this.collectionStack.Pop();
if (this.collectionStack.Count == 0)
{
return false;
}
return this.MoveNext();
}
/// <summary>
/// Pushes a collection onto the stack.
/// </summary>
/// <param name="elementCollection">The collection to push.</param>
private void PushCollection(ElementCollection elementCollection)
{
if (elementCollection.Count <= 0)
{
throw new ArgumentException(String.Format(
CultureInfo.InvariantCulture,
WixHarvesterStrings.EXP_CollectionMustHaveAtLeastOneElement,
elementCollection.Count));
}
CollectionSymbol symbol = new CollectionSymbol(elementCollection);
this.collectionStack.Push(symbol);
this.FindNext(symbol);
}
/// <summary>
/// Finds the next item from a given symbol.
/// </summary>
/// <param name="symbol">The symbol to start looking from.</param>
/// <returns>True if a next element is found, false otherwise.</returns>
private bool FindNext(CollectionSymbol symbol)
{
object container = symbol.Collection.items[symbol.ContainerIndex];
CollectionItem collectionItem = container as CollectionItem;
if (collectionItem != null)
{
if (symbol.ItemIndex + 1 < collectionItem.Elements.Count)
{
symbol.ItemIndex++;
return true;
}
}
ElementCollection elementCollection = container as ElementCollection;
if (elementCollection != null && elementCollection.Count > 0 && symbol.ItemIndex == -1)
{
symbol.ItemIndex++;
this.PushCollection(elementCollection);
return true;
}
symbol.ItemIndex = 0;
for (int i = symbol.ContainerIndex + 1; i < symbol.Collection.items.Count; ++i)
{
object nestedContainer = symbol.Collection.items[i];
CollectionItem nestedCollectionItem = nestedContainer as CollectionItem;
if (nestedCollectionItem != null)
{
if (nestedCollectionItem.Elements.Count > 0)
{
symbol.ContainerIndex = i;
return true;
}
}
ElementCollection nestedElementCollection = nestedContainer as ElementCollection;
if (nestedElementCollection != null && nestedElementCollection.Count > 0)
{
symbol.ContainerIndex = i;
this.PushCollection(nestedElementCollection);
return true;
}
}
return false;
}
/// <summary>
/// Class representing a single point in the collection. Consists of an ElementCollection,
/// a container index, and an index into the container.
/// </summary>
private class CollectionSymbol
{
private ElementCollection collection;
private int containerIndex;
private int itemIndex = -1;
/// <summary>
/// Creates a new CollectionSymbol.
/// </summary>
/// <param name="collection">The collection for the symbol.</param>
public CollectionSymbol(ElementCollection collection)
{
this.collection = collection;
}
/// <summary>
/// Gets the collection for the symbol.
/// </summary>
public ElementCollection Collection
{
get { return this.collection; }
}
/// <summary>
/// Gets and sets the index of the container in the collection.
/// </summary>
public int ContainerIndex
{
get { return this.containerIndex; }
set { this.containerIndex = value; }
}
/// <summary>
/// Gets and sets the index of the item in the container.
/// </summary>
public int ItemIndex
{
get { return this.itemIndex; }
set { this.itemIndex = value; }
}
}
}
}
}
|