The Labs \ Source Viewer \ SSCLI \ System.Xml.Schema \ BuilderNamespaceManager

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

Developer Fusion