merge -r 58060:58217
[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                         XsName = BuildSchemaType ("Name", "token");\r
143                         XsNCName = BuildSchemaType ("NCName", "Name");\r
144 \r
145                         XsID = BuildSchemaType ("ID", "NCName");\r
146                         XsIDRef = BuildSchemaType ("IDREF", "NCName");\r
147                         XsEntity = BuildSchemaType ("ENTITY", "NCName");\r
148 \r
149                         XsInteger = BuildSchemaType ("integer", "decimal");\r
150                         XsNonPositiveInteger = BuildSchemaType ("nonPositiveInteger", "integer");\r
151                         XsNegativeInteger = BuildSchemaType ("negativeInteger", "nonPositiveInteger");\r
152                         XsLong = BuildSchemaType ("long", "integer");\r
153                         XsInt = BuildSchemaType ("int", "long");\r
154                         XsShort = BuildSchemaType ("short", "int");\r
155                         XsByte = BuildSchemaType ("byte", "short");\r
156                         XsNonNegativeInteger = BuildSchemaType ("nonNegativeInteger", "integer");\r
157                         XsUnsignedLong = BuildSchemaType ("unsignedLong", "nonNegativeInteger");\r
158                         XsUnsignedInt = BuildSchemaType ("unsignedInt", "unsignedLong");\r
159                         XsUnsignedShort = BuildSchemaType ("unsignedShort", "unsignedInt");\r
160                         XsUnsignedByte = BuildSchemaType ("unsignedByte", "unsignedShort");\r
161                         XsPositiveInteger = BuildSchemaType ("positiveInteger", "nonNegativeInteger");\r
162 \r
163                         // xdt:*\r
164                         XdtAnyAtomicType = BuildSchemaType ("anyAtomicType", "anySimpleType", true, false);\r
165                         XdtUntypedAtomic = BuildSchemaType ("untypedAtomic", "anyAtomicType", true, true);\r
166                         XdtDayTimeDuration = BuildSchemaType ("dayTimeDuration", "duration", true, false);\r
167                         XdtYearMonthDuration = BuildSchemaType ("yearMonthDuration", "duration", true, false);\r
168 \r
169                         // NMTOKENS, IDREFS, ENTITIES - lists\r
170                         XsIDRefs = new XmlSchemaSimpleType ();\r
171                         XmlSchemaSimpleTypeList sl = new XmlSchemaSimpleTypeList ();\r
172                         sl.ItemType = XsIDRef;\r
173                         XsIDRefs.Content = sl;\r
174                         XsEntities = new XmlSchemaSimpleType ();\r
175                         sl = new XmlSchemaSimpleTypeList ();\r
176                         sl.ItemType = XsEntity;\r
177                         XsEntities.Content = sl;\r
178                         XsNMTokens = new XmlSchemaSimpleType ();\r
179                         sl = new XmlSchemaSimpleTypeList ();\r
180                         sl.ItemType = XsNMToken;\r
181                         XsNMTokens.Content = sl;\r
182 #endif\r
183                 }\r
184 \r
185 #if NET_2_0\r
186                 private static XmlSchemaSimpleType BuildSchemaType (string name, string baseName)\r
187                 {\r
188                         return BuildSchemaType (name, baseName, false, false);\r
189                 }\r
190 \r
191                 private static XmlSchemaSimpleType BuildSchemaType (string name, string baseName, bool xdt, bool baseXdt)\r
192                 {\r
193                         string ns = xdt ? "http://www.w3.org/2003/11/xpath-datatypes" : XmlSchema.Namespace;\r
194                         string ns2 = baseXdt ? "http://www.w3.org/2003/11/xpath-datatypes" : XmlSchema.Namespace;\r
195                         XmlSchemaSimpleType st = new XmlSchemaSimpleType ();\r
196                         st.QNameInternal = new XmlQualifiedName (name, ns);\r
197                         if (baseName != null)\r
198                                 st.BaseXmlSchemaTypeInternal = XmlSchemaType. GetBuiltInSimpleType (new XmlQualifiedName (baseName, ns2));\r
199                         st.DatatypeInternal = XmlSchemaDatatype.FromName (st.QualifiedName);\r
200                         return st;\r
201                 }\r
202 #endif\r
203 \r
204                 internal static XsdAnySimpleType AnySimpleType {\r
205                         get { return XsdAnySimpleType.Instance; }\r
206                 }\r
207 \r
208                 internal static XmlSchemaSimpleType SchemaLocationType {\r
209                         get { return schemaLocationType; }\r
210                 }\r
211 \r
212                 public XmlSchemaSimpleType ()\r
213                 {\r
214                 }\r
215 \r
216                 [XmlElement("restriction",typeof(XmlSchemaSimpleTypeRestriction))]\r
217                 [XmlElement("list",typeof(XmlSchemaSimpleTypeList))]\r
218                 [XmlElement("union",typeof(XmlSchemaSimpleTypeUnion))]\r
219                 public XmlSchemaSimpleTypeContent Content\r
220                 {\r
221                         get{ return  content; } \r
222                         set{ content = value; }\r
223                 }\r
224 \r
225                 internal XmlSchemaDerivationMethod Variety\r
226                 {\r
227                         get{ return variety; }\r
228                 }\r
229 \r
230                 /// <remarks>\r
231                 /// For a simple Type:\r
232                 ///             1. Content must be present\r
233                 ///             2. id if present, must have be a valid ID\r
234                 ///             a) If the simpletype is local\r
235                 ///                     1-      are from <xs:complexType name="simpleType"> and <xs:complexType name="localSimpleType">\r
236                 ///                     1. name  is prohibited\r
237                 ///                     2. final is prohibited\r
238                 ///             b) If the simpletype is toplevel\r
239                 ///                     1-  are from <xs:complexType name="simpleType"> and <xs:complexType name="topLevelSimpleType">\r
240                 ///                     1. name is required, type must be NCName\r
241                 ///                     2. Content is required\r
242                 ///                     3. final can have values : #all | (list | union | restriction)\r
243                 ///                     4. If final is set, finalResolved is same as final (but within the values of b.3)\r
244                 ///                     5. If final is not set, the finalDefault of the schema (ie. only #all and restriction)\r
245                 ///                     6. Base type is:\r
246                 ///                             4.1 If restriction is chosen,the base type of restriction or elements\r
247                 ///                             4.2 otherwise simple ur-type\r
248                 /// </remarks>\r
249                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)\r
250                 {\r
251                         // If this is already compiled this time, simply skip.\r
252                         if (this.IsComplied (schema.CompilationId))\r
253                                 return 0;\r
254 #if NET_2_0\r
255                         if (Content != null)\r
256                                 Content.Parent = this;\r
257 #endif\r
258 \r
259                         errorCount = 0;\r
260 \r
261                         if(this.islocal) // a\r
262                         {\r
263                                 if(this.Name != null) // a.1\r
264                                         error(h,"Name is prohibited in a local simpletype");\r
265                                 else\r
266                                         this.QNameInternal = new XmlQualifiedName(this.Name,schema.TargetNamespace);\r
267                                 if(this.Final != XmlSchemaDerivationMethod.None) //a.2\r
268                                         error(h,"Final is prohibited in a local simpletype");\r
269                         }\r
270                         else //b\r
271                         {\r
272                                 if(this.Name == null) //b.1\r
273                                         error(h,"Name is required in top level simpletype");\r
274                                 else if(!XmlSchemaUtil.CheckNCName(this.Name)) // b.1.2\r
275                                         error(h,"name attribute of a simpleType must be NCName");\r
276                                 else\r
277                                         this.QNameInternal = new XmlQualifiedName(this.Name,schema.TargetNamespace);\r
278                                 \r
279                                 //NOTE: Although the FinalResolved can be Empty, it is not a valid value for Final\r
280                                 //DEVIATION: If an error occurs, the finaldefault is always consulted. This deviates\r
281                                 //                       from the way MS implementation works.\r
282                                 switch(this.Final) //b.3, b.4\r
283                                 {\r
284                                         case XmlSchemaDerivationMethod.All:\r
285                                                 this.finalResolved = XmlSchemaDerivationMethod.All;\r
286                                                 break;\r
287                                         case XmlSchemaDerivationMethod.List:\r
288                                         case XmlSchemaDerivationMethod.Union:\r
289                                         case XmlSchemaDerivationMethod.Restriction:\r
290                                                 this.finalResolved = Final;\r
291                                                 break;\r
292                                         default:\r
293                                                 error(h,"The value of final attribute is not valid for simpleType");\r
294                                                 goto case XmlSchemaDerivationMethod.None;\r
295                                                 // use assignment from finaldefault on schema.\r
296                                         case XmlSchemaDerivationMethod.None: // b.5\r
297                                                 XmlSchemaDerivationMethod flags = \r
298                                                         (XmlSchemaDerivationMethod.Restriction | XmlSchemaDerivationMethod.List |\r
299                                                         XmlSchemaDerivationMethod.Extension | XmlSchemaDerivationMethod.Union );\r
300                                                 switch (schema.FinalDefault) {\r
301                                                 case XmlSchemaDerivationMethod.All:\r
302                                                         finalResolved = XmlSchemaDerivationMethod.All;\r
303                                                         break;\r
304                                                 case XmlSchemaDerivationMethod.None:\r
305                                                         finalResolved = XmlSchemaDerivationMethod.Empty;\r
306                                                         break;\r
307                                                 default:\r
308                                                         finalResolved = schema.FinalDefault & flags;\r
309                                                         break;\r
310                                                 }\r
311                                                 break;\r
312                                 }\r
313                         }\r
314 \r
315                         XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);\r
316 \r
317                         if (Content != null)\r
318                                 Content.OwnerType = this;\r
319 \r
320                         if(this.Content == null) //a.3,b.2\r
321                                 error(h,"Content is required in a simpletype");\r
322                         else if(Content is XmlSchemaSimpleTypeRestriction)\r
323                         {\r
324                                 this.resolvedDerivedBy = XmlSchemaDerivationMethod.Restriction;\r
325                                 errorCount += ((XmlSchemaSimpleTypeRestriction)Content).Compile(h,schema);\r
326                         }\r
327                         else if(Content is XmlSchemaSimpleTypeList)\r
328                         {\r
329                                 this.resolvedDerivedBy = XmlSchemaDerivationMethod.List;\r
330                                 errorCount += ((XmlSchemaSimpleTypeList)Content).Compile(h,schema);\r
331                         }\r
332                         else if(Content is XmlSchemaSimpleTypeUnion)\r
333                         {\r
334                                 this.resolvedDerivedBy = XmlSchemaDerivationMethod.Union;\r
335                                 errorCount += ((XmlSchemaSimpleTypeUnion)Content).Compile(h,schema);\r
336                         }\r
337 \r
338                         this.CompilationId = schema.CompilationId;\r
339                         return errorCount;\r
340                 }\r
341 \r
342                 internal void CollectBaseType (ValidationEventHandler h, XmlSchema schema)\r
343                 {\r
344                         if (Content is XmlSchemaSimpleTypeRestriction) {\r
345                                 object o = ((XmlSchemaSimpleTypeRestriction) Content).GetActualType (h, schema, false);\r
346                                 BaseXmlSchemaTypeInternal = o as XmlSchemaSimpleType;\r
347                                 if (BaseXmlSchemaTypeInternal != null)\r
348                                         DatatypeInternal = BaseXmlSchemaTypeInternal.Datatype;\r
349                                 else\r
350                                         DatatypeInternal = o as XmlSchemaDatatype;\r
351                         }\r
352                         // otherwise, actualBaseSchemaType is null\r
353                         else\r
354                                 DatatypeInternal = XmlSchemaSimpleType.AnySimpleType;\r
355                 }\r
356                 \r
357                 internal override int Validate(ValidationEventHandler h, XmlSchema schema)\r
358                 {\r
359                         // 3.14.6 Properties Correct.\r
360                         // \r
361                         // 1. Post Compilation Properties\r
362                         // {name}, {target namespace} => QNameInternal. Already Compile()d.\r
363                         // {base type definition} => baseSchemaTypeInternal\r
364                         // {final} => finalResolved. Already Compile()d.\r
365                         // {variety} => resolvedDerivedBy. Already Compile()d.\r
366                         //\r
367                         // 2. Should be checked by "recursed" field.\r
368 \r
369                         if(IsValidated (schema.ValidationId))\r
370                                 return errorCount;\r
371 \r
372                         if (recursed) {\r
373                                 error (h, "Circular type reference was found.");\r
374                                 return errorCount;\r
375                         }\r
376                         recursed = true;\r
377 \r
378                         CollectBaseType (h, schema);\r
379 \r
380                         if (content != null)\r
381                                 errorCount += content.Validate (h, schema);\r
382 \r
383 /*\r
384                         // BaseSchemaType property\r
385                         BaseXmlSchemaTypeInternal = content.ActualBaseSchemaType as XmlSchemaType;\r
386                         if (this.BaseXmlSchemaTypeInternal == null)\r
387                                 this.DatatypeInternal = content.ActualBaseSchemaType as XmlSchemaDatatype;\r
388 */\r
389 \r
390                         // Datatype property\r
391                         XmlSchemaSimpleType simple = BaseXmlSchemaType as XmlSchemaSimpleType;\r
392                         if (simple != null)\r
393                                 this.DatatypeInternal = simple.Datatype;\r
394 //                      else\r
395 //                              DatatypeInternal = BaseSchemaType as XmlSchemaDatatype;\r
396 \r
397                         // 3.\r
398                         XmlSchemaSimpleType baseSType = BaseXmlSchemaType as XmlSchemaSimpleType;\r
399                         if (baseSType != null) {\r
400                                 if ((baseSType.FinalResolved & this.resolvedDerivedBy) != 0)\r
401                                         error (h, "Specified derivation is prohibited by the base simple type.");\r
402                         }\r
403 \r
404                         // {variety}\r
405                         if (this.resolvedDerivedBy == XmlSchemaDerivationMethod.Restriction &&\r
406                                 baseSType != null)\r
407                                 this.variety = baseSType.Variety;\r
408                         else\r
409                                 this.variety = this.resolvedDerivedBy;\r
410 \r
411                         // 3.14.6 Derivation Valid (Restriction, Simple)\r
412                         XmlSchemaSimpleTypeRestriction r = Content as XmlSchemaSimpleTypeRestriction;\r
413                         object baseType = BaseXmlSchemaType != null ? (object) BaseXmlSchemaType : Datatype;\r
414                         if (r != null)\r
415                                 ValidateDerivationValid (baseType, r.Facets, h, schema);\r
416 \r
417                         // TODO: describe which validation term this belongs to.\r
418                         XmlSchemaSimpleTypeList l = Content as XmlSchemaSimpleTypeList;\r
419                         if (l != null) {\r
420                                 XmlSchemaSimpleType itemSimpleType = l.ValidatedListItemType as XmlSchemaSimpleType;\r
421                                 if (itemSimpleType != null && itemSimpleType.Content is XmlSchemaSimpleTypeList)\r
422                                         error (h, "List type must not be derived from another list type.");\r
423                         }\r
424 \r
425                         recursed = false;\r
426                         ValidationId = schema.ValidationId;\r
427                         return errorCount;\r
428                 }\r
429 \r
430                 // 3.14.6 Derivation Valid (RestrictionSimple)
431                 internal void ValidateDerivationValid (object baseType, XmlSchemaObjectCollection facets,
432                         ValidationEventHandler h, XmlSchema schema)
433                 {
434                         // TODO
435                         XmlSchemaSimpleType baseSimpleType = baseType as XmlSchemaSimpleType;
436                         switch (this.Variety) {
437                         // 1. atomic type
438                         case XmlSchemaDerivationMethod.Restriction:
439                                 // 1.1
440                                 if (baseSimpleType != null && baseSimpleType.resolvedDerivedBy != XmlSchemaDerivationMethod.Restriction)
441                                         error (h, "Base schema type is not either atomic type or primitive type.");
442                                 // 1.2
443                                 if (baseSimpleType != null && 
444                                         (baseSimpleType.FinalResolved & XmlSchemaDerivationMethod.Restriction) != 0)
445                                         error (h, "Derivation by restriction is prohibited by the base simple type.");
446                                 // TODO: 1.3 facet restriction valid.
447                                 break;
448                         case XmlSchemaDerivationMethod.List:
449                                 /*\r
450                                 XmlSchemaSimpleTypeList thisList = Content as XmlSchemaSimpleTypeList;
451                                 // 2.1 item list type not allowed
452                                 if (baseSimpleType != null && baseSimpleType.resolvedDerivedBy == XmlSchemaDerivationMethod.List)
453                                         error (h, "Base list schema type is not allowed.");
454                                 XmlSchemaSimpleTypeUnion baseUnion = baseSimpleType.Content as XmlSchemaSimpleTypeUnion;
455                                 if (baseUnion != null) {
456                                         bool errorFound = false;
457                                         foreach (object memberType in baseUnion.ValidatedTypes) {
458                                                 XmlSchemaSimpleType memberST = memberType as XmlSchemaSimpleType;
459                                                 if (memberST != null && memberST.resolvedDerivedBy == XmlSchemaDerivationMethod.List)
460                                                         errorFound = true;
461                                         }
462                                         if (errorFound)
463                                                 error (h, "Base union schema type should not contain list types.");
464                                 }
465                                 */
466                                 // 2.2 facets limited
467                                 if (facets != null)
468                                         foreach (XmlSchemaFacet facet in facets) {
469                                                 if (facet is XmlSchemaLengthFacet ||
470                                                         facet is XmlSchemaMaxLengthFacet ||
471                                                         facet is XmlSchemaMinLengthFacet ||
472                                                         facet is XmlSchemaEnumerationFacet ||
473                                                         facet is XmlSchemaPatternFacet)
474                                                         continue;
475                                                 else
476                                                         error (h, "Not allowed facet was found on this simple type which derives list type.");
477                                         }
478                                 break;
479                         case XmlSchemaDerivationMethod.Union:
480                                 // 3.1
481
482                                 // 3.2
483                                 if (facets != null)
484                                         foreach (XmlSchemaFacet facet in facets) {
485                                                 if (facet is XmlSchemaEnumerationFacet ||
486                                                         facet is XmlSchemaPatternFacet)
487                                                         continue;
488                                                 else
489                                                         error (h, "Not allowed facet was found on this simple type which derives list type.");
490                                         }
491                                 break;
492                         }
493                 }
494
495                 // 3.14.6 Type Derivation OK (Simple)
496                 internal bool ValidateTypeDerivationOK (object baseType,
497                         ValidationEventHandler h, XmlSchema schema, bool raiseError)
498                 {
499                         // 1
500                         // Note that anyType should also be allowed as anySimpleType.
501                         if (this == baseType || baseType == XmlSchemaSimpleType.AnySimpleType ||
502                                 baseType == XmlSchemaComplexType.AnyType)
503                                 return true;
504
505                         // 2.1
506                         XmlSchemaSimpleType baseSimpleType = baseType as XmlSchemaSimpleType;
507                         if (baseSimpleType != null && 
508                                 (baseSimpleType.FinalResolved & resolvedDerivedBy) != 0) {
509                                 if (raiseError)
510                                         error (h, "Specified derivation is prohibited by the base type.");
511                                 return false;
512                         }
513
514                         // 2.2.1
515                         if (BaseXmlSchemaType == baseType || Datatype == baseType)
516                                 return true;
517
518                         // 2.2.2
519                         XmlSchemaSimpleType thisBaseSimpleType = BaseXmlSchemaType as XmlSchemaSimpleType;
520                         if (thisBaseSimpleType != null) {
521                                 if (thisBaseSimpleType.ValidateTypeDerivationOK (baseType, h, schema, false))
522                                         return true;
523                         }
524
525                         // 2.2.3
526                         switch (Variety) {
527                         case XmlSchemaDerivationMethod.Union:
528                         case XmlSchemaDerivationMethod.List:
529                                 if (baseType == XmlSchemaSimpleType.AnySimpleType)
530                                         return true;
531                                 break;
532                         }
533
534                         // 2.2.4 validly derived from one of the union member type.
535                         if (baseSimpleType != null && baseSimpleType.Variety == XmlSchemaDerivationMethod.Union) {
536                                 foreach (object memberType in ((XmlSchemaSimpleTypeUnion) baseSimpleType.Content).ValidatedTypes)
537                                         if (this.ValidateTypeDerivationOK (memberType, h, schema, false))
538                                                 return true;
539                         }
540
541                         if (raiseError)
542                                 error(h, "Invalid simple type derivation was found.");
543                         return false;
544                 }
545
546                 internal string Normalize (string s, XmlNameTable nt, XmlNamespaceManager nsmgr)
547                 {
548                         return Content.Normalize (s, nt, nsmgr);
549                 }
550
551                 //<simpleType \r
552                 //  final = (#all | (list | union | restriction)) \r
553                 //  id = ID \r
554                 //  name = NCName \r
555                 //  {any attributes with non-schema namespace . . .}>\r
556                 //  Content: (annotation?, (restriction | list | union))\r
557                 //</simpleType>\r
558                 internal static XmlSchemaSimpleType Read(XmlSchemaReader reader, ValidationEventHandler h)\r
559                 {\r
560                         XmlSchemaSimpleType stype = new XmlSchemaSimpleType();\r
561                         reader.MoveToElement();\r
562 \r
563                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
564                         {\r
565                                 error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);\r
566                                 reader.Skip();\r
567                                 return null;\r
568                         }\r
569 \r
570                         stype.LineNumber = reader.LineNumber;\r
571                         stype.LinePosition = reader.LinePosition;\r
572                         stype.SourceUri = reader.BaseURI;\r
573 \r
574                         while(reader.MoveToNextAttribute())\r
575                         {\r
576                                 if(reader.Name == "final")\r
577                                 {\r
578                                         Exception innerex;\r
579                                         stype.Final = XmlSchemaUtil.ReadDerivationAttribute(reader, out innerex, "final",\r
580                                                 XmlSchemaUtil.FinalAllowed);\r
581                                         if(innerex != null)\r
582                                                 error(h, "some invalid values not a valid value for final", innerex);\r
583                                 }\r
584                                 else if(reader.Name == "id")\r
585                                 {\r
586                                         stype.Id = reader.Value;\r
587                                 }\r
588                                 else if(reader.Name == "name")\r
589                                 {\r
590                                         stype.Name = reader.Value;\r
591                                 }\r
592                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
593                                 {\r
594                                         error(h,reader.Name + " is not a valid attribute for simpleType",null);\r
595                                 }\r
596                                 else\r
597                                 {\r
598                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,stype);\r
599                                 }\r
600                         }\r
601                         \r
602                         reader.MoveToElement();\r
603                         if(reader.IsEmptyElement)\r
604                                 return stype;\r
605 \r
606                         //      Content: (annotation?, (restriction | list | union))\r
607                         int level = 1;\r
608                         while(reader.ReadNextElement())\r
609                         {\r
610                                 if(reader.NodeType == XmlNodeType.EndElement)\r
611                                 {\r
612                                         if(reader.LocalName != xmlname)\r
613                                                 error(h,"Should not happen :2: XmlSchemaSimpleType.Read, name="+reader.Name,null);\r
614                                         break;\r
615                                 }\r
616                                 if(level <= 1 && reader.LocalName == "annotation")\r
617                                 {\r
618                                         level = 2; //Only one annotation\r
619                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
620                                         if(annotation != null)\r
621                                                 stype.Annotation = annotation;\r
622                                         continue;\r
623                                 }\r
624                                 if(level <= 2)\r
625                                 {\r
626                                         if(reader.LocalName == "restriction")\r
627                                         {\r
628                                                 level = 3;\r
629                                                 XmlSchemaSimpleTypeRestriction restriction = XmlSchemaSimpleTypeRestriction.Read(reader,h);\r
630                                                 if(restriction != null)\r
631                                                         stype.content = restriction;\r
632                                                 continue;\r
633                                         }\r
634                                         if(reader.LocalName == "list")\r
635                                         {\r
636                                                 level = 3;\r
637                                                 XmlSchemaSimpleTypeList list = XmlSchemaSimpleTypeList.Read(reader,h);\r
638                                                 if(list != null)\r
639                                                         stype.content = list;\r
640                                                 continue;\r
641                                         }\r
642                                         if(reader.LocalName == "union")\r
643                                         {\r
644                                                 level = 3;\r
645                                                 XmlSchemaSimpleTypeUnion union = XmlSchemaSimpleTypeUnion.Read(reader,h);\r
646                                                 if(union != null)\r
647                                                         stype.content = union;\r
648                                                 continue;\r
649                                         }\r
650                                 }\r
651                                 reader.RaiseInvalidElementError();\r
652                         }\r
653                         return stype;\r
654                 }\r
655 \r
656 \r
657         }\r
658 }\r