2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaSimpleType.cs
index 8b5ef5651014c6807697f74bb2aab18419a64520..2be423e58cd3f2b0d5d50539084eada07fd7fbc4 100755 (executable)
@@ -1,8 +1,35 @@
-// Author: Dwivedi, Ajay kumar\r
-//            Adwiv@Yahoo.com\r
+//\r
+// System.Xml.Schema.XmlSchemaSimpleType.cs\r
+//\r
+// Author:\r
+//     Dwivedi, Ajay kumar  Adwiv@Yahoo.com\r
+//     Atsushi Enomoto  ginga@kit.hi-ho.ne.jp\r
+//\r
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
 using System;\r
 using System.Xml.Serialization;\r
 using System.Xml;\r
+using Mono.Xml.Schema;\r
 \r
 namespace System.Xml.Schema\r
 {\r
@@ -11,49 +38,233 @@ namespace System.Xml.Schema
        /// </summary>\r
        public class XmlSchemaSimpleType : XmlSchemaType\r
        {\r
+               const string xmlname = "simpleType";\r
+               private static XmlSchemaSimpleType schemaLocationType;\r
+\r
                private XmlSchemaSimpleTypeContent content;\r
                //compilation vars\r
-               internal bool islocal = false;\r
-               private  bool errorOccured = false;\r
+               internal bool islocal = true; // Assuming local means we have to specify islocal=false only in XmlSchema\r
+               private bool recursed;\r
+               private XmlSchemaDerivationMethod variety;\r
+\r
+#if NET_2_0\r
+               // predefined simple types\r
+               internal static readonly XmlSchemaSimpleType XsAnySimpleType;\r
+               internal static readonly XmlSchemaSimpleType XsString;\r
+               internal static readonly XmlSchemaSimpleType XsBoolean;\r
+               internal static readonly XmlSchemaSimpleType XsDecimal;\r
+               internal static readonly XmlSchemaSimpleType XsFloat;\r
+               internal static readonly XmlSchemaSimpleType XsDouble;\r
+               internal static readonly XmlSchemaSimpleType XsDuration;\r
+               internal static readonly XmlSchemaSimpleType XsDateTime;\r
+               internal static readonly XmlSchemaSimpleType XsTime;\r
+               internal static readonly XmlSchemaSimpleType XsDate;\r
+               internal static readonly XmlSchemaSimpleType XsGYearMonth;\r
+               internal static readonly XmlSchemaSimpleType XsGYear;\r
+               internal static readonly XmlSchemaSimpleType XsGMonthDay;\r
+               internal static readonly XmlSchemaSimpleType XsGDay;\r
+               internal static readonly XmlSchemaSimpleType XsGMonth;\r
+               internal static readonly XmlSchemaSimpleType XsHexBinary;\r
+               internal static readonly XmlSchemaSimpleType XsBase64Binary;\r
+               internal static readonly XmlSchemaSimpleType XsAnyUri;\r
+               internal static readonly XmlSchemaSimpleType XsQName;\r
+               internal static readonly XmlSchemaSimpleType XsNotation;\r
+               internal static readonly XmlSchemaSimpleType XsNormalizedString;\r
+               internal static readonly XmlSchemaSimpleType XsToken;\r
+               internal static readonly XmlSchemaSimpleType XsLanguage;\r
+               internal static readonly XmlSchemaSimpleType XsNMToken;\r
+               internal static readonly XmlSchemaSimpleType XsNMTokens;\r
+               internal static readonly XmlSchemaSimpleType XsName;\r
+               internal static readonly XmlSchemaSimpleType XsNCName;\r
+               internal static readonly XmlSchemaSimpleType XsID;\r
+               internal static readonly XmlSchemaSimpleType XsIDRef;\r
+               internal static readonly XmlSchemaSimpleType XsIDRefs;\r
+               internal static readonly XmlSchemaSimpleType XsEntity;\r
+               internal static readonly XmlSchemaSimpleType XsEntities;\r
+               internal static readonly XmlSchemaSimpleType XsInteger;\r
+               internal static readonly XmlSchemaSimpleType XsNonPositiveInteger;\r
+               internal static readonly XmlSchemaSimpleType XsNegativeInteger;\r
+               internal static readonly XmlSchemaSimpleType XsLong;\r
+               internal static readonly XmlSchemaSimpleType XsInt;\r
+               internal static readonly XmlSchemaSimpleType XsShort;\r
+               internal static readonly XmlSchemaSimpleType XsByte;\r
+               internal static readonly XmlSchemaSimpleType XsNonNegativeInteger;\r
+               internal static readonly XmlSchemaSimpleType XsUnsignedLong;\r
+               internal static readonly XmlSchemaSimpleType XsUnsignedInt;\r
+               internal static readonly XmlSchemaSimpleType XsUnsignedShort;\r
+               internal static readonly XmlSchemaSimpleType XsUnsignedByte;\r
+               internal static readonly XmlSchemaSimpleType XsPositiveInteger;\r
+               // xdt:*\r
+               internal static readonly XmlSchemaSimpleType XdtUntypedAtomic;\r
+               internal static readonly XmlSchemaSimpleType XdtAnyAtomicType;\r
+               internal static readonly XmlSchemaSimpleType XdtYearMonthDuration;\r
+               internal static readonly XmlSchemaSimpleType XdtDayTimeDuration;\r
+#endif\r
+\r
+               static XmlSchemaSimpleType ()\r
+               {\r
+                       // This is not used in the meantime.\r
+                       XmlSchemaSimpleType st = new XmlSchemaSimpleType ();\r
+                       XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList ();\r
+                       list.ItemTypeName = new XmlQualifiedName ("anyURI", XmlSchema.Namespace);\r
+                       st.Content = list;\r
+                       st.BaseXmlSchemaTypeInternal = null;\r
+                       st.variety = XmlSchemaDerivationMethod.List;\r
+                       schemaLocationType = st;\r
+\r
+#if NET_2_0\r
+                       // Built-In schema types\r
+                       XsAnySimpleType = BuildSchemaType ("anySimpleType", null);\r
+                       XsString = BuildSchemaType ("string", "anySimpleType");\r
+                       XsBoolean = BuildSchemaType ("boolean", "anySimpleType");\r
+                       XsDecimal = BuildSchemaType ("decimal", "anySimpleType");\r
+                       XsFloat = BuildSchemaType ("float", "anySimpleType");\r
+                       XsDouble = BuildSchemaType ("double", "anySimpleType");\r
+                       XsDuration = BuildSchemaType ("duration", "anySimpleType");\r
+                       XsDateTime = BuildSchemaType ("dateTime", "anySimpleType");\r
+                       XsTime = BuildSchemaType ("time", "anySimpleType");\r
+                       XsDate = BuildSchemaType ("date", "anySimpleType");\r
+                       XsGYearMonth = BuildSchemaType ("gYearMonth", "anySimpleType");\r
+                       XsGYear = BuildSchemaType ("gYear", "anySimpleType");\r
+                       XsGMonthDay = BuildSchemaType ("gMonthDay", "anySimpleType");\r
+                       XsGDay = BuildSchemaType ("gDay", "anySimpleType");\r
+                       XsGMonth = BuildSchemaType ("gMonth", "anySimpleType");\r
+                       XsHexBinary = BuildSchemaType ("hexBinary", "anySimpleType");\r
+                       XsBase64Binary = BuildSchemaType ("base64Binary", "anySimpleType");\r
+                       XsAnyUri = BuildSchemaType ("anyURI", "anySimpleType");\r
+                       XsQName = BuildSchemaType ("QName", "anySimpleType");\r
+                       XsNotation = BuildSchemaType ("NOTATION", "anySimpleType");\r
+                       // derived types\r
+                       XsNormalizedString = BuildSchemaType ("normalizedString", "string");\r
+                       XsToken = BuildSchemaType ("token", "normalizedString");\r
+                       XsLanguage = BuildSchemaType ("language", "token");\r
+                       XsNMToken = BuildSchemaType ("NMTOKEN", "token");\r
+                       XsName = BuildSchemaType ("Name", "token");\r
+                       XsNCName = BuildSchemaType ("NCName", "Name");\r
+\r
+                       XsID = BuildSchemaType ("ID", "NCName");\r
+                       XsIDRef = BuildSchemaType ("IDREF", "NCName");\r
+                       XsEntity = BuildSchemaType ("ENTITY", "NCName");\r
+\r
+                       XsInteger = BuildSchemaType ("integer", "decimal");\r
+                       XsNonPositiveInteger = BuildSchemaType ("nonPositiveInteger", "integer");\r
+                       XsNegativeInteger = BuildSchemaType ("negativeInteger", "nonPositiveInteger");\r
+                       XsLong = BuildSchemaType ("long", "integer");\r
+                       XsInt = BuildSchemaType ("int", "long");\r
+                       XsShort = BuildSchemaType ("short", "int");\r
+                       XsByte = BuildSchemaType ("byte", "short");\r
+                       XsNonNegativeInteger = BuildSchemaType ("nonNegativeInteger", "integer");\r
+                       XsUnsignedLong = BuildSchemaType ("unsignedLong", "nonNegativeInteger");\r
+                       XsUnsignedInt = BuildSchemaType ("unsignedInt", "unsignedLong");\r
+                       XsUnsignedShort = BuildSchemaType ("unsignedShort", "unsignedInt");\r
+                       XsUnsignedByte = BuildSchemaType ("unsignedByte", "unsignedShort");\r
+                       XsPositiveInteger = BuildSchemaType ("positiveInteger", "nonNegativeInteger");\r
+\r
+                       // xdt:*\r
+                       XdtAnyAtomicType = BuildSchemaType ("anyAtomicType", "anySimpleType", true, false);\r
+                       XdtUntypedAtomic = BuildSchemaType ("untypedAtomic", "anyAtomicType", true, true);\r
+                       XdtDayTimeDuration = BuildSchemaType ("dayTimeDuration", "duration", true, false);\r
+                       XdtYearMonthDuration = BuildSchemaType ("yearMonthDuration", "duration", true, false);\r
+\r
+                       // NMTOKENS, IDREFS, ENTITIES - lists\r
+                       XsIDRefs = new XmlSchemaSimpleType ();\r
+                       XmlSchemaSimpleTypeList sl = new XmlSchemaSimpleTypeList ();\r
+                       sl.ItemType = XsIDRef;\r
+                       XsIDRefs.Content = sl;\r
+                       XsEntities = new XmlSchemaSimpleType ();\r
+                       sl = new XmlSchemaSimpleTypeList ();\r
+                       sl.ItemType = XsEntity;\r
+                       XsEntities.Content = sl;\r
+                       XsNMTokens = new XmlSchemaSimpleType ();\r
+                       sl = new XmlSchemaSimpleTypeList ();\r
+                       sl.ItemType = XsNMToken;\r
+                       XsNMTokens.Content = sl;\r
+#endif\r
+               }\r
+\r
+#if NET_2_0\r
+               private static XmlSchemaSimpleType BuildSchemaType (string name, string baseName)\r
+               {\r
+                       return BuildSchemaType (name, baseName, false, false);\r
+               }\r
 \r
-               public XmlSchemaSimpleType()\r
+               private static XmlSchemaSimpleType BuildSchemaType (string name, string baseName, bool xdt, bool baseXdt)\r
                {\r
+                       string ns = xdt ? "http://www.w3.org/2003/11/xpath-datatypes" : XmlSchema.Namespace;\r
+                       string ns2 = baseXdt ? "http://www.w3.org/2003/11/xpath-datatypes" : XmlSchema.Namespace;\r
+                       XmlSchemaSimpleType st = new XmlSchemaSimpleType ();\r
+                       st.QNameInternal = new XmlQualifiedName (name, ns);\r
+                       if (baseName != null)\r
+                               st.BaseXmlSchemaTypeInternal = XmlSchemaType. GetBuiltInSimpleType (new XmlQualifiedName (baseName, ns2));\r
+                       st.DatatypeInternal = XmlSchemaDatatype.FromName (st.QualifiedName);\r
+                       st.ValueConverter = XmlSchemaType.BuildConverter (st.TypeCode);\r
+                       return st;\r
+               }\r
+#endif\r
+\r
+               internal static XsdAnySimpleType AnySimpleType {\r
+                       get { return XsdAnySimpleType.Instance; }\r
+               }\r
+\r
+               internal static XmlSchemaSimpleType SchemaLocationType {\r
+                       get { return schemaLocationType; }\r
                }\r
 \r
-               [XmlElement("restriction",typeof(XmlSchemaSimpleTypeRestriction),Namespace="http://www.w3.org/2001/XMLSchema")]\r
-               [XmlElement("list",typeof(XmlSchemaSimpleTypeList),Namespace="http://www.w3.org/2001/XMLSchema")]\r
-               [XmlElement("union",typeof(XmlSchemaSimpleTypeUnion),Namespace="http://www.w3.org/2001/XMLSchema")]\r
+               public XmlSchemaSimpleType ()\r
+               {\r
+               }\r
+\r
+               [XmlElement("restriction",typeof(XmlSchemaSimpleTypeRestriction),Namespace=XmlSchema.Namespace)]\r
+               [XmlElement("list",typeof(XmlSchemaSimpleTypeList),Namespace=XmlSchema.Namespace)]\r
+               [XmlElement("union",typeof(XmlSchemaSimpleTypeUnion),Namespace=XmlSchema.Namespace)]\r
                public XmlSchemaSimpleTypeContent Content\r
                {\r
                        get{ return  content; } \r
                        set{ content = value; }\r
                }\r
 \r
+               internal XmlSchemaDerivationMethod Variety\r
+               {\r
+                       get{ return variety; }\r
+               }\r
+\r
                /// <remarks>\r
                /// For a simple Type:\r
+               ///             1. Content must be present\r
+               ///             2. id if present, must have be a valid ID\r
                ///             a) If the simpletype is local\r
                ///                     1-      are from <xs:complexType name="simpleType"> and <xs:complexType name="localSimpleType">\r
                ///                     1. name  is prohibited\r
                ///                     2. final is prohibited\r
-               ///                     3. group of simplederivation (Content) is required.\r
                ///             b) If the simpletype is toplevel\r
                ///                     1-  are from <xs:complexType name="simpleType"> and <xs:complexType name="topLevelSimpleType">\r
                ///                     1. name is required, type must be NCName\r
                ///                     2. Content is required\r
                ///                     3. final can have values : #all | (list | union | restriction)\r
                ///                     4. If final is set, finalResolved is same as final (but within the values of b.3)\r
-               ///                     5. If final is not set, the finalDefault of the schema (but within the values of b.3)\r
+               ///                     5. If final is not set, the finalDefault of the schema (ie. only #all and restriction)\r
                ///                     6. Base type is:\r
                ///                             4.1 If restriction is chosen,the base type of restriction or elements\r
                ///                             4.2 otherwise simple ur-type\r
                /// </remarks>\r
-               [MonoTODO]\r
-               internal bool Compile(ValidationEventHandler h, XmlSchemaInfo info)\r
+               internal override int Compile(ValidationEventHandler h, XmlSchema schema)\r
                {\r
+                       // If this is already compiled this time, simply skip.\r
+                       if (this.IsComplied (schema.CompilationId))\r
+                               return 0;\r
+#if NET_2_0\r
+                       if (Content != null)\r
+                               Content.Parent = this;\r
+#endif\r
+\r
+                       errorCount = 0;\r
+\r
                        if(this.islocal) // a\r
                        {\r
                                if(this.Name != null) // a.1\r
                                        error(h,"Name is prohibited in a local simpletype");\r
+                               else\r
+                                       this.QNameInternal = new XmlQualifiedName(this.Name,schema.TargetNamespace);\r
                                if(this.Final != XmlSchemaDerivationMethod.None) //a.2\r
                                        error(h,"Final is prohibited in a local simpletype");\r
                        }\r
@@ -61,65 +272,388 @@ namespace System.Xml.Schema
                        {\r
                                if(this.Name == null) //b.1\r
                                        error(h,"Name is required in top level simpletype");\r
-                                       // FIXME: A better way to check NCName? Something like IsNCName()?\r
-                               else if(this.Name.IndexOf(":") != -1) // b.1.2\r
-                                       error(h,"attribute name must be NCName");\r
+                               else if(!XmlSchemaUtil.CheckNCName(this.Name)) // b.1.2\r
+                                       error(h,"name attribute of a simpleType must be NCName");\r
                                else\r
-                                       this.qName = new XmlQualifiedName(this.Name,info.targetNS);//Add target namespace\r
+                                       this.QNameInternal = new XmlQualifiedName(this.Name,schema.TargetNamespace);\r
                                \r
-                               XmlSchemaDerivationMethod finaltmp;\r
-\r
-                               if(this.Final != XmlSchemaDerivationMethod.None)\r
+                               //NOTE: Although the FinalResolved can be Empty, it is not a valid value for Final\r
+                               //DEVIATION: If an error occurs, the finaldefault is always consulted. This deviates\r
+                               //                       from the way MS implementation works.\r
+                               switch(this.Final) //b.3, b.4\r
                                {\r
-                                       finaltmp = this.Final;\r
-                               }\r
-                               else\r
-                               {\r
-                                       finaltmp = info.finalDefault;\r
-                               }\r
-                               switch(finaltmp) //b.3, b.4, b.5\r
-                               {\r
-                                       case XmlSchemaDerivationMethod.Substitution:\r
-                                               error(h,"substition is not a valid value for final in a simpletype");\r
-                                               break;\r
-                                       case XmlSchemaDerivationMethod.Extension:\r
-                                               error(h,"extension is not a valid value for final in a simpletype");\r
+                                       case XmlSchemaDerivationMethod.All:\r
+                                               this.finalResolved = XmlSchemaDerivationMethod.All;\r
                                                break;\r
+                                       case XmlSchemaDerivationMethod.List:\r
                                        case XmlSchemaDerivationMethod.Union:\r
-                                               error(h,"union is not a valid value for final in simpletype");\r
+                                       case XmlSchemaDerivationMethod.Restriction:\r
+                                               this.finalResolved = Final;\r
                                                break;\r
                                        default:\r
-                                               this.finalResolved = this.Final;\r
+                                               error(h,"The value of final attribute is not valid for simpleType");\r
+                                               goto case XmlSchemaDerivationMethod.None;\r
+                                               // use assignment from finaldefault on schema.\r
+                                       case XmlSchemaDerivationMethod.None: // b.5\r
+                                               XmlSchemaDerivationMethod flags = \r
+                                                       (XmlSchemaDerivationMethod.Restriction | XmlSchemaDerivationMethod.List |\r
+                                                       XmlSchemaDerivationMethod.Extension | XmlSchemaDerivationMethod.Union );\r
+                                               switch (schema.FinalDefault) {\r
+                                               case XmlSchemaDerivationMethod.All:\r
+                                                       finalResolved = XmlSchemaDerivationMethod.All;\r
+                                                       break;\r
+                                               case XmlSchemaDerivationMethod.None:\r
+                                                       finalResolved = XmlSchemaDerivationMethod.Empty;\r
+                                                       break;\r
+                                               default:\r
+                                                       finalResolved = schema.FinalDefault & flags;\r
+                                                       break;\r
+                                               }\r
                                                break;\r
                                }\r
                        }\r
+\r
+                       XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);\r
+\r
+                       if (Content != null)\r
+                               Content.OwnerType = this;\r
+\r
                        if(this.Content == null) //a.3,b.2\r
-                               error(h,"Content is required in a local simpletype");\r
-                       if(Content is XmlSchemaSimpleTypeRestriction)\r
+                               error(h,"Content is required in a simpletype");\r
+                       else if(Content is XmlSchemaSimpleTypeRestriction)\r
                        {\r
-                               ((XmlSchemaSimpleTypeRestriction)Content).Compile(h,info);\r
+                               this.resolvedDerivedBy = XmlSchemaDerivationMethod.Restriction;\r
+                               errorCount += ((XmlSchemaSimpleTypeRestriction)Content).Compile(h,schema);\r
                        }\r
                        else if(Content is XmlSchemaSimpleTypeList)\r
                        {\r
-                               ((XmlSchemaSimpleTypeList)Content).Compile(h,info);\r
+                               this.resolvedDerivedBy = XmlSchemaDerivationMethod.List;\r
+                               errorCount += ((XmlSchemaSimpleTypeList)Content).Compile(h,schema);\r
                        }\r
                        else if(Content is XmlSchemaSimpleTypeUnion)\r
                        {\r
-                               ((XmlSchemaSimpleTypeUnion)Content).Compile(h,info);\r
+                               this.resolvedDerivedBy = XmlSchemaDerivationMethod.Union;\r
+                               errorCount += ((XmlSchemaSimpleTypeUnion)Content).Compile(h,schema);\r
                        }\r
-                       return errorOccured;\r
+\r
+                       this.CompilationId = schema.CompilationId;\r
+                       return errorCount;\r
                }\r
-               \r
-               [MonoTODO]\r
-               internal bool Validate(ValidationEventHandler h)\r
+\r
+               internal void CollectBaseType (ValidationEventHandler h, XmlSchema schema)\r
                {\r
-                       return false;\r
+                       if (Content is XmlSchemaSimpleTypeRestriction) {\r
+                               object o = ((XmlSchemaSimpleTypeRestriction) Content).GetActualType (h, schema, false);\r
+                               BaseXmlSchemaTypeInternal = o as XmlSchemaSimpleType;\r
+                               if (BaseXmlSchemaTypeInternal != null)\r
+                                       DatatypeInternal = BaseXmlSchemaTypeInternal.Datatype;\r
+                               else\r
+                                       DatatypeInternal = o as XmlSchemaDatatype;\r
+                       }\r
+                       // otherwise, actualBaseSchemaType is null\r
+                       else\r
+                               DatatypeInternal = XmlSchemaSimpleType.AnySimpleType;\r
                }\r
                \r
-               internal void error(ValidationEventHandler handle,string message)\r
+               internal override int Validate(ValidationEventHandler h, XmlSchema schema)\r
                {\r
-                       this.errorOccured = true;\r
-                       ValidationHandler.RaiseValidationError(handle,this,message);\r
+                       // 3.14.6 Properties Correct.\r
+                       // \r
+                       // 1. Post Compilation Properties\r
+                       // {name}, {target namespace} => QNameInternal. Already Compile()d.\r
+                       // {base type definition} => baseSchemaTypeInternal\r
+                       // {final} => finalResolved. Already Compile()d.\r
+                       // {variety} => resolvedDerivedBy. Already Compile()d.\r
+                       //\r
+                       // 2. Should be checked by "recursed" field.\r
+\r
+                       if(IsValidated (schema.ValidationId))\r
+                               return errorCount;\r
+\r
+                       if (recursed) {\r
+                               error (h, "Circular type reference was found.");\r
+                               return errorCount;\r
+                       }\r
+                       recursed = true;\r
+\r
+                       CollectBaseType (h, schema);\r
+\r
+                       if (content != null)\r
+                               errorCount += content.Validate (h, schema);\r
+\r
+/*\r
+                       // BaseSchemaType property\r
+                       BaseXmlSchemaTypeInternal = content.ActualBaseSchemaType as XmlSchemaType;\r
+                       if (this.BaseXmlSchemaTypeInternal == null)\r
+                               this.DatatypeInternal = content.ActualBaseSchemaType as XmlSchemaDatatype;\r
+*/\r
+\r
+                       // Datatype property\r
+                       XmlSchemaSimpleType simple = BaseXmlSchemaType as XmlSchemaSimpleType;\r
+                       if (simple != null)\r
+                               this.DatatypeInternal = simple.Datatype;\r
+//                     else\r
+//                             DatatypeInternal = BaseSchemaType as XmlSchemaDatatype;\r
+\r
+                       // 3.\r
+                       XmlSchemaSimpleType baseSType = BaseXmlSchemaType as XmlSchemaSimpleType;\r
+                       if (baseSType != null) {\r
+                               if ((baseSType.FinalResolved & this.resolvedDerivedBy) != 0)\r
+                                       error (h, "Specified derivation is prohibited by the base simple type.");\r
+                       }\r
+\r
+                       // {variety}\r
+                       if (this.resolvedDerivedBy == XmlSchemaDerivationMethod.Restriction &&\r
+                               baseSType != null)\r
+                               this.variety = baseSType.Variety;\r
+                       else\r
+                               this.variety = this.resolvedDerivedBy;\r
+\r
+                       // 3.14.6 Derivation Valid (Restriction, Simple)\r
+                       XmlSchemaSimpleTypeRestriction r = Content as XmlSchemaSimpleTypeRestriction;\r
+                       object baseType = BaseXmlSchemaType != null ? (object) BaseXmlSchemaType : Datatype;\r
+                       if (r != null)\r
+                               ValidateDerivationValid (baseType, r.Facets, h, schema);\r
+\r
+                       // TODO: describe which validation term this belongs to.\r
+                       XmlSchemaSimpleTypeList l = Content as XmlSchemaSimpleTypeList;\r
+                       if (l != null) {\r
+                               XmlSchemaSimpleType itemSimpleType = l.ValidatedListItemType as XmlSchemaSimpleType;\r
+                               if (itemSimpleType != null && itemSimpleType.Content is XmlSchemaSimpleTypeList)\r
+                                       error (h, "List type must not be derived from another list type.");\r
+                       }\r
+\r
+                       recursed = false;\r
+                       ValidationId = schema.ValidationId;\r
+                       return errorCount;\r
                }\r
+\r
+               // 3.14.6 Derivation Valid (RestrictionSimple)
+               internal void ValidateDerivationValid (object baseType, XmlSchemaObjectCollection facets,
+                       ValidationEventHandler h, XmlSchema schema)
+               {
+                       // TODO
+                       XmlSchemaSimpleType baseSimpleType = baseType as XmlSchemaSimpleType;
+                       switch (this.Variety) {
+                       // 1. atomic type
+                       case XmlSchemaDerivationMethod.Restriction:
+                               // 1.1
+                               if (baseSimpleType != null && baseSimpleType.resolvedDerivedBy != XmlSchemaDerivationMethod.Restriction)
+                                       error (h, "Base schema type is not either atomic type or primitive type.");
+                               // 1.2
+                               if (baseSimpleType != null && 
+                                       (baseSimpleType.FinalResolved & XmlSchemaDerivationMethod.Restriction) != 0)
+                                       error (h, "Derivation by restriction is prohibited by the base simple type.");
+                               // TODO: 1.3 facet restriction valid.
+                               break;
+                       case XmlSchemaDerivationMethod.List:
+                               /*\r
+                               XmlSchemaSimpleTypeList thisList = Content as XmlSchemaSimpleTypeList;
+                               // 2.1 item list type not allowed
+                               if (baseSimpleType != null && baseSimpleType.resolvedDerivedBy == XmlSchemaDerivationMethod.List)
+                                       error (h, "Base list schema type is not allowed.");
+                               XmlSchemaSimpleTypeUnion baseUnion = baseSimpleType.Content as XmlSchemaSimpleTypeUnion;
+                               if (baseUnion != null) {
+                                       bool errorFound = false;
+                                       foreach (object memberType in baseUnion.ValidatedTypes) {
+                                               XmlSchemaSimpleType memberST = memberType as XmlSchemaSimpleType;
+                                               if (memberST != null && memberST.resolvedDerivedBy == XmlSchemaDerivationMethod.List)
+                                                       errorFound = true;
+                                       }
+                                       if (errorFound)
+                                               error (h, "Base union schema type should not contain list types.");
+                               }
+                               */
+                               // 2.2 facets limited
+                               if (facets != null)
+                                       foreach (XmlSchemaFacet facet in facets) {
+                                               if (facet is XmlSchemaLengthFacet ||
+                                                       facet is XmlSchemaMaxLengthFacet ||
+                                                       facet is XmlSchemaMinLengthFacet ||
+                                                       facet is XmlSchemaEnumerationFacet ||
+                                                       facet is XmlSchemaPatternFacet)
+                                                       continue;
+                                               else
+                                                       error (h, "Not allowed facet was found on this simple type which derives list type.");
+                                       }
+                               break;
+                       case XmlSchemaDerivationMethod.Union:
+                               // 3.1
+
+                               // 3.2
+                               if (facets != null)
+                                       foreach (XmlSchemaFacet facet in facets) {
+                                               if (facet is XmlSchemaEnumerationFacet ||
+                                                       facet is XmlSchemaPatternFacet)
+                                                       continue;
+                                               else
+                                                       error (h, "Not allowed facet was found on this simple type which derives list type.");
+                                       }
+                               break;
+                       }
+               }
+
+               // 3.14.6 Type Derivation OK (Simple)
+               internal bool ValidateTypeDerivationOK (object baseType,
+                       ValidationEventHandler h, XmlSchema schema, bool raiseError)
+               {
+                       // 1
+                       // Note that anyType should also be allowed as anySimpleType.
+                       if (this == baseType || baseType == XmlSchemaSimpleType.AnySimpleType ||
+                               baseType == XmlSchemaComplexType.AnyType)
+                               return true;
+
+                       // 2.1
+                       XmlSchemaSimpleType baseSimpleType = baseType as XmlSchemaSimpleType;
+                       if (baseSimpleType != null && 
+                               (baseSimpleType.FinalResolved & resolvedDerivedBy) != 0) {
+                               if (raiseError)
+                                       error (h, "Specified derivation is prohibited by the base type.");
+                               return false;
+                       }
+
+                       // 2.2.1
+                       if (BaseXmlSchemaType == baseType || Datatype == baseType)
+                               return true;
+
+                       // 2.2.2
+                       XmlSchemaSimpleType thisBaseSimpleType = BaseXmlSchemaType as XmlSchemaSimpleType;
+                       if (thisBaseSimpleType != null) {
+                               if (thisBaseSimpleType.ValidateTypeDerivationOK (baseType, h, schema, false))
+                                       return true;
+                       }
+
+                       // 2.2.3
+                       switch (Variety) {
+                       case XmlSchemaDerivationMethod.Union:
+                       case XmlSchemaDerivationMethod.List:
+                               if (baseType == XmlSchemaSimpleType.AnySimpleType)
+                                       return true;
+                               break;
+                       }
+
+                       // 2.2.4 validly derived from one of the union member type.
+                       if (baseSimpleType != null && baseSimpleType.Variety == XmlSchemaDerivationMethod.Union) {
+                               foreach (object memberType in ((XmlSchemaSimpleTypeUnion) baseSimpleType.Content).ValidatedTypes)
+                                       if (this.ValidateTypeDerivationOK (memberType, h, schema, false))
+                                               return true;
+                       }
+
+                       if (raiseError)
+                               error(h, "Invalid simple type derivation was found.");
+                       return false;
+               }
+
+               internal string Normalize (string s, XmlNameTable nt, XmlNamespaceManager nsmgr)
+               {
+                       return Content.Normalize (s, nt, nsmgr);
+               }
+
+               //<simpleType \r
+               //  final = (#all | (list | union | restriction)) \r
+               //  id = ID \r
+               //  name = NCName \r
+               //  {any attributes with non-schema namespace . . .}>\r
+               //  Content: (annotation?, (restriction | list | union))\r
+               //</simpleType>\r
+               internal static XmlSchemaSimpleType Read(XmlSchemaReader reader, ValidationEventHandler h)\r
+               {\r
+                       XmlSchemaSimpleType stype = new XmlSchemaSimpleType();\r
+                       reader.MoveToElement();\r
+\r
+                       if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
+                       {\r
+                               error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);\r
+                               reader.Skip();\r
+                               return null;\r
+                       }\r
+\r
+                       stype.LineNumber = reader.LineNumber;\r
+                       stype.LinePosition = reader.LinePosition;\r
+                       stype.SourceUri = reader.BaseURI;\r
+\r
+                       while(reader.MoveToNextAttribute())\r
+                       {\r
+                               if(reader.Name == "final")\r
+                               {\r
+                                       Exception innerex;\r
+                                       stype.Final = XmlSchemaUtil.ReadDerivationAttribute(reader, out innerex, "final",\r
+                                               XmlSchemaUtil.FinalAllowed);\r
+                                       if(innerex != null)\r
+                                               error(h, "some invalid values not a valid value for final", innerex);\r
+                               }\r
+                               else if(reader.Name == "id")\r
+                               {\r
+                                       stype.Id = reader.Value;\r
+                               }\r
+                               else if(reader.Name == "name")\r
+                               {\r
+                                       stype.Name = reader.Value;\r
+                               }\r
+                               else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
+                               {\r
+                                       error(h,reader.Name + " is not a valid attribute for simpleType",null);\r
+                               }\r
+                               else\r
+                               {\r
+                                       XmlSchemaUtil.ReadUnhandledAttribute(reader,stype);\r
+                               }\r
+                       }\r
+                       \r
+                       reader.MoveToElement();\r
+                       if(reader.IsEmptyElement)\r
+                               return stype;\r
+\r
+                       //      Content: (annotation?, (restriction | list | union))\r
+                       int level = 1;\r
+                       while(reader.ReadNextElement())\r
+                       {\r
+                               if(reader.NodeType == XmlNodeType.EndElement)\r
+                               {\r
+                                       if(reader.LocalName != xmlname)\r
+                                               error(h,"Should not happen :2: XmlSchemaSimpleType.Read, name="+reader.Name,null);\r
+                                       break;\r
+                               }\r
+                               if(level <= 1 && reader.LocalName == "annotation")\r
+                               {\r
+                                       level = 2; //Only one annotation\r
+                                       XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
+                                       if(annotation != null)\r
+                                               stype.Annotation = annotation;\r
+                                       continue;\r
+                               }\r
+                               if(level <= 2)\r
+                               {\r
+                                       if(reader.LocalName == "restriction")\r
+                                       {\r
+                                               level = 3;\r
+                                               XmlSchemaSimpleTypeRestriction restriction = XmlSchemaSimpleTypeRestriction.Read(reader,h);\r
+                                               if(restriction != null)\r
+                                                       stype.content = restriction;\r
+                                               continue;\r
+                                       }\r
+                                       if(reader.LocalName == "list")\r
+                                       {\r
+                                               level = 3;\r
+                                               XmlSchemaSimpleTypeList list = XmlSchemaSimpleTypeList.Read(reader,h);\r
+                                               if(list != null)\r
+                                                       stype.content = list;\r
+                                               continue;\r
+                                       }\r
+                                       if(reader.LocalName == "union")\r
+                                       {\r
+                                               level = 3;\r
+                                               XmlSchemaSimpleTypeUnion union = XmlSchemaSimpleTypeUnion.Read(reader,h);\r
+                                               if(union != null)\r
+                                                       stype.content = union;\r
+                                               continue;\r
+                                       }\r
+                               }\r
+                               reader.RaiseInvalidElementError();\r
+                       }\r
+                       return stype;\r
+               }\r
+\r
+\r
        }\r
 }\r