/
XmlSchemaComplexType.cs
571 lines (507 loc) · 22.7 KB
/
XmlSchemaComplexType.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Xml.Serialization;
namespace System.Xml.Schema
{
public class XmlSchemaComplexType : XmlSchemaType
{
private static readonly XmlSchemaComplexType s_anyTypeLax = CreateAnyType(XmlSchemaContentProcessing.Lax);
private static readonly XmlSchemaComplexType s_anyTypeSkip = CreateAnyType(XmlSchemaContentProcessing.Skip);
private static readonly XmlSchemaComplexType s_untypedAnyType = CreateUntypedAnyType();
private XmlSchemaDerivationMethod _block = XmlSchemaDerivationMethod.None;
private XmlSchemaContentModel? _contentModel;
private XmlSchemaParticle? _particle;
private XmlSchemaObjectCollection? _attributes;
private XmlSchemaAnyAttribute? _anyAttribute;
private XmlSchemaParticle _contentTypeParticle = XmlSchemaParticle.Empty;
private XmlSchemaDerivationMethod _blockResolved;
private XmlSchemaObjectTable? _localElements;
private XmlSchemaObjectTable? _attributeUses;
private XmlSchemaAnyAttribute? _attributeWildcard;
//additional info for Partial validation
private byte _pvFlags;
private const byte wildCardMask = 0x01;
private const byte isMixedMask = 0x02;
private const byte isAbstractMask = 0x04;
//const byte dupDeclMask = 0x08;
private static XmlSchemaComplexType CreateUntypedAnyType()
{
// Create xdt:untypedAny
var untypedAny = new XmlSchemaComplexType();
untypedAny.SetQualifiedName(new XmlQualifiedName("untypedAny", XmlReservedNs.NsXQueryDataType));
untypedAny.IsMixed = true;
untypedAny.SetContentTypeParticle(s_anyTypeLax.ContentTypeParticle);
untypedAny.SetContentType(XmlSchemaContentType.Mixed);
untypedAny.ElementDecl = SchemaElementDecl.CreateAnyTypeElementDecl();
untypedAny.ElementDecl.SchemaType = untypedAny;
untypedAny.ElementDecl.ContentValidator = AnyTypeContentValidator;
return untypedAny;
}
private static XmlSchemaComplexType CreateAnyType(XmlSchemaContentProcessing processContents)
{
XmlSchemaComplexType localAnyType = new XmlSchemaComplexType();
localAnyType.SetQualifiedName(DatatypeImplementation.QnAnyType);
XmlSchemaAny anyElement = new XmlSchemaAny();
anyElement.MinOccurs = decimal.Zero;
anyElement.MaxOccurs = decimal.MaxValue;
anyElement.ProcessContents = processContents;
anyElement.BuildNamespaceList(null);
XmlSchemaSequence seq = new XmlSchemaSequence();
seq.Items.Add(anyElement);
localAnyType.SetContentTypeParticle(seq);
localAnyType.SetContentType(XmlSchemaContentType.Mixed);
localAnyType.ElementDecl = SchemaElementDecl.CreateAnyTypeElementDecl();
localAnyType.ElementDecl.SchemaType = localAnyType;
//Create contentValidator for Any
ParticleContentValidator contentValidator = new ParticleContentValidator(XmlSchemaContentType.Mixed);
contentValidator.Start();
contentValidator.OpenGroup();
contentValidator.AddNamespaceList(anyElement.NamespaceList!, anyElement);
contentValidator.AddStar();
contentValidator.CloseGroup();
ContentValidator anyContentValidator = contentValidator.Finish(true);
localAnyType.ElementDecl.ContentValidator = anyContentValidator;
XmlSchemaAnyAttribute anyAttribute = new XmlSchemaAnyAttribute();
anyAttribute.ProcessContents = processContents;
anyAttribute.BuildNamespaceList(null);
localAnyType.SetAttributeWildcard(anyAttribute);
localAnyType.ElementDecl.AnyAttribute = anyAttribute;
return localAnyType;
}
public XmlSchemaComplexType()
{
}
[XmlIgnore]
internal static XmlSchemaComplexType AnyType
{
get { return s_anyTypeLax; }
}
[XmlIgnore]
internal static XmlSchemaComplexType UntypedAnyType
{
get { return s_untypedAnyType; }
}
[XmlIgnore]
internal static XmlSchemaComplexType AnyTypeSkip
{
get { return s_anyTypeSkip; }
}
internal static ContentValidator AnyTypeContentValidator
{
get
{
return s_anyTypeLax.ElementDecl!.ContentValidator!;
}
}
[XmlAttribute("abstract"), DefaultValue(false)]
public bool IsAbstract
{
get
{
return (_pvFlags & isAbstractMask) != 0;
}
set
{
if (value)
{
_pvFlags = (byte)(_pvFlags | isAbstractMask);
}
else
{
_pvFlags = (byte)(_pvFlags & ~isAbstractMask);
}
}
}
[XmlAttribute("block"), DefaultValue(XmlSchemaDerivationMethod.None)]
public XmlSchemaDerivationMethod Block
{
get { return _block; }
set { _block = value; }
}
[XmlAttribute("mixed"), DefaultValue(false)]
public override bool IsMixed
{
get
{
return (_pvFlags & isMixedMask) != 0;
}
set
{
if (value)
{
_pvFlags = (byte)(_pvFlags | isMixedMask);
}
else
{
_pvFlags = (byte)(_pvFlags & ~isMixedMask);
}
}
}
[XmlElement("simpleContent", typeof(XmlSchemaSimpleContent)),
XmlElement("complexContent", typeof(XmlSchemaComplexContent))]
public XmlSchemaContentModel? ContentModel
{
get { return _contentModel; }
set { _contentModel = value; }
}
[XmlElement("group", typeof(XmlSchemaGroupRef)),
XmlElement("choice", typeof(XmlSchemaChoice)),
XmlElement("all", typeof(XmlSchemaAll)),
XmlElement("sequence", typeof(XmlSchemaSequence))]
public XmlSchemaParticle? Particle
{
get { return _particle; }
set { _particle = value; }
}
[XmlElement("attribute", typeof(XmlSchemaAttribute)),
XmlElement("attributeGroup", typeof(XmlSchemaAttributeGroupRef))]
public XmlSchemaObjectCollection Attributes =>
_attributes ??= new XmlSchemaObjectCollection();
[XmlElement("anyAttribute")]
public XmlSchemaAnyAttribute? AnyAttribute
{
get { return _anyAttribute; }
set { _anyAttribute = value; }
}
[XmlIgnore]
public XmlSchemaContentType ContentType
{
get { return SchemaContentType; }
}
[XmlIgnore]
public XmlSchemaParticle ContentTypeParticle
{
get { return _contentTypeParticle; }
}
[XmlIgnore]
public XmlSchemaDerivationMethod BlockResolved
{
get { return _blockResolved; }
}
[XmlIgnore]
public XmlSchemaObjectTable AttributeUses =>
_attributeUses ??= new XmlSchemaObjectTable();
[XmlIgnore]
public XmlSchemaAnyAttribute? AttributeWildcard
{
get { return _attributeWildcard; }
}
[XmlIgnore]
internal XmlSchemaObjectTable LocalElements =>
_localElements ??= new XmlSchemaObjectTable();
internal void SetContentTypeParticle(XmlSchemaParticle value)
{
_contentTypeParticle = value;
}
internal void SetBlockResolved(XmlSchemaDerivationMethod value)
{
_blockResolved = value;
}
internal void SetAttributeWildcard(XmlSchemaAnyAttribute? value)
{
_attributeWildcard = value;
}
internal bool HasWildCard
{
get
{
return (_pvFlags & wildCardMask) != 0;
}
set
{
if (value)
{
_pvFlags = (byte)(_pvFlags | wildCardMask);
}
else
{
_pvFlags = (byte)(_pvFlags & ~wildCardMask);
}
}
}
internal override XmlQualifiedName DerivedFrom
{
get
{
if (_contentModel == null)
{
// type derived from anyType
return XmlQualifiedName.Empty;
}
if (_contentModel.Content is XmlSchemaComplexContentRestriction)
return ((XmlSchemaComplexContentRestriction)_contentModel.Content).BaseTypeName;
else if (_contentModel.Content is XmlSchemaComplexContentExtension)
return ((XmlSchemaComplexContentExtension)_contentModel.Content).BaseTypeName;
else if (_contentModel.Content is XmlSchemaSimpleContentRestriction)
return ((XmlSchemaSimpleContentRestriction)_contentModel.Content).BaseTypeName;
else if (_contentModel.Content is XmlSchemaSimpleContentExtension)
return ((XmlSchemaSimpleContentExtension)_contentModel.Content).BaseTypeName;
else
return XmlQualifiedName.Empty;
}
}
internal void SetAttributes(XmlSchemaObjectCollection newAttributes)
{
_attributes = newAttributes;
}
internal bool ContainsIdAttribute(bool findAll)
{
int idCount = 0;
foreach (XmlSchemaAttribute? attribute in this.AttributeUses.Values)
{
if (attribute!.Use != XmlSchemaUse.Prohibited)
{
XmlSchemaDatatype? datatype = attribute.Datatype;
if (datatype != null && datatype.TypeCode == XmlTypeCode.Id)
{
idCount++;
if (idCount > 1)
{ //two or more attributes is error
break;
}
}
}
}
return findAll ? (idCount > 1) : (idCount > 0);
}
internal override XmlSchemaObject Clone()
{
System.Diagnostics.Debug.Fail("Should never call Clone() on XmlSchemaComplexType. Call Clone(XmlSchema) instead.");
return Clone(null);
}
internal XmlSchemaObject Clone(XmlSchema? parentSchema)
{
XmlSchemaComplexType complexType = (XmlSchemaComplexType)MemberwiseClone();
//Deep clone the QNames as these will be updated on chameleon includes
if (complexType.ContentModel != null)
{ //simpleContent or complexContent
XmlSchemaSimpleContent? simpleContent = complexType.ContentModel as XmlSchemaSimpleContent;
if (simpleContent != null)
{
XmlSchemaSimpleContent newSimpleContent = (XmlSchemaSimpleContent)simpleContent.Clone();
XmlSchemaSimpleContentExtension? simpleExt = simpleContent.Content as XmlSchemaSimpleContentExtension;
if (simpleExt != null)
{
XmlSchemaSimpleContentExtension newSimpleExt = (XmlSchemaSimpleContentExtension)simpleExt.Clone();
newSimpleExt.BaseTypeName = simpleExt.BaseTypeName.Clone();
newSimpleExt.SetAttributes(CloneAttributes(simpleExt.Attributes));
newSimpleContent.Content = newSimpleExt;
}
else
{ //simpleContent.Content is XmlSchemaSimpleContentRestriction
XmlSchemaSimpleContentRestriction simpleRest = (XmlSchemaSimpleContentRestriction)simpleContent.Content!;
XmlSchemaSimpleContentRestriction newSimpleRest = (XmlSchemaSimpleContentRestriction)simpleRest.Clone();
newSimpleRest.BaseTypeName = simpleRest.BaseTypeName.Clone();
newSimpleRest.SetAttributes(CloneAttributes(simpleRest.Attributes));
newSimpleContent.Content = newSimpleRest;
}
complexType.ContentModel = newSimpleContent;
}
else
{ // complexType.ContentModel is XmlSchemaComplexContent
XmlSchemaComplexContent complexContent = (XmlSchemaComplexContent)complexType.ContentModel;
XmlSchemaComplexContent newComplexContent = (XmlSchemaComplexContent)complexContent.Clone();
XmlSchemaComplexContentExtension? complexExt = complexContent.Content as XmlSchemaComplexContentExtension;
if (complexExt != null)
{
XmlSchemaComplexContentExtension newComplexExt = (XmlSchemaComplexContentExtension)complexExt.Clone();
newComplexExt.BaseTypeName = complexExt.BaseTypeName.Clone();
newComplexExt.SetAttributes(CloneAttributes(complexExt.Attributes));
if (HasParticleRef(complexExt.Particle, parentSchema))
{
newComplexExt.Particle = CloneParticle(complexExt.Particle, parentSchema);
}
newComplexContent.Content = newComplexExt;
}
else
{ // complexContent.Content is XmlSchemaComplexContentRestriction
XmlSchemaComplexContentRestriction complexRest = (complexContent.Content as XmlSchemaComplexContentRestriction)!;
XmlSchemaComplexContentRestriction newComplexRest = (XmlSchemaComplexContentRestriction)complexRest.Clone();
newComplexRest.BaseTypeName = complexRest.BaseTypeName.Clone();
newComplexRest.SetAttributes(CloneAttributes(complexRest.Attributes));
if (HasParticleRef(newComplexRest.Particle, parentSchema))
{
newComplexRest.Particle = CloneParticle(newComplexRest.Particle, parentSchema);
}
newComplexContent.Content = newComplexRest;
}
complexType.ContentModel = newComplexContent;
}
}
else
{ //equals XmlSchemaComplexContent with baseType is anyType
if (HasParticleRef(complexType.Particle, parentSchema))
{
complexType.Particle = CloneParticle(complexType.Particle, parentSchema);
}
complexType.SetAttributes(CloneAttributes(complexType.Attributes));
}
complexType.ClearCompiledState();
return complexType;
}
private void ClearCompiledState()
{
//Re-set post-compiled state for cloned object
_attributeUses = null;
_localElements = null;
_attributeWildcard = null;
_contentTypeParticle = XmlSchemaParticle.Empty;
_blockResolved = XmlSchemaDerivationMethod.None;
}
internal static XmlSchemaObjectCollection CloneAttributes(XmlSchemaObjectCollection attributes)
{
if (HasAttributeQNameRef(attributes))
{
XmlSchemaObjectCollection newAttributes = attributes.Clone();
XmlSchemaAttributeGroupRef? attributeGroupRef;
XmlSchemaAttributeGroupRef newAttGroupRef;
XmlSchemaObject xso;
XmlSchemaAttribute att;
for (int i = 0; i < attributes.Count; i++)
{
xso = attributes[i];
attributeGroupRef = xso as XmlSchemaAttributeGroupRef;
if (attributeGroupRef != null)
{
newAttGroupRef = (XmlSchemaAttributeGroupRef)attributeGroupRef.Clone();
newAttGroupRef.RefName = attributeGroupRef.RefName.Clone();
newAttributes[i] = newAttGroupRef;
}
else
{ //Its XmlSchemaAttribute
att = (xso as XmlSchemaAttribute)!;
if (!att.RefName.IsEmpty || !att.SchemaTypeName.IsEmpty)
{
newAttributes[i] = att.Clone();
}
}
}
return newAttributes;
}
return attributes;
}
private static XmlSchemaObjectCollection CloneGroupBaseParticles(XmlSchemaObjectCollection groupBaseParticles, XmlSchema? parentSchema)
{
XmlSchemaObjectCollection newParticles = groupBaseParticles.Clone();
for (int i = 0; i < groupBaseParticles.Count; i++)
{
XmlSchemaParticle p = (XmlSchemaParticle)groupBaseParticles[i];
newParticles[i] = CloneParticle(p, parentSchema);
}
return newParticles;
}
[return: NotNullIfNotNull(nameof(particle))]
internal static XmlSchemaParticle? CloneParticle(XmlSchemaParticle? particle, XmlSchema? parentSchema)
{
XmlSchemaGroupBase? groupBase = particle as XmlSchemaGroupBase;
if (groupBase != null)
{ //Choice or sequence
XmlSchemaGroupBase newGroupBase;
XmlSchemaObjectCollection newGroupbaseParticles = CloneGroupBaseParticles(groupBase.Items, parentSchema);
newGroupBase = (XmlSchemaGroupBase)groupBase.Clone();
newGroupBase.SetItems(newGroupbaseParticles);
return newGroupBase;
}
else if (particle is XmlSchemaGroupRef)
{ // group ref
XmlSchemaGroupRef newGroupRef = (XmlSchemaGroupRef)particle.Clone();
newGroupRef.RefName = newGroupRef.RefName.Clone();
return newGroupRef;
}
else
{
XmlSchemaElement? oldElem = particle as XmlSchemaElement;
// If the particle is an element and one of the following is true:
// - it references another element by name
// - it references its type by name
// - it's form (effective) is qualified (meaning it will inherint namespace from chameleon includes if that happens)
// then the element itself needs to be cloned.
if (oldElem != null && (!oldElem.RefName.IsEmpty || !oldElem.SchemaTypeName.IsEmpty ||
GetResolvedElementForm(parentSchema, oldElem) == XmlSchemaForm.Qualified))
{
XmlSchemaElement newElem = (XmlSchemaElement)oldElem.Clone(parentSchema);
return newElem;
}
}
return particle;
}
// This method returns the effective value of the "element form" for the specified element in the specified
// parentSchema. Element form is either qualified, unqualified or none. If it's qualified it means that
// if the element doesn't declare its own namespace the targetNamespace of the schema is used instead.
// The element form can be either specified on the element itself via the "form" attribute or
// if that one is not present its inheritted from the value of the elementFormDefault attribute on the owning
// schema.
private static XmlSchemaForm GetResolvedElementForm(XmlSchema? parentSchema, XmlSchemaElement element)
{
if (element.Form == XmlSchemaForm.None && parentSchema != null)
{
return parentSchema.ElementFormDefault;
}
else
{
return element.Form;
}
}
internal static bool HasParticleRef(XmlSchemaParticle? particle, XmlSchema? parentSchema)
{
XmlSchemaGroupBase? groupBase = particle as XmlSchemaGroupBase;
if (groupBase != null)
{
bool foundRef = false;
int i = 0;
while (i < groupBase.Items.Count && !foundRef)
{
XmlSchemaParticle p = (XmlSchemaParticle)groupBase.Items[i++];
if (p is XmlSchemaGroupRef)
{
foundRef = true;
}
else
{
XmlSchemaElement? elem = p as XmlSchemaElement;
// This is the same condition as in the CloneParticle method
// that's on purpose. This method is used to determine if we need to clone the whole particle.
// If we do, then the CloneParticle is called and it will try to clone only
// those elements which need cloning - and those are the ones matching this condition.
if (elem != null && (!elem.RefName.IsEmpty || !elem.SchemaTypeName.IsEmpty ||
GetResolvedElementForm(parentSchema, elem) == XmlSchemaForm.Qualified))
{
foundRef = true;
}
else
{
foundRef = HasParticleRef(p, parentSchema);
}
}
}
return foundRef;
}
else if (particle is XmlSchemaGroupRef)
{
return true;
}
return false;
}
internal static bool HasAttributeQNameRef(XmlSchemaObjectCollection attributes)
{
for (int i = 0; i < attributes.Count; ++i)
{
if (attributes[i] is XmlSchemaAttributeGroupRef)
{
return true;
}
else
{
XmlSchemaAttribute attribute = (attributes[i] as XmlSchemaAttribute)!;
if (!attribute.RefName.IsEmpty || !attribute.SchemaTypeName.IsEmpty)
{
return true;
}
}
}
return false;
}
}
}