File: XmlSchemaSimpleType.cs

package info (click to toggle)
mono 2.6.7-5.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 327,344 kB
  • ctags: 413,649
  • sloc: cs: 2,471,883; xml: 1,768,594; ansic: 350,665; sh: 13,644; makefile: 8,640; perl: 1,784; asm: 717; cpp: 209; python: 146; sql: 81; sed: 16
file content (661 lines) | stat: -rw-r--r-- 24,663 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
//
// System.Xml.Schema.XmlSchemaSimpleType.cs
//
// Author:
//	Dwivedi, Ajay kumar  Adwiv@Yahoo.com
//	Atsushi Enomoto  ginga@kit.hi-ho.ne.jp
//

//
// 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;
using System.Xml.Serialization;
using System.Xml;
using Mono.Xml.Schema;

namespace System.Xml.Schema
{
	/// <summary>
	/// Summary description for XmlSchemaSimpleType.
	/// </summary>
	public class XmlSchemaSimpleType : XmlSchemaType
	{
		const string xmlname = "simpleType";
		private static XmlSchemaSimpleType schemaLocationType;

		private XmlSchemaSimpleTypeContent content;
		//compilation vars
		internal bool islocal = true; // Assuming local means we have to specify islocal=false only in XmlSchema
		private bool recursed;
		private XmlSchemaDerivationMethod variety;

#if NET_2_0
		// predefined simple types
		internal static readonly XmlSchemaSimpleType XsAnySimpleType;
		internal static readonly XmlSchemaSimpleType XsString;
		internal static readonly XmlSchemaSimpleType XsBoolean;
		internal static readonly XmlSchemaSimpleType XsDecimal;
		internal static readonly XmlSchemaSimpleType XsFloat;
		internal static readonly XmlSchemaSimpleType XsDouble;
		internal static readonly XmlSchemaSimpleType XsDuration;
		internal static readonly XmlSchemaSimpleType XsDateTime;
		internal static readonly XmlSchemaSimpleType XsTime;
		internal static readonly XmlSchemaSimpleType XsDate;
		internal static readonly XmlSchemaSimpleType XsGYearMonth;
		internal static readonly XmlSchemaSimpleType XsGYear;
		internal static readonly XmlSchemaSimpleType XsGMonthDay;
		internal static readonly XmlSchemaSimpleType XsGDay;
		internal static readonly XmlSchemaSimpleType XsGMonth;
		internal static readonly XmlSchemaSimpleType XsHexBinary;
		internal static readonly XmlSchemaSimpleType XsBase64Binary;
		internal static readonly XmlSchemaSimpleType XsAnyUri;
		internal static readonly XmlSchemaSimpleType XsQName;
		internal static readonly XmlSchemaSimpleType XsNotation;
		internal static readonly XmlSchemaSimpleType XsNormalizedString;
		internal static readonly XmlSchemaSimpleType XsToken;
		internal static readonly XmlSchemaSimpleType XsLanguage;
		internal static readonly XmlSchemaSimpleType XsNMToken;
		internal static readonly XmlSchemaSimpleType XsNMTokens;
		internal static readonly XmlSchemaSimpleType XsName;
		internal static readonly XmlSchemaSimpleType XsNCName;
		internal static readonly XmlSchemaSimpleType XsID;
		internal static readonly XmlSchemaSimpleType XsIDRef;
		internal static readonly XmlSchemaSimpleType XsIDRefs;
		internal static readonly XmlSchemaSimpleType XsEntity;
		internal static readonly XmlSchemaSimpleType XsEntities;
		internal static readonly XmlSchemaSimpleType XsInteger;
		internal static readonly XmlSchemaSimpleType XsNonPositiveInteger;
		internal static readonly XmlSchemaSimpleType XsNegativeInteger;
		internal static readonly XmlSchemaSimpleType XsLong;
		internal static readonly XmlSchemaSimpleType XsInt;
		internal static readonly XmlSchemaSimpleType XsShort;
		internal static readonly XmlSchemaSimpleType XsByte;
		internal static readonly XmlSchemaSimpleType XsNonNegativeInteger;
		internal static readonly XmlSchemaSimpleType XsUnsignedLong;
		internal static readonly XmlSchemaSimpleType XsUnsignedInt;
		internal static readonly XmlSchemaSimpleType XsUnsignedShort;
		internal static readonly XmlSchemaSimpleType XsUnsignedByte;
		internal static readonly XmlSchemaSimpleType XsPositiveInteger;
		// xdt:*
		internal static readonly XmlSchemaSimpleType XdtUntypedAtomic;
		internal static readonly XmlSchemaSimpleType XdtAnyAtomicType;
		internal static readonly XmlSchemaSimpleType XdtYearMonthDuration;
		internal static readonly XmlSchemaSimpleType XdtDayTimeDuration;
#endif

		static XmlSchemaSimpleType ()
		{
			// This is not used in the meantime.
			XmlSchemaSimpleType st = new XmlSchemaSimpleType ();
			XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList ();
			list.ItemTypeName = new XmlQualifiedName ("anyURI", XmlSchema.Namespace);
			st.Content = list;
			st.BaseXmlSchemaTypeInternal = null;
			st.variety = XmlSchemaDerivationMethod.List;
			schemaLocationType = st;

#if NET_2_0
			// Built-In schema types
			XsAnySimpleType = BuildSchemaType ("anySimpleType", null);
			XsString = BuildSchemaType ("string", "anySimpleType");
			XsBoolean = BuildSchemaType ("boolean", "anySimpleType");
			XsDecimal = BuildSchemaType ("decimal", "anySimpleType");
			XsFloat = BuildSchemaType ("float", "anySimpleType");
			XsDouble = BuildSchemaType ("double", "anySimpleType");
			XsDuration = BuildSchemaType ("duration", "anySimpleType");
			XsDateTime = BuildSchemaType ("dateTime", "anySimpleType");
			XsTime = BuildSchemaType ("time", "anySimpleType");
			XsDate = BuildSchemaType ("date", "anySimpleType");
			XsGYearMonth = BuildSchemaType ("gYearMonth", "anySimpleType");
			XsGYear = BuildSchemaType ("gYear", "anySimpleType");
			XsGMonthDay = BuildSchemaType ("gMonthDay", "anySimpleType");
			XsGDay = BuildSchemaType ("gDay", "anySimpleType");
			XsGMonth = BuildSchemaType ("gMonth", "anySimpleType");
			XsHexBinary = BuildSchemaType ("hexBinary", "anySimpleType");
			XsBase64Binary = BuildSchemaType ("base64Binary", "anySimpleType");
			XsAnyUri = BuildSchemaType ("anyURI", "anySimpleType");
			XsQName = BuildSchemaType ("QName", "anySimpleType");
			XsNotation = BuildSchemaType ("NOTATION", "anySimpleType");
			// derived types
			XsNormalizedString = BuildSchemaType ("normalizedString", "string");
			XsToken = BuildSchemaType ("token", "normalizedString");
			XsLanguage = BuildSchemaType ("language", "token");
			XsNMToken = BuildSchemaType ("NMTOKEN", "token");
			XsName = BuildSchemaType ("Name", "token");
			XsNCName = BuildSchemaType ("NCName", "Name");

			XsID = BuildSchemaType ("ID", "NCName");
			XsIDRef = BuildSchemaType ("IDREF", "NCName");
			XsEntity = BuildSchemaType ("ENTITY", "NCName");

			XsInteger = BuildSchemaType ("integer", "decimal");
			XsNonPositiveInteger = BuildSchemaType ("nonPositiveInteger", "integer");
			XsNegativeInteger = BuildSchemaType ("negativeInteger", "nonPositiveInteger");
			XsLong = BuildSchemaType ("long", "integer");
			XsInt = BuildSchemaType ("int", "long");
			XsShort = BuildSchemaType ("short", "int");
			XsByte = BuildSchemaType ("byte", "short");
			XsNonNegativeInteger = BuildSchemaType ("nonNegativeInteger", "integer");
			XsUnsignedLong = BuildSchemaType ("unsignedLong", "nonNegativeInteger");
			XsUnsignedInt = BuildSchemaType ("unsignedInt", "unsignedLong");
			XsUnsignedShort = BuildSchemaType ("unsignedShort", "unsignedInt");
			XsUnsignedByte = BuildSchemaType ("unsignedByte", "unsignedShort");
			XsPositiveInteger = BuildSchemaType ("positiveInteger", "nonNegativeInteger");

			// xdt:*
			XdtAnyAtomicType = BuildSchemaType ("anyAtomicType", "anySimpleType", true, false);
			XdtUntypedAtomic = BuildSchemaType ("untypedAtomic", "anyAtomicType", true, true);
			XdtDayTimeDuration = BuildSchemaType ("dayTimeDuration", "duration", true, false);
			XdtYearMonthDuration = BuildSchemaType ("yearMonthDuration", "duration", true, false);

			// NMTOKENS, IDREFS, ENTITIES - lists
			XsIDRefs = new XmlSchemaSimpleType ();
			XmlSchemaSimpleTypeList sl = new XmlSchemaSimpleTypeList ();
			sl.ItemType = XsIDRef;
			XsIDRefs.Content = sl;
			XsEntities = new XmlSchemaSimpleType ();
			sl = new XmlSchemaSimpleTypeList ();
			sl.ItemType = XsEntity;
			XsEntities.Content = sl;
			XsNMTokens = new XmlSchemaSimpleType ();
			sl = new XmlSchemaSimpleTypeList ();
			sl.ItemType = XsNMToken;
			XsNMTokens.Content = sl;
#endif
		}

#if NET_2_0
		private static XmlSchemaSimpleType BuildSchemaType (string name, string baseName)
		{
			return BuildSchemaType (name, baseName, false, false);
		}

		private static XmlSchemaSimpleType BuildSchemaType (string name, string baseName, bool xdt, bool baseXdt)
		{
			string ns = xdt ? "http://www.w3.org/2003/11/xpath-datatypes" : XmlSchema.Namespace;
			string ns2 = baseXdt ? "http://www.w3.org/2003/11/xpath-datatypes" : XmlSchema.Namespace;
			XmlSchemaSimpleType st = new XmlSchemaSimpleType ();
			st.QNameInternal = new XmlQualifiedName (name, ns);
			if (baseName != null)
				st.BaseXmlSchemaTypeInternal = XmlSchemaType. GetBuiltInSimpleType (new XmlQualifiedName (baseName, ns2));
			st.DatatypeInternal = XmlSchemaDatatype.FromName (st.QualifiedName);
			return st;
		}
#endif

		internal static XsdAnySimpleType AnySimpleType {
			get { return XsdAnySimpleType.Instance; }
		}

		internal static XmlSchemaSimpleType SchemaLocationType {
			get { return schemaLocationType; }
		}

		public XmlSchemaSimpleType ()
		{
		}

		[XmlElement("restriction",typeof(XmlSchemaSimpleTypeRestriction))]
		[XmlElement("list",typeof(XmlSchemaSimpleTypeList))]
		[XmlElement("union",typeof(XmlSchemaSimpleTypeUnion))]
		public XmlSchemaSimpleTypeContent Content
		{
			get{ return  content; } 
			set{ content = value; }
		}

		internal XmlSchemaDerivationMethod Variety
		{
			get{ return variety; }
		}

		internal override void SetParent (XmlSchemaObject parent)
		{
			base.SetParent (parent);
			if (Content != null)
				Content.SetParent (this);
		}

		/// <remarks>
		/// For a simple Type:
		///		1. Content must be present
		///		2. id if present, must have be a valid ID
		///		a) If the simpletype is local
		///			1-	are from <xs:complexType name="simpleType"> and <xs:complexType name="localSimpleType">
		///			1. name  is prohibited
		///			2. final is prohibited
		///		b) If the simpletype is toplevel
		///			1-  are from <xs:complexType name="simpleType"> and <xs:complexType name="topLevelSimpleType">
		///			1. name is required, type must be NCName
		///			2. Content is required
		///			3. final can have values : #all | (list | union | restriction)
		///			4. If final is set, finalResolved is same as final (but within the values of b.3)
		///			5. If final is not set, the finalDefault of the schema (ie. only #all and restriction)
		///			6. Base type is:
		///				4.1 If restriction is chosen,the base type of restriction or elements
		///				4.2 otherwise simple ur-type
		/// </remarks>
		internal override int Compile(ValidationEventHandler h, XmlSchema schema)
		{
			// If this is already compiled this time, simply skip.
			if (CompilationId == schema.CompilationId)
				return 0;

			errorCount = 0;

			if(this.islocal) // a
			{
				if(this.Name != null) // a.1
					error(h,"Name is prohibited in a local simpletype");
				else
					this.QNameInternal = new XmlQualifiedName(this.Name, AncestorSchema.TargetNamespace);
				if(this.Final != XmlSchemaDerivationMethod.None) //a.2
					error(h,"Final is prohibited in a local simpletype");
			}
			else //b
			{
				if(this.Name == null) //b.1
					error(h,"Name is required in top level simpletype");
				else if(!XmlSchemaUtil.CheckNCName(this.Name)) // b.1.2
					error(h,"name attribute of a simpleType must be NCName");
				else
					this.QNameInternal = new XmlQualifiedName(this.Name, AncestorSchema.TargetNamespace);
				
				//NOTE: Although the FinalResolved can be Empty, it is not a valid value for Final
				//DEVIATION: If an error occurs, the finaldefault is always consulted. This deviates
				//			 from the way MS implementation works.
				switch(this.Final) //b.3, b.4
				{
					case XmlSchemaDerivationMethod.All:
						this.finalResolved = XmlSchemaDerivationMethod.All;
						break;
					case XmlSchemaDerivationMethod.List:
					case XmlSchemaDerivationMethod.Union:
					case XmlSchemaDerivationMethod.Restriction:
						this.finalResolved = Final;
						break;
					default:
						error(h,"The value of final attribute is not valid for simpleType");
						goto case XmlSchemaDerivationMethod.None;
						// use assignment from finaldefault on schema.
					case XmlSchemaDerivationMethod.None: // b.5
						XmlSchemaDerivationMethod flags = 
							(XmlSchemaDerivationMethod.Restriction | XmlSchemaDerivationMethod.List |
							XmlSchemaDerivationMethod.Extension | XmlSchemaDerivationMethod.Union );
						switch (schema.FinalDefault) {
						case XmlSchemaDerivationMethod.All:
							finalResolved = XmlSchemaDerivationMethod.All;
							break;
						case XmlSchemaDerivationMethod.None:
							finalResolved = XmlSchemaDerivationMethod.Empty;
							break;
						default:
							finalResolved = schema.FinalDefault & flags;
							break;
						}
						break;
				}
			}

			XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);

			if (Content != null)
				Content.OwnerType = this;

			if(this.Content == null) //a.3,b.2
				error(h,"Content is required in a simpletype");
			else if(Content is XmlSchemaSimpleTypeRestriction)
			{
				this.resolvedDerivedBy = XmlSchemaDerivationMethod.Restriction;
				errorCount += ((XmlSchemaSimpleTypeRestriction)Content).Compile(h,schema);
			}
			else if(Content is XmlSchemaSimpleTypeList)
			{
				this.resolvedDerivedBy = XmlSchemaDerivationMethod.List;
				errorCount += ((XmlSchemaSimpleTypeList)Content).Compile(h,schema);
			}
			else if(Content is XmlSchemaSimpleTypeUnion)
			{
				this.resolvedDerivedBy = XmlSchemaDerivationMethod.Union;
				errorCount += ((XmlSchemaSimpleTypeUnion)Content).Compile(h,schema);
			}

			this.CompilationId = schema.CompilationId;
			return errorCount;
		}

		internal void CollectBaseType (ValidationEventHandler h, XmlSchema schema)
		{
			if (Content is XmlSchemaSimpleTypeRestriction) {
				object o = ((XmlSchemaSimpleTypeRestriction) Content).GetActualType (h, schema, false);
				BaseXmlSchemaTypeInternal = o as XmlSchemaSimpleType;
				if (BaseXmlSchemaTypeInternal != null)
					DatatypeInternal = BaseXmlSchemaTypeInternal.Datatype;
				else
					DatatypeInternal = o as XmlSchemaDatatype;
			}
			// otherwise, actualBaseSchemaType is null
			else
				DatatypeInternal = XmlSchemaSimpleType.AnySimpleType;
		}
		
		internal override int Validate(ValidationEventHandler h, XmlSchema schema)
		{
			// 3.14.6 Properties Correct.
			// 
			// 1. Post Compilation Properties
			// {name}, {target namespace} => QNameInternal. Already Compile()d.
			// {base type definition} => baseSchemaTypeInternal
			// {final} => finalResolved. Already Compile()d.
			// {variety} => resolvedDerivedBy. Already Compile()d.
			//
			// 2. Should be checked by "recursed" field.

			if(IsValidated (schema.ValidationId))
				return errorCount;

			if (recursed) {
				error (h, "Circular type reference was found.");
				return errorCount;
			}
			recursed = true;

			CollectBaseType (h, schema);

			if (content != null)
				errorCount += content.Validate (h, schema);

/*
			// BaseSchemaType property
			BaseXmlSchemaTypeInternal = content.ActualBaseSchemaType as XmlSchemaType;
			if (this.BaseXmlSchemaTypeInternal == null)
				this.DatatypeInternal = content.ActualBaseSchemaType as XmlSchemaDatatype;
*/

			// Datatype property
			XmlSchemaSimpleType simple = BaseXmlSchemaType as XmlSchemaSimpleType;
			if (simple != null)
				this.DatatypeInternal = simple.Datatype;
//			else
//				DatatypeInternal = BaseSchemaType as XmlSchemaDatatype;

			// 3.
			XmlSchemaSimpleType baseSType = BaseXmlSchemaType as XmlSchemaSimpleType;
			if (baseSType != null) {
				if ((baseSType.FinalResolved & this.resolvedDerivedBy) != 0)
					error (h, "Specified derivation is prohibited by the base simple type.");
			}

			// {variety}
			if (this.resolvedDerivedBy == XmlSchemaDerivationMethod.Restriction &&
				baseSType != null)
				this.variety = baseSType.Variety;
			else
				this.variety = this.resolvedDerivedBy;

			// 3.14.6 Derivation Valid (Restriction, Simple)
			XmlSchemaSimpleTypeRestriction r = Content as XmlSchemaSimpleTypeRestriction;
			object baseType = BaseXmlSchemaType != null ? (object) BaseXmlSchemaType : Datatype;
			if (r != null)
				ValidateDerivationValid (baseType, r.Facets, h, schema);

			// TODO: describe which validation term this belongs to.
			XmlSchemaSimpleTypeList l = Content as XmlSchemaSimpleTypeList;
			if (l != null) {
				XmlSchemaSimpleType itemSimpleType = l.ValidatedListItemType as XmlSchemaSimpleType;
				if (itemSimpleType != null && itemSimpleType.Content is XmlSchemaSimpleTypeList)
					error (h, "List type must not be derived from another list type.");
			}

			recursed = false;
			ValidationId = schema.ValidationId;
			return errorCount;
		}

		// 3.14.6 Derivation Valid (RestrictionSimple)
		internal void ValidateDerivationValid (object baseType, XmlSchemaObjectCollection facets,
			ValidationEventHandler h, XmlSchema schema)
		{
			// TODO
			XmlSchemaSimpleType baseSimpleType = baseType as XmlSchemaSimpleType;
			switch (this.Variety) {
			// 1. atomic type
			case XmlSchemaDerivationMethod.Restriction:
				// 1.1
				if (baseSimpleType != null && baseSimpleType.resolvedDerivedBy != XmlSchemaDerivationMethod.Restriction)
					error (h, "Base schema type is not either atomic type or primitive type.");
				// 1.2
				if (baseSimpleType != null && 
					(baseSimpleType.FinalResolved & XmlSchemaDerivationMethod.Restriction) != 0)
					error (h, "Derivation by restriction is prohibited by the base simple type.");
				// TODO: 1.3 facet restriction valid.
				break;
			case XmlSchemaDerivationMethod.List:
				/*
				XmlSchemaSimpleTypeList thisList = Content as XmlSchemaSimpleTypeList;
				// 2.1 item list type not allowed
				if (baseSimpleType != null && baseSimpleType.resolvedDerivedBy == XmlSchemaDerivationMethod.List)
					error (h, "Base list schema type is not allowed.");
				XmlSchemaSimpleTypeUnion baseUnion = baseSimpleType.Content as XmlSchemaSimpleTypeUnion;
				if (baseUnion != null) {
					bool errorFound = false;
					foreach (object memberType in baseUnion.ValidatedTypes) {
						XmlSchemaSimpleType memberST = memberType as XmlSchemaSimpleType;
						if (memberST != null && memberST.resolvedDerivedBy == XmlSchemaDerivationMethod.List)
							errorFound = true;
					}
					if (errorFound)
						error (h, "Base union schema type should not contain list types.");
				}
				*/
				// 2.2 facets limited
				if (facets != null)
					foreach (XmlSchemaFacet facet in facets) {
						if (facet is XmlSchemaLengthFacet ||
							facet is XmlSchemaMaxLengthFacet ||
							facet is XmlSchemaMinLengthFacet ||
							facet is XmlSchemaEnumerationFacet ||
							facet is XmlSchemaPatternFacet)
							continue;
						else
							error (h, "Not allowed facet was found on this simple type which derives list type.");
					}
				break;
			case XmlSchemaDerivationMethod.Union:
				// 3.1

				// 3.2
				if (facets != null)
					foreach (XmlSchemaFacet facet in facets) {
						if (facet is XmlSchemaEnumerationFacet ||
							facet is XmlSchemaPatternFacet)
							continue;
						else
							error (h, "Not allowed facet was found on this simple type which derives list type.");
					}
				break;
			}
		}

		// 3.14.6 Type Derivation OK (Simple)
		internal bool ValidateTypeDerivationOK (object baseType,
			ValidationEventHandler h, XmlSchema schema, bool raiseError)
		{
			// 1
			// Note that anyType should also be allowed as anySimpleType.
			if (this == baseType || baseType == XmlSchemaSimpleType.AnySimpleType ||
				baseType == XmlSchemaComplexType.AnyType)
				return true;

			// 2.1
			XmlSchemaSimpleType baseSimpleType = baseType as XmlSchemaSimpleType;
			if (baseSimpleType != null && 
				(baseSimpleType.FinalResolved & resolvedDerivedBy) != 0) {
				if (raiseError)
					error (h, "Specified derivation is prohibited by the base type.");
				return false;
			}

			// 2.2.1
			if (BaseXmlSchemaType == baseType || Datatype == baseType)
				return true;

			// 2.2.2
			XmlSchemaSimpleType thisBaseSimpleType = BaseXmlSchemaType as XmlSchemaSimpleType;
			if (thisBaseSimpleType != null) {
				if (thisBaseSimpleType.ValidateTypeDerivationOK (baseType, h, schema, false))
					return true;
			}

			// 2.2.3
			switch (Variety) {
			case XmlSchemaDerivationMethod.Union:
			case XmlSchemaDerivationMethod.List:
				if (baseType == XmlSchemaSimpleType.AnySimpleType)
					return true;
				break;
			}

			// 2.2.4 validly derived from one of the union member type.
			if (baseSimpleType != null && baseSimpleType.Variety == XmlSchemaDerivationMethod.Union) {
				foreach (object memberType in ((XmlSchemaSimpleTypeUnion) baseSimpleType.Content).ValidatedTypes)
					if (this.ValidateTypeDerivationOK (memberType, h, schema, false))
						return true;
			}

			if (raiseError)
				error(h, "Invalid simple type derivation was found.");
			return false;
		}

		internal string Normalize (string s, XmlNameTable nt, XmlNamespaceManager nsmgr)
		{
			return Content.Normalize (s, nt, nsmgr);
		}

		//<simpleType 
		//  final = (#all | (list | union | restriction)) 
		//  id = ID 
		//  name = NCName 
		//  {any attributes with non-schema namespace . . .}>
		//  Content: (annotation?, (restriction | list | union))
		//</simpleType>
		internal static XmlSchemaSimpleType Read(XmlSchemaReader reader, ValidationEventHandler h)
		{
			XmlSchemaSimpleType stype = new XmlSchemaSimpleType();
			reader.MoveToElement();

			if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
			{
				error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);
				reader.Skip();
				return null;
			}

			stype.LineNumber = reader.LineNumber;
			stype.LinePosition = reader.LinePosition;
			stype.SourceUri = reader.BaseURI;

			while(reader.MoveToNextAttribute())
			{
				if(reader.Name == "final")
				{
					Exception innerex;
					stype.Final = XmlSchemaUtil.ReadDerivationAttribute(reader, out innerex, "final",
						XmlSchemaUtil.FinalAllowed);
					if(innerex != null)
						error(h, "some invalid values not a valid value for final", innerex);
				}
				else if(reader.Name == "id")
				{
					stype.Id = reader.Value;
				}
				else if(reader.Name == "name")
				{
					stype.Name = reader.Value;
				}
				else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
				{
					error(h,reader.Name + " is not a valid attribute for simpleType",null);
				}
				else
				{
					XmlSchemaUtil.ReadUnhandledAttribute(reader,stype);
				}
			}
			
			reader.MoveToElement();
			if(reader.IsEmptyElement)
				return stype;

			//	Content: (annotation?, (restriction | list | union))
			int level = 1;
			while(reader.ReadNextElement())
			{
				if(reader.NodeType == XmlNodeType.EndElement)
				{
					if(reader.LocalName != xmlname)
						error(h,"Should not happen :2: XmlSchemaSimpleType.Read, name="+reader.Name,null);
					break;
				}
				if(level <= 1 && reader.LocalName == "annotation")
				{
					level = 2; //Only one annotation
					XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
					if(annotation != null)
						stype.Annotation = annotation;
					continue;
				}
				if(level <= 2)
				{
					if(reader.LocalName == "restriction")
					{
						level = 3;
						XmlSchemaSimpleTypeRestriction restriction = XmlSchemaSimpleTypeRestriction.Read(reader,h);
						if(restriction != null)
							stype.content = restriction;
						continue;
					}
					if(reader.LocalName == "list")
					{
						level = 3;
						XmlSchemaSimpleTypeList list = XmlSchemaSimpleTypeList.Read(reader,h);
						if(list != null)
							stype.content = list;
						continue;
					}
					if(reader.LocalName == "union")
					{
						level = 3;
						XmlSchemaSimpleTypeUnion union = XmlSchemaSimpleTypeUnion.Read(reader,h);
						if(union != null)
							stype.content = union;
						continue;
					}
				}
				reader.RaiseInvalidElementError();
			}
			return stype;
		}


	}
}