2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaSimpleType.cs
index 9ff0163d119049bca89c72b254cde6271aecf066..2be423e58cd3f2b0d5d50539084eada07fd7fbc4 100755 (executable)
@@ -5,6 +5,27 @@
 //     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
@@ -17,18 +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
@@ -65,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 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
@@ -130,6 +315,9 @@ namespace System.Xml.Schema
 \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
@@ -151,8 +339,22 @@ namespace System.Xml.Schema
                        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 override int Validate(ValidationEventHandler h, XmlSchema schema)\r
                {\r
                        // 3.14.6 Properties Correct.\r
@@ -174,19 +376,27 @@ namespace System.Xml.Schema
                        }\r
                        recursed = true;\r
 \r
-                       errorCount += content.Validate (h, schema);\r
+                       CollectBaseType (h, schema);\r
+\r
+                       if (content != null)\r
+                               errorCount += content.Validate (h, schema);\r
+\r
+/*\r
                        // BaseSchemaType property\r
-                       this.baseSchemaTypeInternal = content.ActualBaseSchemaType;\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 = BaseSchemaType as XmlSchemaSimpleType;\r
+                       XmlSchemaSimpleType simple = BaseXmlSchemaType as XmlSchemaSimpleType;\r
                        if (simple != null)\r
-                               this.datatypeInternal = simple.Datatype;\r
-                       else\r
-                               this.datatypeInternal = BaseSchemaType as XmlSchemaDatatype;\r
+                               this.DatatypeInternal = simple.Datatype;\r
+//                     else\r
+//                             DatatypeInternal = BaseSchemaType as XmlSchemaDatatype;\r
 \r
                        // 3.\r
-                       XmlSchemaSimpleType baseSType = baseSchemaTypeInternal as XmlSchemaSimpleType;\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
@@ -201,8 +411,17 @@ namespace System.Xml.Schema
 \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 (BaseSchemaType, r.Facets, h, schema);\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
@@ -228,8 +447,8 @@ namespace System.Xml.Schema
                                // 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.");
@@ -294,11 +513,11 @@ namespace System.Xml.Schema
                        }
 
                        // 2.2.1
-                       if (BaseSchemaType == baseType)
+                       if (BaseXmlSchemaType == baseType || Datatype == baseType)
                                return true;
 
                        // 2.2.2
-                       XmlSchemaSimpleType thisBaseSimpleType = BaseSchemaType as XmlSchemaSimpleType;
+                       XmlSchemaSimpleType thisBaseSimpleType = BaseXmlSchemaType as XmlSchemaSimpleType;
                        if (thisBaseSimpleType != null) {
                                if (thisBaseSimpleType.ValidateTypeDerivationOK (baseType, h, schema, false))
                                        return true;
@@ -325,6 +544,11 @@ namespace System.Xml.Schema
                        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