2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / class / System.XML / System.Xml.Schema / XmlSchemaSimpleType.cs
blob4892a2887b7c119210027e60e3084b7d7af92d84
1 //
2 // System.Xml.Schema.XmlSchemaSimpleType.cs
3 //
4 // Author:
5 // Dwivedi, Ajay kumar Adwiv@Yahoo.com
6 // Atsushi Enomoto ginga@kit.hi-ho.ne.jp
7 //
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.
29 using System;
30 using System.Xml.Serialization;
31 using System.Xml;
32 using Mono.Xml.Schema;
34 namespace System.Xml.Schema
36 /// <summary>
37 /// Summary description for XmlSchemaSimpleType.
38 /// </summary>
39 public class XmlSchemaSimpleType : XmlSchemaType
41 const string xmlname = "simpleType";
42 private static XmlSchemaSimpleType schemaLocationType;
44 private XmlSchemaSimpleTypeContent content;
45 //compilation vars
46 internal bool islocal = true; // Assuming local means we have to specify islocal=false only in XmlSchema
47 private bool recursed;
48 private XmlSchemaDerivationMethod variety;
50 #if NET_2_0
51 // predefined simple types
52 internal static readonly XmlSchemaSimpleType XsAnySimpleType;
53 internal static readonly XmlSchemaSimpleType XsString;
54 internal static readonly XmlSchemaSimpleType XsBoolean;
55 internal static readonly XmlSchemaSimpleType XsDecimal;
56 internal static readonly XmlSchemaSimpleType XsFloat;
57 internal static readonly XmlSchemaSimpleType XsDouble;
58 internal static readonly XmlSchemaSimpleType XsDuration;
59 internal static readonly XmlSchemaSimpleType XsDateTime;
60 internal static readonly XmlSchemaSimpleType XsTime;
61 internal static readonly XmlSchemaSimpleType XsDate;
62 internal static readonly XmlSchemaSimpleType XsGYearMonth;
63 internal static readonly XmlSchemaSimpleType XsGYear;
64 internal static readonly XmlSchemaSimpleType XsGMonthDay;
65 internal static readonly XmlSchemaSimpleType XsGDay;
66 internal static readonly XmlSchemaSimpleType XsGMonth;
67 internal static readonly XmlSchemaSimpleType XsHexBinary;
68 internal static readonly XmlSchemaSimpleType XsBase64Binary;
69 internal static readonly XmlSchemaSimpleType XsAnyUri;
70 internal static readonly XmlSchemaSimpleType XsQName;
71 internal static readonly XmlSchemaSimpleType XsNotation;
72 internal static readonly XmlSchemaSimpleType XsNormalizedString;
73 internal static readonly XmlSchemaSimpleType XsToken;
74 internal static readonly XmlSchemaSimpleType XsLanguage;
75 internal static readonly XmlSchemaSimpleType XsNMToken;
76 internal static readonly XmlSchemaSimpleType XsNMTokens;
77 internal static readonly XmlSchemaSimpleType XsName;
78 internal static readonly XmlSchemaSimpleType XsNCName;
79 internal static readonly XmlSchemaSimpleType XsID;
80 internal static readonly XmlSchemaSimpleType XsIDRef;
81 internal static readonly XmlSchemaSimpleType XsIDRefs;
82 internal static readonly XmlSchemaSimpleType XsEntity;
83 internal static readonly XmlSchemaSimpleType XsEntities;
84 internal static readonly XmlSchemaSimpleType XsInteger;
85 internal static readonly XmlSchemaSimpleType XsNonPositiveInteger;
86 internal static readonly XmlSchemaSimpleType XsNegativeInteger;
87 internal static readonly XmlSchemaSimpleType XsLong;
88 internal static readonly XmlSchemaSimpleType XsInt;
89 internal static readonly XmlSchemaSimpleType XsShort;
90 internal static readonly XmlSchemaSimpleType XsByte;
91 internal static readonly XmlSchemaSimpleType XsNonNegativeInteger;
92 internal static readonly XmlSchemaSimpleType XsUnsignedLong;
93 internal static readonly XmlSchemaSimpleType XsUnsignedInt;
94 internal static readonly XmlSchemaSimpleType XsUnsignedShort;
95 internal static readonly XmlSchemaSimpleType XsUnsignedByte;
96 internal static readonly XmlSchemaSimpleType XsPositiveInteger;
97 // xdt:*
98 internal static readonly XmlSchemaSimpleType XdtUntypedAtomic;
99 internal static readonly XmlSchemaSimpleType XdtAnyAtomicType;
100 internal static readonly XmlSchemaSimpleType XdtYearMonthDuration;
101 internal static readonly XmlSchemaSimpleType XdtDayTimeDuration;
102 #endif
104 static XmlSchemaSimpleType ()
106 // This is not used in the meantime.
107 XmlSchemaSimpleType st = new XmlSchemaSimpleType ();
108 XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList ();
109 list.ItemTypeName = new XmlQualifiedName ("anyURI", XmlSchema.Namespace);
110 st.Content = list;
111 st.BaseXmlSchemaTypeInternal = null;
112 st.variety = XmlSchemaDerivationMethod.List;
113 schemaLocationType = st;
115 #if NET_2_0
116 // Built-In schema types
117 XsAnySimpleType = BuildSchemaType ("anySimpleType", null);
118 XsString = BuildSchemaType ("string", "anySimpleType");
119 XsBoolean = BuildSchemaType ("boolean", "anySimpleType");
120 XsDecimal = BuildSchemaType ("decimal", "anySimpleType");
121 XsFloat = BuildSchemaType ("float", "anySimpleType");
122 XsDouble = BuildSchemaType ("double", "anySimpleType");
123 XsDuration = BuildSchemaType ("duration", "anySimpleType");
124 XsDateTime = BuildSchemaType ("dateTime", "anySimpleType");
125 XsTime = BuildSchemaType ("time", "anySimpleType");
126 XsDate = BuildSchemaType ("date", "anySimpleType");
127 XsGYearMonth = BuildSchemaType ("gYearMonth", "anySimpleType");
128 XsGYear = BuildSchemaType ("gYear", "anySimpleType");
129 XsGMonthDay = BuildSchemaType ("gMonthDay", "anySimpleType");
130 XsGDay = BuildSchemaType ("gDay", "anySimpleType");
131 XsGMonth = BuildSchemaType ("gMonth", "anySimpleType");
132 XsHexBinary = BuildSchemaType ("hexBinary", "anySimpleType");
133 XsBase64Binary = BuildSchemaType ("base64Binary", "anySimpleType");
134 XsAnyUri = BuildSchemaType ("anyURI", "anySimpleType");
135 XsQName = BuildSchemaType ("QName", "anySimpleType");
136 XsNotation = BuildSchemaType ("NOTATION", "anySimpleType");
137 // derived types
138 XsNormalizedString = BuildSchemaType ("normalizedString", "string");
139 XsToken = BuildSchemaType ("token", "normalizedString");
140 XsLanguage = BuildSchemaType ("language", "token");
141 XsNMToken = BuildSchemaType ("NMTOKEN", "token");
142 XsName = BuildSchemaType ("Name", "token");
143 XsNCName = BuildSchemaType ("NCName", "Name");
145 XsID = BuildSchemaType ("ID", "NCName");
146 XsIDRef = BuildSchemaType ("IDREF", "NCName");
147 XsEntity = BuildSchemaType ("ENTITY", "NCName");
149 XsInteger = BuildSchemaType ("integer", "decimal");
150 XsNonPositiveInteger = BuildSchemaType ("nonPositiveInteger", "integer");
151 XsNegativeInteger = BuildSchemaType ("negativeInteger", "nonPositiveInteger");
152 XsLong = BuildSchemaType ("long", "integer");
153 XsInt = BuildSchemaType ("int", "long");
154 XsShort = BuildSchemaType ("short", "int");
155 XsByte = BuildSchemaType ("byte", "short");
156 XsNonNegativeInteger = BuildSchemaType ("nonNegativeInteger", "integer");
157 XsUnsignedLong = BuildSchemaType ("unsignedLong", "nonNegativeInteger");
158 XsUnsignedInt = BuildSchemaType ("unsignedInt", "unsignedLong");
159 XsUnsignedShort = BuildSchemaType ("unsignedShort", "unsignedInt");
160 XsUnsignedByte = BuildSchemaType ("unsignedByte", "unsignedShort");
161 XsPositiveInteger = BuildSchemaType ("positiveInteger", "nonNegativeInteger");
163 // xdt:*
164 XdtAnyAtomicType = BuildSchemaType ("anyAtomicType", "anySimpleType", true, false);
165 XdtUntypedAtomic = BuildSchemaType ("untypedAtomic", "anyAtomicType", true, true);
166 XdtDayTimeDuration = BuildSchemaType ("dayTimeDuration", "duration", true, false);
167 XdtYearMonthDuration = BuildSchemaType ("yearMonthDuration", "duration", true, false);
169 // NMTOKENS, IDREFS, ENTITIES - lists
170 XsIDRefs = new XmlSchemaSimpleType ();
171 XmlSchemaSimpleTypeList sl = new XmlSchemaSimpleTypeList ();
172 sl.ItemType = XsIDRef;
173 XsIDRefs.Content = sl;
174 XsEntities = new XmlSchemaSimpleType ();
175 sl = new XmlSchemaSimpleTypeList ();
176 sl.ItemType = XsEntity;
177 XsEntities.Content = sl;
178 XsNMTokens = new XmlSchemaSimpleType ();
179 sl = new XmlSchemaSimpleTypeList ();
180 sl.ItemType = XsNMToken;
181 XsNMTokens.Content = sl;
182 #endif
185 #if NET_2_0
186 private static XmlSchemaSimpleType BuildSchemaType (string name, string baseName)
188 return BuildSchemaType (name, baseName, false, false);
191 private static XmlSchemaSimpleType BuildSchemaType (string name, string baseName, bool xdt, bool baseXdt)
193 string ns = xdt ? "http://www.w3.org/2003/11/xpath-datatypes" : XmlSchema.Namespace;
194 string ns2 = baseXdt ? "http://www.w3.org/2003/11/xpath-datatypes" : XmlSchema.Namespace;
195 XmlSchemaSimpleType st = new XmlSchemaSimpleType ();
196 st.QNameInternal = new XmlQualifiedName (name, ns);
197 if (baseName != null)
198 st.BaseXmlSchemaTypeInternal = XmlSchemaType. GetBuiltInSimpleType (new XmlQualifiedName (baseName, ns2));
199 st.DatatypeInternal = XmlSchemaDatatype.FromName (st.QualifiedName);
200 return st;
202 #endif
204 internal static XsdAnySimpleType AnySimpleType {
205 get { return XsdAnySimpleType.Instance; }
208 internal static XmlSchemaSimpleType SchemaLocationType {
209 get { return schemaLocationType; }
212 public XmlSchemaSimpleType ()
216 [XmlElement("restriction",typeof(XmlSchemaSimpleTypeRestriction))]
217 [XmlElement("list",typeof(XmlSchemaSimpleTypeList))]
218 [XmlElement("union",typeof(XmlSchemaSimpleTypeUnion))]
219 public XmlSchemaSimpleTypeContent Content
221 get{ return content; }
222 set{ content = value; }
225 internal XmlSchemaDerivationMethod Variety
227 get{ return variety; }
230 internal override void SetParent (XmlSchemaObject parent)
232 base.SetParent (parent);
233 if (Content != null)
234 Content.SetParent (this);
237 /// <remarks>
238 /// For a simple Type:
239 /// 1. Content must be present
240 /// 2. id if present, must have be a valid ID
241 /// a) If the simpletype is local
242 /// 1- are from <xs:complexType name="simpleType"> and <xs:complexType name="localSimpleType">
243 /// 1. name is prohibited
244 /// 2. final is prohibited
245 /// b) If the simpletype is toplevel
246 /// 1- are from <xs:complexType name="simpleType"> and <xs:complexType name="topLevelSimpleType">
247 /// 1. name is required, type must be NCName
248 /// 2. Content is required
249 /// 3. final can have values : #all | (list | union | restriction)
250 /// 4. If final is set, finalResolved is same as final (but within the values of b.3)
251 /// 5. If final is not set, the finalDefault of the schema (ie. only #all and restriction)
252 /// 6. Base type is:
253 /// 4.1 If restriction is chosen,the base type of restriction or elements
254 /// 4.2 otherwise simple ur-type
255 /// </remarks>
256 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
258 // If this is already compiled this time, simply skip.
259 if (CompilationId == schema.CompilationId)
260 return 0;
262 errorCount = 0;
264 if(this.islocal) // a
266 if(this.Name != null) // a.1
267 error(h,"Name is prohibited in a local simpletype");
268 else
269 this.QNameInternal = new XmlQualifiedName(this.Name, AncestorSchema.TargetNamespace);
270 if(this.Final != XmlSchemaDerivationMethod.None) //a.2
271 error(h,"Final is prohibited in a local simpletype");
273 else //b
275 if(this.Name == null) //b.1
276 error(h,"Name is required in top level simpletype");
277 else if(!XmlSchemaUtil.CheckNCName(this.Name)) // b.1.2
278 error(h,"name attribute of a simpleType must be NCName");
279 else
280 this.QNameInternal = new XmlQualifiedName(this.Name, AncestorSchema.TargetNamespace);
282 //NOTE: Although the FinalResolved can be Empty, it is not a valid value for Final
283 //DEVIATION: If an error occurs, the finaldefault is always consulted. This deviates
284 // from the way MS implementation works.
285 switch(this.Final) //b.3, b.4
287 case XmlSchemaDerivationMethod.All:
288 this.finalResolved = XmlSchemaDerivationMethod.All;
289 break;
290 case XmlSchemaDerivationMethod.List:
291 case XmlSchemaDerivationMethod.Union:
292 case XmlSchemaDerivationMethod.Restriction:
293 this.finalResolved = Final;
294 break;
295 default:
296 error(h,"The value of final attribute is not valid for simpleType");
297 goto case XmlSchemaDerivationMethod.None;
298 // use assignment from finaldefault on schema.
299 case XmlSchemaDerivationMethod.None: // b.5
300 XmlSchemaDerivationMethod flags =
301 (XmlSchemaDerivationMethod.Restriction | XmlSchemaDerivationMethod.List |
302 XmlSchemaDerivationMethod.Extension | XmlSchemaDerivationMethod.Union );
303 switch (schema.FinalDefault) {
304 case XmlSchemaDerivationMethod.All:
305 finalResolved = XmlSchemaDerivationMethod.All;
306 break;
307 case XmlSchemaDerivationMethod.None:
308 finalResolved = XmlSchemaDerivationMethod.Empty;
309 break;
310 default:
311 finalResolved = schema.FinalDefault & flags;
312 break;
314 break;
318 XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
320 if (Content != null)
321 Content.OwnerType = this;
323 if(this.Content == null) //a.3,b.2
324 error(h,"Content is required in a simpletype");
325 else if(Content is XmlSchemaSimpleTypeRestriction)
327 this.resolvedDerivedBy = XmlSchemaDerivationMethod.Restriction;
328 errorCount += ((XmlSchemaSimpleTypeRestriction)Content).Compile(h,schema);
330 else if(Content is XmlSchemaSimpleTypeList)
332 this.resolvedDerivedBy = XmlSchemaDerivationMethod.List;
333 errorCount += ((XmlSchemaSimpleTypeList)Content).Compile(h,schema);
335 else if(Content is XmlSchemaSimpleTypeUnion)
337 this.resolvedDerivedBy = XmlSchemaDerivationMethod.Union;
338 errorCount += ((XmlSchemaSimpleTypeUnion)Content).Compile(h,schema);
341 this.CompilationId = schema.CompilationId;
342 return errorCount;
345 internal void CollectBaseType (ValidationEventHandler h, XmlSchema schema)
347 if (Content is XmlSchemaSimpleTypeRestriction) {
348 object o = ((XmlSchemaSimpleTypeRestriction) Content).GetActualType (h, schema, false);
349 BaseXmlSchemaTypeInternal = o as XmlSchemaSimpleType;
350 if (BaseXmlSchemaTypeInternal != null)
351 DatatypeInternal = BaseXmlSchemaTypeInternal.Datatype;
352 else
353 DatatypeInternal = o as XmlSchemaDatatype;
355 // otherwise, actualBaseSchemaType is null
356 else
357 DatatypeInternal = XmlSchemaSimpleType.AnySimpleType;
360 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
362 // 3.14.6 Properties Correct.
364 // 1. Post Compilation Properties
365 // {name}, {target namespace} => QNameInternal. Already Compile()d.
366 // {base type definition} => baseSchemaTypeInternal
367 // {final} => finalResolved. Already Compile()d.
368 // {variety} => resolvedDerivedBy. Already Compile()d.
370 // 2. Should be checked by "recursed" field.
372 if(IsValidated (schema.ValidationId))
373 return errorCount;
375 if (recursed) {
376 error (h, "Circular type reference was found.");
377 return errorCount;
379 recursed = true;
381 CollectBaseType (h, schema);
383 if (content != null)
384 errorCount += content.Validate (h, schema);
387 // BaseSchemaType property
388 BaseXmlSchemaTypeInternal = content.ActualBaseSchemaType as XmlSchemaType;
389 if (this.BaseXmlSchemaTypeInternal == null)
390 this.DatatypeInternal = content.ActualBaseSchemaType as XmlSchemaDatatype;
393 // Datatype property
394 XmlSchemaSimpleType simple = BaseXmlSchemaType as XmlSchemaSimpleType;
395 if (simple != null)
396 this.DatatypeInternal = simple.Datatype;
397 // else
398 // DatatypeInternal = BaseSchemaType as XmlSchemaDatatype;
400 // 3.
401 XmlSchemaSimpleType baseSType = BaseXmlSchemaType as XmlSchemaSimpleType;
402 if (baseSType != null) {
403 if ((baseSType.FinalResolved & this.resolvedDerivedBy) != 0)
404 error (h, "Specified derivation is prohibited by the base simple type.");
407 // {variety}
408 if (this.resolvedDerivedBy == XmlSchemaDerivationMethod.Restriction &&
409 baseSType != null)
410 this.variety = baseSType.Variety;
411 else
412 this.variety = this.resolvedDerivedBy;
414 // 3.14.6 Derivation Valid (Restriction, Simple)
415 XmlSchemaSimpleTypeRestriction r = Content as XmlSchemaSimpleTypeRestriction;
416 object baseType = BaseXmlSchemaType != null ? (object) BaseXmlSchemaType : Datatype;
417 if (r != null)
418 ValidateDerivationValid (baseType, r.Facets, h, schema);
420 // TODO: describe which validation term this belongs to.
421 XmlSchemaSimpleTypeList l = Content as XmlSchemaSimpleTypeList;
422 if (l != null) {
423 XmlSchemaSimpleType itemSimpleType = l.ValidatedListItemType as XmlSchemaSimpleType;
424 if (itemSimpleType != null && itemSimpleType.Content is XmlSchemaSimpleTypeList)
425 error (h, "List type must not be derived from another list type.");
428 recursed = false;
429 ValidationId = schema.ValidationId;
430 return errorCount;
433 // 3.14.6 Derivation Valid (RestrictionSimple)
434 internal void ValidateDerivationValid (object baseType, XmlSchemaObjectCollection facets,
435 ValidationEventHandler h, XmlSchema schema)
437 // TODO
438 XmlSchemaSimpleType baseSimpleType = baseType as XmlSchemaSimpleType;
439 switch (this.Variety) {
440 // 1. atomic type
441 case XmlSchemaDerivationMethod.Restriction:
442 // 1.1
443 if (baseSimpleType != null && baseSimpleType.resolvedDerivedBy != XmlSchemaDerivationMethod.Restriction)
444 error (h, "Base schema type is not either atomic type or primitive type.");
445 // 1.2
446 if (baseSimpleType != null &&
447 (baseSimpleType.FinalResolved & XmlSchemaDerivationMethod.Restriction) != 0)
448 error (h, "Derivation by restriction is prohibited by the base simple type.");
449 // TODO: 1.3 facet restriction valid.
450 break;
451 case XmlSchemaDerivationMethod.List:
453 XmlSchemaSimpleTypeList thisList = Content as XmlSchemaSimpleTypeList;
454 // 2.1 item list type not allowed
455 if (baseSimpleType != null && baseSimpleType.resolvedDerivedBy == XmlSchemaDerivationMethod.List)
456 error (h, "Base list schema type is not allowed.");
457 XmlSchemaSimpleTypeUnion baseUnion = baseSimpleType.Content as XmlSchemaSimpleTypeUnion;
458 if (baseUnion != null) {
459 bool errorFound = false;
460 foreach (object memberType in baseUnion.ValidatedTypes) {
461 XmlSchemaSimpleType memberST = memberType as XmlSchemaSimpleType;
462 if (memberST != null && memberST.resolvedDerivedBy == XmlSchemaDerivationMethod.List)
463 errorFound = true;
465 if (errorFound)
466 error (h, "Base union schema type should not contain list types.");
469 // 2.2 facets limited
470 if (facets != null)
471 foreach (XmlSchemaFacet facet in facets) {
472 if (facet is XmlSchemaLengthFacet ||
473 facet is XmlSchemaMaxLengthFacet ||
474 facet is XmlSchemaMinLengthFacet ||
475 facet is XmlSchemaEnumerationFacet ||
476 facet is XmlSchemaPatternFacet)
477 continue;
478 else
479 error (h, "Not allowed facet was found on this simple type which derives list type.");
481 break;
482 case XmlSchemaDerivationMethod.Union:
483 // 3.1
485 // 3.2
486 if (facets != null)
487 foreach (XmlSchemaFacet facet in facets) {
488 if (facet is XmlSchemaEnumerationFacet ||
489 facet is XmlSchemaPatternFacet)
490 continue;
491 else
492 error (h, "Not allowed facet was found on this simple type which derives list type.");
494 break;
498 // 3.14.6 Type Derivation OK (Simple)
499 internal bool ValidateTypeDerivationOK (object baseType,
500 ValidationEventHandler h, XmlSchema schema, bool raiseError)
502 // 1
503 // Note that anyType should also be allowed as anySimpleType.
504 if (this == baseType || baseType == XmlSchemaSimpleType.AnySimpleType ||
505 baseType == XmlSchemaComplexType.AnyType)
506 return true;
508 // 2.1
509 XmlSchemaSimpleType baseSimpleType = baseType as XmlSchemaSimpleType;
510 if (baseSimpleType != null &&
511 (baseSimpleType.FinalResolved & resolvedDerivedBy) != 0) {
512 if (raiseError)
513 error (h, "Specified derivation is prohibited by the base type.");
514 return false;
517 // 2.2.1
518 if (BaseXmlSchemaType == baseType || Datatype == baseType)
519 return true;
521 // 2.2.2
522 XmlSchemaSimpleType thisBaseSimpleType = BaseXmlSchemaType as XmlSchemaSimpleType;
523 if (thisBaseSimpleType != null) {
524 if (thisBaseSimpleType.ValidateTypeDerivationOK (baseType, h, schema, false))
525 return true;
528 // 2.2.3
529 switch (Variety) {
530 case XmlSchemaDerivationMethod.Union:
531 case XmlSchemaDerivationMethod.List:
532 if (baseType == XmlSchemaSimpleType.AnySimpleType)
533 return true;
534 break;
537 // 2.2.4 validly derived from one of the union member type.
538 if (baseSimpleType != null && baseSimpleType.Variety == XmlSchemaDerivationMethod.Union) {
539 foreach (object memberType in ((XmlSchemaSimpleTypeUnion) baseSimpleType.Content).ValidatedTypes)
540 if (this.ValidateTypeDerivationOK (memberType, h, schema, false))
541 return true;
544 if (raiseError)
545 error(h, "Invalid simple type derivation was found.");
546 return false;
549 internal string Normalize (string s, XmlNameTable nt, XmlNamespaceManager nsmgr)
551 return Content.Normalize (s, nt, nsmgr);
554 //<simpleType
555 // final = (#all | (list | union | restriction))
556 // id = ID
557 // name = NCName
558 // {any attributes with non-schema namespace . . .}>
559 // Content: (annotation?, (restriction | list | union))
560 //</simpleType>
561 internal static XmlSchemaSimpleType Read(XmlSchemaReader reader, ValidationEventHandler h)
563 XmlSchemaSimpleType stype = new XmlSchemaSimpleType();
564 reader.MoveToElement();
566 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
568 error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);
569 reader.Skip();
570 return null;
573 stype.LineNumber = reader.LineNumber;
574 stype.LinePosition = reader.LinePosition;
575 stype.SourceUri = reader.BaseURI;
577 while(reader.MoveToNextAttribute())
579 if(reader.Name == "final")
581 Exception innerex;
582 stype.Final = XmlSchemaUtil.ReadDerivationAttribute(reader, out innerex, "final",
583 XmlSchemaUtil.FinalAllowed);
584 if(innerex != null)
585 error(h, "some invalid values not a valid value for final", innerex);
587 else if(reader.Name == "id")
589 stype.Id = reader.Value;
591 else if(reader.Name == "name")
593 stype.Name = reader.Value;
595 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
597 error(h,reader.Name + " is not a valid attribute for simpleType",null);
599 else
601 XmlSchemaUtil.ReadUnhandledAttribute(reader,stype);
605 reader.MoveToElement();
606 if(reader.IsEmptyElement)
607 return stype;
609 // Content: (annotation?, (restriction | list | union))
610 int level = 1;
611 while(reader.ReadNextElement())
613 if(reader.NodeType == XmlNodeType.EndElement)
615 if(reader.LocalName != xmlname)
616 error(h,"Should not happen :2: XmlSchemaSimpleType.Read, name="+reader.Name,null);
617 break;
619 if(level <= 1 && reader.LocalName == "annotation")
621 level = 2; //Only one annotation
622 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
623 if(annotation != null)
624 stype.Annotation = annotation;
625 continue;
627 if(level <= 2)
629 if(reader.LocalName == "restriction")
631 level = 3;
632 XmlSchemaSimpleTypeRestriction restriction = XmlSchemaSimpleTypeRestriction.Read(reader,h);
633 if(restriction != null)
634 stype.content = restriction;
635 continue;
637 if(reader.LocalName == "list")
639 level = 3;
640 XmlSchemaSimpleTypeList list = XmlSchemaSimpleTypeList.Read(reader,h);
641 if(list != null)
642 stype.content = list;
643 continue;
645 if(reader.LocalName == "union")
647 level = 3;
648 XmlSchemaSimpleTypeUnion union = XmlSchemaSimpleTypeUnion.Read(reader,h);
649 if(union != null)
650 stype.content = union;
651 continue;
654 reader.RaiseInvalidElementError();
656 return stype;