2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaSimpleType.cs
index 8f7758d6a07fde5e46e94ea9e34d3639b8440572..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,12 +38,179 @@ 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 = true; // Assuming local means we have to specify islocal=false only in XmlSchema\r
-               private static string xmlname = "simpleType";\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
+               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
-               public XmlSchemaSimpleType()\r
+               internal static XmlSchemaSimpleType SchemaLocationType {\r
+                       get { return schemaLocationType; }\r
+               }\r
+\r
+               public XmlSchemaSimpleType ()\r
                {\r
                }\r
 \r
@@ -29,6 +223,11 @@ namespace System.Xml.Schema
                        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
@@ -48,12 +247,15 @@ namespace System.Xml.Schema
                ///                             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 int Compile(ValidationEventHandler h, XmlSchema schema)\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
@@ -61,6 +263,8 @@ namespace System.Xml.Schema
                        {\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
@@ -82,13 +286,9 @@ namespace System.Xml.Schema
                                                this.finalResolved = XmlSchemaDerivationMethod.All;\r
                                                break;\r
                                        case XmlSchemaDerivationMethod.List:\r
-                                               this.finalResolved = XmlSchemaDerivationMethod.List;\r
-                                               break;\r
                                        case XmlSchemaDerivationMethod.Union:\r
-                                               this.finalResolved = XmlSchemaDerivationMethod.Union;\r
-                                               break;\r
                                        case XmlSchemaDerivationMethod.Restriction:\r
-                                               this.finalResolved = XmlSchemaDerivationMethod.Restriction;\r
+                                               this.finalResolved = Final;\r
                                                break;\r
                                        default:\r
                                                error(h,"The value of final attribute is not valid for simpleType");\r
@@ -98,45 +298,257 @@ namespace System.Xml.Schema
                                                XmlSchemaDerivationMethod flags = \r
                                                        (XmlSchemaDerivationMethod.Restriction | XmlSchemaDerivationMethod.List |\r
                                                        XmlSchemaDerivationMethod.Extension | XmlSchemaDerivationMethod.Union );\r
-                                               if(schema.FinalDefault == XmlSchemaDerivationMethod.All)\r
+                                               switch (schema.FinalDefault) {\r
+                                               case XmlSchemaDerivationMethod.All:\r
                                                        finalResolved = XmlSchemaDerivationMethod.All;\r
-                                               else if(schema.FinalDefault == XmlSchemaDerivationMethod.None)\r
-                                                       finalResolved = flags;\r
-                                               else \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 simpletype");\r
                        else if(Content is XmlSchemaSimpleTypeRestriction)\r
                        {\r
+                               this.resolvedDerivedBy = XmlSchemaDerivationMethod.Restriction;\r
                                errorCount += ((XmlSchemaSimpleTypeRestriction)Content).Compile(h,schema);\r
                        }\r
                        else if(Content is XmlSchemaSimpleTypeList)\r
                        {\r
+                               this.resolvedDerivedBy = XmlSchemaDerivationMethod.List;\r
                                errorCount += ((XmlSchemaSimpleTypeList)Content).Compile(h,schema);\r
                        }\r
                        else if(Content is XmlSchemaSimpleTypeUnion)\r
                        {\r
+                               this.resolvedDerivedBy = XmlSchemaDerivationMethod.Union;\r
                                errorCount += ((XmlSchemaSimpleTypeUnion)Content).Compile(h,schema);\r
                        }\r
+\r
                        this.CompilationId = schema.CompilationId;\r
                        return errorCount;\r
                }\r
+\r
+               internal void CollectBaseType (ValidationEventHandler h, XmlSchema schema)\r
+               {\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
-               [MonoTODO]\r
-               internal int Validate(ValidationEventHandler h, XmlSchema schema)\r
+               internal override int Validate(ValidationEventHandler h, XmlSchema schema)\r
                {\r
-                       if(isCompiled)\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
@@ -165,7 +577,8 @@ namespace System.Xml.Schema
                                if(reader.Name == "final")\r
                                {\r
                                        Exception innerex;\r
-                                       stype.Final = XmlSchemaUtil.ReadDerivationAttribute(reader, out innerex, "final");\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