**** Merged r36954 from MCS ****
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaSimpleType.cs
1 //\r
2 // System.Xml.Schema.XmlSchemaSimpleType.cs\r
3 //\r
4 // Author:\r
5 //      Dwivedi, Ajay kumar  Adwiv@Yahoo.com\r
6 //      Atsushi Enomoto  ginga@kit.hi-ho.ne.jp\r
7 //\r
8
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 using System;\r
30 using System.Xml.Serialization;\r
31 using System.Xml;\r
32 using Mono.Xml.Schema;\r
33 \r
34 namespace System.Xml.Schema\r
35 {\r
36         /// <summary>\r
37         /// Summary description for XmlSchemaSimpleType.\r
38         /// </summary>\r
39         public class XmlSchemaSimpleType : XmlSchemaType\r
40         {\r
41                 const string xmlname = "simpleType";\r
42                 private static XmlSchemaSimpleType schemaLocationType;\r
43 \r
44                 private XmlSchemaSimpleTypeContent content;\r
45                 //compilation vars\r
46                 internal bool islocal = true; // Assuming local means we have to specify islocal=false only in XmlSchema\r
47                 private bool recursed;\r
48                 private XmlSchemaDerivationMethod variety;\r
49 \r
50 #if NET_2_0\r
51                 // predefined simple types\r
52                 internal static readonly XmlSchemaSimpleType XsAnySimpleType;\r
53                 internal static readonly XmlSchemaSimpleType XsString;\r
54                 internal static readonly XmlSchemaSimpleType XsBoolean;\r
55                 internal static readonly XmlSchemaSimpleType XsDecimal;\r
56                 internal static readonly XmlSchemaSimpleType XsFloat;\r
57                 internal static readonly XmlSchemaSimpleType XsDouble;\r
58                 internal static readonly XmlSchemaSimpleType XsDuration;\r
59                 internal static readonly XmlSchemaSimpleType XsDateTime;\r
60                 internal static readonly XmlSchemaSimpleType XsTime;\r
61                 internal static readonly XmlSchemaSimpleType XsDate;\r
62                 internal static readonly XmlSchemaSimpleType XsGYearMonth;\r
63                 internal static readonly XmlSchemaSimpleType XsGYear;\r
64                 internal static readonly XmlSchemaSimpleType XsGMonthDay;\r
65                 internal static readonly XmlSchemaSimpleType XsGDay;\r
66                 internal static readonly XmlSchemaSimpleType XsGMonth;\r
67                 internal static readonly XmlSchemaSimpleType XsHexBinary;\r
68                 internal static readonly XmlSchemaSimpleType XsBase64Binary;\r
69                 internal static readonly XmlSchemaSimpleType XsAnyUri;\r
70                 internal static readonly XmlSchemaSimpleType XsQName;\r
71                 internal static readonly XmlSchemaSimpleType XsNotation;\r
72                 internal static readonly XmlSchemaSimpleType XsNormalizedString;\r
73                 internal static readonly XmlSchemaSimpleType XsToken;\r
74                 internal static readonly XmlSchemaSimpleType XsLanguage;\r
75                 internal static readonly XmlSchemaSimpleType XsNMToken;\r
76                 internal static readonly XmlSchemaSimpleType XsNMTokens;\r
77                 internal static readonly XmlSchemaSimpleType XsName;\r
78                 internal static readonly XmlSchemaSimpleType XsNCName;\r
79                 internal static readonly XmlSchemaSimpleType XsID;\r
80                 internal static readonly XmlSchemaSimpleType XsIDRef;\r
81                 internal static readonly XmlSchemaSimpleType XsIDRefs;\r
82                 internal static readonly XmlSchemaSimpleType XsEntity;\r
83                 internal static readonly XmlSchemaSimpleType XsEntities;\r
84                 internal static readonly XmlSchemaSimpleType XsInteger;\r
85                 internal static readonly XmlSchemaSimpleType XsNonPositiveInteger;\r
86                 internal static readonly XmlSchemaSimpleType XsNegativeInteger;\r
87                 internal static readonly XmlSchemaSimpleType XsLong;\r
88                 internal static readonly XmlSchemaSimpleType XsInt;\r
89                 internal static readonly XmlSchemaSimpleType XsShort;\r
90                 internal static readonly XmlSchemaSimpleType XsByte;\r
91                 internal static readonly XmlSchemaSimpleType XsNonNegativeInteger;\r
92                 internal static readonly XmlSchemaSimpleType XsUnsignedLong;\r
93                 internal static readonly XmlSchemaSimpleType XsUnsignedInt;\r
94                 internal static readonly XmlSchemaSimpleType XsUnsignedShort;\r
95                 internal static readonly XmlSchemaSimpleType XsUnsignedByte;\r
96                 internal static readonly XmlSchemaSimpleType XsPositiveInteger;\r
97                 // xdt:*\r
98                 internal static readonly XmlSchemaSimpleType XdtUntypedAtomic;\r
99                 internal static readonly XmlSchemaSimpleType XdtAnyAtomicType;\r
100                 internal static readonly XmlSchemaSimpleType XdtYearMonthDuration;\r
101                 internal static readonly XmlSchemaSimpleType XdtDayTimeDuration;\r
102 #endif\r
103 \r
104                 static XmlSchemaSimpleType ()\r
105                 {\r
106                         // This is not used in the meantime.\r
107                         XmlSchemaSimpleType st = new XmlSchemaSimpleType ();\r
108                         XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList ();\r
109                         list.ItemTypeName = new XmlQualifiedName ("anyURI", XmlSchema.Namespace);\r
110                         st.Content = list;\r
111                         st.BaseXmlSchemaTypeInternal = null;\r
112                         st.variety = XmlSchemaDerivationMethod.List;\r
113                         schemaLocationType = st;\r
114 \r
115 #if NET_2_0\r
116                         // Built-In schema types\r
117                         XsAnySimpleType = BuildSchemaType ("anySimpleType", null);\r
118                         XsString = BuildSchemaType ("string", "anySimpleType");\r
119                         XsBoolean = BuildSchemaType ("boolean", "anySimpleType");\r
120                         XsDecimal = BuildSchemaType ("decimal", "anySimpleType");\r
121                         XsFloat = BuildSchemaType ("float", "anySimpleType");\r
122                         XsDouble = BuildSchemaType ("double", "anySimpleType");\r
123                         XsDuration = BuildSchemaType ("duration", "anySimpleType");\r
124                         XsDateTime = BuildSchemaType ("dateTime", "anySimpleType");\r
125                         XsTime = BuildSchemaType ("time", "anySimpleType");\r
126                         XsDate = BuildSchemaType ("date", "anySimpleType");\r
127                         XsGYearMonth = BuildSchemaType ("gYearMonth", "anySimpleType");\r
128                         XsGYear = BuildSchemaType ("gYear", "anySimpleType");\r
129                         XsGMonthDay = BuildSchemaType ("gMonthDay", "anySimpleType");\r
130                         XsGDay = BuildSchemaType ("gDay", "anySimpleType");\r
131                         XsGMonth = BuildSchemaType ("gMonth", "anySimpleType");\r
132                         XsHexBinary = BuildSchemaType ("hexBinary", "anySimpleType");\r
133                         XsBase64Binary = BuildSchemaType ("base64Binary", "anySimpleType");\r
134                         XsAnyUri = BuildSchemaType ("anyURI", "anySimpleType");\r
135                         XsQName = BuildSchemaType ("QName", "anySimpleType");\r
136                         XsNotation = BuildSchemaType ("NOTATION", "anySimpleType");\r
137                         // derived types\r
138                         XsNormalizedString = BuildSchemaType ("normalizedString", "string");\r
139                         XsToken = BuildSchemaType ("token", "normalizedString");\r
140                         XsLanguage = BuildSchemaType ("language", "token");\r
141                         XsNMToken = BuildSchemaType ("NMTOKEN", "token");\r
142 \r
143                         XsID = BuildSchemaType ("ID", "NCName");\r
144                         XsIDRef = BuildSchemaType ("IDREF", "NCName");\r
145                         XsEntity = BuildSchemaType ("ENTITY", "NCName");\r
146 \r
147                         XsInteger = BuildSchemaType ("integer", "decimal");\r
148                         XsNonPositiveInteger = BuildSchemaType ("nonPositiveInteger", "integer");\r
149                         XsNegativeInteger = BuildSchemaType ("negativeInteger", "nonPositiveInteger");\r
150                         XsLong = BuildSchemaType ("long", "integer");\r
151                         XsInt = BuildSchemaType ("int", "long");\r
152                         XsShort = BuildSchemaType ("short", "int");\r
153                         XsByte = BuildSchemaType ("byte", "short");\r
154                         XsNonNegativeInteger = BuildSchemaType ("nonNegativeInteger", "integer");\r
155                         XsUnsignedLong = BuildSchemaType ("unsignedLong", "nonNegativeInteger");\r
156                         XsUnsignedInt = BuildSchemaType ("unsignedInt", "unsignedLong");\r
157                         XsUnsignedShort = BuildSchemaType ("unsignedShort ", "unsignedInt");\r
158                         XsUnsignedByte = BuildSchemaType ("unsignedByte", "unsignedByte");\r
159                         XsPositiveInteger = BuildSchemaType ("positiveInteger", "nonNegativeInteger");\r
160 \r
161                         // xdt:*\r
162                         XdtAnyAtomicType = BuildSchemaType ("anyAtomicType", "anySimpleType", true, false);\r
163                         XdtUntypedAtomic = BuildSchemaType ("untypedAtomic", "anyAtomicType", true, true);\r
164                         XdtDayTimeDuration = BuildSchemaType ("dayTimeDuration", "duration", true, false);\r
165                         XdtYearMonthDuration = BuildSchemaType ("yearMonthDuration", "duration", true, false);\r
166 \r
167                         // NMTOKENS, IDREFS, ENTITIES - lists\r
168                         XsIDRefs = new XmlSchemaSimpleType ();\r
169                         XmlSchemaSimpleTypeList sl = new XmlSchemaSimpleTypeList ();\r
170                         sl.ItemType = XsIDRef;\r
171                         XsIDRefs.Content = sl;\r
172                         XsEntities = new XmlSchemaSimpleType ();\r
173                         sl = new XmlSchemaSimpleTypeList ();\r
174                         sl.ItemType = XsEntity;\r
175                         XsEntities.Content = sl;\r
176                         XsNMTokens = new XmlSchemaSimpleType ();\r
177                         sl = new XmlSchemaSimpleTypeList ();\r
178                         sl.ItemType = XsNMToken;\r
179                         XsNMTokens.Content = sl;\r
180 #endif\r
181                 }\r
182 \r
183 #if NET_2_0\r
184                 private static XmlSchemaSimpleType BuildSchemaType (string name, string baseName)\r
185                 {\r
186                         return BuildSchemaType (name, baseName, false, false);\r
187                 }\r
188 \r
189                 private static XmlSchemaSimpleType BuildSchemaType (string name, string baseName, bool xdt, bool baseXdt)\r
190                 {\r
191                         string ns = xdt ? "http://www.w3.org/2003/11/xpath-datatypes" : XmlSchema.Namespace;\r
192                         string ns2 = baseXdt ? "http://www.w3.org/2003/11/xpath-datatypes" : XmlSchema.Namespace;\r
193                         XmlSchemaSimpleType st = new XmlSchemaSimpleType ();\r
194                         st.QNameInternal = new XmlQualifiedName (name, ns);\r
195                         if (baseName != null)\r
196                                 st.BaseXmlSchemaTypeInternal = XmlSchemaType. GetBuiltInSimpleType (new XmlQualifiedName (baseName, ns2));\r
197                         st.DatatypeInternal = XmlSchemaDatatype.FromName (st.QualifiedName);\r
198                         return st;\r
199                 }\r
200 #endif\r
201 \r
202                 internal static XsdAnySimpleType AnySimpleType {\r
203                         get { return XsdAnySimpleType.Instance; }\r
204                 }\r
205 \r
206                 internal static XmlSchemaSimpleType SchemaLocationType {\r
207                         get { return schemaLocationType; }\r
208                 }\r
209 \r
210                 public XmlSchemaSimpleType ()\r
211                 {\r
212                 }\r
213 \r
214                 [XmlElement("restriction",typeof(XmlSchemaSimpleTypeRestriction),Namespace=XmlSchema.Namespace)]\r
215                 [XmlElement("list",typeof(XmlSchemaSimpleTypeList),Namespace=XmlSchema.Namespace)]\r
216                 [XmlElement("union",typeof(XmlSchemaSimpleTypeUnion),Namespace=XmlSchema.Namespace)]\r
217                 public XmlSchemaSimpleTypeContent Content\r
218                 {\r
219                         get{ return  content; } \r
220                         set{ content = value; }\r
221                 }\r
222 \r
223                 internal XmlSchemaDerivationMethod Variety\r
224                 {\r
225                         get{ return variety; }\r
226                 }\r
227 \r
228                 /// <remarks>\r
229                 /// For a simple Type:\r
230                 ///             1. Content must be present\r
231                 ///             2. id if present, must have be a valid ID\r
232                 ///             a) If the simpletype is local\r
233                 ///                     1-      are from <xs:complexType name="simpleType"> and <xs:complexType name="localSimpleType">\r
234                 ///                     1. name  is prohibited\r
235                 ///                     2. final is prohibited\r
236                 ///             b) If the simpletype is toplevel\r
237                 ///                     1-  are from <xs:complexType name="simpleType"> and <xs:complexType name="topLevelSimpleType">\r
238                 ///                     1. name is required, type must be NCName\r
239                 ///                     2. Content is required\r
240                 ///                     3. final can have values : #all | (list | union | restriction)\r
241                 ///                     4. If final is set, finalResolved is same as final (but within the values of b.3)\r
242                 ///                     5. If final is not set, the finalDefault of the schema (ie. only #all and restriction)\r
243                 ///                     6. Base type is:\r
244                 ///                             4.1 If restriction is chosen,the base type of restriction or elements\r
245                 ///                             4.2 otherwise simple ur-type\r
246                 /// </remarks>\r
247                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)\r
248                 {\r
249                         // If this is already compiled this time, simply skip.\r
250                         if (this.IsComplied (schema.CompilationId))\r
251                                 return 0;\r
252 #if NET_2_0\r
253                         if (Content != null)\r
254                                 Content.Parent = this;\r
255 #endif\r
256 \r
257                         errorCount = 0;\r
258 \r
259                         if(this.islocal) // a\r
260                         {\r
261                                 if(this.Name != null) // a.1\r
262                                         error(h,"Name is prohibited in a local simpletype");\r
263                                 else\r
264                                         this.QNameInternal = new XmlQualifiedName(this.Name,schema.TargetNamespace);\r
265                                 if(this.Final != XmlSchemaDerivationMethod.None) //a.2\r
266                                         error(h,"Final is prohibited in a local simpletype");\r
267                         }\r
268                         else //b\r
269                         {\r
270                                 if(this.Name == null) //b.1\r
271                                         error(h,"Name is required in top level simpletype");\r
272                                 else if(!XmlSchemaUtil.CheckNCName(this.Name)) // b.1.2\r
273                                         error(h,"name attribute of a simpleType must be NCName");\r
274                                 else\r
275                                         this.QNameInternal = new XmlQualifiedName(this.Name,schema.TargetNamespace);\r
276                                 \r
277                                 //NOTE: Although the FinalResolved can be Empty, it is not a valid value for Final\r
278                                 //DEVIATION: If an error occurs, the finaldefault is always consulted. This deviates\r
279                                 //                       from the way MS implementation works.\r
280                                 switch(this.Final) //b.3, b.4\r
281                                 {\r
282                                         case XmlSchemaDerivationMethod.All:\r
283                                                 this.finalResolved = XmlSchemaDerivationMethod.All;\r
284                                                 break;\r
285                                         case XmlSchemaDerivationMethod.List:\r
286                                         case XmlSchemaDerivationMethod.Union:\r
287                                         case XmlSchemaDerivationMethod.Restriction:\r
288                                                 this.finalResolved = Final;\r
289                                                 break;\r
290                                         default:\r
291                                                 error(h,"The value of final attribute is not valid for simpleType");\r
292                                                 goto case XmlSchemaDerivationMethod.None;\r
293                                                 // use assignment from finaldefault on schema.\r
294                                         case XmlSchemaDerivationMethod.None: // b.5\r
295                                                 XmlSchemaDerivationMethod flags = \r
296                                                         (XmlSchemaDerivationMethod.Restriction | XmlSchemaDerivationMethod.List |\r
297                                                         XmlSchemaDerivationMethod.Extension | XmlSchemaDerivationMethod.Union );\r
298                                                 switch (schema.FinalDefault) {\r
299                                                 case XmlSchemaDerivationMethod.All:\r
300                                                         finalResolved = XmlSchemaDerivationMethod.All;\r
301                                                         break;\r
302                                                 case XmlSchemaDerivationMethod.None:\r
303                                                         finalResolved = XmlSchemaDerivationMethod.Empty;\r
304                                                         break;\r
305                                                 default:\r
306                                                         finalResolved = schema.FinalDefault & flags;\r
307                                                         break;\r
308                                                 }\r
309                                                 break;\r
310                                 }\r
311                         }\r
312 \r
313                         XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);\r
314 \r
315                         if (Content != null)\r
316                                 Content.OwnerType = this;\r
317 \r
318                         if(this.Content == null) //a.3,b.2\r
319                                 error(h,"Content is required in a simpletype");\r
320                         else if(Content is XmlSchemaSimpleTypeRestriction)\r
321                         {\r
322                                 this.resolvedDerivedBy = XmlSchemaDerivationMethod.Restriction;\r
323                                 errorCount += ((XmlSchemaSimpleTypeRestriction)Content).Compile(h,schema);\r
324                         }\r
325                         else if(Content is XmlSchemaSimpleTypeList)\r
326                         {\r
327                                 this.resolvedDerivedBy = XmlSchemaDerivationMethod.List;\r
328                                 errorCount += ((XmlSchemaSimpleTypeList)Content).Compile(h,schema);\r
329                         }\r
330                         else if(Content is XmlSchemaSimpleTypeUnion)\r
331                         {\r
332                                 this.resolvedDerivedBy = XmlSchemaDerivationMethod.Union;\r
333                                 errorCount += ((XmlSchemaSimpleTypeUnion)Content).Compile(h,schema);\r
334                         }\r
335 \r
336                         this.CompilationId = schema.CompilationId;\r
337                         return errorCount;\r
338                 }\r
339 \r
340                 internal void CollectBaseType (ValidationEventHandler h, XmlSchema schema)\r
341                 {\r
342                         if (Content is XmlSchemaSimpleTypeRestriction) {\r
343                                 object o = ((XmlSchemaSimpleTypeRestriction) Content).GetActualType (h, schema, false);\r
344                                 BaseXmlSchemaTypeInternal = o as XmlSchemaSimpleType;\r
345                                 if (BaseXmlSchemaTypeInternal != null)\r
346                                         DatatypeInternal = BaseXmlSchemaTypeInternal.Datatype;\r
347                                 else\r
348                                         DatatypeInternal = o as XmlSchemaDatatype;\r
349                         }\r
350                         // otherwise, actualBaseSchemaType is null\r
351                         else\r
352                                 DatatypeInternal = XmlSchemaSimpleType.AnySimpleType;\r
353                 }\r
354                 \r
355                 internal override int Validate(ValidationEventHandler h, XmlSchema schema)\r
356                 {\r
357                         // 3.14.6 Properties Correct.\r
358                         // \r
359                         // 1. Post Compilation Properties\r
360                         // {name}, {target namespace} => QNameInternal. Already Compile()d.\r
361                         // {base type definition} => baseSchemaTypeInternal\r
362                         // {final} => finalResolved. Already Compile()d.\r
363                         // {variety} => resolvedDerivedBy. Already Compile()d.\r
364                         //\r
365                         // 2. Should be checked by "recursed" field.\r
366 \r
367                         if(IsValidated (schema.ValidationId))\r
368                                 return errorCount;\r
369 \r
370                         if (recursed) {\r
371                                 error (h, "Circular type reference was found.");\r
372                                 return errorCount;\r
373                         }\r
374                         recursed = true;\r
375 \r
376                         CollectBaseType (h, schema);\r
377 \r
378                         if (content != null)\r
379                                 errorCount += content.Validate (h, schema);\r
380 \r
381 /*\r
382                         // BaseSchemaType property\r
383                         BaseXmlSchemaTypeInternal = content.ActualBaseSchemaType as XmlSchemaType;\r
384                         if (this.BaseXmlSchemaTypeInternal == null)\r
385                                 this.DatatypeInternal = content.ActualBaseSchemaType as XmlSchemaDatatype;\r
386 */\r
387 \r
388                         // Datatype property\r
389                         XmlSchemaSimpleType simple = BaseXmlSchemaType as XmlSchemaSimpleType;\r
390                         if (simple != null)\r
391                                 this.DatatypeInternal = simple.Datatype;\r
392 //                      else\r
393 //                              DatatypeInternal = BaseSchemaType as XmlSchemaDatatype;\r
394 \r
395                         // 3.\r
396                         XmlSchemaSimpleType baseSType = BaseXmlSchemaType as XmlSchemaSimpleType;\r
397                         if (baseSType != null) {\r
398                                 if ((baseSType.FinalResolved & this.resolvedDerivedBy) != 0)\r
399                                         error (h, "Specified derivation is prohibited by the base simple type.");\r
400                         }\r
401 \r
402                         // {variety}\r
403                         if (this.resolvedDerivedBy == XmlSchemaDerivationMethod.Restriction &&\r
404                                 baseSType != null)\r
405                                 this.variety = baseSType.Variety;\r
406                         else\r
407                                 this.variety = this.resolvedDerivedBy;\r
408 \r
409                         // 3.14.6 Derivation Valid (Restriction, Simple)\r
410                         XmlSchemaSimpleTypeRestriction r = Content as XmlSchemaSimpleTypeRestriction;\r
411                         object baseType = BaseXmlSchemaType != null ? (object) BaseXmlSchemaType : Datatype;\r
412                         if (r != null)\r
413                                 ValidateDerivationValid (baseType, r.Facets, h, schema);\r
414 \r
415                         // TODO: describe which validation term this belongs to.\r
416                         XmlSchemaSimpleTypeList l = Content as XmlSchemaSimpleTypeList;\r
417                         if (l != null) {\r
418                                 XmlSchemaSimpleType itemSimpleType = l.ValidatedListItemType as XmlSchemaSimpleType;\r
419                                 if (itemSimpleType != null && itemSimpleType.Content is XmlSchemaSimpleTypeList)\r
420                                         error (h, "List type must not be derived from another list type.");\r
421                         }\r
422 \r
423                         recursed = false;\r
424                         ValidationId = schema.ValidationId;\r
425                         return errorCount;\r
426                 }\r
427 \r
428                 // 3.14.6 Derivation Valid (RestrictionSimple)
429                 internal void ValidateDerivationValid (object baseType, XmlSchemaObjectCollection facets,
430                         ValidationEventHandler h, XmlSchema schema)
431                 {
432                         // TODO
433                         XmlSchemaSimpleType baseSimpleType = baseType as XmlSchemaSimpleType;
434                         switch (this.Variety) {
435                         // 1. atomic type
436                         case XmlSchemaDerivationMethod.Restriction:
437                                 // 1.1
438                                 if (baseSimpleType != null && baseSimpleType.resolvedDerivedBy != XmlSchemaDerivationMethod.Restriction)
439                                         error (h, "Base schema type is not either atomic type or primitive type.");
440                                 // 1.2
441                                 if (baseSimpleType != null && 
442                                         (baseSimpleType.FinalResolved & XmlSchemaDerivationMethod.Restriction) != 0)
443                                         error (h, "Derivation by restriction is prohibited by the base simple type.");
444                                 // TODO: 1.3 facet restriction valid.
445                                 break;
446                         case XmlSchemaDerivationMethod.List:
447                                 /*\r
448                                 XmlSchemaSimpleTypeList thisList = Content as XmlSchemaSimpleTypeList;
449                                 // 2.1 item list type not allowed
450                                 if (baseSimpleType != null && baseSimpleType.resolvedDerivedBy == XmlSchemaDerivationMethod.List)
451                                         error (h, "Base list schema type is not allowed.");
452                                 XmlSchemaSimpleTypeUnion baseUnion = baseSimpleType.Content as XmlSchemaSimpleTypeUnion;
453                                 if (baseUnion != null) {
454                                         bool errorFound = false;
455                                         foreach (object memberType in baseUnion.ValidatedTypes) {
456                                                 XmlSchemaSimpleType memberST = memberType as XmlSchemaSimpleType;
457                                                 if (memberST != null && memberST.resolvedDerivedBy == XmlSchemaDerivationMethod.List)
458                                                         errorFound = true;
459                                         }
460                                         if (errorFound)
461                                                 error (h, "Base union schema type should not contain list types.");
462                                 }
463                                 */
464                                 // 2.2 facets limited
465                                 if (facets != null)
466                                         foreach (XmlSchemaFacet facet in facets) {
467                                                 if (facet is XmlSchemaLengthFacet ||
468                                                         facet is XmlSchemaMaxLengthFacet ||
469                                                         facet is XmlSchemaMinLengthFacet ||
470                                                         facet is XmlSchemaEnumerationFacet ||
471                                                         facet is XmlSchemaPatternFacet)
472                                                         continue;
473                                                 else
474                                                         error (h, "Not allowed facet was found on this simple type which derives list type.");
475                                         }
476                                 break;
477                         case XmlSchemaDerivationMethod.Union:
478                                 // 3.1
479
480                                 // 3.2
481                                 if (facets != null)
482                                         foreach (XmlSchemaFacet facet in facets) {
483                                                 if (facet is XmlSchemaEnumerationFacet ||
484                                                         facet is XmlSchemaPatternFacet)
485                                                         continue;
486                                                 else
487                                                         error (h, "Not allowed facet was found on this simple type which derives list type.");
488                                         }
489                                 break;
490                         }
491                 }
492
493                 // 3.14.6 Type Derivation OK (Simple)
494                 internal bool ValidateTypeDerivationOK (object baseType,
495                         ValidationEventHandler h, XmlSchema schema, bool raiseError)
496                 {
497                         // 1
498                         // Note that anyType should also be allowed as anySimpleType.
499                         if (this == baseType || baseType == XmlSchemaSimpleType.AnySimpleType ||
500                                 baseType == XmlSchemaComplexType.AnyType)
501                                 return true;
502
503                         // 2.1
504                         XmlSchemaSimpleType baseSimpleType = baseType as XmlSchemaSimpleType;
505                         if (baseSimpleType != null && 
506                                 (baseSimpleType.FinalResolved & resolvedDerivedBy) != 0) {
507                                 if (raiseError)
508                                         error (h, "Specified derivation is prohibited by the base type.");
509                                 return false;
510                         }
511
512                         // 2.2.1
513                         if (BaseXmlSchemaType == baseType || Datatype == baseType)
514                                 return true;
515
516                         // 2.2.2
517                         XmlSchemaSimpleType thisBaseSimpleType = BaseXmlSchemaType as XmlSchemaSimpleType;
518                         if (thisBaseSimpleType != null) {
519                                 if (thisBaseSimpleType.ValidateTypeDerivationOK (baseType, h, schema, false))
520                                         return true;
521                         }
522
523                         // 2.2.3
524                         switch (Variety) {
525                         case XmlSchemaDerivationMethod.Union:
526                         case XmlSchemaDerivationMethod.List:
527                                 if (baseType == XmlSchemaSimpleType.AnySimpleType)
528                                         return true;
529                                 break;
530                         }
531
532                         // 2.2.4 validly derived from one of the union member type.
533                         if (baseSimpleType != null && baseSimpleType.Variety == XmlSchemaDerivationMethod.Union) {
534                                 foreach (object memberType in ((XmlSchemaSimpleTypeUnion) baseSimpleType.Content).ValidatedTypes)
535                                         if (this.ValidateTypeDerivationOK (memberType, h, schema, false))
536                                                 return true;
537                         }
538
539                         if (raiseError)
540                                 error(h, "Invalid simple type derivation was found.");
541                         return false;
542                 }
543
544                 internal string Normalize (string s, XmlNameTable nt, XmlNamespaceManager nsmgr)
545                 {
546                         return Content.Normalize (s, nt, nsmgr);
547                 }
548
549                 //<simpleType \r
550                 //  final = (#all | (list | union | restriction)) \r
551                 //  id = ID \r
552                 //  name = NCName \r
553                 //  {any attributes with non-schema namespace . . .}>\r
554                 //  Content: (annotation?, (restriction | list | union))\r
555                 //</simpleType>\r
556                 internal static XmlSchemaSimpleType Read(XmlSchemaReader reader, ValidationEventHandler h)\r
557                 {\r
558                         XmlSchemaSimpleType stype = new XmlSchemaSimpleType();\r
559                         reader.MoveToElement();\r
560 \r
561                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
562                         {\r
563                                 error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);\r
564                                 reader.Skip();\r
565                                 return null;\r
566                         }\r
567 \r
568                         stype.LineNumber = reader.LineNumber;\r
569                         stype.LinePosition = reader.LinePosition;\r
570                         stype.SourceUri = reader.BaseURI;\r
571 \r
572                         while(reader.MoveToNextAttribute())\r
573                         {\r
574                                 if(reader.Name == "final")\r
575                                 {\r
576                                         Exception innerex;\r
577                                         stype.Final = XmlSchemaUtil.ReadDerivationAttribute(reader, out innerex, "final",\r
578                                                 XmlSchemaUtil.FinalAllowed);\r
579                                         if(innerex != null)\r
580                                                 error(h, "some invalid values not a valid value for final", innerex);\r
581                                 }\r
582                                 else if(reader.Name == "id")\r
583                                 {\r
584                                         stype.Id = reader.Value;\r
585                                 }\r
586                                 else if(reader.Name == "name")\r
587                                 {\r
588                                         stype.Name = reader.Value;\r
589                                 }\r
590                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
591                                 {\r
592                                         error(h,reader.Name + " is not a valid attribute for simpleType",null);\r
593                                 }\r
594                                 else\r
595                                 {\r
596                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,stype);\r
597                                 }\r
598                         }\r
599                         \r
600                         reader.MoveToElement();\r
601                         if(reader.IsEmptyElement)\r
602                                 return stype;\r
603 \r
604                         //      Content: (annotation?, (restriction | list | union))\r
605                         int level = 1;\r
606                         while(reader.ReadNextElement())\r
607                         {\r
608                                 if(reader.NodeType == XmlNodeType.EndElement)\r
609                                 {\r
610                                         if(reader.LocalName != xmlname)\r
611                                                 error(h,"Should not happen :2: XmlSchemaSimpleType.Read, name="+reader.Name,null);\r
612                                         break;\r
613                                 }\r
614                                 if(level <= 1 && reader.LocalName == "annotation")\r
615                                 {\r
616                                         level = 2; //Only one annotation\r
617                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
618                                         if(annotation != null)\r
619                                                 stype.Annotation = annotation;\r
620                                         continue;\r
621                                 }\r
622                                 if(level <= 2)\r
623                                 {\r
624                                         if(reader.LocalName == "restriction")\r
625                                         {\r
626                                                 level = 3;\r
627                                                 XmlSchemaSimpleTypeRestriction restriction = XmlSchemaSimpleTypeRestriction.Read(reader,h);\r
628                                                 if(restriction != null)\r
629                                                         stype.content = restriction;\r
630                                                 continue;\r
631                                         }\r
632                                         if(reader.LocalName == "list")\r
633                                         {\r
634                                                 level = 3;\r
635                                                 XmlSchemaSimpleTypeList list = XmlSchemaSimpleTypeList.Read(reader,h);\r
636                                                 if(list != null)\r
637                                                         stype.content = list;\r
638                                                 continue;\r
639                                         }\r
640                                         if(reader.LocalName == "union")\r
641                                         {\r
642                                                 level = 3;\r
643                                                 XmlSchemaSimpleTypeUnion union = XmlSchemaSimpleTypeUnion.Read(reader,h);\r
644                                                 if(union != null)\r
645                                                         stype.content = union;\r
646                                                 continue;\r
647                                         }\r
648                                 }\r
649                                 reader.RaiseInvalidElementError();\r
650                         }\r
651                         return stype;\r
652                 }\r
653 \r
654 \r
655         }\r
656 }\r