1 //------------------------------------------------------------------------------
2 // <copyright file="XsdBuilder.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
7 
8 namespace System.Xml.Schema {
9 
10     using System.IO;
11     using System.Collections;
12     using System.Diagnostics;
13     using System.Xml.Serialization;
14 
15 
16     internal sealed class XsdBuilder : SchemaBuilder {
17         private enum State {
18             Root,
19             Schema,
20             Annotation,
21             Include,
22             Import,
23             Element,
24             Attribute,
25             AttributeGroup,
26             AttributeGroupRef,
27             AnyAttribute,
28             Group,
29             GroupRef,
30             All,
31             Choice,
32             Sequence,
33             Any,
34             Notation,
35             SimpleType,
36             ComplexType,
37             ComplexContent,
38             ComplexContentRestriction,
39             ComplexContentExtension,
40             SimpleContent,
41             SimpleContentExtension,
42             SimpleContentRestriction,
43             SimpleTypeUnion,
44             SimpleTypeList,
45             SimpleTypeRestriction,
46             Unique,
47             Key,
48             KeyRef,
49             Selector,
50             Field,
51             MinExclusive,
52             MinInclusive,
53             MaxExclusive,
54             MaxInclusive,
55             TotalDigits,
56             FractionDigits,
57             Length,
58             MinLength,
59             MaxLength,
60             Enumeration,
61             Pattern,
62             WhiteSpace,
63             AppInfo,
64             Documentation,
65             Redefine,
66         }
67         private const int STACK_INCREMENT         = 10;
68 
XsdBuildFunction(XsdBuilder builder, string value)69         private delegate void XsdBuildFunction(XsdBuilder builder, string value);
XsdInitFunction(XsdBuilder builder, string value)70         private delegate void XsdInitFunction(XsdBuilder builder, string value);
XsdEndChildFunction(XsdBuilder builder)71         private delegate void XsdEndChildFunction(XsdBuilder builder);
72 
73         private sealed class XsdAttributeEntry {
74             public SchemaNames.Token Attribute;               // possible attribute names
75             public XsdBuildFunction BuildFunc;  // Corresponding build functions for attribute value
76 
XsdAttributeEntry(SchemaNames.Token a, XsdBuildFunction build)77             public XsdAttributeEntry(SchemaNames.Token a, XsdBuildFunction build) {
78                 Attribute = a;
79                 BuildFunc = build;
80             }
81         };
82 
83         //
84         // XsdEntry controls the states of parsing a schema document
85         // and calls the corresponding "init", "end" and "build" functions when necessary
86         //
87         private sealed class XsdEntry {
88             public SchemaNames.Token Name;                  // the name of the object it is comparing to
89             public State CurrentState;
90             public State[] NextStates;                   // possible next states
91             public XsdAttributeEntry[] Attributes;       // allowed attributes
92             public XsdInitFunction InitFunc;             // "init" functions in XsdBuilder
93             public XsdEndChildFunction EndChildFunc;     // "end" functions in XsdBuilder for EndChildren
94             public bool ParseContent;                       // whether text content is allowed
95 
XsdEntry(SchemaNames.Token n, State state, State[] nextStates, XsdAttributeEntry[] attributes, XsdInitFunction init, XsdEndChildFunction end, bool parseContent)96             public XsdEntry(SchemaNames.Token n,
97                             State   state,
98                             State[] nextStates,
99                             XsdAttributeEntry[] attributes,
100                             XsdInitFunction init,
101                             XsdEndChildFunction end,
102                             bool parseContent) {
103                 Name = n;
104                 CurrentState = state;
105                 NextStates = nextStates;
106                 Attributes = attributes;
107                 InitFunc = init;
108                 EndChildFunc = end;
109                 ParseContent = parseContent;
110             }
111         };
112 
113 
114         //required for Parsing QName
115         class BuilderNamespaceManager : XmlNamespaceManager {
116             XmlNamespaceManager nsMgr;
117             XmlReader reader;
118 
BuilderNamespaceManager(XmlNamespaceManager nsMgr, XmlReader reader)119             public BuilderNamespaceManager(XmlNamespaceManager nsMgr, XmlReader reader) {
120                 this.nsMgr = nsMgr;
121                 this.reader = reader;
122             }
123 
LookupNamespace(string prefix)124             public override string LookupNamespace(string prefix) {
125                 string ns = nsMgr.LookupNamespace(prefix);
126                 if (ns == null) {
127                     ns = reader.LookupNamespace(prefix);
128                 }
129                 return ns;
130             }
131         }
132         //////////////////////////////////////////////////////////////////////////////////////////////
133         // Data structures for XSD Schema, Sept 2000 version
134         //
135 
136         //
137         //Elements
138         //
139         private static readonly State[] SchemaElement = {
140             State.Schema};
141         private static readonly State[] SchemaSubelements = {
142             State.Annotation, State.Include, State.Import, State.Redefine,
143             State.ComplexType, State.SimpleType, State.Element, State.Attribute,
144             State.AttributeGroup, State.Group, State.Notation};
145         private static readonly State[] AttributeSubelements = {
146             State.Annotation, State.SimpleType};
147         private static readonly State[] ElementSubelements   = {
148             State.Annotation, State.SimpleType, State.ComplexType,
149             State.Unique, State.Key, State.KeyRef};
150         private static readonly State[] ComplexTypeSubelements = {
151             State.Annotation, State.SimpleContent, State.ComplexContent,
152             State.GroupRef, State.All, State.Choice, State.Sequence,
153             State.Attribute, State.AttributeGroupRef, State.AnyAttribute};
154         private static readonly State[] SimpleContentSubelements = {
155             State.Annotation, State.SimpleContentRestriction, State.SimpleContentExtension };
156         private static readonly State[] SimpleContentExtensionSubelements = {
157             State.Annotation, State.Attribute, State.AttributeGroupRef, State.AnyAttribute};
158         private static readonly State[] SimpleContentRestrictionSubelements = {
159             State.Annotation, State.SimpleType,
160             State.Enumeration, State.Length, State.MaxExclusive, State.MaxInclusive, State.MaxLength, State.MinExclusive,
161             State.MinInclusive, State.MinLength, State.Pattern, State.TotalDigits, State.FractionDigits, State.WhiteSpace,
162             State.Attribute, State.AttributeGroupRef, State.AnyAttribute};
163         private static readonly State[] ComplexContentSubelements = {
164             State.Annotation, State.ComplexContentRestriction, State.ComplexContentExtension };
165         private static readonly State[] ComplexContentExtensionSubelements = {
166             State.Annotation, State.GroupRef, State.All, State.Choice, State.Sequence,
167             State.Attribute, State.AttributeGroupRef, State.AnyAttribute};
168         private static readonly State[] ComplexContentRestrictionSubelements = {
169             State.Annotation, State.GroupRef, State.All, State.Choice, State.Sequence,
170             State.Attribute, State.AttributeGroupRef, State.AnyAttribute};
171         private static readonly State[] SimpleTypeSubelements   = {
172             State.Annotation, State.SimpleTypeList, State.SimpleTypeRestriction, State.SimpleTypeUnion};
173         private static readonly State[] SimpleTypeRestrictionSubelements   = {
174             State.Annotation, State.SimpleType,
175             State.Enumeration, State.Length, State.MaxExclusive, State.MaxInclusive, State.MaxLength, State.MinExclusive,
176             State.MinInclusive, State.MinLength, State.Pattern, State.TotalDigits, State.FractionDigits, State.WhiteSpace};
177         private static readonly State[] SimpleTypeListSubelements   = {
178             State.Annotation, State.SimpleType};
179         private static readonly State[] SimpleTypeUnionSubelements   = {
180             State.Annotation, State.SimpleType};
181         private static readonly State[] RedefineSubelements   = {
182             State.Annotation, State.AttributeGroup, State.ComplexType, State.Group, State.SimpleType };
183         private static readonly State[] AttributeGroupSubelements = {
184             State.Annotation, State.Attribute, State.AttributeGroupRef, State.AnyAttribute};
185         private static readonly State[] GroupSubelements = {
186             State.Annotation, State.All, State.Choice, State.Sequence};
187         private static readonly State[] AllSubelements = {
188             State.Annotation, State.Element};
189         private static readonly State[] ChoiceSequenceSubelements = {
190             State.Annotation, State.Element, State.GroupRef, State.Choice, State.Sequence, State.Any};
191         private static readonly State[] IdentityConstraintSubelements = {
192             State.Annotation, State.Selector, State.Field};
193         private static readonly State[] AnnotationSubelements = {
194             State.AppInfo, State.Documentation};
195         private static readonly State[] AnnotatedSubelements = {
196             State.Annotation};
197 
198 
199         //
200         //Attributes
201         //
202         private static readonly XsdAttributeEntry[] SchemaAttributes = {
203             new XsdAttributeEntry(SchemaNames.Token.SchemaAttributeFormDefault,    new XsdBuildFunction(BuildSchema_AttributeFormDefault) ),
204             new XsdAttributeEntry(SchemaNames.Token.SchemaElementFormDefault,      new XsdBuildFunction(BuildSchema_ElementFormDefault) ),
205             new XsdAttributeEntry(SchemaNames.Token.SchemaTargetNamespace,         new XsdBuildFunction(BuildSchema_TargetNamespace) ),
206             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
207             new XsdAttributeEntry(SchemaNames.Token.SchemaVersion,                 new XsdBuildFunction(BuildSchema_Version) ),
208             new XsdAttributeEntry(SchemaNames.Token.SchemaFinalDefault,            new XsdBuildFunction(BuildSchema_FinalDefault) ),
209             new XsdAttributeEntry(SchemaNames.Token.SchemaBlockDefault,            new XsdBuildFunction(BuildSchema_BlockDefault) )
210         };
211 
212         private static readonly XsdAttributeEntry[] AttributeAttributes = {
213             new XsdAttributeEntry(SchemaNames.Token.SchemaDefault,                 new XsdBuildFunction(BuildAttribute_Default) ),
214             new XsdAttributeEntry(SchemaNames.Token.SchemaFixed,                   new XsdBuildFunction(BuildAttribute_Fixed) ),
215             new XsdAttributeEntry(SchemaNames.Token.SchemaForm,                    new XsdBuildFunction(BuildAttribute_Form) ),
216             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
217             new XsdAttributeEntry(SchemaNames.Token.SchemaName,                    new XsdBuildFunction(BuildAttribute_Name) ),
218             new XsdAttributeEntry(SchemaNames.Token.SchemaRef,                     new XsdBuildFunction(BuildAttribute_Ref) ),
219             new XsdAttributeEntry(SchemaNames.Token.SchemaType,                    new XsdBuildFunction(BuildAttribute_Type) ),
220             new XsdAttributeEntry(SchemaNames.Token.SchemaUse,                     new XsdBuildFunction(BuildAttribute_Use) )
221         };
222 
223         private static readonly XsdAttributeEntry[] ElementAttributes = {
224             new XsdAttributeEntry(SchemaNames.Token.SchemaAbstract,                new XsdBuildFunction(BuildElement_Abstract) ),
225             new XsdAttributeEntry(SchemaNames.Token.SchemaBlock,                   new XsdBuildFunction(BuildElement_Block) ),
226             new XsdAttributeEntry(SchemaNames.Token.SchemaDefault,                 new XsdBuildFunction(BuildElement_Default) ),
227             new XsdAttributeEntry(SchemaNames.Token.SchemaFinal,                   new XsdBuildFunction(BuildElement_Final) ),
228             new XsdAttributeEntry(SchemaNames.Token.SchemaFixed,                   new XsdBuildFunction(BuildElement_Fixed) ),
229             new XsdAttributeEntry(SchemaNames.Token.SchemaForm,                    new XsdBuildFunction(BuildElement_Form) ),
230             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
231             new XsdAttributeEntry(SchemaNames.Token.SchemaMaxOccurs,               new XsdBuildFunction(BuildElement_MaxOccurs) ),
232             new XsdAttributeEntry(SchemaNames.Token.SchemaMinOccurs,               new XsdBuildFunction(BuildElement_MinOccurs) ),
233             new XsdAttributeEntry(SchemaNames.Token.SchemaName,                    new XsdBuildFunction(BuildElement_Name) ),
234             new XsdAttributeEntry(SchemaNames.Token.SchemaNillable,                new XsdBuildFunction(BuildElement_Nillable) ),
235             new XsdAttributeEntry(SchemaNames.Token.SchemaRef,                     new XsdBuildFunction(BuildElement_Ref) ),
236             new XsdAttributeEntry(SchemaNames.Token.SchemaSubstitutionGroup,       new XsdBuildFunction(BuildElement_SubstitutionGroup) ),
237             new XsdAttributeEntry(SchemaNames.Token.SchemaType,                    new XsdBuildFunction(BuildElement_Type) )
238         };
239 
240         private static readonly XsdAttributeEntry[] ComplexTypeAttributes = {
241             new XsdAttributeEntry(SchemaNames.Token.SchemaAbstract,                new XsdBuildFunction(BuildComplexType_Abstract) ),
242             new XsdAttributeEntry(SchemaNames.Token.SchemaBlock,                   new XsdBuildFunction(BuildComplexType_Block) ),
243             new XsdAttributeEntry(SchemaNames.Token.SchemaFinal,                   new XsdBuildFunction(BuildComplexType_Final) ),
244             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
245             new XsdAttributeEntry(SchemaNames.Token.SchemaMixed,                   new XsdBuildFunction(BuildComplexType_Mixed) ),
246             new XsdAttributeEntry(SchemaNames.Token.SchemaName,                    new XsdBuildFunction(BuildComplexType_Name) )
247         };
248 
249         private static readonly XsdAttributeEntry[] SimpleContentAttributes = {
250             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
251         };
252 
253         private static readonly XsdAttributeEntry[] SimpleContentExtensionAttributes = {
254             new XsdAttributeEntry(SchemaNames.Token.SchemaBase,                    new XsdBuildFunction(BuildSimpleContentExtension_Base) ),
255             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) )
256         };
257 
258         private static readonly XsdAttributeEntry[] SimpleContentRestrictionAttributes = {
259             new XsdAttributeEntry(SchemaNames.Token.SchemaBase,                    new XsdBuildFunction(BuildSimpleContentRestriction_Base) ),
260             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
261         };
262 
263         private static readonly XsdAttributeEntry[] ComplexContentAttributes = {
264             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
265             new XsdAttributeEntry(SchemaNames.Token.SchemaMixed,                   new XsdBuildFunction(BuildComplexContent_Mixed) ),
266         };
267 
268         private static readonly XsdAttributeEntry[] ComplexContentExtensionAttributes = {
269             new XsdAttributeEntry(SchemaNames.Token.SchemaBase,                    new XsdBuildFunction(BuildComplexContentExtension_Base) ),
270             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
271         };
272 
273         private static readonly XsdAttributeEntry[] ComplexContentRestrictionAttributes = {
274             new XsdAttributeEntry(SchemaNames.Token.SchemaBase,                    new XsdBuildFunction(BuildComplexContentRestriction_Base) ),
275             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
276         };
277 
278         private static readonly XsdAttributeEntry[] SimpleTypeAttributes = {
279             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
280             new XsdAttributeEntry(SchemaNames.Token.SchemaFinal,                   new XsdBuildFunction(BuildSimpleType_Final) ),
281             new XsdAttributeEntry(SchemaNames.Token.SchemaName,                    new XsdBuildFunction(BuildSimpleType_Name) )
282         };
283 
284         private static readonly XsdAttributeEntry[] SimpleTypeRestrictionAttributes = {
285             new XsdAttributeEntry(SchemaNames.Token.SchemaBase,                    new XsdBuildFunction(BuildSimpleTypeRestriction_Base) ),
286             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
287         };
288 
289         private static readonly XsdAttributeEntry[] SimpleTypeUnionAttributes = {
290             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
291             new XsdAttributeEntry(SchemaNames.Token.SchemaMemberTypes,             new XsdBuildFunction(BuildSimpleTypeUnion_MemberTypes) ),
292         };
293 
294         private static readonly XsdAttributeEntry[] SimpleTypeListAttributes = {
295             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
296             new XsdAttributeEntry(SchemaNames.Token.SchemaItemType,                new XsdBuildFunction(BuildSimpleTypeList_ItemType) ),
297         };
298 
299         private static readonly XsdAttributeEntry[] AttributeGroupAttributes = {
300             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
301             new XsdAttributeEntry(SchemaNames.Token.SchemaName,                    new XsdBuildFunction(BuildAttributeGroup_Name) ),
302         };
303 
304         private static readonly XsdAttributeEntry[] AttributeGroupRefAttributes = {
305             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
306             new XsdAttributeEntry(SchemaNames.Token.SchemaRef,                     new XsdBuildFunction(BuildAttributeGroupRef_Ref) )
307         };
308 
309         private static readonly XsdAttributeEntry[] GroupAttributes = {
310             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
311             new XsdAttributeEntry(SchemaNames.Token.SchemaName,                    new XsdBuildFunction(BuildGroup_Name) ),
312         };
313 
314         private static readonly XsdAttributeEntry[] GroupRefAttributes = {
315             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
316             new XsdAttributeEntry(SchemaNames.Token.SchemaMaxOccurs,               new XsdBuildFunction(BuildParticle_MaxOccurs) ),
317             new XsdAttributeEntry(SchemaNames.Token.SchemaMinOccurs,               new XsdBuildFunction(BuildParticle_MinOccurs) ),
318             new XsdAttributeEntry(SchemaNames.Token.SchemaRef,                     new XsdBuildFunction(BuildGroupRef_Ref) )
319         };
320 
321         private static readonly XsdAttributeEntry[] ParticleAttributes = {
322             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
323             new XsdAttributeEntry(SchemaNames.Token.SchemaMaxOccurs,               new XsdBuildFunction(BuildParticle_MaxOccurs) ),
324             new XsdAttributeEntry(SchemaNames.Token.SchemaMinOccurs,               new XsdBuildFunction(BuildParticle_MinOccurs) ),
325         };
326 
327 
328         private static readonly XsdAttributeEntry[] AnyAttributes = {
329             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
330             new XsdAttributeEntry(SchemaNames.Token.SchemaMaxOccurs,               new XsdBuildFunction(BuildParticle_MaxOccurs) ),
331             new XsdAttributeEntry(SchemaNames.Token.SchemaMinOccurs,               new XsdBuildFunction(BuildParticle_MinOccurs) ),
332             new XsdAttributeEntry(SchemaNames.Token.SchemaNamespace,               new XsdBuildFunction(BuildAny_Namespace) ),
333             new XsdAttributeEntry(SchemaNames.Token.SchemaProcessContents,         new XsdBuildFunction(BuildAny_ProcessContents) )
334         };
335 
336         private static readonly XsdAttributeEntry[] IdentityConstraintAttributes = {
337             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
338             new XsdAttributeEntry(SchemaNames.Token.SchemaName,                    new XsdBuildFunction(BuildIdentityConstraint_Name) ),
339             new XsdAttributeEntry(SchemaNames.Token.SchemaRefer,                   new XsdBuildFunction(BuildIdentityConstraint_Refer) )
340         };
341 
342         private static readonly XsdAttributeEntry[] SelectorAttributes = {
343             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
344             new XsdAttributeEntry(SchemaNames.Token.SchemaXPath,                   new XsdBuildFunction(BuildSelector_XPath) )
345         };
346 
347         private static readonly XsdAttributeEntry[] FieldAttributes = {
348             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
349             new XsdAttributeEntry(SchemaNames.Token.SchemaXPath,                   new XsdBuildFunction(BuildField_XPath) )
350         };
351 
352         private static readonly XsdAttributeEntry[] NotationAttributes = {
353             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
354             new XsdAttributeEntry(SchemaNames.Token.SchemaName,                    new XsdBuildFunction(BuildNotation_Name) ),
355             new XsdAttributeEntry(SchemaNames.Token.SchemaPublic,                  new XsdBuildFunction(BuildNotation_Public) ),
356             new XsdAttributeEntry(SchemaNames.Token.SchemaSystem,                  new XsdBuildFunction(BuildNotation_System) )
357         };
358 
359         private static readonly XsdAttributeEntry[] IncludeAttributes = {
360             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
361             new XsdAttributeEntry(SchemaNames.Token.SchemaSchemaLocation,          new XsdBuildFunction(BuildInclude_SchemaLocation) )
362         };
363 
364         private static readonly XsdAttributeEntry[] ImportAttributes = {
365             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
366             new XsdAttributeEntry(SchemaNames.Token.SchemaNamespace,               new XsdBuildFunction(BuildImport_Namespace) ),
367             new XsdAttributeEntry(SchemaNames.Token.SchemaSchemaLocation,          new XsdBuildFunction(BuildImport_SchemaLocation) )
368         };
369 
370         private static readonly XsdAttributeEntry[] FacetAttributes = {
371             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
372             new XsdAttributeEntry(SchemaNames.Token.SchemaFixed,                   new XsdBuildFunction(BuildFacet_Fixed) ),
373             new XsdAttributeEntry(SchemaNames.Token.SchemaValue,                   new XsdBuildFunction(BuildFacet_Value) )
374         };
375 
376         private static readonly XsdAttributeEntry[] AnyAttributeAttributes = {
377             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
378             new XsdAttributeEntry(SchemaNames.Token.SchemaNamespace,               new XsdBuildFunction(BuildAnyAttribute_Namespace) ),
379             new XsdAttributeEntry(SchemaNames.Token.SchemaProcessContents,         new XsdBuildFunction(BuildAnyAttribute_ProcessContents) )
380         };
381 
382         private static readonly XsdAttributeEntry[] DocumentationAttributes = {
383             new XsdAttributeEntry(SchemaNames.Token.SchemaSource,                  new XsdBuildFunction(BuildDocumentation_Source) ),
384             new XsdAttributeEntry(SchemaNames.Token.XmlLang,                       new XsdBuildFunction(BuildDocumentation_XmlLang) )
385         };
386 
387         private static readonly XsdAttributeEntry[] AppinfoAttributes = {
388             new XsdAttributeEntry(SchemaNames.Token.SchemaSource,                  new XsdBuildFunction(BuildAppinfo_Source) )
389         };
390 
391         private static readonly XsdAttributeEntry[] RedefineAttributes = {
392             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
393             new XsdAttributeEntry(SchemaNames.Token.SchemaSchemaLocation,          new XsdBuildFunction(BuildRedefine_SchemaLocation) )
394         };
395 
396         private static readonly XsdAttributeEntry[] AnnotationAttributes = {
397             new XsdAttributeEntry(SchemaNames.Token.SchemaId,                      new XsdBuildFunction(BuildAnnotated_Id) ),
398         };
399         //
400         // XSD Schema entries
401         //
402 
403        private static readonly XsdEntry[] SchemaEntries = {
404        /* Root */                       new XsdEntry( SchemaNames.Token.Empty, State.Root, SchemaElement, null,
405                                                       null,
406                                                       null,
407                                                       true),
408        /* Schema */                     new XsdEntry( SchemaNames.Token.XsdSchema, State.Schema,     SchemaSubelements, SchemaAttributes,
409                                                       new XsdInitFunction(InitSchema),
410                                                       null,
411                                                       true),
412        /* Annotation */                 new XsdEntry( SchemaNames.Token.XsdAnnotation, State.Annotation,     AnnotationSubelements, AnnotationAttributes,
413                                                       new XsdInitFunction(InitAnnotation),
414                                                       null,
415                                                       true),
416        /* Include */                    new XsdEntry( SchemaNames.Token.XsdInclude, State.Include,    AnnotatedSubelements, IncludeAttributes,
417                                                       new XsdInitFunction(InitInclude),
418                                                       null,
419                                                       true),
420        /* Import */                     new XsdEntry( SchemaNames.Token.XsdImport, State.Import,     AnnotatedSubelements, ImportAttributes,
421                                                       new XsdInitFunction(InitImport),
422                                                       null,
423                                                       true),
424        /* Element */                    new XsdEntry( SchemaNames.Token.XsdElement, State.Element,     ElementSubelements, ElementAttributes,
425                                                       new XsdInitFunction(InitElement),
426                                                       null,
427                                                       true),
428        /* Attribute */                  new XsdEntry( SchemaNames.Token.XsdAttribute, State.Attribute,     AttributeSubelements, AttributeAttributes,
429                                                       new XsdInitFunction(InitAttribute),
430                                                       null,
431                                                       true),
432        /* AttributeGroup */             new XsdEntry( SchemaNames.Token.xsdAttributeGroup, State.AttributeGroup,     AttributeGroupSubelements, AttributeGroupAttributes,
433                                                       new XsdInitFunction(InitAttributeGroup),
434                                                       null,
435                                                       true),
436        /* AttributeGroupRef */          new XsdEntry( SchemaNames.Token.xsdAttributeGroup, State.AttributeGroupRef,  AnnotatedSubelements, AttributeGroupRefAttributes,
437                                                       new XsdInitFunction(InitAttributeGroupRef),
438                                                       null,
439                                                       true),
440        /* AnyAttribute */               new XsdEntry( SchemaNames.Token.XsdAnyAttribute, State.AnyAttribute,     AnnotatedSubelements, AnyAttributeAttributes,
441                                                       new XsdInitFunction(InitAnyAttribute),
442                                                       null,
443                                                       true),
444        /* Group */                      new XsdEntry( SchemaNames.Token.XsdGroup, State.Group,     GroupSubelements, GroupAttributes,
445                                                       new XsdInitFunction(InitGroup),
446                                                       null,
447                                                       true),
448        /* GroupRef */                   new XsdEntry( SchemaNames.Token.XsdGroup, State.GroupRef,     AnnotatedSubelements, GroupRefAttributes,
449                                                       new XsdInitFunction(InitGroupRef),
450                                                       null,
451                                                       true),
452        /* All */                        new XsdEntry( SchemaNames.Token.XsdAll, State.All,     AllSubelements, ParticleAttributes,
453                                                       new XsdInitFunction(InitAll),
454                                                       null,
455                                                       true),
456        /* Choice */                     new XsdEntry( SchemaNames.Token.XsdChoice, State.Choice,     ChoiceSequenceSubelements, ParticleAttributes,
457                                                       new XsdInitFunction(InitChoice),
458                                                       null,
459                                                       true),
460        /* Sequence */                   new XsdEntry( SchemaNames.Token.XsdSequence, State.Sequence,     ChoiceSequenceSubelements, ParticleAttributes,
461                                                       new XsdInitFunction(InitSequence),
462                                                       null,
463                                                       true),
464        /* Any */                        new XsdEntry( SchemaNames.Token.XsdAny, State.Any,     AnnotatedSubelements, AnyAttributes,
465                                                       new XsdInitFunction(InitAny),
466                                                       null,
467                                                       true),
468        /* Notation */                   new XsdEntry( SchemaNames.Token.XsdNotation, State.Notation,     AnnotatedSubelements, NotationAttributes,
469                                                       new XsdInitFunction(InitNotation),
470                                                       null,
471                                                       true),
472        /* SimpleType */                 new XsdEntry( SchemaNames.Token.XsdSimpleType, State.SimpleType,     SimpleTypeSubelements, SimpleTypeAttributes,
473                                                       new XsdInitFunction(InitSimpleType),
474                                                       null,
475                                                       true),
476        /* ComplexType */                new XsdEntry( SchemaNames.Token.XsdComplexType, State.ComplexType,     ComplexTypeSubelements, ComplexTypeAttributes,
477                                                       new XsdInitFunction(InitComplexType),
478                                                       null,
479                                                       true),
480        /* ComplexContent */             new XsdEntry( SchemaNames.Token.XsdComplexContent, State.ComplexContent,  ComplexContentSubelements, ComplexContentAttributes,
481                                                       new XsdInitFunction(InitComplexContent),
482                                                       null,
483                                                       true),
484        /* ComplexContentRestriction */    new XsdEntry( SchemaNames.Token.XsdComplexContentRestriction, State.ComplexContentRestriction,  ComplexContentRestrictionSubelements, ComplexContentRestrictionAttributes,
485                                                       new XsdInitFunction(InitComplexContentRestriction),
486                                                       null,
487                                                       true),
488        /* ComplexContentExtension */  new XsdEntry( SchemaNames.Token.XsdComplexContentExtension, State.ComplexContentExtension,  ComplexContentExtensionSubelements, ComplexContentExtensionAttributes,
489                                                       new XsdInitFunction(InitComplexContentExtension),
490                                                       null,
491                                                       true),
492        /* SimpleContent */              new XsdEntry( SchemaNames.Token.XsdSimpleContent, State.SimpleContent,  SimpleContentSubelements, SimpleContentAttributes,
493                                                       new XsdInitFunction(InitSimpleContent),
494                                                       null,
495                                                       true),
496        /* SimpleContentExtension */     new XsdEntry( SchemaNames.Token.XsdSimpleContentExtension, State.SimpleContentExtension,  SimpleContentExtensionSubelements, SimpleContentExtensionAttributes,
497                                                       new XsdInitFunction(InitSimpleContentExtension),
498                                                       null,
499                                                       true),
500        /* SimpleContentRestriction */   new XsdEntry( SchemaNames.Token.XsdSimpleContentRestriction, State.SimpleContentRestriction,  SimpleContentRestrictionSubelements, SimpleContentRestrictionAttributes,
501                                                       new XsdInitFunction(InitSimpleContentRestriction),
502                                                       null,
503                                                       true),
504        /* SimpleTypeUnion */            new XsdEntry( SchemaNames.Token.XsdSimpleTypeUnion, State.SimpleTypeUnion,    SimpleTypeUnionSubelements, SimpleTypeUnionAttributes,
505                                                       new XsdInitFunction(InitSimpleTypeUnion),
506                                                       null,
507                                                       true),
508        /* SimpleTypeList */             new XsdEntry( SchemaNames.Token.XsdSimpleTypeList, State.SimpleTypeList,     SimpleTypeListSubelements, SimpleTypeListAttributes,
509                                                       new XsdInitFunction(InitSimpleTypeList),
510                                                       null,
511                                                       true),
512        /* SimpleTypeRestriction */      new XsdEntry( SchemaNames.Token.XsdSimpleTypeRestriction, State.SimpleTypeRestriction,  SimpleTypeRestrictionSubelements, SimpleTypeRestrictionAttributes,
513                                                       new XsdInitFunction(InitSimpleTypeRestriction),
514                                                       null,
515                                                       true),
516        /* Unique */                     new XsdEntry( SchemaNames.Token.XsdUnique,  State.Unique,    IdentityConstraintSubelements, IdentityConstraintAttributes,
517                                                       new XsdInitFunction(InitIdentityConstraint),
518                                                       null,
519                                                       true),
520        /* Key */                        new XsdEntry( SchemaNames.Token.XsdKey, State.Key,        IdentityConstraintSubelements, IdentityConstraintAttributes,
521                                                       new XsdInitFunction(InitIdentityConstraint),
522                                                       null,
523                                                       true),
524        /* KeyRef */                     new XsdEntry( SchemaNames.Token.XsdKeyref, State.KeyRef,     IdentityConstraintSubelements, IdentityConstraintAttributes,
525                                                       new XsdInitFunction(InitIdentityConstraint),
526                                                       null,
527                                                       true),
528        /* Selector */                   new XsdEntry( SchemaNames.Token.XsdSelector, State.Selector,     AnnotatedSubelements, SelectorAttributes,
529                                                       new XsdInitFunction(InitSelector),
530                                                       null,
531                                                       true),
532        /* Field */                      new XsdEntry( SchemaNames.Token.XsdField, State.Field,     AnnotatedSubelements, FieldAttributes,
533                                                       new XsdInitFunction(InitField),
534                                                       null,
535                                                       true),
536        /* MinExclusive */               new XsdEntry( SchemaNames.Token.XsdMinExclusive, State.MinExclusive,     AnnotatedSubelements, FacetAttributes,
537                                                       new XsdInitFunction(InitFacet),
538                                                       null,
539                                                       true),
540        /* MinInclusive */               new XsdEntry( SchemaNames.Token.XsdMinInclusive, State.MinInclusive,     AnnotatedSubelements, FacetAttributes,
541                                                       new XsdInitFunction(InitFacet),
542                                                       null,
543                                                       true),
544        /* MaxExclusive */               new XsdEntry( SchemaNames.Token.XsdMaxExclusive, State.MaxExclusive,     AnnotatedSubelements, FacetAttributes,
545                                                       new XsdInitFunction(InitFacet),
546                                                       null,
547                                                       true),
548        /* MaxInclusive */               new XsdEntry( SchemaNames.Token.XsdMaxInclusive, State.MaxInclusive,     AnnotatedSubelements, FacetAttributes,
549                                                       new XsdInitFunction(InitFacet),
550                                                       null,
551                                                       true),
552        /* TotalDigits */                new XsdEntry( SchemaNames.Token.XsdTotalDigits, State.TotalDigits,     AnnotatedSubelements, FacetAttributes,
553                                                       new XsdInitFunction(InitFacet),
554                                                       null,
555                                                       true),
556        /* FractionDigits */             new XsdEntry( SchemaNames.Token.XsdFractionDigits, State.FractionDigits,     AnnotatedSubelements, FacetAttributes,
557                                                       new XsdInitFunction(InitFacet),
558                                                       null,
559                                                       true),
560        /* Length */                     new XsdEntry( SchemaNames.Token.XsdLength, State.Length,     AnnotatedSubelements, FacetAttributes,
561                                                       new XsdInitFunction(InitFacet),
562                                                       null,
563                                                       true),
564        /* MinLength */                  new XsdEntry( SchemaNames.Token.XsdMinLength, State.MinLength,     AnnotatedSubelements, FacetAttributes,
565                                                       new XsdInitFunction(InitFacet),
566                                                       null,
567                                                       true),
568        /* MaxLength */                  new XsdEntry( SchemaNames.Token.XsdMaxLength, State.MaxLength,     AnnotatedSubelements, FacetAttributes,
569                                                       new XsdInitFunction(InitFacet),
570                                                       null,
571                                                       true),
572        /* Enumeration */                new XsdEntry( SchemaNames.Token.XsdEnumeration, State.Enumeration,    AnnotatedSubelements, FacetAttributes,
573                                                       new XsdInitFunction(InitFacet),
574                                                       null,
575                                                       true),
576        /* Pattern */                    new XsdEntry( SchemaNames.Token.XsdPattern, State.Pattern,    AnnotatedSubelements, FacetAttributes,
577                                                       new XsdInitFunction(InitFacet),
578                                                       null,
579                                                       true),
580        /* WhiteSpace */                 new XsdEntry( SchemaNames.Token.XsdWhitespace, State.WhiteSpace, AnnotatedSubelements, FacetAttributes,
581                                                       new XsdInitFunction(InitFacet),
582                                                       null,
583                                                       true),
584        /* AppInfo */                    new XsdEntry( SchemaNames.Token.XsdAppInfo, State.AppInfo,    null, AppinfoAttributes,
585                                                       new XsdInitFunction(InitAppinfo),
586                                                       new XsdEndChildFunction(EndAppinfo),
587                                                       false),
588        /* Documentation */              new XsdEntry( SchemaNames.Token.XsdDocumentation, State.Documentation,    null, DocumentationAttributes,
589                                                       new XsdInitFunction(InitDocumentation),
590                                                       new XsdEndChildFunction(EndDocumentation),
591                                                       false),
592        /* Redefine */                   new XsdEntry( SchemaNames.Token.XsdRedefine, State.Redefine,    RedefineSubelements, RedefineAttributes,
593                                                       new XsdInitFunction(InitRedefine),
594                                                       new XsdEndChildFunction(EndRedefine),
595                                                       true)
596         };
597 
598         //
599         // for 'block' and 'final' attribute values
600         //
601         private static readonly int[]    DerivationMethodValues = {
602             (int)XmlSchemaDerivationMethod.Substitution,
603             (int)XmlSchemaDerivationMethod.Extension,
604             (int)XmlSchemaDerivationMethod.Restriction,
605             (int)XmlSchemaDerivationMethod.List,
606             (int)XmlSchemaDerivationMethod.Union,
607             (int)XmlSchemaDerivationMethod.All,
608         };
609         private static readonly string[] DerivationMethodStrings = {
610             "substitution",
611             "extension",
612             "restriction",
613             "list",
614             "union",
615             "#all",
616         };
617 
618         private static readonly string[] FormStringValues = { "qualified", "unqualified"};
619         private static readonly string[] UseStringValues = { "optional", "prohibited", "required" };
620         private static readonly string[] ProcessContentsStringValues = {"skip", "lax", "strict"};
621 
622         private XmlReader reader;
623         private PositionInfo positionInfo;
624         private XsdEntry currentEntry;
625         private XsdEntry nextEntry;
626         private bool hasChild;
627         private HWStack stateHistory = new HWStack(STACK_INCREMENT);
628         private Stack containerStack = new Stack();
629         private XmlNameTable nameTable;
630         private SchemaNames schemaNames;
631         private XmlNamespaceManager namespaceManager;
632         private bool canIncludeImport;
633 
634         private XmlSchema schema;
635         private XmlSchemaObject xso;
636         private XmlSchemaElement element;
637         private XmlSchemaAny anyElement;
638         private XmlSchemaAttribute attribute;
639         private XmlSchemaAnyAttribute anyAttribute;
640         private XmlSchemaComplexType complexType;
641         private XmlSchemaSimpleType simpleType;
642         private XmlSchemaComplexContent complexContent;
643         private XmlSchemaComplexContentExtension complexContentExtension;
644         private XmlSchemaComplexContentRestriction complexContentRestriction;
645         private XmlSchemaSimpleContent simpleContent;
646         private XmlSchemaSimpleContentExtension simpleContentExtension;
647         private XmlSchemaSimpleContentRestriction simpleContentRestriction;
648         private XmlSchemaSimpleTypeUnion simpleTypeUnion;
649         private XmlSchemaSimpleTypeList simpleTypeList;
650         private XmlSchemaSimpleTypeRestriction simpleTypeRestriction;
651         private XmlSchemaGroup group;
652         private XmlSchemaGroupRef groupRef;
653         private XmlSchemaAll all;
654         private XmlSchemaChoice choice;
655         private XmlSchemaSequence sequence;
656         private XmlSchemaParticle particle;
657         private XmlSchemaAttributeGroup attributeGroup;
658         private XmlSchemaAttributeGroupRef attributeGroupRef;
659         private XmlSchemaNotation notation;
660         private XmlSchemaIdentityConstraint identityConstraint;
661         private XmlSchemaXPath xpath;
662         private XmlSchemaInclude include;
663         private XmlSchemaImport import;
664         private XmlSchemaAnnotation annotation;
665         private XmlSchemaAppInfo appInfo;
666         private XmlSchemaDocumentation documentation;
667         private XmlSchemaFacet facet;
668         private XmlNode[] markup;
669         private XmlSchemaRedefine redefine;
670 
671         private ValidationEventHandler validationEventHandler;
672         private ArrayList unhandledAttributes = new ArrayList();
673         private Hashtable namespaces;
674 
XsdBuilder( XmlReader reader, XmlNamespaceManager curmgr, XmlSchema schema, XmlNameTable nameTable, SchemaNames schemaNames, ValidationEventHandler eventhandler )675         internal XsdBuilder(
676                            XmlReader reader,
677                            XmlNamespaceManager curmgr,
678                            XmlSchema schema,
679                            XmlNameTable nameTable,
680                            SchemaNames schemaNames,
681                            ValidationEventHandler eventhandler
682                            ) {
683             this.reader = reader;
684             this.xso = this.schema = schema;
685             this.namespaceManager = new BuilderNamespaceManager(curmgr, reader);
686             this.validationEventHandler = eventhandler;
687             this.nameTable = nameTable;
688             this.schemaNames = schemaNames;
689             this.stateHistory = new HWStack(STACK_INCREMENT);
690             this.currentEntry = SchemaEntries[0];
691             positionInfo = PositionInfo.GetPositionInfo(reader);
692         }
693 
ProcessElement(string prefix, string name, string ns)694         internal override bool ProcessElement(string prefix, string name, string ns) {
695             XmlQualifiedName qname = new XmlQualifiedName(name, ns);
696             if (GetNextState(qname)) {
697                 Push();
698                 Debug.Assert(this.currentEntry.InitFunc != null);
699                 xso = null;
700                 this.currentEntry.InitFunc(this, null);
701                 Debug.Assert(xso != null);
702                 RecordPosition();
703             }
704             else {
705                 if (!IsSkipableElement(qname)) {
706                     SendValidationEvent(Res.Sch_UnsupportedElement, qname.ToString());
707                 }
708                 return false;
709             }
710             return true;
711         }
712 
ProcessAttribute(string prefix, string name, string ns, string value)713         internal override void ProcessAttribute(string prefix, string name, string ns, string value) {
714             XmlQualifiedName qname = new XmlQualifiedName(name, ns);
715             if (this.currentEntry.Attributes != null) {
716                 for (int i = 0; i < this.currentEntry.Attributes.Length; i++) {
717                     XsdAttributeEntry a = this.currentEntry.Attributes[i];
718                     if (this.schemaNames.TokenToQName[(int)a.Attribute].Equals(qname)) {
719                         try {
720                             a.BuildFunc(this, value);
721                         }
722                         catch (XmlSchemaException e) {
723                             e.SetSource(this.reader.BaseURI, this.positionInfo.LineNumber, this.positionInfo.LinePosition);
724                             SendValidationEvent(Res.Sch_InvalidXsdAttributeDatatypeValue, new string[] {name, e.Message},XmlSeverityType.Error);
725                         }
726                         return;
727                     }
728                 }
729             }
730 
731             // Check non-supported attribute
732             if ((ns != this.schemaNames.NsXs) && (ns.Length != 0)) {
733                 if (ns == this.schemaNames.NsXmlNs) {
734                     if (this.namespaces == null) {
735                         this.namespaces = new Hashtable();
736                     }
737                     this.namespaces.Add((name == this.schemaNames.QnXmlNs.Name) ? string.Empty : name, value);
738                 }
739                 else {
740                     XmlAttribute attribute = new XmlAttribute(prefix, name, ns, this.schema.Document);
741                     attribute.Value = value;
742                     this.unhandledAttributes.Add(attribute);
743                 }
744             }
745             else {
746                 SendValidationEvent(Res.Sch_UnsupportedAttribute, qname.ToString());
747             }
748         }
749 
IsContentParsed()750         internal override bool IsContentParsed() {
751             return this.currentEntry.ParseContent;
752         }
753 
ProcessMarkup(XmlNode[] markup)754         internal override void ProcessMarkup(XmlNode[] markup) {
755             this.markup = markup;
756         }
757 
ProcessCData(string value)758         internal override void ProcessCData(string value) {
759             SendValidationEvent(Res.Sch_TextNotAllowed, value);
760         }
761 
StartChildren()762         internal override void StartChildren() {
763             if (this.xso != null ) {
764                 if (this.namespaces != null && this.namespaces.Count > 0) {
765                     this.xso.Namespaces.Namespaces = this.namespaces;
766                     this.namespaces = null;
767                 }
768                 if (this.unhandledAttributes.Count != 0) {
769                     this.xso.SetUnhandledAttributes((XmlAttribute[])this.unhandledAttributes.ToArray(typeof(System.Xml.XmlAttribute)));
770                     this.unhandledAttributes.Clear();
771                 }
772             }
773         }
774 
EndChildren()775         internal override void EndChildren() {
776             if (this.currentEntry.EndChildFunc != null) {
777                 (this.currentEntry.EndChildFunc)(this);
778             }
779             Pop();
780         }
781 
782 
783         // State stack push & pop
Push()784         private void Push() {
785             this.stateHistory.Push();
786             this.stateHistory[this.stateHistory.Length - 1] = this.currentEntry;
787             containerStack.Push(GetContainer(this.currentEntry.CurrentState));
788             this.currentEntry = this.nextEntry;
789             if (this.currentEntry.Name != SchemaNames.Token.XsdAnnotation) {
790                 this.hasChild = false;
791             }
792         }
793 
Pop()794         private void Pop() {
795             this.currentEntry = (XsdEntry)this.stateHistory.Pop();
796             SetContainer(this.currentEntry.CurrentState, containerStack.Pop());
797             this.hasChild = true;
798         }
799 
800         private SchemaNames.Token CurrentElement {
801             get { return this.currentEntry.Name;}
802         }
803 
804         private SchemaNames.Token ParentElement {
805             get { return((XsdEntry)this.stateHistory[this.stateHistory.Length - 1]).Name;}
806         }
807 
808         private XmlSchemaObject ParentContainer {
809             get { return (XmlSchemaObject)containerStack.Peek(); }
810         }
811 
GetContainer(State state)812         private XmlSchemaObject GetContainer(State state) {
813             XmlSchemaObject container = null;
814             switch (state) {
815                 case State.Root:
816                     break;
817                 case State.Schema:
818                     container = this.schema;
819                     break;
820                 case State.Annotation:
821                     container = this.annotation;
822                     break;
823                 case State.Include:
824                     container = this.include;
825                     break;
826                 case State.Import:
827                     container = this.import;
828                     break;
829                 case State.Element:
830                     container = this.element;
831                     break;
832                 case State.Attribute:
833                     container = this.attribute;
834                     break;
835                 case State.AttributeGroup:
836                     container = this.attributeGroup;
837                     break;
838                 case State.AttributeGroupRef:
839                     container = this.attributeGroupRef;
840                     break;
841                 case State.AnyAttribute:
842                     container = this.anyAttribute;
843                     break;
844                 case State.Group:
845                     container = this.group;
846                     break;
847                 case State.GroupRef:
848                     container = this.groupRef;
849                     break;
850                 case State.All:
851                     container = this.all;
852                     break;
853                 case State.Choice:
854                     container = this.choice;
855                     break;
856                 case State.Sequence:
857                     container = this.sequence;
858                     break;
859                 case State.Any:
860                     container = this.anyElement;
861                     break;
862                 case State.Notation:
863                     container = this.notation;
864                     break;
865                 case State.SimpleType:
866                     container = this.simpleType;
867                     break;
868                 case State.ComplexType:
869                     container = this.complexType;
870                     break;
871                 case State.ComplexContent:
872                     container = this.complexContent;
873                     break;
874                 case State.ComplexContentExtension:
875                     container = this.complexContentExtension;
876                     break;
877                 case State.ComplexContentRestriction:
878                     container = this.complexContentRestriction;
879                     break;
880                 case State.SimpleContent:
881                     container = this.simpleContent;
882                     break;
883                 case State.SimpleContentExtension:
884                     container = this.simpleContentExtension;
885                     break;
886                 case State.SimpleContentRestriction:
887                     container = this.simpleContentRestriction;
888                     break;
889                 case State.SimpleTypeUnion:
890                     container = this.simpleTypeUnion;
891                     break;
892                 case State.SimpleTypeList:
893                     container = this.simpleTypeList;
894                     break;
895                 case State.SimpleTypeRestriction:
896                     container = this.simpleTypeRestriction;
897                     break;
898                 case State.Unique:
899                 case State.Key:
900                 case State.KeyRef:
901                     container = this.identityConstraint;
902                     break;
903                 case State.Selector:
904                 case State.Field:
905                     container = this.xpath;
906                     break;
907                 case State.MinExclusive:
908                 case State.MinInclusive:
909                 case State.MaxExclusive:
910                 case State.MaxInclusive:
911                 case State.TotalDigits:
912                 case State.FractionDigits:
913                 case State.Length:
914                 case State.MinLength:
915                 case State.MaxLength:
916                 case State.Enumeration:
917                 case State.Pattern:
918                 case State.WhiteSpace:
919                     container = this.facet;
920                     break;
921                 case State.AppInfo:
922                     container = this.appInfo;
923                     break;
924                 case State.Documentation:
925                     container = this.documentation;
926                     break;
927                 case State.Redefine:
928                     container = this.redefine;
929                     break;
930                 default:
931                     Debug.Assert(false, "State is " + state);
932                     break;
933             }
934             return container;
935         }
936 
SetContainer(State state, object container)937         private void SetContainer(State state, object container) {
938             switch (state) {
939                 case State.Root:
940                     break;
941                 case State.Schema:
942                     break;
943                 case State.Annotation:
944                     this.annotation = (XmlSchemaAnnotation)container;
945                     break;
946                 case State.Include:
947                     this.include = (XmlSchemaInclude)container;
948                     break;
949                 case State.Import:
950                     this.import = (XmlSchemaImport)container;
951                     break;
952                 case State.Element:
953                     this.element = (XmlSchemaElement)container;
954                     break;
955                 case State.Attribute:
956                     this.attribute = (XmlSchemaAttribute)container;
957                     break;
958                 case State.AttributeGroup:
959                     this.attributeGroup = (XmlSchemaAttributeGroup)container;
960                     break;
961                 case State.AttributeGroupRef:
962                     this.attributeGroupRef = (XmlSchemaAttributeGroupRef)container;
963                     break;
964                 case State.AnyAttribute:
965                     this.anyAttribute = (XmlSchemaAnyAttribute)container;
966                     break;
967                 case State.Group:
968                     this.group = (XmlSchemaGroup)container;
969                     break;
970                 case State.GroupRef:
971                     this.groupRef = (XmlSchemaGroupRef)container;
972                     break;
973                 case State.All:
974                     this.all = (XmlSchemaAll)container;
975                     break;
976                 case State.Choice:
977                     this.choice = (XmlSchemaChoice)container;
978                     break;
979                 case State.Sequence:
980                     this.sequence = (XmlSchemaSequence)container;
981                     break;
982                 case State.Any:
983                     this.anyElement = (XmlSchemaAny)container;
984                     break;
985                 case State.Notation:
986                     this.notation = (XmlSchemaNotation)container;
987                     break;
988                 case State.SimpleType:
989                     this.simpleType = (XmlSchemaSimpleType)container;
990                     break;
991                 case State.ComplexType:
992                     this.complexType = (XmlSchemaComplexType)container;
993                     break;
994                 case State.ComplexContent:
995                     this.complexContent = (XmlSchemaComplexContent)container;
996                     break;
997                 case State.ComplexContentExtension:
998                     this.complexContentExtension = (XmlSchemaComplexContentExtension)container;
999                     break;
1000                 case State.ComplexContentRestriction:
1001                     this.complexContentRestriction = (XmlSchemaComplexContentRestriction)container;
1002                     break;
1003                 case State.SimpleContent:
1004                     this.simpleContent = (XmlSchemaSimpleContent)container;
1005                     break;
1006                 case State.SimpleContentExtension:
1007                     this.simpleContentExtension = (XmlSchemaSimpleContentExtension)container;
1008                     break;
1009                 case State.SimpleContentRestriction:
1010                     this.simpleContentRestriction = (XmlSchemaSimpleContentRestriction)container;
1011                     break;
1012                 case State.SimpleTypeUnion:
1013                     this.simpleTypeUnion = (XmlSchemaSimpleTypeUnion)container;
1014                     break;
1015                 case State.SimpleTypeList:
1016                     this.simpleTypeList = (XmlSchemaSimpleTypeList)container;
1017                     break;
1018                 case State.SimpleTypeRestriction:
1019                     this.simpleTypeRestriction = (XmlSchemaSimpleTypeRestriction)container;
1020                     break;
1021                 case State.Unique:
1022                 case State.Key:
1023                 case State.KeyRef:
1024                     this.identityConstraint = (XmlSchemaIdentityConstraint)container;
1025                     break;
1026                 case State.Selector:
1027                 case State.Field:
1028                     this.xpath = (XmlSchemaXPath)container;
1029                     break;
1030                 case State.MinExclusive:
1031                 case State.MinInclusive:
1032                 case State.MaxExclusive:
1033                 case State.MaxInclusive:
1034                 case State.TotalDigits:
1035                 case State.FractionDigits:
1036                 case State.Length:
1037                 case State.MinLength:
1038                 case State.MaxLength:
1039                 case State.Enumeration:
1040                 case State.Pattern:
1041                 case State.WhiteSpace:
1042                     this.facet = (XmlSchemaFacet)container;
1043                     break;
1044                 case State.AppInfo:
1045                     this.appInfo = (XmlSchemaAppInfo)container;
1046                     break;
1047                 case State.Documentation:
1048                     this.documentation = (XmlSchemaDocumentation)container;
1049                     break;
1050                 case State.Redefine:
1051                     this.redefine = (XmlSchemaRedefine)container;
1052                     break;
1053                 default:
1054                     Debug.Assert(false, "State is " + state);
1055                     break;
1056             }
1057         }
1058 
1059         /////////////////////////////////////////////////////////////////////////////////////////////////////////
1060         // XSD Schema
1061         //
1062 
BuildAnnotated_Id(XsdBuilder builder, string value)1063         private static void BuildAnnotated_Id(XsdBuilder builder, string value) {
1064             builder.xso.IdAttribute = value;
1065         }
1066 
1067         /*
1068             <schema
1069               attributeFormDefault = qualified | unqualified : unqualified
1070               blockDefault = #all or (possibly empty) subset of {substitution, extension, restriction}
1071               elementFormDefault = qualified | unqualified : unqualified
1072               finalDefault = #all or (possibly empty) subset of {extension, restriction}
1073               id = ID
1074               targetNamespace = uriReference
1075               version = string
1076               {any attributes with non-schema namespace . . .}>
1077               Content: ((include | import | redefine | annotation)* , ((attribute | attributeGroup | complexType | element | group | notation | simpleType) , annotation*)*)
1078             </schema>
1079         */
1080 
BuildSchema_AttributeFormDefault(XsdBuilder builder, string value)1081         private static void BuildSchema_AttributeFormDefault(XsdBuilder builder, string value) {
1082             builder.schema.AttributeFormDefault = (XmlSchemaForm)builder.ParseEnum(value, "attributeFormDefault", FormStringValues);
1083         }
1084 
BuildSchema_ElementFormDefault(XsdBuilder builder, string value)1085         private static void BuildSchema_ElementFormDefault(XsdBuilder builder, string value) {
1086             builder.schema.ElementFormDefault = (XmlSchemaForm)builder.ParseEnum(value, "elementFormDefault", FormStringValues);
1087         }
1088 
BuildSchema_TargetNamespace(XsdBuilder builder, string value)1089         private static void BuildSchema_TargetNamespace(XsdBuilder builder, string value) {
1090             builder.schema.TargetNamespace = value;
1091         }
1092 
BuildSchema_Version(XsdBuilder builder, string value)1093         private static void BuildSchema_Version(XsdBuilder builder, string value) {
1094             builder.schema.Version = value;
1095         }
1096 
BuildSchema_FinalDefault(XsdBuilder builder, string value)1097         private static void BuildSchema_FinalDefault(XsdBuilder builder, string value) {
1098             builder.schema.FinalDefault = (XmlSchemaDerivationMethod)builder.ParseBlockFinalEnum(value, "finalDefault");
1099         }
1100 
BuildSchema_BlockDefault(XsdBuilder builder, string value)1101         private static void BuildSchema_BlockDefault(XsdBuilder builder, string value) {
1102             builder.schema.BlockDefault = (XmlSchemaDerivationMethod)builder.ParseBlockFinalEnum(value, "blockDefault");
1103         }
1104 
InitSchema(XsdBuilder builder, string value)1105         private static void InitSchema(XsdBuilder builder, string value) {
1106             builder.canIncludeImport = true;
1107             builder.xso = builder.schema;
1108         }
1109 
1110         /*
1111             <include
1112               id = ID
1113               schemaLocation = uriReference
1114               {any attributes with non-schema namespace . . .}>
1115               Content: (annotation?)
1116             </include>
1117         */
InitInclude(XsdBuilder builder, string value)1118         private static void InitInclude(XsdBuilder builder, string value) {
1119             if (!builder.canIncludeImport) {
1120                 builder.SendValidationEvent(Res.Sch_IncludeLocation, null);
1121             }
1122             builder.xso = builder.include = new XmlSchemaInclude();
1123             builder.schema.Includes.Add(builder.include);
1124         }
1125 
BuildInclude_SchemaLocation(XsdBuilder builder, string value)1126         private static void BuildInclude_SchemaLocation(XsdBuilder builder, string value) {
1127             builder.include.SchemaLocation = value;
1128         }
1129 
1130         /*
1131             <import
1132               id = ID
1133               namespace = uriReference
1134               schemaLocation = uriReference
1135               {any attributes with non-schema namespace . . .}>
1136               Content: (annotation?)
1137             </import>
1138         */
InitImport(XsdBuilder builder, string value)1139         private static void InitImport(XsdBuilder builder, string value) {
1140             if (!builder.canIncludeImport) {
1141                 builder.SendValidationEvent(Res.Sch_ImportLocation, null);
1142             }
1143             builder.xso = builder.import = new XmlSchemaImport();
1144             builder.schema.Includes.Add(builder.import);
1145         }
1146 
BuildImport_Namespace(XsdBuilder builder, string value)1147         private static void BuildImport_Namespace(XsdBuilder builder, string value) {
1148             builder.import.Namespace = value;
1149         }
1150 
BuildImport_SchemaLocation(XsdBuilder builder, string value)1151         private static void BuildImport_SchemaLocation(XsdBuilder builder, string value) {
1152             builder.import.SchemaLocation = value;
1153         }
1154 
1155         /*
1156             <redefine
1157               schemaLocation = uriReference
1158               {any attributes with non-schema namespace . . .}>
1159               Content: (annotation | (attributeGroup | complexType | group | simpleType))*
1160             </redefine>
1161         */
InitRedefine(XsdBuilder builder, string value)1162         private static void InitRedefine(XsdBuilder builder, string value) {
1163             if (!builder.canIncludeImport) {
1164                 builder.SendValidationEvent(Res.Sch_RedefineLocation, null);
1165             }
1166             builder.xso = builder.redefine = new XmlSchemaRedefine();
1167             builder.schema.Includes.Add(builder.redefine);
1168         }
1169 
BuildRedefine_SchemaLocation(XsdBuilder builder, string value)1170         private static void BuildRedefine_SchemaLocation(XsdBuilder builder, string value) {
1171             builder.redefine.SchemaLocation = value;
1172         }
1173 
EndRedefine(XsdBuilder builder)1174         private static void EndRedefine(XsdBuilder builder) {
1175             builder.canIncludeImport = true;
1176         }
1177 
1178         /*
1179             <attribute
1180               form = qualified | unqualified
1181               id = ID
1182               name = NCName
1183               ref = QName
1184               type = QName
1185               use = prohibited | optional | required | default | fixed : optional
1186               value = string
1187               {any attributes with non-schema namespace . . .}>
1188               Content: (annotation? , (simpleType?))
1189             </attribute>
1190         */
InitAttribute(XsdBuilder builder, string value)1191         private static void InitAttribute(XsdBuilder builder, string value) {
1192             builder.xso = builder.attribute = new XmlSchemaAttribute();
1193             if (builder.ParentElement == SchemaNames.Token.XsdSchema)
1194                 builder.schema.Items.Add(builder.attribute);
1195             else
1196                 builder.AddAttribute(builder.attribute);
1197             builder.canIncludeImport = false;  // disable import and include elements in schema
1198         }
1199 
BuildAttribute_Default(XsdBuilder builder, string value)1200         private static void BuildAttribute_Default(XsdBuilder builder, string value) {
1201             builder.attribute.DefaultValue = value;
1202         }
1203 
BuildAttribute_Fixed(XsdBuilder builder, string value)1204         private static void BuildAttribute_Fixed(XsdBuilder builder, string value) {
1205             builder.attribute.FixedValue = value;
1206         }
1207 
BuildAttribute_Form(XsdBuilder builder, string value)1208         private static void BuildAttribute_Form(XsdBuilder builder, string value) {
1209             builder.attribute.Form = (XmlSchemaForm)builder.ParseEnum(value, "form", FormStringValues);
1210         }
1211 
BuildAttribute_Use(XsdBuilder builder, string value)1212         private static void BuildAttribute_Use(XsdBuilder builder, string value) {
1213             builder.attribute.Use = (XmlSchemaUse)builder.ParseEnum(value, "use", UseStringValues);
1214         }
1215 
BuildAttribute_Ref(XsdBuilder builder, string value)1216         private static void BuildAttribute_Ref(XsdBuilder builder, string value) {
1217             builder.attribute.RefName = builder.ParseQName(value, "ref");
1218         }
1219 
BuildAttribute_Name(XsdBuilder builder, string value)1220         private static void BuildAttribute_Name(XsdBuilder builder, string value) {
1221             builder.attribute.Name = value;
1222         }
1223 
BuildAttribute_Type(XsdBuilder builder, string value)1224         private static void BuildAttribute_Type(XsdBuilder builder, string value) {
1225             builder.attribute.SchemaTypeName = builder.ParseQName(value, "type");
1226         }
1227 
1228         /*
1229             <element
1230               abstract = boolean : false
1231               block = #all or (possibly empty) subset of {substitution, extension, restriction}
1232               default = string
1233               final = #all or (possibly empty) subset of {extension, restriction}
1234               fixed = string
1235               form = qualified | unqualified
1236               id = ID
1237               maxOccurs = for maxOccurs : 1
1238               minOccurs = nonNegativeInteger : 1
1239               name = NCName
1240               nillable = boolean : false
1241               ref = QName
1242               substitutionGroup = QName
1243               type = QName
1244               {any attributes with non-schema namespace . . .}>
1245               Content: (annotation? , ((simpleType | complexType)? , (key | keyref | unique)*))
1246             </element>
1247         */
InitElement(XsdBuilder builder, string value)1248         private static void InitElement(XsdBuilder builder, string value) {
1249             builder.xso = builder.element = new XmlSchemaElement();
1250             builder.canIncludeImport = false;
1251             switch (builder.ParentElement) {
1252                 case SchemaNames.Token.XsdSchema:
1253                     builder.schema.Items.Add(builder.element);
1254                     break;
1255                 case SchemaNames.Token.XsdAll:
1256                     builder.all.Items.Add(builder.element);
1257                     break;
1258                 case SchemaNames.Token.XsdChoice:
1259                     builder.choice.Items.Add(builder.element);
1260                     break;
1261                 case SchemaNames.Token.XsdSequence:
1262                     builder.sequence.Items.Add(builder.element);
1263                     break;
1264                 default:
1265                     Debug.Assert(false);
1266                     break;
1267             }
1268         }
1269 
BuildElement_Abstract(XsdBuilder builder, string value)1270         private static void BuildElement_Abstract(XsdBuilder builder, string value) {
1271             builder.element.IsAbstract = builder.ParseBoolean(value, "abstract");
1272         }
1273 
BuildElement_Block(XsdBuilder builder, string value)1274         private static void BuildElement_Block(XsdBuilder builder, string value) {
1275             builder.element.Block = (XmlSchemaDerivationMethod)builder.ParseBlockFinalEnum(value, "block");
1276         }
1277 
BuildElement_Default(XsdBuilder builder, string value)1278         private static void BuildElement_Default(XsdBuilder builder, string value) {
1279             builder.element.DefaultValue = value;
1280         }
1281 
BuildElement_Form(XsdBuilder builder, string value)1282         private static void BuildElement_Form(XsdBuilder builder, string value) {
1283             builder.element.Form = (XmlSchemaForm)builder.ParseEnum(value, "form", FormStringValues);
1284         }
1285 
BuildElement_SubstitutionGroup(XsdBuilder builder, string value)1286         private static void BuildElement_SubstitutionGroup(XsdBuilder builder, string value) {
1287             builder.element.SubstitutionGroup = builder.ParseQName(value, "substitutionGroup");
1288         }
1289 
BuildElement_Final(XsdBuilder builder, string value)1290         private static void BuildElement_Final(XsdBuilder builder, string value) {
1291             builder.element.Final = (XmlSchemaDerivationMethod)builder.ParseBlockFinalEnum(value, "final");
1292         }
1293 
BuildElement_Fixed(XsdBuilder builder, string value)1294         private static void BuildElement_Fixed(XsdBuilder builder, string value) {
1295             builder.element.FixedValue = value;
1296         }
1297 
BuildElement_MaxOccurs(XsdBuilder builder, string value)1298         private static void BuildElement_MaxOccurs(XsdBuilder builder, string value) {
1299             builder.SetMaxOccurs(builder.element, value);
1300         }
1301 
BuildElement_MinOccurs(XsdBuilder builder, string value)1302         private static void BuildElement_MinOccurs(XsdBuilder builder, string value) {
1303             builder.SetMinOccurs(builder.element, value);
1304         }
1305 
BuildElement_Name(XsdBuilder builder, string value)1306         private static void BuildElement_Name(XsdBuilder builder, string value) {
1307             builder.element.Name = value;
1308         }
1309 
BuildElement_Nillable(XsdBuilder builder, string value)1310         private static void BuildElement_Nillable(XsdBuilder builder, string value) {
1311             builder.element.IsNillable = builder.ParseBoolean(value, "nillable");
1312         }
1313 
BuildElement_Ref(XsdBuilder builder, string value)1314         private static void BuildElement_Ref(XsdBuilder builder, string value) {
1315             builder.element.RefName = builder.ParseQName(value, "ref");
1316         }
1317 
BuildElement_Type(XsdBuilder builder, string value)1318         private static void BuildElement_Type(XsdBuilder builder, string value) {
1319             builder.element.SchemaTypeName = builder.ParseQName(value, "type");
1320         }
1321 
1322         /*
1323             <simpleType
1324               id = ID
1325               name = NCName
1326               {any attributes with non-schema namespace . . .}>
1327               Content: (annotation? , ((list | restriction | union)))
1328             </simpleType>
1329         */
InitSimpleType(XsdBuilder builder, string value)1330         private static void InitSimpleType(XsdBuilder builder, string value) {
1331             builder.xso = builder.simpleType = new XmlSchemaSimpleType();
1332             switch (builder.ParentElement) {
1333                 case SchemaNames.Token.XsdSchema:
1334                     builder.canIncludeImport = false;  // disable import and include elements in schema
1335                     builder.schema.Items.Add(builder.simpleType);
1336                     break;
1337                 case SchemaNames.Token.XsdRedefine:
1338                     builder.redefine.Items.Add(builder.simpleType);
1339                     break;
1340                 case SchemaNames.Token.XsdAttribute:
1341                     if (builder.attribute.SchemaType != null) {
1342                         builder.SendValidationEvent(Res.Sch_DupXsdElement, "simpleType");
1343                     }
1344                     builder.attribute.SchemaType = builder.simpleType;
1345                     break;
1346                 case SchemaNames.Token.XsdElement:
1347                     if (builder.element.SchemaType != null) {
1348                         builder.SendValidationEvent(Res.Sch_DupXsdElement, "simpleType");
1349                     }
1350                     if (builder.element.Constraints.Count != 0) {
1351                         builder.SendValidationEvent(Res.Sch_TypeAfterConstraints, null);
1352                     }
1353                     builder.element.SchemaType = builder.simpleType;
1354                     break;
1355                 case SchemaNames.Token.XsdSimpleTypeList:
1356                     if (builder.simpleTypeList.ItemType != null) {
1357                         builder.SendValidationEvent(Res.Sch_DupXsdElement, "simpleType");
1358                     }
1359                     builder.simpleTypeList.ItemType = builder.simpleType;
1360                     break;
1361                 case SchemaNames.Token.XsdSimpleTypeRestriction:
1362                     if (builder.simpleTypeRestriction.BaseType != null) {
1363                         builder.SendValidationEvent(Res.Sch_DupXsdElement, "simpleType");
1364                     }
1365                     builder.simpleTypeRestriction.BaseType = builder.simpleType;
1366                     break;
1367                 case SchemaNames.Token.XsdSimpleContentRestriction:
1368                     if (builder.simpleContentRestriction.BaseType != null) {
1369                         builder.SendValidationEvent(Res.Sch_DupXsdElement, "simpleType");
1370                     }
1371                     if (
1372                         builder.simpleContentRestriction.Attributes.Count != 0 ||
1373                         builder.simpleContentRestriction.AnyAttribute != null ||
1374                         builder.simpleContentRestriction.Facets.Count != 0
1375                     ) {
1376                         builder.SendValidationEvent(Res.Sch_SimpleTypeRestriction, null);
1377                     }
1378                     builder.simpleContentRestriction.BaseType = builder.simpleType;
1379                     break;
1380 
1381                 case SchemaNames.Token.XsdSimpleTypeUnion:
1382                     builder.simpleTypeUnion.BaseTypes.Add(builder.simpleType);
1383                     break;
1384             }
1385         }
1386 
BuildSimpleType_Name(XsdBuilder builder, string value)1387         private static void BuildSimpleType_Name(XsdBuilder builder, string value) {
1388             builder.simpleType.Name =value;
1389         }
1390 
BuildSimpleType_Final(XsdBuilder builder, string value)1391         private static void BuildSimpleType_Final(XsdBuilder builder, string value) {
1392             builder.simpleType.Final = (XmlSchemaDerivationMethod)builder.ParseBlockFinalEnum(value, "final");
1393         }
1394 
1395 
1396         /*
1397             <union
1398               id = ID
1399               memberTypes = List of [anon]
1400               {any attributes with non-schema namespace . . .}>
1401               Content: (annotation? , (simpleType*))
1402             </union>
1403         */
InitSimpleTypeUnion(XsdBuilder builder, string value)1404         private static void InitSimpleTypeUnion(XsdBuilder builder, string value) {
1405             if (builder.simpleType.Content != null) {
1406                 builder.SendValidationEvent(Res.Sch_DupSimpleTypeChild, null);
1407             }
1408             builder.xso = builder.simpleTypeUnion = new XmlSchemaSimpleTypeUnion();
1409             builder.simpleType.Content = builder.simpleTypeUnion;
1410         }
1411 
BuildSimpleTypeUnion_MemberTypes(XsdBuilder builder, string value)1412         private static void BuildSimpleTypeUnion_MemberTypes(XsdBuilder builder, string value) {
1413             XmlSchemaDatatype dt = XmlSchemaDatatype.FromXmlTokenizedTypeXsd(XmlTokenizedType.QName).DeriveByList(null);
1414             try {
1415                 builder.simpleTypeUnion.MemberTypes = (XmlQualifiedName[])dt.ParseValue(value, builder.nameTable, builder.namespaceManager);
1416             }
1417             catch (XmlSchemaException e) {
1418                 e.SetSource(builder.reader.BaseURI, builder.positionInfo.LineNumber, builder.positionInfo.LinePosition);
1419                 builder.SendValidationEvent(e);
1420             }
1421         }
1422 
1423 
1424         /*
1425             <list
1426               id = ID
1427               itemType = QName
1428               {any attributes with non-schema namespace . . .}>
1429               Content: (annotation? , (simpleType?))
1430             </list>
1431         */
InitSimpleTypeList(XsdBuilder builder, string value)1432         private static void InitSimpleTypeList(XsdBuilder builder, string value) {
1433             if (builder.simpleType.Content != null) {
1434                 builder.SendValidationEvent(Res.Sch_DupSimpleTypeChild, null);
1435             }
1436             builder.xso = builder.simpleTypeList = new XmlSchemaSimpleTypeList();
1437             builder.simpleType.Content = builder.simpleTypeList;
1438         }
1439 
BuildSimpleTypeList_ItemType(XsdBuilder builder, string value)1440         private static void BuildSimpleTypeList_ItemType(XsdBuilder builder, string value) {
1441             builder.simpleTypeList.ItemTypeName = builder.ParseQName(value, "itemType");
1442         }
1443 
1444         /*
1445             <restriction
1446               base = QName
1447               id = ID
1448               {any attributes with non-schema namespace . . .}>
1449               Content: (annotation? , (simpleType? , ((duration | encoding | enumeration | length | maxExclusive | maxInclusive | maxLength | minExclusive | minInclusive | minLength | pattern | period | TotalDigits | FractionDigits)*)))
1450             </restriction>
1451         */
InitSimpleTypeRestriction(XsdBuilder builder, string value)1452         private static void InitSimpleTypeRestriction(XsdBuilder builder, string value) {
1453             if (builder.simpleType.Content != null) {
1454                 builder.SendValidationEvent(Res.Sch_DupSimpleTypeChild, null);
1455             }
1456             builder.xso = builder.simpleTypeRestriction = new XmlSchemaSimpleTypeRestriction();
1457             builder.simpleType.Content = builder.simpleTypeRestriction;
1458         }
1459 
BuildSimpleTypeRestriction_Base(XsdBuilder builder, string value)1460         private static void BuildSimpleTypeRestriction_Base(XsdBuilder builder, string value) {
1461             builder.simpleTypeRestriction.BaseTypeName = builder.ParseQName(value, "base");
1462         }
1463 
1464         /*
1465             <complexType
1466               abstract = boolean : false
1467               block = #all or (possibly empty) subset of {extension, restriction}
1468               final = #all or (possibly empty) subset of {extension, restriction}
1469               id = ID
1470               mixed = boolean : false
1471               name = NCName
1472               {any attributes with non-schema namespace . . .}>
1473               Content: (annotation? , (simpleContent | complexContent | ((group | all | choice | sequence)? , ((attribute | attributeGroup)* , anyAttribute?))))
1474             </complexType>
1475         */
InitComplexType(XsdBuilder builder, string value)1476         private static void InitComplexType(XsdBuilder builder, string value) {
1477             builder.xso = builder.complexType = new XmlSchemaComplexType();
1478             switch (builder.ParentElement) {
1479                 case SchemaNames.Token.XsdSchema:
1480                     builder.canIncludeImport = false;  // disable import and include elements in schema
1481                     builder.schema.Items.Add(builder.complexType);
1482                     break;
1483                 case SchemaNames.Token.XsdRedefine:
1484                     builder.redefine.Items.Add(builder.complexType);
1485                     break;
1486                 case SchemaNames.Token.XsdElement:
1487                     if (builder.element.SchemaType != null) {
1488                         builder.SendValidationEvent(Res.Sch_DupElement, "complexType");
1489                     }
1490                     if (builder.element.Constraints.Count != 0) {
1491                         builder.SendValidationEvent(Res.Sch_TypeAfterConstraints, null);
1492                     }
1493                     builder.element.SchemaType = builder.complexType;
1494                     break;
1495             }
1496         }
1497 
BuildComplexType_Abstract(XsdBuilder builder, string value)1498         private static void BuildComplexType_Abstract(XsdBuilder builder, string value) {
1499             builder.complexType.IsAbstract = builder.ParseBoolean(value, "abstract");
1500         }
1501 
BuildComplexType_Block(XsdBuilder builder, string value)1502         private static void BuildComplexType_Block(XsdBuilder builder, string value) {
1503             builder.complexType.Block = (XmlSchemaDerivationMethod)builder.ParseBlockFinalEnum(value, "block");
1504         }
1505 
BuildComplexType_Final(XsdBuilder builder, string value)1506         private static void BuildComplexType_Final(XsdBuilder builder, string value) {
1507             builder.complexType.Final = (XmlSchemaDerivationMethod)builder.ParseBlockFinalEnum(value, "final");
1508         }
1509 
BuildComplexType_Mixed(XsdBuilder builder, string value)1510         private static void BuildComplexType_Mixed(XsdBuilder builder, string value) {
1511             builder.complexType.IsMixed = builder.ParseBoolean(value, "mixed");
1512         }
1513 
BuildComplexType_Name(XsdBuilder builder, string value)1514         private static void BuildComplexType_Name(XsdBuilder builder, string value) {
1515             builder.complexType.Name = value;
1516         }
1517 
1518         /*
1519             <complexContent
1520               id = ID
1521               mixed = boolean
1522               {any attributes with non-schema namespace . . .}>
1523               Content: (annotation? , (restriction | extension))
1524             </complexContent>
1525         */
InitComplexContent(XsdBuilder builder, string value)1526         private static void InitComplexContent(XsdBuilder builder, string value) {
1527             if ( (builder.complexType.ContentModel != null) ||
1528                  (builder.complexType.Particle != null || builder.complexType.Attributes.Count != 0 || builder.complexType.AnyAttribute != null)
1529                ) {
1530                  builder.SendValidationEvent(Res.Sch_ComplexTypeContentModel, "complexContent");
1531             }
1532             builder.xso = builder.complexContent = new XmlSchemaComplexContent();
1533             builder.complexType.ContentModel = builder.complexContent;
1534         }
1535 
BuildComplexContent_Mixed(XsdBuilder builder, string value)1536         private static void BuildComplexContent_Mixed(XsdBuilder builder, string value) {
1537             builder.complexContent.IsMixed = builder.ParseBoolean(value, "mixed");
1538         }
1539 
1540         /*
1541             <extension
1542               base = QName
1543               id = ID
1544               {any attributes with non-schema namespace . . .}>
1545               Content: (annotation? , ((group | all | choice | sequence)? , ((attribute | attributeGroup)* , anyAttribute?)))
1546             </extension>
1547         */
InitComplexContentExtension(XsdBuilder builder, string value)1548         private static void InitComplexContentExtension(XsdBuilder builder, string value) {
1549             if (builder.complexContent.Content != null) {
1550                 builder.SendValidationEvent(Res.Sch_ComplexContentContentModel, "extension");
1551             }
1552             builder.xso = builder.complexContentExtension = new XmlSchemaComplexContentExtension();
1553             builder.complexContent.Content = builder.complexContentExtension;
1554         }
1555 
BuildComplexContentExtension_Base(XsdBuilder builder, string value)1556         private static void BuildComplexContentExtension_Base(XsdBuilder builder, string value) {
1557             builder.complexContentExtension.BaseTypeName = builder.ParseQName(value, "base");
1558         }
1559 
1560         /*
1561             <restriction
1562               base = QName
1563               id = ID
1564               {any attributes with non-schema namespace . . .}>
1565               Content: (annotation? , (group | all | choice | sequence)? , ((attribute | attributeGroup)* , anyAttribute?))
1566             </restriction>
1567         */
InitComplexContentRestriction(XsdBuilder builder, string value)1568         private static void InitComplexContentRestriction(XsdBuilder builder, string value) {
1569             builder.xso = builder.complexContentRestriction = new XmlSchemaComplexContentRestriction();
1570             builder.complexContent.Content = builder.complexContentRestriction;
1571         }
1572 
BuildComplexContentRestriction_Base(XsdBuilder builder, string value)1573         private static void BuildComplexContentRestriction_Base(XsdBuilder builder, string value) {
1574             builder.complexContentRestriction.BaseTypeName = builder.ParseQName(value, "base");
1575         }
1576 
1577         /*
1578             <simpleContent
1579               id = ID
1580               {any attributes with non-schema namespace . . .}>
1581               Content: (annotation? , (restriction | extension))
1582             </simpleContent>
1583         */
InitSimpleContent(XsdBuilder builder, string value)1584         private static void InitSimpleContent(XsdBuilder builder, string value) {
1585             if ( (builder.complexType.ContentModel != null) ||
1586                  (builder.complexType.Particle != null || builder.complexType.Attributes.Count != 0 || builder.complexType.AnyAttribute != null)
1587                  ) {
1588                    builder.SendValidationEvent(Res.Sch_ComplexTypeContentModel, "simpleContent");
1589             }
1590             builder.xso = builder.simpleContent = new XmlSchemaSimpleContent();
1591             builder.complexType.ContentModel = builder.simpleContent;
1592         }
1593 
1594         /*
1595             <extension
1596               base = QName
1597               id = ID
1598               {any attributes with non-schema namespace . . .}>
1599               Content: (annotation? , ((attribute | attributeGroup)* , anyAttribute?))
1600             </extension>
1601         */
1602 
InitSimpleContentExtension(XsdBuilder builder, string value)1603         private static void InitSimpleContentExtension(XsdBuilder builder, string value) {
1604             if (builder.simpleContent.Content != null) {
1605                 builder.SendValidationEvent(Res.Sch_DupElement, "extension");
1606             }
1607             builder.xso = builder.simpleContentExtension = new XmlSchemaSimpleContentExtension();
1608             builder.simpleContent.Content = builder.simpleContentExtension;
1609         }
1610 
BuildSimpleContentExtension_Base(XsdBuilder builder, string value)1611         private static void BuildSimpleContentExtension_Base(XsdBuilder builder, string value) {
1612             builder.simpleContentExtension.BaseTypeName = builder.ParseQName(value, "base");
1613         }
1614 
1615 
1616         /*
1617             <restriction
1618               base = QName
1619               id = ID
1620               {any attributes with non-schema namespace . . .}>
1621               Content: (annotation? , ((duration | encoding | enumeration | length | maxExclusive | maxInclusive | maxLength | minExclusive | minInclusive | minLength | pattern | period | totalDigits | fractionDigits)*)? , ((attribute | attributeGroup)* , anyAttribute?))
1622             </restriction>
1623         */
InitSimpleContentRestriction(XsdBuilder builder, string value)1624         private static void InitSimpleContentRestriction(XsdBuilder builder, string value) {
1625             if (builder.simpleContent.Content != null) {
1626                 builder.SendValidationEvent(Res.Sch_DupElement, "restriction");
1627             }
1628             builder.xso = builder.simpleContentRestriction = new XmlSchemaSimpleContentRestriction();
1629             builder.simpleContent.Content = builder.simpleContentRestriction;
1630         }
1631 
BuildSimpleContentRestriction_Base(XsdBuilder builder, string value)1632         private static void BuildSimpleContentRestriction_Base(XsdBuilder builder, string value) {
1633             builder.simpleContentRestriction.BaseTypeName = builder.ParseQName(value, "base");
1634         }
1635 
1636         /*
1637             <attributeGroup
1638               id = ID
1639               name = NCName
1640               ref = QName
1641               {any attributes with non-schema namespace . . .}>
1642               Content: (annotation? , ((attribute | attributeGroup)* , anyAttribute?))
1643             </attributeGroup>
1644         */
InitAttributeGroup(XsdBuilder builder, string value)1645         private static void InitAttributeGroup(XsdBuilder builder, string value) {
1646             builder.canIncludeImport = false;
1647             builder.xso = builder.attributeGroup = new XmlSchemaAttributeGroup();
1648             switch (builder.ParentElement) {
1649                 case SchemaNames.Token.XsdSchema:
1650                     builder.schema.Items.Add(builder.attributeGroup);
1651                     break;
1652                 case SchemaNames.Token.XsdRedefine:
1653                     builder.redefine.Items.Add(builder.attributeGroup);
1654                     break;
1655             }
1656         }
1657 
BuildAttributeGroup_Name(XsdBuilder builder, string value)1658         private static void BuildAttributeGroup_Name(XsdBuilder builder, string value) {
1659             builder.attributeGroup.Name = value;
1660         }
1661 
1662         /*
1663             <attributeGroup
1664               id = ID
1665               ref = QName
1666               {any attributes with non-schema namespace . . .}>
1667               Content: (annotation?)
1668             </attributeGroup>
1669         */
InitAttributeGroupRef(XsdBuilder builder, string value)1670         private static void InitAttributeGroupRef(XsdBuilder builder, string value) {
1671             builder.xso = builder.attributeGroupRef = new XmlSchemaAttributeGroupRef();
1672             builder.AddAttribute(builder.attributeGroupRef);
1673         }
1674 
BuildAttributeGroupRef_Ref(XsdBuilder builder, string value)1675         private static void BuildAttributeGroupRef_Ref(XsdBuilder builder, string value) {
1676             builder.attributeGroupRef.RefName = builder.ParseQName(value, "ref");
1677         }
1678 
1679         /*
1680             <anyAttribute
1681               id = ID
1682               namespace = ##any | ##other | list of {uri, ##targetNamespace, ##local} : ##any
1683               processContents = skip | lax | strict : strict
1684               {any attributes with non-schema namespace . . .}>
1685               Content: (annotation?)
1686             </anyAttribute>
1687         */
InitAnyAttribute(XsdBuilder builder, string value)1688         private static void InitAnyAttribute(XsdBuilder builder, string value) {
1689             builder.xso = builder.anyAttribute = new XmlSchemaAnyAttribute();
1690             switch (builder.ParentElement) {
1691                 case SchemaNames.Token.XsdComplexType:
1692                     if (builder.complexType.ContentModel != null) {
1693                         builder.SendValidationEvent(Res.Sch_AttributeMutuallyExclusive, "anyAttribute");
1694                     }
1695                     if (builder.complexType.AnyAttribute != null) {
1696                         builder.SendValidationEvent(Res.Sch_DupElement, "anyAttribute");
1697                     }
1698                     builder.complexType.AnyAttribute = builder.anyAttribute;
1699                     break;
1700                 case SchemaNames.Token.XsdSimpleContentRestriction:
1701                     if (builder.simpleContentRestriction.AnyAttribute != null) {
1702                         builder.SendValidationEvent(Res.Sch_DupElement, "anyAttribute");
1703                     }
1704                     builder.simpleContentRestriction.AnyAttribute = builder.anyAttribute;
1705                     break;
1706                 case SchemaNames.Token.XsdSimpleContentExtension:
1707                     if (builder.simpleContentExtension.AnyAttribute != null) {
1708                         builder.SendValidationEvent(Res.Sch_DupElement, "anyAttribute");
1709                     }
1710                     builder.simpleContentExtension.AnyAttribute = builder.anyAttribute;
1711                     break;
1712                 case SchemaNames.Token.XsdComplexContentExtension:
1713                     if (builder.complexContentExtension.AnyAttribute != null) {
1714                         builder.SendValidationEvent(Res.Sch_DupElement, "anyAttribute");
1715                     }
1716                     builder.complexContentExtension.AnyAttribute = builder.anyAttribute;
1717                     break;
1718                 case SchemaNames.Token.XsdComplexContentRestriction:
1719                     if (builder.complexContentRestriction.AnyAttribute != null) {
1720                         builder.SendValidationEvent(Res.Sch_DupElement, "anyAttribute");
1721                     }
1722                     builder.complexContentRestriction.AnyAttribute = builder.anyAttribute;
1723                     break;
1724                 case SchemaNames.Token.xsdAttributeGroup:
1725                     if (builder.attributeGroup.AnyAttribute != null) {
1726                         builder.SendValidationEvent(Res.Sch_DupElement, "anyAttribute");
1727                     }
1728                     builder.attributeGroup.AnyAttribute = builder.anyAttribute;
1729                     break;
1730             }
1731         }
1732 
BuildAnyAttribute_Namespace(XsdBuilder builder, string value)1733         private static void BuildAnyAttribute_Namespace(XsdBuilder builder, string value) {
1734             builder.anyAttribute.Namespace = value;
1735         }
1736 
BuildAnyAttribute_ProcessContents(XsdBuilder builder, string value)1737         private static void BuildAnyAttribute_ProcessContents(XsdBuilder builder, string value) {
1738             builder.anyAttribute.ProcessContents = (XmlSchemaContentProcessing)builder.ParseEnum(value, "processContents", ProcessContentsStringValues);
1739         }
1740 
1741         /*
1742             <group
1743               id = ID
1744               name = NCName
1745               {any attributes with non-schema namespace . . .}>
1746               Content: (annotation? , (all | choice | sequence)?)
1747             </group>
1748         */
InitGroup(XsdBuilder builder, string value)1749         private static void InitGroup(XsdBuilder builder, string value) {
1750             builder.xso = builder.group = new XmlSchemaGroup();
1751             builder.canIncludeImport = false;  // disable import and include elements in schema
1752             switch (builder.ParentElement) {
1753                 case SchemaNames.Token.XsdSchema:
1754                     builder.schema.Items.Add(builder.group);
1755                     break;
1756                 case SchemaNames.Token.XsdRedefine:
1757                     builder.redefine.Items.Add(builder.group);
1758                     break;
1759             }
1760         }
1761 
BuildGroup_Name(XsdBuilder builder, string value)1762         private static void BuildGroup_Name(XsdBuilder builder, string value) {
1763             builder.group.Name = value;
1764         }
1765 
1766         /*
1767             <group
1768               id = ID
1769               maxOccurs = for maxOccurs : 1
1770               minOccurs = nonNegativeInteger : 1
1771               ref = QName
1772               {any attributes with non-schema namespace . . .}>
1773               Content: (annotation?)
1774             </group>
1775         */
InitGroupRef(XsdBuilder builder, string value)1776         private static void InitGroupRef(XsdBuilder builder, string value) {
1777             builder.xso = builder.particle = builder.groupRef = new XmlSchemaGroupRef();
1778             builder.AddParticle(builder.groupRef);
1779         }
1780 
BuildParticle_MaxOccurs(XsdBuilder builder, string value)1781         private static void BuildParticle_MaxOccurs(XsdBuilder builder, string value) {
1782             builder.SetMaxOccurs(builder.particle, value);
1783         }
1784 
BuildParticle_MinOccurs(XsdBuilder builder, string value)1785         private static void BuildParticle_MinOccurs(XsdBuilder builder, string value) {
1786             builder.SetMinOccurs(builder.particle, value);
1787         }
1788 
BuildGroupRef_Ref(XsdBuilder builder, string value)1789         private static void BuildGroupRef_Ref(XsdBuilder builder, string value) {
1790             builder.groupRef.RefName = builder.ParseQName(value, "ref");
1791         }
1792 
1793         /*
1794             <all
1795               id = ID
1796               maxOccurs = for maxOccurs : 1
1797               minOccurs = nonNegativeInteger : 1
1798               {any attributes with non-schema namespace . . .}>
1799               Content: (annotation? , element*)
1800             </all>
1801         */
InitAll(XsdBuilder builder, string value)1802         private static void InitAll(XsdBuilder builder, string value) {
1803             builder.xso = builder.particle = builder.all = new XmlSchemaAll();
1804             builder.AddParticle(builder.all);
1805         }
1806 
1807         /*
1808             <choice
1809               id = ID
1810               maxOccurs = for maxOccurs : 1
1811               minOccurs = nonNegativeInteger : 1
1812               {any attributes with non-schema namespace . . .}>
1813               Content: (annotation? , (element | group | choice | sequence | any)*)
1814             </choice>
1815         */
InitChoice(XsdBuilder builder, string value)1816         private static void InitChoice(XsdBuilder builder, string value) {
1817             builder.xso = builder.particle = builder.choice = new XmlSchemaChoice();
1818             builder.AddParticle(builder.choice);
1819         }
1820 
1821         /*
1822              <sequence
1823               id = ID
1824               maxOccurs = for maxOccurs : 1
1825               minOccurs = nonNegativeInteger : 1
1826               {any attributes with non-schema namespace . . .}>
1827               Content: (annotation? , (element | group | choice | sequence | any)*)
1828             </sequence>
1829         */
InitSequence(XsdBuilder builder, string value)1830         private static void InitSequence(XsdBuilder builder, string value) {
1831             builder.xso = builder.particle = builder.sequence = new XmlSchemaSequence();
1832             builder.AddParticle(builder.sequence);
1833         }
1834 
1835         /*
1836             <any
1837               id = ID
1838               maxOccurs = for maxOccurs : 1
1839               minOccurs = nonNegativeInteger : 1
1840               namespace = ##any | ##other | list of {uri, ##targetNamespace, ##local} : ##any
1841               processContents = skip | lax | strict : strict
1842               {any attributes with non-schema namespace . . .}>
1843               Content: (annotation?)
1844             </any>
1845         */
InitAny(XsdBuilder builder, string value)1846         private static void InitAny(XsdBuilder builder, string value) {
1847             builder.xso = builder.particle = builder.anyElement = new XmlSchemaAny();
1848             builder.AddParticle(builder.anyElement);
1849         }
1850 
BuildAny_Namespace(XsdBuilder builder, string value)1851         private static void BuildAny_Namespace(XsdBuilder builder, string value) {
1852             builder.anyElement.Namespace = value;
1853         }
1854 
BuildAny_ProcessContents(XsdBuilder builder, string value)1855         private static void BuildAny_ProcessContents(XsdBuilder builder, string value) {
1856             builder.anyElement.ProcessContents = (XmlSchemaContentProcessing)builder.ParseEnum(value, "processContents", ProcessContentsStringValues);
1857         }
1858 
1859         /*
1860             <notation
1861               id = ID
1862               name = NCName
1863               public = A public identifier, per ISO 8879
1864               system = uriReference
1865               {any attributes with non-schema namespace . . .}>
1866               Content: (annotation?)
1867             </notation>
1868         */
InitNotation(XsdBuilder builder, string value)1869         private static void InitNotation(XsdBuilder builder, string value) {
1870             builder.xso = builder.notation = new XmlSchemaNotation();
1871             builder.canIncludeImport = false;
1872             builder.schema.Items.Add(builder.notation);
1873         }
1874 
BuildNotation_Name(XsdBuilder builder, string value)1875         private static void BuildNotation_Name(XsdBuilder builder, string value) {
1876             builder.notation.Name = value;
1877         }
1878 
BuildNotation_Public(XsdBuilder builder, string value)1879         private static void BuildNotation_Public(XsdBuilder builder, string value) {
1880             builder.notation.Public = value;
1881         }
1882 
BuildNotation_System(XsdBuilder builder, string value)1883         private static void BuildNotation_System(XsdBuilder builder, string value) {
1884             builder.notation.System = value;
1885         }
1886 
1887         //
1888         // Facets
1889         //
1890         /*
1891             <duration
1892               id = ID
1893               value = timeDuration
1894               fixed = boolean : false>
1895               Content: (annotation?)
1896             </duration>
1897         */
InitFacet(XsdBuilder builder, string value)1898         private static void InitFacet(XsdBuilder builder, string value) {
1899             switch (builder.CurrentElement) {
1900                 case SchemaNames.Token.XsdEnumeration:
1901                     builder.facet = new XmlSchemaEnumerationFacet();
1902                     break;
1903                 case SchemaNames.Token.XsdLength:
1904                     builder.facet = new XmlSchemaLengthFacet();
1905                     break;
1906                 case SchemaNames.Token.XsdMaxExclusive:
1907                     builder.facet = new XmlSchemaMaxExclusiveFacet();
1908                     break;
1909                 case SchemaNames.Token.XsdMaxInclusive:
1910                     builder.facet = new XmlSchemaMaxInclusiveFacet();
1911                     break;
1912                 case SchemaNames.Token.XsdMaxLength:
1913                     builder.facet = new XmlSchemaMaxLengthFacet();
1914                     break;
1915                 case SchemaNames.Token.XsdMinExclusive:
1916                     builder.facet = new XmlSchemaMinExclusiveFacet();
1917                     break;
1918                 case SchemaNames.Token.XsdMinInclusive:
1919                     builder.facet = new XmlSchemaMinInclusiveFacet();
1920                     break;
1921                 case SchemaNames.Token.XsdMinLength:
1922                     builder.facet = new XmlSchemaMinLengthFacet();
1923                     break;
1924                 case SchemaNames.Token.XsdPattern:
1925                     builder.facet = new XmlSchemaPatternFacet();
1926                     break;
1927                 case SchemaNames.Token.XsdTotalDigits:
1928                     builder.facet = new XmlSchemaTotalDigitsFacet();
1929                     break;
1930                 case SchemaNames.Token.XsdFractionDigits:
1931                     builder.facet = new XmlSchemaFractionDigitsFacet();
1932                     break;
1933                 case SchemaNames.Token.XsdWhitespace:
1934                     builder.facet = new XmlSchemaWhiteSpaceFacet();
1935                     break;
1936             }
1937             builder.xso = builder.facet;
1938             if (SchemaNames.Token.XsdSimpleTypeRestriction == builder.ParentElement) {
1939                 builder.simpleTypeRestriction.Facets.Add(builder.facet);
1940             }
1941             else {
1942                 if (builder.simpleContentRestriction.Attributes.Count != 0 || (builder.simpleContentRestriction.AnyAttribute != null)) {
1943                     builder.SendValidationEvent(Res.Sch_InvalidFacetPosition, null);
1944                 }
1945                 builder.simpleContentRestriction.Facets.Add(builder.facet);
1946             }
1947         }
1948 
BuildFacet_Fixed(XsdBuilder builder, string value)1949         private static void BuildFacet_Fixed(XsdBuilder builder, string value) {
1950             builder.facet.IsFixed = builder.ParseBoolean(value, "fixed");
1951         }
1952 
BuildFacet_Value(XsdBuilder builder, string value)1953         private static void BuildFacet_Value(XsdBuilder builder, string value) {
1954             builder.facet.Value = value;
1955         }
1956 
1957         /*
1958             <unique
1959               id = ID
1960               name = NCName
1961               {any attributes with non-schema namespace . . .}>
1962               Content: (annotation? , (selector , field+))
1963             </unique>
1964 
1965             <key
1966               id = ID
1967               name = NCName
1968               {any attributes with non-schema namespace . . .}>
1969               Content: (annotation? , (selector , field+))
1970             </key>
1971 
1972             <keyref
1973               id = ID
1974               name = NCName
1975               refer = QName
1976               {any attributes with non-schema namespace . . .}>
1977               Content: (annotation? , (selector , field+))
1978             </keyref>
1979         */
InitIdentityConstraint(XsdBuilder builder, string value)1980         private static void InitIdentityConstraint(XsdBuilder builder, string value) {
1981             if (!builder.element.RefName.IsEmpty) {
1982                 builder.SendValidationEvent(Res.Sch_ElementRef, null);
1983             }
1984 
1985             switch (builder.CurrentElement) {
1986                 case SchemaNames.Token.XsdUnique:
1987                     builder.xso = builder.identityConstraint = new XmlSchemaUnique();
1988                     break;
1989                 case SchemaNames.Token.XsdKey:
1990                     builder.xso = builder.identityConstraint = new XmlSchemaKey();
1991                     break;
1992                 case SchemaNames.Token.XsdKeyref:
1993                     builder.xso = builder.identityConstraint = new XmlSchemaKeyref();
1994                     break;
1995             }
1996             builder.element.Constraints.Add(builder.identityConstraint);
1997         }
1998 
BuildIdentityConstraint_Name(XsdBuilder builder, string value)1999         private static void BuildIdentityConstraint_Name(XsdBuilder builder, string value) {
2000             builder.identityConstraint.Name = value;
2001         }
2002 
BuildIdentityConstraint_Refer(XsdBuilder builder, string value)2003         private static void BuildIdentityConstraint_Refer(XsdBuilder builder, string value) {
2004             if (builder.identityConstraint is XmlSchemaKeyref) {
2005                 ((XmlSchemaKeyref)builder.identityConstraint).Refer = builder.ParseQName(value, "refer");
2006             }
2007             else {
2008                 builder.SendValidationEvent(Res.Sch_UnsupportedAttribute, "refer");
2009             }
2010         }
2011 
2012         /*
2013             <selector
2014               id = ID
2015               xpath = An XPath expression
2016               {any attributes with non-schema namespace . . .}>
2017               Content: (annotation?)
2018             </selector>
2019         */
InitSelector(XsdBuilder builder, string value)2020         private static void InitSelector(XsdBuilder builder, string value) {
2021             builder.xso = builder.xpath = new XmlSchemaXPath();
2022             if ( builder.identityConstraint.Selector == null ) {
2023                 builder.identityConstraint.Selector = builder.xpath;
2024             }
2025             else {
2026                 builder.SendValidationEvent(Res.Sch_DupSelector, builder.identityConstraint.Name);
2027             }
2028         }
2029 
BuildSelector_XPath(XsdBuilder builder, string value)2030         private static void BuildSelector_XPath(XsdBuilder builder, string value) {
2031             builder.xpath.XPath = value;
2032         }
2033 
2034         /*
2035             <field
2036               id = ID
2037               xpath = An XPath expression
2038               {any attributes with non-schema namespace . . .}>
2039               Content: (annotation?)
2040             </field>
2041         */
InitField(XsdBuilder builder, string value)2042         private static void InitField(XsdBuilder builder, string value) {
2043             builder.xso = builder.xpath = new XmlSchemaXPath();
2044             // no selector before fields?
2045             if ( builder.identityConstraint.Selector == null ) {
2046                 builder.SendValidationEvent(Res.Sch_SelectorBeforeFields, builder.identityConstraint.Name);
2047             }
2048             builder.identityConstraint.Fields.Add(builder.xpath);
2049         }
2050 
BuildField_XPath(XsdBuilder builder, string value)2051         private static void BuildField_XPath(XsdBuilder builder, string value) {
2052             builder.xpath.XPath = value;
2053         }
2054 
2055         /*
2056             <annotation>
2057               Content: (appinfo | documentation)*
2058             </annotation>
2059         */
InitAnnotation(XsdBuilder builder, string value)2060         private static void InitAnnotation(XsdBuilder builder, string value) {
2061             // On most elements annotations are only allowed to be the first child
2062             //   (so the element must not have any children by now), and only one annotation is allowed.
2063             // Exceptions are xs:schema and xs:redefine, these can have any number of annotations
2064             //   in any place.
2065             if (builder.hasChild &&
2066                 builder.ParentElement != SchemaNames.Token.XsdSchema &&
2067                 builder.ParentElement != SchemaNames.Token.XsdRedefine) {
2068                 builder.SendValidationEvent(Res.Sch_AnnotationLocation, null);
2069             }
2070             builder.xso = builder.annotation = new XmlSchemaAnnotation();
2071             builder.ParentContainer.AddAnnotation(builder.annotation);
2072         }
2073 
2074         /*
2075             <appinfo
2076               source = uriReference>
2077               Content: ({any})*
2078             </appinfo>
2079         */
InitAppinfo(XsdBuilder builder, string value)2080         private static void InitAppinfo(XsdBuilder builder, string value) {
2081             builder.xso = builder.appInfo = new XmlSchemaAppInfo();
2082             builder.annotation.Items.Add(builder.appInfo);
2083             builder.markup = new XmlNode[] {};
2084         }
2085 
BuildAppinfo_Source(XsdBuilder builder, string value)2086         private static void BuildAppinfo_Source(XsdBuilder builder, string value) {
2087             builder.appInfo.Source = ParseUriReference(value);
2088         }
2089 
EndAppinfo(XsdBuilder builder)2090         private static void EndAppinfo(XsdBuilder builder) {
2091             builder.appInfo.Markup = builder.markup;
2092         }
2093 
2094 
2095         /*
2096             <documentation
2097               source = uriReference>
2098               Content: ({any})*
2099             </documentation>
2100         */
InitDocumentation(XsdBuilder builder, string value)2101         private static void InitDocumentation(XsdBuilder builder, string value) {
2102             builder.xso = builder.documentation = new XmlSchemaDocumentation();
2103             builder.annotation.Items.Add(builder.documentation);
2104             builder.markup = new XmlNode[] {};
2105         }
2106 
BuildDocumentation_Source(XsdBuilder builder, string value)2107         private static void BuildDocumentation_Source(XsdBuilder builder, string value) {
2108             builder.documentation.Source = ParseUriReference(value);
2109         }
2110 
BuildDocumentation_XmlLang(XsdBuilder builder, string value)2111         private static void BuildDocumentation_XmlLang(XsdBuilder builder, string value) {
2112             try {
2113                 builder.documentation.Language = value;
2114             }
2115             catch (XmlSchemaException e) {
2116                 e.SetSource(builder.reader.BaseURI, builder.positionInfo.LineNumber, builder.positionInfo.LinePosition);
2117                 builder.SendValidationEvent(e);
2118             }
2119         }
2120 
EndDocumentation(XsdBuilder builder)2121         private static void EndDocumentation(XsdBuilder builder) {
2122             builder.documentation.Markup = builder.markup;
2123 
2124         }
2125 
2126 
2127         ///////////////////////////////////////////////////////////////////////////////////////////////
2128         //
2129         // helper functions
2130 
AddAttribute(XmlSchemaObject value)2131         private void AddAttribute(XmlSchemaObject value) {
2132             switch (this.ParentElement) {
2133                 case SchemaNames.Token.XsdComplexType:
2134                     if (complexType.ContentModel != null) {
2135                         SendValidationEvent(Res.Sch_AttributeMutuallyExclusive, "attribute");
2136                     }
2137                     if (complexType.AnyAttribute != null) {
2138                         SendValidationEvent(Res.Sch_AnyAttributeLastChild, null);
2139                     }
2140                     this.complexType.Attributes.Add(value);
2141                     break;
2142                 case SchemaNames.Token.XsdSimpleContentRestriction:
2143                     if (simpleContentRestriction.AnyAttribute != null) {
2144                         SendValidationEvent(Res.Sch_AnyAttributeLastChild, null);
2145                     }
2146                     this.simpleContentRestriction.Attributes.Add(value);
2147                     break;
2148                 case SchemaNames.Token.XsdSimpleContentExtension:
2149                     if (simpleContentExtension.AnyAttribute != null) {
2150                         SendValidationEvent(Res.Sch_AnyAttributeLastChild, null);
2151                     }
2152                     this.simpleContentExtension.Attributes.Add(value);
2153                     break;
2154                 case SchemaNames.Token.XsdComplexContentExtension:
2155                     if (complexContentExtension.AnyAttribute != null) {
2156                         SendValidationEvent(Res.Sch_AnyAttributeLastChild, null);
2157                     }
2158                     this.complexContentExtension.Attributes.Add(value);
2159                     break;
2160                 case SchemaNames.Token.XsdComplexContentRestriction:
2161                     if (complexContentRestriction.AnyAttribute != null) {
2162                         SendValidationEvent(Res.Sch_AnyAttributeLastChild, null);
2163                     }
2164                     this.complexContentRestriction.Attributes.Add(value);
2165                     break;
2166                 case SchemaNames.Token.xsdAttributeGroup:
2167                     if (attributeGroup.AnyAttribute != null) {
2168                         SendValidationEvent(Res.Sch_AnyAttributeLastChild, null);
2169                     }
2170                     this.attributeGroup.Attributes.Add(value);
2171                     break;
2172                 default:
2173                     Debug.Assert(false);
2174                     break;
2175             }
2176         }
2177 
AddParticle(XmlSchemaParticle particle)2178         private void AddParticle(XmlSchemaParticle particle) {
2179             switch (this.ParentElement) {
2180                 case SchemaNames.Token.XsdComplexType:
2181                     if ( (complexType.ContentModel != null) ||
2182                          (complexType.Attributes.Count != 0 || complexType.AnyAttribute != null) ||
2183                          (complexType.Particle != null)
2184                          ) {
2185                         SendValidationEvent(Res.Sch_ComplexTypeContentModel, "complexType");
2186                     }
2187                     complexType.Particle = particle;
2188                     break;
2189                 case SchemaNames.Token.XsdComplexContentExtension:
2190                     if ( (complexContentExtension.Particle != null) ||
2191                          (complexContentExtension.Attributes.Count != 0 || complexContentExtension.AnyAttribute != null)
2192                        ) {
2193                          SendValidationEvent(Res.Sch_ComplexContentContentModel, "ComplexContentExtension");
2194                     }
2195                     complexContentExtension.Particle = particle;
2196                     break;
2197                 case SchemaNames.Token.XsdComplexContentRestriction:
2198                     if ( (complexContentRestriction.Particle != null) ||
2199                          (complexContentRestriction.Attributes.Count != 0 || complexContentRestriction.AnyAttribute != null)
2200                        ) {
2201                          SendValidationEvent(Res.Sch_ComplexContentContentModel, "ComplexContentExtension");
2202                     }
2203                     complexContentRestriction.Particle = particle;
2204                     break;
2205                 case SchemaNames.Token.XsdGroup:
2206                     if (group.Particle != null) {
2207                         SendValidationEvent(Res.Sch_DupGroupParticle, "particle");
2208                     }
2209                     group.Particle = (XmlSchemaGroupBase)particle;
2210                     break;
2211                 case SchemaNames.Token.XsdChoice:
2212                 case SchemaNames.Token.XsdSequence:
2213                     ((XmlSchemaGroupBase)this.ParentContainer).Items.Add(particle);
2214                     break;
2215                 default:
2216                     Debug.Assert(false);
2217                     break;
2218             }
2219         }
2220 
GetNextState(XmlQualifiedName qname)2221         private bool GetNextState(XmlQualifiedName qname) {
2222             if (this.currentEntry.NextStates != null) {
2223                 for (int i = 0; i < this.currentEntry.NextStates.Length; ++i) {
2224                     int state = (int)this.currentEntry.NextStates[i];
2225                     if (this.schemaNames.TokenToQName[(int)SchemaEntries[state].Name].Equals(qname)) {
2226                         this.nextEntry = SchemaEntries[state];
2227                         return true;
2228                     }
2229                 }
2230             }
2231 
2232             return false;
2233         }
2234 
IsSkipableElement(XmlQualifiedName qname)2235         private bool IsSkipableElement(XmlQualifiedName qname) {
2236             return ((CurrentElement == SchemaNames.Token.XsdDocumentation) ||
2237                     (CurrentElement == SchemaNames.Token.XsdAppInfo));
2238         }
2239 
SetMinOccurs(XmlSchemaParticle particle, string value)2240         private void SetMinOccurs(XmlSchemaParticle particle, string value) {
2241             try {
2242                 particle.MinOccursString = value;
2243             }
2244             catch(Exception) {
2245                 SendValidationEvent(Res.Sch_MinOccursInvalidXsd, null);
2246             }
2247         }
2248 
SetMaxOccurs(XmlSchemaParticle particle, string value)2249         private void SetMaxOccurs(XmlSchemaParticle particle, string value) {
2250             try {
2251                 particle.MaxOccursString = value;
2252             }
2253             catch(Exception) {
2254                 SendValidationEvent(Res.Sch_MaxOccursInvalidXsd, null);
2255             }
2256         }
2257 
ParseBoolean(string value, string attributeName)2258         private bool ParseBoolean(string value, string attributeName) {
2259             try {
2260                 return XmlConvert.ToBoolean(value);
2261             }
2262             catch(Exception) {
2263                 SendValidationEvent(Res.Sch_InvalidXsdAttributeValue, attributeName, value, null);
2264                 return false;
2265             }
2266         }
2267 
ParseEnum(string value, string attributeName, string[] values)2268         private int ParseEnum(string value, string attributeName, string[] values) {
2269             string s = value.Trim();
2270             for (int i = 0; i < values.Length; i++) {
2271                 if (values[i] == s)
2272                     return i + 1;
2273             }
2274             SendValidationEvent(Res.Sch_InvalidXsdAttributeValue, attributeName, s, null);
2275             return 0;
2276         }
2277 
ParseQName(string value, string attributeName)2278         private XmlQualifiedName ParseQName(string value, string attributeName) {
2279             try {
2280                 string prefix;
2281 				value = XmlComplianceUtil.NonCDataNormalize(value); //Normalize QName
2282                 return XmlQualifiedName.Parse(value, this.namespaceManager, out prefix);
2283             }
2284             catch(Exception) {
2285                 SendValidationEvent(Res.Sch_InvalidXsdAttributeValue, attributeName, value, null);
2286                 return XmlQualifiedName.Empty;
2287             }
2288         }
2289 
ParseBlockFinalEnum(string value, string attributeName)2290         private int ParseBlockFinalEnum(string value, string attributeName) {
2291             const int HashAllLength = 4; // Length of "#all"
2292             int r = 0;
2293             string[] stringValues = XmlConvert.SplitString(value);
2294             for (int i = 0; i < stringValues.Length; i++) {
2295                 bool matched = false;
2296                 for (int j = 0; j < DerivationMethodStrings.Length; j++) {
2297                     if (stringValues[i] == DerivationMethodStrings[j]) {
2298                         if ((r & DerivationMethodValues[j]) != 0 && (r & DerivationMethodValues[j]) != DerivationMethodValues[j]) {
2299                             SendValidationEvent(Res.Sch_InvalidXsdAttributeValue, attributeName, value, null);
2300                             return 0;
2301                         }
2302                         r |= DerivationMethodValues[j];
2303                         matched = true;
2304                         break;
2305                     }
2306                 }
2307                 if (!matched) {
2308                     SendValidationEvent(Res.Sch_InvalidXsdAttributeValue, attributeName, value, null);
2309                     return 0;
2310                 }
2311                 if (r == (int)XmlSchemaDerivationMethod.All && value.Length > HashAllLength) { //#all is not allowed with other values
2312                     SendValidationEvent(Res.Sch_InvalidXsdAttributeValue, attributeName, value, null);
2313                     return 0;
2314                 }
2315             }
2316             return r;
2317 
2318         }
2319 
ParseUriReference(string s)2320         private static string ParseUriReference(string s) {
2321             return s;
2322         }
2323 
SendValidationEvent(string code, string arg0, string arg1, string arg2)2324         private void SendValidationEvent(string code, string arg0, string arg1, string arg2) {
2325             SendValidationEvent(new XmlSchemaException(code, new string[] { arg0, arg1, arg2 }, this.reader.BaseURI, this.positionInfo.LineNumber, this.positionInfo.LinePosition));
2326         }
2327 
SendValidationEvent(string code, string msg)2328         private void SendValidationEvent(string code, string msg) {
2329             SendValidationEvent(new XmlSchemaException(code, msg, this.reader.BaseURI, this.positionInfo.LineNumber, this.positionInfo.LinePosition));
2330         }
2331 
SendValidationEvent(string code, string[] args, XmlSeverityType severity)2332         private void SendValidationEvent(string code, string[] args, XmlSeverityType severity) {
2333             SendValidationEvent(new XmlSchemaException(code, args, this.reader.BaseURI, this.positionInfo.LineNumber, this.positionInfo.LinePosition), severity);
2334         }
2335 
SendValidationEvent(XmlSchemaException e, XmlSeverityType severity)2336         private void SendValidationEvent(XmlSchemaException e, XmlSeverityType severity) {
2337             this.schema.ErrorCount++;
2338             e.SetSchemaObject(this.schema);
2339             if (validationEventHandler != null) {
2340                 validationEventHandler(null, new ValidationEventArgs(e, severity));
2341             }
2342             else if (severity == XmlSeverityType.Error) {
2343                 throw e;
2344             }
2345         }
2346 
SendValidationEvent(XmlSchemaException e)2347         private void SendValidationEvent(XmlSchemaException e) {
2348             SendValidationEvent(e, XmlSeverityType.Error);
2349         }
2350 
RecordPosition()2351         private void RecordPosition() {
2352             this.xso.SourceUri = this.reader.BaseURI;
2353             this.xso.LineNumber = this.positionInfo.LineNumber;
2354             this.xso.LinePosition = this.positionInfo.LinePosition;
2355             if (this.xso != this.schema) {
2356                 this.xso.Parent = this.ParentContainer;
2357             }
2358 
2359         }
2360 
2361     }; // class XsdBuilder
2362 
2363 } // namespace System.Xml.Schema
2364