2006-02-20 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaComplexType.cs
1 //\r
2 // System.Xml.Schema.XmlSchemaComplexType.cs\r
3 //\r
4 // Authors:\r
5 //      Dwivedi, Ajay kumar  Adwiv@Yahoo.com\r
6 //      Enomoto, Atsushi     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.Collections;\r
31 using System.Xml;\r
32 using System.ComponentModel;\r
33 using System.Xml.Serialization;\r
34 \r
35 namespace System.Xml.Schema\r
36 {\r
37         /// <summary>\r
38         /// Summary description for XmlSchemaComplexType.\r
39         /// </summary>\r
40         public class XmlSchemaComplexType : XmlSchemaType\r
41         {\r
42                 private XmlSchemaAnyAttribute anyAttribute;\r
43                 private XmlSchemaObjectCollection attributes;\r
44                 private XmlSchemaObjectTable attributeUses;\r
45                 private XmlSchemaAnyAttribute attributeWildcard;\r
46                 private XmlSchemaDerivationMethod block;\r
47                 private XmlSchemaDerivationMethod blockResolved;\r
48                 private XmlSchemaContentModel contentModel;\r
49                 private XmlSchemaParticle validatableParticle;\r
50                 private XmlSchemaParticle contentTypeParticle;\r
51                 private bool isAbstract;\r
52                 private bool isMixed;\r
53                 private XmlSchemaParticle particle;\r
54                 private XmlSchemaContentType resolvedContentType;\r
55 \r
56                 internal bool ValidatedIsAbstract;\r
57                 internal bool ParentIsSchema = false;\r
58 \r
59                 const string xmlname = "complexType";\r
60 \r
61                 private static XmlSchemaComplexType anyType;\r
62 \r
63                 internal static XmlSchemaComplexType AnyType {\r
64                         get {\r
65                                 if (anyType == null) {\r
66                                         anyType = new XmlSchemaComplexType ();\r
67                                         anyType.Name = "";      // In MS.NET, it is not "anyType"\r
68                                         anyType.QNameInternal = XmlQualifiedName.Empty; // Not xs:anyType as well.\r
69 #if BUGGY_MS_COMPLIANT\r
70                                         anyType.validatableParticle = XmlSchemaParticle.Empty; // This code makes validator handles these schemas incorrectly: particlesIb001, mgM013, mgH014, ctE004, ctD004\r
71 #else\r
72                                         anyType.validatableParticle = XmlSchemaAny.AnyTypeContent;\r
73 #endif\r
74                                         anyType.contentTypeParticle = anyType.validatableParticle;\r
75                                         anyType.DatatypeInternal = XmlSchemaSimpleType.AnySimpleType;\r
76                                         anyType.isMixed = true;\r
77                                         anyType.resolvedContentType = XmlSchemaContentType.Mixed;\r
78                                 }\r
79                                 return anyType;\r
80                         }\r
81                 }\r
82 \r
83                 internal static readonly XmlQualifiedName AnyTypeName = new XmlQualifiedName ("anyType", XmlSchema.Namespace);\r
84 \r
85                 public XmlSchemaComplexType ()\r
86                 {\r
87                         attributes = new XmlSchemaObjectCollection();\r
88                         block = XmlSchemaDerivationMethod.None;\r
89                         attributeUses = new XmlSchemaObjectTable();\r
90                         validatableParticle = XmlSchemaParticle.Empty;\r
91                         contentTypeParticle = validatableParticle;\r
92                 }\r
93 \r
94                 #region Attributes\r
95 \r
96                 [DefaultValue(false)]\r
97                 [System.Xml.Serialization.XmlAttribute("abstract")]\r
98                 public bool IsAbstract \r
99                 {\r
100                         get{ return  isAbstract; }\r
101                         set{ isAbstract = value; }\r
102                 }\r
103                 [DefaultValue(XmlSchemaDerivationMethod.None)]\r
104                 [System.Xml.Serialization.XmlAttribute("block")]\r
105                 public XmlSchemaDerivationMethod Block\r
106                 {\r
107                         get{ return  block; }\r
108                         set{ block = value; }\r
109                 }\r
110                 [DefaultValue(false)]\r
111                 [System.Xml.Serialization.XmlAttribute("mixed")]\r
112                 public override bool IsMixed\r
113                 {\r
114                         get{ return  isMixed; }\r
115                         set{ isMixed = value; }\r
116                 }\r
117                 \r
118                 #endregion\r
119                 \r
120                 #region Elements\r
121                                 \r
122                 [XmlElement("simpleContent",typeof(XmlSchemaSimpleContent))]\r
123                 [XmlElement("complexContent",typeof(XmlSchemaComplexContent))]\r
124                 public XmlSchemaContentModel ContentModel \r
125                 {\r
126                         get{ return  contentModel; } \r
127                         set{ contentModel = value; }\r
128                 }\r
129 \r
130                 //LAMESPEC: The default value for particle in Schema is of Type EmptyParticle (internal?)\r
131                 [XmlElement("group",typeof(XmlSchemaGroupRef))]\r
132                 [XmlElement("all",typeof(XmlSchemaAll))]\r
133                 [XmlElement("choice",typeof(XmlSchemaChoice))]\r
134                 [XmlElement("sequence",typeof(XmlSchemaSequence))]\r
135                 public XmlSchemaParticle Particle \r
136                 {\r
137                         get{ return  particle; } \r
138                         set{ particle = value; }\r
139                 }\r
140 \r
141                 [XmlElement("attribute",typeof(XmlSchemaAttribute))]\r
142                 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef))]\r
143                 public XmlSchemaObjectCollection Attributes \r
144                 {\r
145                         get{ return attributes; }\r
146                 }\r
147 \r
148                 [XmlElement("anyAttribute")]\r
149                 public XmlSchemaAnyAttribute AnyAttribute \r
150                 {\r
151                         get{ return  anyAttribute; }\r
152                         set{ anyAttribute = value; }\r
153                 }\r
154                 #endregion\r
155 \r
156                 #region XmlIgnore\r
157                 [XmlIgnore]\r
158                 public XmlSchemaContentType ContentType \r
159                 {\r
160                         get{ return resolvedContentType; }\r
161                 }\r
162                 [XmlIgnore]\r
163                 public XmlSchemaParticle ContentTypeParticle \r
164                 {\r
165                         get{ return contentTypeParticle; }\r
166                 }\r
167                 [XmlIgnore]\r
168                 public XmlSchemaDerivationMethod BlockResolved \r
169                 {\r
170                         get{ return blockResolved; }\r
171                 }\r
172                 [XmlIgnore]\r
173                 public XmlSchemaObjectTable AttributeUses \r
174                 {\r
175                         get{ return attributeUses; }\r
176                 }\r
177                 [XmlIgnore]\r
178                 public XmlSchemaAnyAttribute AttributeWildcard \r
179                 {\r
180                         get{ return attributeWildcard; }\r
181                 }\r
182 \r
183                 #endregion\r
184 \r
185                 internal XmlSchemaParticle ValidatableParticle \r
186                 {\r
187                         get{ return contentTypeParticle; }\r
188                 }\r
189 \r
190                 /// <remarks>\r
191                 /// 1. If ContentModel is present, neither particle nor Attributes nor AnyAttribute can be present.\r
192                 /// 2. If particle is present, \r
193                 /// a. For a topLevelComplexType\r
194                 ///             1. name must be present and type NCName\r
195                 ///             2. if block is #all, blockdefault is #all, else List of (extension | restriction)\r
196                 ///             3. if final is #all, finaldefault is #all, else List of (extension | restriction)\r
197                 ///     b. For a local Complex type \r
198                 ///             1. abstract must be false\r
199                 ///             2. Name must be absent\r
200                 ///             3. final must be absent\r
201                 ///             4. block must be absent\r
202                 ///             \r
203                 /// </remarks>\r
204                 internal override int Compile (ValidationEventHandler h, XmlSchema schema)\r
205                 {\r
206                         // If this is already compiled this time, simply skip.\r
207                         if (this.IsComplied (schema.CompilationId))\r
208                                 return errorCount;\r
209 \r
210 #if NET_2_0\r
211                         if (ContentModel != null)\r
212                                 ContentModel.Parent = this;\r
213                         if (Particle != null)\r
214                                 Particle.Parent = this;\r
215                         if (AnyAttribute != null)\r
216                                 AnyAttribute.Parent = this;\r
217                         foreach (XmlSchemaObject obj in Attributes)\r
218                                 obj.Parent = this;\r
219 #endif\r
220 \r
221                         ValidatedIsAbstract = isAbstract;\r
222 \r
223                         if (isRedefinedComponent) {\r
224                                 if (Annotation != null)\r
225                                         Annotation.isRedefinedComponent = true;\r
226                                 if (AnyAttribute != null)\r
227                                         AnyAttribute.isRedefinedComponent = true;\r
228                                 foreach (XmlSchemaObject obj in Attributes)\r
229                                         obj.isRedefinedComponent = true;\r
230                                 if (ContentModel != null)\r
231                                         ContentModel.isRedefinedComponent = true;\r
232                                 if (Particle != null)\r
233                                         Particle.isRedefinedComponent = true;\r
234                         }\r
235 \r
236                         // block/final resolution\r
237                         if(ParentIsSchema || isRedefineChild)\r
238                         {\r
239                                 if(this.Name == null || this.Name == string.Empty)\r
240                                         error(h,"name must be present in a top level complex type");\r
241                                 else if(!XmlSchemaUtil.CheckNCName(Name))\r
242                                         error(h,"name must be a NCName");\r
243                                 else\r
244                                         this.QNameInternal = new XmlQualifiedName(Name, schema.TargetNamespace);\r
245                                 \r
246                                 if(Block != XmlSchemaDerivationMethod.None)\r
247                                 {\r
248                                         if(Block == XmlSchemaDerivationMethod.All)\r
249                                         {\r
250                                                 blockResolved = XmlSchemaDerivationMethod.All;\r
251                                         }\r
252                                         else\r
253                                         {\r
254                                                 if ((Block & XmlSchemaUtil.ComplexTypeBlockAllowed) != Block)\r
255                                                         error (h, "Invalid block specification.");\r
256                                                 blockResolved = Block & XmlSchemaUtil.ComplexTypeBlockAllowed;\r
257                                         }\r
258                                 }\r
259                                 else\r
260                                 {\r
261                                         switch (schema.BlockDefault) {\r
262                                         case XmlSchemaDerivationMethod.All:\r
263                                                 blockResolved = XmlSchemaDerivationMethod.All;\r
264                                                 break;\r
265                                         case XmlSchemaDerivationMethod.None:\r
266                                                 blockResolved = XmlSchemaDerivationMethod.Empty;\r
267                                                 break;\r
268                                         default:\r
269                                                 blockResolved = schema.BlockDefault & XmlSchemaUtil.ComplexTypeBlockAllowed;\r
270                                                 break;\r
271                                         }\r
272                                 }\r
273 \r
274                                 if(Final != XmlSchemaDerivationMethod.None)\r
275                                 {\r
276                                         if(Final == XmlSchemaDerivationMethod.All)\r
277                                                 finalResolved = XmlSchemaDerivationMethod.All;\r
278                                         else if ((Final & XmlSchemaUtil.FinalAllowed) != Final)\r
279                                                 error (h, "Invalid final specification.");\r
280                                         else\r
281                                                 finalResolved = Final;\r
282                                 }\r
283                                 else\r
284                                 {\r
285                                         switch (schema.FinalDefault) {\r
286                                         case XmlSchemaDerivationMethod.All:\r
287                                                 finalResolved = XmlSchemaDerivationMethod.All;\r
288                                                 break;\r
289                                         case XmlSchemaDerivationMethod.None:\r
290                                                 finalResolved = XmlSchemaDerivationMethod.Empty;\r
291                                                 break;\r
292                                         default:\r
293                                                 finalResolved = schema.FinalDefault & XmlSchemaUtil.FinalAllowed;\r
294                                                 break;\r
295                                         }\r
296                                 }\r
297                         }\r
298                         else // Not Top Level\r
299                         {\r
300                                 if(isAbstract)\r
301                                         error(h,"abstract must be false in a local complex type");\r
302                                 if(Name != null)\r
303                                         error(h,"name must be absent in a local complex type");\r
304                                 if(Final != XmlSchemaDerivationMethod.None)\r
305                                         error(h,"final must be absent in a local complex type");\r
306                                 if(block != XmlSchemaDerivationMethod.None)\r
307                                         error(h,"block must be absent in a local complex type");\r
308                         }\r
309 \r
310                         // Process contents and BaseSchemaType\r
311                         if(contentModel != null)\r
312                         {\r
313                                 if(anyAttribute != null || Attributes.Count != 0 || Particle != null)\r
314                                         error(h,"attributes, particles or anyattribute is not allowed if ContentModel is present");\r
315                                 errorCount += contentModel.Compile (h, schema);\r
316 \r
317                                 XmlSchemaSimpleContent smodel = ContentModel as XmlSchemaSimpleContent;\r
318                                 if(smodel != null)\r
319                                 {\r
320                                         XmlSchemaSimpleContentExtension sscx = smodel.Content as XmlSchemaSimpleContentExtension;\r
321                                         if (sscx == null) {\r
322                                                 XmlSchemaSimpleContentRestriction sscr = smodel.Content as XmlSchemaSimpleContentRestriction;\r
323                                                 if (sscr != null) {\r
324                                                         if (sscr.BaseType != null) {\r
325                                                                 sscr.BaseType.Compile (h, schema);\r
326                                                                 BaseXmlSchemaTypeInternal = sscr.BaseType;\r
327                                                         }\r
328                                                 }\r
329                                         }\r
330                                 }\r
331                         }\r
332                         else\r
333                         {\r
334                                 if (Particle != null)\r
335                                         errorCount += Particle.Compile (h, schema);\r
336 \r
337                                 if(this.anyAttribute != null)\r
338                                 {\r
339                                         AnyAttribute.Compile(h,schema);\r
340                                 }\r
341                                 foreach(XmlSchemaObject obj in Attributes)\r
342                                 {\r
343                                         if(obj is XmlSchemaAttribute)\r
344                                         {\r
345                                                 XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;\r
346                                                 errorCount += attr.Compile(h,schema);\r
347                                         }\r
348                                         else if(obj is XmlSchemaAttributeGroupRef)\r
349                                         {\r
350                                                 XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;\r
351                                                 errorCount += atgrp.Compile(h,schema);\r
352                                         }\r
353                                         else\r
354                                                 error(h,obj.GetType() +" is not valid in this place::ComplexType");\r
355                                 }\r
356                         }\r
357 \r
358                         XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);\r
359                         this.CompilationId = schema.CompilationId;\r
360                         return errorCount;\r
361                 }\r
362 \r
363                 Guid CollectProcessId;\r
364 \r
365                 private void CollectSchemaComponent (ValidationEventHandler h, XmlSchema schema)\r
366                 {\r
367                         if (CollectProcessId == schema.CompilationId)\r
368                                 return;\r
369                         // Below are already contributed by Compile():\r
370                         // {name}, {namespace} => QualifiedName, QNameInternal\r
371                         // {abstract} => ValidatedIsAbstract\r
372                         // {prohibited substitutions} => BlockResolved\r
373                         // {final} => FinalResolved\r
374                         // {annotations} => Annotation (XmlSchemaAnnotated)\r
375 \r
376                         // Below are different properties depending on simpleContent | complexContent.\r
377                         // {base type definition}\r
378                         // {derivation method}\r
379                         // {attribute uses} => AttributeUses (later)\r
380                         // {attribute wildcard} => AttributeWildcard (later)\r
381                         // {content type}\r
382 \r
383 \r
384                         // {base type definition} => baseSchemaTypeInternal (later)\r
385                         if (contentModel != null) {\r
386                                 BaseSchemaTypeName = contentModel.Content != null ? contentModel.Content.GetBaseTypeName () : XmlQualifiedName.Empty;\r
387 \r
388                                 BaseXmlSchemaTypeInternal = schema.SchemaTypes [BaseSchemaTypeName] as XmlSchemaType;\r
389                         }\r
390                         // Resolve redefine.\r
391                         if (this.isRedefineChild && BaseXmlSchemaType != null && this.QualifiedName == BaseSchemaTypeName) {\r
392                                 XmlSchemaType redType = (XmlSchemaType) redefinedObject;\r
393                                 if (redType == null)\r
394                                         error (h, "Redefinition base type was not found.");\r
395                                 else\r
396                                         BaseXmlSchemaTypeInternal = redType;\r
397                         }\r
398 \r
399                         // {derivation method} => resolvedDerivedBy\r
400                         if (contentModel != null && contentModel.Content != null) {\r
401                                 resolvedDerivedBy =\r
402                                         contentModel.Content.IsExtension ?\r
403                                         XmlSchemaDerivationMethod.Extension :\r
404                                         XmlSchemaDerivationMethod.Restriction;\r
405                         }\r
406                         else\r
407                                 resolvedDerivedBy = XmlSchemaDerivationMethod.Empty;\r
408 \r
409 \r
410                         // {content type} => ContentType and ContentTypeParticle (later)\r
411                         if (ContentModel != null) {\r
412                                 CollectContentTypeFromContentModel (h, schema);\r
413                         } else\r
414                                 CollectContentTypeFromImmediateContent ();\r
415                         contentTypeParticle = validatableParticle.GetOptimizedParticle (true);\r
416                         if (contentTypeParticle == XmlSchemaParticle.Empty && resolvedContentType == XmlSchemaContentType.ElementOnly)\r
417                                 resolvedContentType = XmlSchemaContentType.Empty;\r
418 \r
419                         CollectProcessId = schema.CompilationId;\r
420                 }\r
421 \r
422                 #region {content type}\r
423                 private void CollectContentTypeFromImmediateContent ()\r
424                 {\r
425                         // leave resolvedDerivedBy as Empty\r
426                         if (Particle != null)\r
427                                 validatableParticle = Particle;\r
428                         if (this == AnyType) {\r
429                                 resolvedContentType = XmlSchemaContentType.Mixed;\r
430                                 return;\r
431                         }\r
432 \r
433                         if (validatableParticle == XmlSchemaParticle.Empty) {\r
434                                 // note that this covers "Particle == null" case\r
435                                 if (this.IsMixed)\r
436                                         resolvedContentType = XmlSchemaContentType.TextOnly;\r
437                                 else\r
438                                         resolvedContentType = XmlSchemaContentType.Empty;\r
439                         } else {\r
440                                 if (this.IsMixed)\r
441                                         resolvedContentType = XmlSchemaContentType.Mixed;\r
442                                 else\r
443                                         resolvedContentType = XmlSchemaContentType.ElementOnly;\r
444                         }\r
445                         if (this != AnyType)\r
446                                 BaseXmlSchemaTypeInternal = XmlSchemaComplexType.AnyType;\r
447                 }\r
448 \r
449                 private void CollectContentTypeFromContentModel (ValidationEventHandler h, XmlSchema schema)\r
450                 {\r
451                         if (ContentModel.Content == null) {\r
452                                 // basically it is error. Recover by specifying empty content.\r
453                                 validatableParticle = XmlSchemaParticle.Empty;\r
454                                 resolvedContentType = XmlSchemaContentType.Empty;\r
455                                 return;\r
456                         }\r
457 \r
458                         if (ContentModel.Content is XmlSchemaComplexContentExtension)\r
459                                 CollectContentTypeFromComplexExtension (h, schema);\r
460                         if (ContentModel.Content is XmlSchemaComplexContentRestriction)\r
461                                 CollectContentTypeFromComplexRestriction ();\r
462                 }\r
463 \r
464                 private void CollectContentTypeFromComplexExtension (ValidationEventHandler h, XmlSchema schema)\r
465                 {\r
466                         XmlSchemaComplexContentExtension cce = (XmlSchemaComplexContentExtension) ContentModel.Content;\r
467                         XmlSchemaComplexType baseComplexType = this.BaseXmlSchemaType as XmlSchemaComplexType;\r
468                         if (baseComplexType != null)\r
469                                 baseComplexType.CollectSchemaComponent (h ,schema);\r
470 \r
471                         // It must exist, but consider validation error case.\r
472                         if (BaseSchemaTypeName == XmlSchemaComplexType.AnyTypeName)\r
473                                 baseComplexType = XmlSchemaComplexType.AnyType;\r
474 \r
475                         // On error case, it simple reject any contents\r
476                         if (baseComplexType == null) {\r
477                                 validatableParticle = XmlSchemaParticle.Empty;\r
478                                 resolvedContentType = XmlSchemaContentType.Empty;\r
479                                 return;\r
480                         }\r
481 \r
482                         // 3.4.2 complex content {content type}\r
483                         if (cce.Particle == null || cce.Particle == XmlSchemaParticle.Empty) {\r
484                                 // - 2.1\r
485                                 if (baseComplexType == null) {\r
486                                         // Basically it is an error. Considering ValidationEventHandler.\r
487                                         validatableParticle = XmlSchemaParticle.Empty;\r
488                                         resolvedContentType = XmlSchemaContentType.Empty;\r
489                                 } else {\r
490                                         validatableParticle = baseComplexType.ValidatableParticle;\r
491                                         resolvedContentType = baseComplexType.resolvedContentType;\r
492                                 }\r
493                         } else if (baseComplexType.validatableParticle == XmlSchemaParticle.Empty\r
494                                 || baseComplexType == XmlSchemaComplexType.AnyType) {\r
495                                 // - 2.2\r
496                                 validatableParticle = cce.Particle;\r
497                                 resolvedContentType = GetComplexContentType (contentModel);\r
498                         } else {\r
499                                 // - 2.3 : create a new sequences that merges both contents.\r
500                                 XmlSchemaSequence seq = new XmlSchemaSequence ();\r
501                                 this.CopyInfo (seq);\r
502                                 seq.Items.Add (baseComplexType.validatableParticle);\r
503                                 seq.Items.Add (cce.Particle);\r
504                                 seq.Compile (h, schema);\r
505                                 seq.Validate (h, schema);\r
506                                 validatableParticle = seq;\r
507                                 resolvedContentType = GetComplexContentType (contentModel);\r
508                         }\r
509                         if (validatableParticle == null)\r
510                                 validatableParticle = XmlSchemaParticle.Empty;\r
511                 }\r
512 \r
513                 private void CollectContentTypeFromComplexRestriction ()\r
514                 {\r
515                         XmlSchemaComplexContentRestriction ccr = (XmlSchemaComplexContentRestriction) ContentModel.Content;\r
516                         // 3.4.2 complex content schema component {content type}\r
517                         // - 1.1.1\r
518                         bool isEmptyParticle = false;\r
519                         if (ccr.Particle == null) \r
520                                 isEmptyParticle = true;\r
521                         else {\r
522                                 XmlSchemaGroupBase gb = ccr.Particle as XmlSchemaGroupBase;\r
523                                 if (gb != null) {\r
524                                         // - 1.1.2\r
525                                         if (!(gb is XmlSchemaChoice) && gb.Items.Count == 0)\r
526                                                 isEmptyParticle = true;\r
527                                         // - 1.1.3\r
528                                         else if (gb is XmlSchemaChoice && gb.Items.Count == 0 && gb.ValidatedMinOccurs == 0)\r
529                                                 isEmptyParticle = true;\r
530                                 }\r
531                         }\r
532                         if (isEmptyParticle) {\r
533                                 resolvedContentType = XmlSchemaContentType.Empty;\r
534                                 validatableParticle = XmlSchemaParticle.Empty;\r
535                         } else {\r
536                                 // - 1.2.1\r
537                                 resolvedContentType = GetComplexContentType (contentModel);\r
538                                 // - 1.2.2\r
539                                 validatableParticle = ccr.Particle;\r
540                         }\r
541                 }\r
542 \r
543                 // 3.4.2 Complex Content Schema Component {content type} 1.2.1\r
544                 private XmlSchemaContentType GetComplexContentType (XmlSchemaContentModel content)\r
545                 {\r
546                         if (this.IsMixed || ((XmlSchemaComplexContent) content).IsMixed)\r
547                                 return XmlSchemaContentType.Mixed;\r
548                         else\r
549                                 return XmlSchemaContentType.ElementOnly;\r
550                 }\r
551                 #endregion\r
552 \r
553                 //\r
554                 // We have to validate:\r
555                 //\r
556                 //      - 3.4.3 Complex Type Definition Representation OK\r
557                 //      - 3.4.6 Type Definition Properties Correct\r
558                 //      - 3.4.6 Derivation Valid (Extension)\r
559                 //      - 3.4.6 Derivation Valid (Restriction, Complex)\r
560                 //\r
561                 // There are many schema errata:\r
562                 // http://www.w3.org/2001/05/xmlschema-errata#Errata1\r
563                 //\r
564                 // E1-43 Derivation Valid (Restriction, Complex) 5.\r
565                 // E1-21 Derivation Valid (Restriction, Complex) 4.3.\r
566                 // E1-17 Type Derivation OK (Complex) 2.1.\r
567                 //\r
568                 // And E1-38, E1-37, E1-30, E1-27\r
569                 //\r
570                 internal override int Validate (ValidationEventHandler h, XmlSchema schema)\r
571                 {\r
572                         if (IsValidated (schema.ValidationId))\r
573                                 return errorCount;\r
574                         // FIXME: omitting it causes StackOverflowException\r
575                         // among the compilation of element and types, but\r
576                         // it may result in insufficient results.\r
577                         ValidationId = schema.ValidationId;\r
578 \r
579                         CollectSchemaComponent (h, schema);\r
580 \r
581                         // 3.4.6: Properties Correct\r
582                         // Term. 1 => 3.4.1 already done by CollectSchemaComponent()\r
583                         //            except for {attribute uses} and {attribute wildcard}\r
584                         // Term. 2, 3 and 4 goes to ValidateContentModel().\r
585                         // Term. 5 follows in this method.\r
586                         //\r
587                         if (ContentModel != null)\r
588                                 ValidateContentModel (h, schema);\r
589                         else {\r
590                                 if (Particle != null)\r
591                                         ValidateImmediateParticle (h, schema);\r
592                                 ValidateImmediateAttributes (h, schema);\r
593                         }\r
594 \r
595                         // Additional support for 3.8.6 All Group Limited\r
596                         if (ContentTypeParticle != null) {\r
597                                 XmlSchemaAll termAll = contentTypeParticle.GetOptimizedParticle (true) as XmlSchemaAll;\r
598                                 if (termAll != null && (termAll.ValidatedMaxOccurs != 1 || contentTypeParticle.ValidatedMaxOccurs != 1)) // here contentTypeParticle is used to check occurence.\r
599                                         error (h, "Particle whose term is -all- and consists of complex type content particle must have maxOccurs = 1.");\r
600                         }\r
601 \r
602 #if NET_2_0\r
603                         if (schema.Schemas.CompilationSettings != null &&\r
604                                 schema.Schemas.CompilationSettings.EnableUpaCheck)\r
605 #endif\r
606                         // This check is optional only after 2.0\r
607                         contentTypeParticle.ValidateUniqueParticleAttribution (new XmlSchemaObjectTable (),\r
608                                 new ArrayList (), h, schema);\r
609                         contentTypeParticle.ValidateUniqueTypeAttribution (\r
610                                 new XmlSchemaObjectTable (), h, schema);\r
611 \r
612                         // 3.4.6 Properties Correct :: 5 (Two distinct ID attributes)\r
613                         XmlSchemaAttribute idAttr = null;\r
614                         foreach (DictionaryEntry entry in attributeUses) {\r
615                                 XmlSchemaAttribute attr = (XmlSchemaAttribute) entry.Value;\r
616                                 XmlSchemaDatatype dt = attr.AttributeType as XmlSchemaDatatype;\r
617                                 if (dt != null && dt.TokenizedType != XmlTokenizedType.ID)\r
618                                         continue;\r
619                                 if (dt == null)\r
620                                         dt = ((XmlSchemaSimpleType) attr.AttributeType).Datatype;\r
621                                 if (dt != null && dt.TokenizedType == XmlTokenizedType.ID) {\r
622                                         if (idAttr != null)\r
623                                                 error (h, "Two or more ID typed attribute declarations in a complex type are found.");\r
624                                         else\r
625                                                 idAttr = attr;\r
626                                 }\r
627                         }\r
628 \r
629                         ValidationId = schema.ValidationId;\r
630                         return errorCount;\r
631                 }\r
632 \r
633                 private void ValidateImmediateParticle (ValidationEventHandler h, XmlSchema schema)\r
634                 {\r
635                         errorCount += particle.Validate (h, schema);\r
636                         XmlSchemaGroupRef pgrp = Particle as XmlSchemaGroupRef;\r
637                         if (pgrp != null) {\r
638                                 if (pgrp.TargetGroup != null)\r
639                                         errorCount += pgrp.TargetGroup.Validate (h,schema);\r
640                                 // otherwise, it might be missing sub components.\r
641                                 else if (!schema.IsNamespaceAbsent (pgrp.RefName.Namespace))\r
642                                         error (h, "Referenced group " + pgrp.RefName + " was not found in the corresponding schema.");\r
643                         }\r
644                 }\r
645 \r
646                 private void ValidateImmediateAttributes (ValidationEventHandler h, XmlSchema schema)\r
647                 {\r
648                         // {attribute uses}\r
649                         // also checks 3.4.6 Properties Correct :: 4 (Distinct attributes)\r
650                         attributeUses = new XmlSchemaObjectTable ();\r
651                         XmlSchemaUtil.ValidateAttributesResolved (attributeUses,\r
652                                 h, schema, attributes, anyAttribute, ref attributeWildcard, null, false);\r
653                 }\r
654 \r
655                 private void ValidateContentModel (ValidationEventHandler h, XmlSchema schema)\r
656                 {\r
657                         XmlSchemaType baseType = BaseXmlSchemaTypeInternal;\r
658 \r
659                         // Here we check 3.4.6 Properties Correct :: 2. and 3.\r
660                         errorCount += contentModel.Validate (h, schema);\r
661                         XmlSchemaComplexContentExtension cce = contentModel.Content as XmlSchemaComplexContentExtension;\r
662                         XmlSchemaComplexContentRestriction ccr = contentModel.Content as XmlSchemaComplexContentRestriction;\r
663                         XmlSchemaSimpleContentExtension sce = contentModel.Content as XmlSchemaSimpleContentExtension;\r
664                         XmlSchemaSimpleContentRestriction scr = contentModel.Content as XmlSchemaSimpleContentRestriction;\r
665 \r
666                         XmlSchemaAnyAttribute localAnyAttribute = null;\r
667                         XmlSchemaAnyAttribute baseAnyAttribute = null;\r
668 \r
669                         // 3.4.6 Properties Correct :: 3. Circular definition prohibited.\r
670                         if (ValidateRecursionCheck ())\r
671                                 error (h, "Circular definition of schema types was found.");\r
672                         if (baseType != null) {\r
673                                 baseType.Validate (h, schema);\r
674                                 // Fill "Datatype" property.\r
675                                 this.DatatypeInternal = baseType.Datatype;\r
676                         } else if (BaseSchemaTypeName == XmlSchemaComplexType.AnyTypeName)\r
677                                 DatatypeInternal = XmlSchemaSimpleType.AnySimpleType;
678                         else if (XmlSchemaUtil.IsBuiltInDatatypeName (BaseSchemaTypeName)) {\r
679                                 DatatypeInternal = XmlSchemaDatatype.FromName (BaseSchemaTypeName);\r
680                         }\r
681 \r
682                         XmlSchemaComplexType baseComplexType = baseType as XmlSchemaComplexType;\r
683                         XmlSchemaSimpleType baseSimpleType = baseType as XmlSchemaSimpleType;\r
684 \r
685                         // 3.4.6 Derivation Valid (common to Extension and Restriction, Complex) :: 1.\r
686                         if (baseType != null && (baseType.FinalResolved & resolvedDerivedBy) != 0)\r
687                                         error (h, "Specified derivation is specified as final by derived schema type.");\r
688 \r
689                         // 3.4.6 Properties Correct :: 2.\r
690                         // Simple {base type definition} and restriction {derivation method} not allowed.\r
691                         if (baseSimpleType != null && resolvedDerivedBy == XmlSchemaDerivationMethod.Restriction)\r
692                                 error (h, "If the base schema type is a simple type, then this type must be extension.");\r
693 \r
694                         // Common to complexContent\r
695                         if (cce != null || ccr != null) {\r
696                                 // 3.4.3 Complex Type Definition Representation OK :: 1.\r
697                                 // base\r
698                                 if (BaseSchemaTypeName == XmlSchemaComplexType.AnyTypeName)\r
699                                         baseComplexType = XmlSchemaComplexType.AnyType;\r
700                                 else if (XmlSchemaUtil.IsBuiltInDatatypeName (BaseSchemaTypeName))\r
701                                         error (h, "Referenced base schema type is XML Schema datatype.");\r
702                                 else if (baseComplexType == null && !schema.IsNamespaceAbsent (BaseSchemaTypeName.Namespace))\r
703                                         error (h, "Referenced base schema type " + BaseSchemaTypeName + " was not complex type or not found in the corresponding schema.");\r
704                         }\r
705                         // Common to simpleContent \r
706                         else {\r
707                                 // ContentType of {content type}\r
708                                 resolvedContentType = XmlSchemaContentType.TextOnly;\r
709 \r
710                                 // 3.4.3 Complex Type Definition Representation OK :: 1.\r
711                                 // base\r
712                                 if (BaseSchemaTypeName == XmlSchemaComplexType.AnyTypeName)\r
713                                         baseComplexType = XmlSchemaComplexType.AnyType;
714 \r
715                                 if (baseComplexType != null && baseComplexType.ContentType != XmlSchemaContentType.TextOnly) {\r
716                                         error (h, "Base schema complex type of a simple content must be simple content type. Base type is " + BaseSchemaTypeName);\r
717                                 } else if (sce == null && (baseSimpleType != null && BaseSchemaTypeName.Namespace != XmlSchema.Namespace)) {\r
718                                         error (h, "If a simple content is not an extension, base schema type must be complex type. Base type is " + BaseSchemaTypeName);\r
719                                 } else if (XmlSchemaUtil.IsBuiltInDatatypeName (BaseSchemaTypeName)) {\r
720                                         // do nothing for particle.\r
721                                 }\r
722                                 // otherwise, it might be missing sub components.\r
723                                 else if (baseType == null && !schema.IsNamespaceAbsent (BaseSchemaTypeName.Namespace))\r
724                                         error (h, "Referenced base schema type " + BaseSchemaTypeName + " was not found in the corresponding schema.");\r
725 \r
726                                 // 3.4.3 Complex Type Definition Representation OK :: 2.\r
727                                 // Note that baseSimpleType is also allowed as to Errata E1-27 (http://www.w3.org/2001/05/xmlschema-errata)\r
728                                 if (baseComplexType != null) {\r
729                                         if (baseComplexType.ContentType == XmlSchemaContentType.TextOnly) {\r
730                                                 // 2.1.1\r
731                                         // Here "baseComplexType.Particle != null" is required for error-ignorant case\r
732                                         } else if (scr != null && baseComplexType.ContentType == XmlSchemaContentType.Mixed && baseComplexType.Particle != null && baseComplexType.Particle.ValidateIsEmptiable () && scr.BaseType != null) {\r
733                                                 // 2.1.2 && 2.2: OK\r
734                                         }\r
735                                         else\r
736                                                 error (h, "Base complex type of a simple content restriction must be text only.");\r
737                                 } else {\r
738                                         if (sce != null && baseComplexType == null) {\r
739                                                 // 2.1.3 : OK\r
740                                         }\r
741                                         else\r
742                                                 error (h, "Not allowed base type of a simple content restriction.");\r
743                                 }\r
744                         }\r
745 \r
746                         // complexType/complexContent/extension\r
747                         if (cce != null) {\r
748                                 // I don't think 3.4.6 Derivation Valid (Extension) :: 1.2\r
749                                 // is constraining anything here, since 3.4.2 {attribute uses}\r
750                                 // defines as to include base type's attribute uses.\r
751                                 localAnyAttribute = cce.AnyAttribute;\r
752                                 if (baseComplexType != null) {\r
753                                         foreach (DictionaryEntry entry in baseComplexType.AttributeUses) {\r
754                                                 XmlSchemaAttribute attr = (XmlSchemaAttribute) entry.Value;\r
755                                                 XmlSchemaUtil.AddToTable (attributeUses, attr, attr.QualifiedName, h);\r
756                                         }\r
757                                         baseAnyAttribute = baseComplexType.AttributeWildcard;\r
758                                 }\r
759                                 // attributes\r
760                                 errorCount += XmlSchemaUtil.ValidateAttributesResolved (\r
761                                         this.attributeUses, h, schema, cce.Attributes, \r
762                                         cce.AnyAttribute , ref attributeWildcard, null, true);\r
763 \r
764                                 // After adding them, test extension validity.\r
765                                 if (baseComplexType != null)\r
766                                         this.ValidateComplexBaseDerivationValidExtension (baseComplexType, h, schema);\r
767                                 else if (baseSimpleType != null)\r
768                                         this.ValidateSimpleBaseDerivationValidExtension (baseSimpleType, h, schema);\r
769                         }\r
770                         // complexType/complexContent/restriction\r
771                         if (ccr != null) {\r
772                                 // For ValidationEventHandler.\r
773                                 if (baseComplexType == null)\r
774                                         baseComplexType = XmlSchemaComplexType.AnyType;\r
775                                 if (ccr.Particle != null)\r
776                                         ccr.Particle.Validate (h, schema);\r
777 \r
778                                 // attributes\r
779                                 localAnyAttribute = ccr.AnyAttribute;\r
780                                 this.attributeWildcard = localAnyAttribute;\r
781                                 if (baseComplexType != null)\r
782                                         baseAnyAttribute = baseComplexType.AttributeWildcard;\r
783                                 if (baseAnyAttribute != null && localAnyAttribute != null)\r
784                                         // 1.3 attribute wildcard subset. (=> 3.10.6)\r
785                                         localAnyAttribute.ValidateWildcardSubset (baseAnyAttribute, h, schema);\r
786 \r
787                                 // 3.4.2 Complex Type Definition with complex content Schema Component\r
788                                 // and its {attribute uses} and {attribute wildcard} are done here (descendantly)\r
789                                 errorCount += XmlSchemaUtil.ValidateAttributesResolved (\r
790                                         this.attributeUses, h, schema, ccr.Attributes, \r
791                                         ccr.AnyAttribute, ref attributeWildcard, null, false);\r
792                                 foreach (DictionaryEntry entry in baseComplexType.AttributeUses) {\r
793                                         XmlSchemaAttribute attr = (XmlSchemaAttribute) entry.Value;\r
794                                         if (attributeUses [attr.QualifiedName] == null)\r
795                                                 XmlSchemaUtil.AddToTable (attributeUses, attr, attr.QualifiedName, h);\r
796                                 }\r
797 \r
798                                 // Derivation Valid (Restriction, Complex) :: 5.\r
799                                 // Also see E1-15 of http://www.w3.org/2001/05/xmlschema-errata#Errata1\r
800                                 // 5.1 shouled be in scr (XmlSchemaSimpleContentRestriction)\r
801                                 this.ValidateDerivationValidRestriction (baseComplexType, h, schema);\r
802                         }\r
803                         // complexType/simpleContent/extension\r
804                         if (sce != null) {\r
805                                 errorCount += XmlSchemaUtil.ValidateAttributesResolved (\r
806                                         this.attributeUses, h, schema, sce.Attributes, \r
807                                         sce.AnyAttribute, ref attributeWildcard, null, true);\r
808 \r
809                                 // Attributes\r
810                                 // I don't think 3.4.6 Derivation Valid (Extension) :: 1.2\r
811                                 // is constraining anything here, since 3.4.2 {attribute uses}\r
812                                 // defines as to include base type's attribute uses.\r
813                                 localAnyAttribute = sce.AnyAttribute;\r
814 \r
815                                 if (baseComplexType != null) {\r
816                                         baseAnyAttribute = baseComplexType.AttributeWildcard;\r
817 \r
818                                         foreach (DictionaryEntry entry in baseComplexType.AttributeUses) {\r
819                                                 XmlSchemaAttribute attr = (XmlSchemaAttribute) entry.Value;\r
820 #if BUGGY_MS_COMPLIANT\r
821                                                 if (attr.Use != XmlSchemaUse.Prohibited)\r
822                                                         XmlSchemaUtil.AddToTable (attributeUses, attr, attr.QualifiedName, h);\r
823 #endif\r
824                                                 XmlSchemaUtil.AddToTable (attributeUses, attr, attr.QualifiedName, h);\r
825                                         }\r
826                                 }\r
827                                 if (baseAnyAttribute != null && localAnyAttribute != null)\r
828                                         // 1.3 attribute wildcard subset. (=> 3.10.6)\r
829                                         localAnyAttribute.ValidateWildcardSubset (baseAnyAttribute, h, schema);\r
830                         }\r
831                         // complexType/simpleContent/restriction\r
832                         if (scr != null) {\r
833                                 // Attributes\r
834                                 baseAnyAttribute = baseComplexType != null ? baseComplexType.AttributeWildcard : null;\r
835 \r
836                                 localAnyAttribute = scr.AnyAttribute;\r
837                                 if (localAnyAttribute != null && baseAnyAttribute != null)\r
838                                         // 1.3 attribute wildcard subset. (=> 3.10.6)\r
839                                         localAnyAttribute.ValidateWildcardSubset (baseAnyAttribute, h, schema);\r
840                                 // 3.4.6 :: 5.1. Beware that There is an errata for 5.1!!\r
841                                 // http://www.w3.org/2001/05/xmlschema-errata#Errata1\r
842 \r
843                                 // 3.4.2 Complex Type Definition with simple content Schema Component\r
844                                 // and its {attribute uses} and {attribute wildcard} are done here (descendantly)\r
845                                 errorCount += XmlSchemaUtil.ValidateAttributesResolved (\r
846                                         this.attributeUses, h, schema, scr.Attributes, \r
847                                         scr.AnyAttribute, ref attributeWildcard, null, false);\r
848                         }\r
849 \r
850                         // Common process of AttributeWildcard.\r
851                         if (localAnyAttribute != null) {\r
852                                 this.attributeWildcard = localAnyAttribute;\r
853                         }\r
854                         else\r
855                                 this.attributeWildcard = baseAnyAttribute;\r
856                 }\r
857 \r
858                 // 3.4.6 Type Derivation OK (Complex)\r
859                 internal void ValidateTypeDerivationOK (object b, ValidationEventHandler h, XmlSchema schema)\r
860                 {\r
861                         // AnyType derives from AnyType itself.\r
862                         if (this == XmlSchemaComplexType.AnyType && BaseXmlSchemaType == this)\r
863                                 return;\r
864 \r
865                         XmlSchemaType bst = b as XmlSchemaType;\r
866                         if (b == this)  // 1 and 2.1\r
867                                 return;\r
868                         if (bst != null && (resolvedDerivedBy & bst.FinalResolved) != 0) // 1.\r
869                                 error (h, "Derivation type " + resolvedDerivedBy + " is prohibited by the base type.");\r
870                         // FIXME: here BaseSchemaType should be \r
871                         // BaseXmlSchemaType, however for some case it \r
872                         // seems not working.\r
873                         if (BaseSchemaType == b) // 2.2\r
874                                 return;\r
875                         if (BaseSchemaType == null || BaseXmlSchemaType == XmlSchemaComplexType.AnyType) { // 2.3.1\r
876                                 error (h, "Derived type's base schema type is anyType.");\r
877                                 return;\r
878                         }\r
879                         // 2.3.2.1\r
880                         XmlSchemaComplexType dbct = BaseXmlSchemaType as XmlSchemaComplexType;\r
881                         if (dbct != null) {\r
882                                 dbct.ValidateTypeDerivationOK (b, h, schema);\r
883                                 return;\r
884                         }\r
885                         // 2.3.2.2\r
886                         XmlSchemaSimpleType dbst = BaseXmlSchemaType as XmlSchemaSimpleType;\r
887                         if (dbst != null) {\r
888                                 dbst.ValidateTypeDerivationOK (b, h, schema, true);\r
889                                 return;\r
890                         }\r
891                 }\r
892 \r
893                 // 3.4.6 Derivation Valid (Extension) - Term. 1 (Complex Type)\r
894                 internal void ValidateComplexBaseDerivationValidExtension (XmlSchemaComplexType baseComplexType,\r
895                         ValidationEventHandler h, XmlSchema schema)\r
896                 {\r
897                         // 1.1\r
898                         if ((baseComplexType.FinalResolved & XmlSchemaDerivationMethod.Extension) != 0)\r
899                                 error (h, "Derivation by extension is prohibited.");\r
900                         // 1.2\r
901                         foreach (DictionaryEntry entry in baseComplexType.AttributeUses) {\r
902                                 XmlSchemaAttribute ba = (XmlSchemaAttribute) entry.Value;\r
903                                 XmlSchemaAttribute da = AttributeUses [ba.QualifiedName] as XmlSchemaAttribute;\r
904                                 if (da == null)\r
905                                         error (h, "Invalid complex type derivation by extension was found. Missing attribute was found: " + ba.QualifiedName + " .");\r
906                         }\r
907                         // 1.3 -> 3.10.6 Wildcard Subset.\r
908                         if (AnyAttribute != null) {\r
909                                 if (baseComplexType.AnyAttribute == null)\r
910                                         error (h, "Invalid complex type derivation by extension was found. Base complex type does not have an attribute wildcard.");\r
911                                 else\r
912                                         baseComplexType.AnyAttribute.ValidateWildcardSubset (AnyAttribute, h, schema);\r
913                         }\r
914 \r
915                         // 1.4 => 1.4.2 (1.4.1 would be included in SimpleContentExtention).\r
916                         // 1.4.2.1\r
917 //                      if (contentTypeParticle == null)\r
918 //                              error (h, "Extended complex type's content type must not be empty.");\r
919                         // 1.4.2.2.1\r
920                         if (baseComplexType.ContentType != XmlSchemaContentType.Empty) {\r
921                                 // 1.4.2.2.2.1\r
922                                 if (this.ContentType != baseComplexType.ContentType)\r
923 //                              if (this.GetContentType (false) != baseComplexType.GetContentType (false))\r
924                                         error (h, "Base complex type has different content type " + baseComplexType.ContentType + ".");\r
925                                 // 1.4.2.2.2.2 => 3.9.6 Particle Valid (Extension)\r
926                                 else if (this.contentTypeParticle == null ||\r
927                                         !this.contentTypeParticle.ParticleEquals (baseComplexType.ContentTypeParticle)) {\r
928                                         XmlSchemaSequence seq = contentTypeParticle as XmlSchemaSequence;\r
929                                         if (contentTypeParticle != XmlSchemaParticle.Empty && (seq == null || contentTypeParticle.ValidatedMinOccurs != 1 || contentTypeParticle.ValidatedMaxOccurs != 1))\r
930                                                 error (h, "Invalid complex content extension was found.");\r
931                                         else {\r
932                                                 // Identical sequence item should be checked, but\r
933                                                 // I think it is naturally achieved as coded above.\r
934                                         }\r
935                                 }\r
936                         }\r
937                 }\r
938 \r
939                 // 3.4.6 Derivation Valid (Extension) - Term. 2 (Simple Type)\r
940                 internal void ValidateSimpleBaseDerivationValidExtension (object baseType,\r
941                         ValidationEventHandler h, XmlSchema schema)\r
942                 {\r
943                         XmlSchemaSimpleType st = baseType as XmlSchemaSimpleType;\r
944                         if (st != null && (st.FinalResolved & XmlSchemaDerivationMethod.Extension) != 0)\r
945                                 error (h, "Extension is prohibited by the base type.");\r
946 \r
947                         XmlSchemaDatatype dt = baseType as XmlSchemaDatatype;\r
948                         if (dt == null)\r
949                                 dt = st.Datatype;\r
950                         if (dt != this.Datatype)\r
951                                 error (h, "To extend simple type, a complex type must have the same content type as the base type.");\r
952                 }\r
953 \r
954                 internal void ValidateDerivationValidRestriction (XmlSchemaComplexType baseType,\r
955                         ValidationEventHandler h, XmlSchema schema)\r
956                 {\r
957                         // 1.\r
958                         if (baseType == null) {\r
959                                 error (h, "Base schema type is not a complex type.");\r
960                                 return;\r
961                         }\r
962                         if ((baseType.FinalResolved & XmlSchemaDerivationMethod.Restriction) != 0) {\r
963                                 error (h, "Prohibited derivation by restriction by base schema type.");\r
964                                 return;\r
965                         }\r
966 \r
967                         // 2.\r
968                         foreach (DictionaryEntry entry in this.AttributeUses) {\r
969                                 XmlSchemaAttribute attr = (XmlSchemaAttribute) entry.Value;\r
970                                 XmlSchemaAttribute baseAttr = baseType.AttributeUses [attr.QualifiedName] as XmlSchemaAttribute;\r
971                                 if (baseAttr != null) {\r
972                                         // 2.1\r
973                                         // 2.1.1\r
974                                         if (baseAttr.ValidatedUse != XmlSchemaUse.Optional && attr.ValidatedUse != XmlSchemaUse.Required)\r
975                                                 error (h, "Invalid attribute derivation by restriction was found for " + attr.QualifiedName + " .");\r
976                                         // 2.1.2\r
977                                         XmlSchemaSimpleType attrSimpleType = attr.AttributeType as XmlSchemaSimpleType;\r
978                                         XmlSchemaSimpleType baseAttrSimpleType = baseAttr.AttributeType as XmlSchemaSimpleType;\r
979                                         bool typeError = false;\r
980                                         if (attrSimpleType != null)\r
981                                                 attrSimpleType.ValidateDerivationValid (baseAttrSimpleType, null, h, schema);\r
982                                         else if (attrSimpleType == null && baseAttrSimpleType != null)\r
983                                                 typeError = true;\r
984                                         else {\r
985                                                 Type t1 = attr.AttributeType.GetType ();\r
986                                                 Type t2 = baseAttr.AttributeType.GetType ();\r
987                                                 if (t1 != t2 && t1.IsSubclassOf (t2))\r
988                                                         typeError = true;\r
989                                         }\r
990                                         if (typeError)\r
991                                                 error (h, "Invalid attribute derivation by restriction because of its type: " + attr.QualifiedName + " .");\r
992                                         // 2.1.3\r
993                                         if (baseAttr.ValidatedFixedValue != null && attr.ValidatedFixedValue != baseAttr.ValidatedFixedValue)\r
994                                                 error (h, "Invalid attribute derivation by restriction because of its fixed value constraint: " + attr.QualifiedName + " .");\r
995                                 } else {\r
996                                         // 2.2\r
997                                         if (baseType.AttributeWildcard != null)\r
998                                                 if (!baseType.AttributeWildcard.ValidateWildcardAllowsNamespaceName (\r
999                                                         attr.QualifiedName.Namespace, schema) &&\r
1000                                                         !schema.IsNamespaceAbsent (attr.QualifiedName.Namespace))\r
1001                                                         error (h, "Invalid attribute derivation by restriction was found for " + attr.QualifiedName + " .");\r
1002                                 }\r
1003                         }\r
1004                         // I think 3. is considered in 2.\r
1005                         // 4.\r
1006                         if (this.AttributeWildcard != null && baseType != XmlSchemaComplexType.AnyType) {\r
1007                                 if (baseType.AttributeWildcard == null)\r
1008                                         error (h, "Invalid attribute derivation by restriction because of attribute wildcard.");\r
1009                                 else\r
1010                                         AttributeWildcard.ValidateWildcardSubset (baseType.AttributeWildcard, h, schema);\r
1011                         }\r
1012 \r
1013                         // 5.\r
1014                         if (this == AnyType)\r
1015                                 return;\r
1016                         if (contentTypeParticle == XmlSchemaParticle.Empty) {\r
1017                                 // 5.1\r
1018                                 if (ContentType != XmlSchemaContentType.Empty) {\r
1019                                         // TODO: 5.1.1\r
1020 //                                      XmlSchemaSimpleType baseST = baseType as XmlSchemaSimpleType;\r
1021                                         // 5.1.2\r
1022                                         if (baseType.ContentType == XmlSchemaContentType.Mixed && !baseType.ContentTypeParticle.ValidateIsEmptiable ())\r
1023                                                 error (h, "Invalid content type derivation.");\r
1024 \r
1025                                 } else {\r
1026                                         // 5.2\r
1027                                         if (baseType.ContentTypeParticle != XmlSchemaParticle.Empty &&\r
1028                                                 !baseType.ContentTypeParticle.ValidateIsEmptiable ())\r
1029                                                 error (h, "Invalid content type derivation.");\r
1030                                 }\r
1031                         } else {\r
1032                                 // 5.3 => 3.9.6 Particle Valid (Restriction)\r
1033                                 if (baseType.ContentTypeParticle != null) {\r
1034                                         // 3.9.6 - 1 : same particle.\r
1035                                         // 3.9.6 - 2 is covered by using ActualParticle.\r
1036                                         if (!contentTypeParticle.ParticleEquals (baseType.ContentTypeParticle))\r
1037                                                 contentTypeParticle.ValidateDerivationByRestriction (baseType.ContentTypeParticle, h, schema, true);\r
1038                                 }\r
1039                         }\r
1040                 }\r
1041 \r
1042 #region Read\r
1043                 //<complexType\r
1044                 //  abstract = boolean : false\r
1045                 //  block = (#all | List of (extension | restriction)) \r
1046                 //  final = (#all | List of (extension | restriction)) \r
1047                 //  id = ID\r
1048                 //  mixed = boolean : false\r
1049                 //  name = NCName\r
1050                 //  {any attributes with non-schema namespace . . .}>\r
1051                 //  Content: (annotation?, (simpleContent | complexContent | ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?))))\r
1052                 //</complexType>\r
1053                 internal static XmlSchemaComplexType Read(XmlSchemaReader reader, ValidationEventHandler h)\r
1054                 {\r
1055                         XmlSchemaComplexType ctype = new XmlSchemaComplexType();\r
1056                         reader.MoveToElement();\r
1057                         Exception innerex;\r
1058 \r
1059                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
1060                         {\r
1061                                 error(h,"Should not happen :1: XmlSchemaComplexType.Read, name="+reader.Name,null);\r
1062                                 reader.SkipToEnd();\r
1063                                 return null;\r
1064                         }\r
1065 \r
1066                         ctype.LineNumber = reader.LineNumber;\r
1067                         ctype.LinePosition = reader.LinePosition;\r
1068                         ctype.SourceUri = reader.BaseURI;\r
1069 \r
1070                         while(reader.MoveToNextAttribute())\r
1071                         {\r
1072                                 if(reader.Name == "abstract")\r
1073                                 {\r
1074                                         ctype.IsAbstract = XmlSchemaUtil.ReadBoolAttribute(reader,out innerex);\r
1075                                         if(innerex != null)\r
1076                                                 error(h,reader.Value + " is invalid value for abstract",innerex);\r
1077                                 }\r
1078                                 else if(reader.Name == "block")\r
1079                                 {\r
1080                                         ctype.block = XmlSchemaUtil.ReadDerivationAttribute(reader,out innerex, "block",\r
1081                                                 XmlSchemaUtil.ComplexTypeBlockAllowed);\r
1082                                         if(innerex != null)\r
1083                                                 error (h,"some invalid values for block attribute were found",innerex);\r
1084                                 }\r
1085                                 else if(reader.Name == "final")\r
1086                                 {\r
1087                                         ctype.Final = XmlSchemaUtil.ReadDerivationAttribute(reader,out innerex, "final",\r
1088                                                 XmlSchemaUtil.FinalAllowed);\r
1089                                         if(innerex != null)\r
1090                                                 error (h,"some invalid values for final attribute were found",innerex);\r
1091                                 }\r
1092                                 else if(reader.Name == "id")\r
1093                                 {\r
1094                                         ctype.Id = reader.Value;\r
1095                                 }\r
1096                                 else if(reader.Name == "mixed")\r
1097                                 {\r
1098                                         ctype.isMixed = XmlSchemaUtil.ReadBoolAttribute(reader,out innerex);\r
1099                                         if(innerex != null)\r
1100                                                 error(h,reader.Value + " is invalid value for mixed",innerex);\r
1101                                 }\r
1102                                 else if(reader.Name == "name")\r
1103                                 {\r
1104                                         ctype.Name = reader.Value;\r
1105                                 }\r
1106                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
1107                                 {\r
1108                                         error(h,reader.Name + " is not a valid attribute for complexType",null);\r
1109                                 }\r
1110                                 else\r
1111                                 {\r
1112                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,ctype);\r
1113                                 }\r
1114                         }\r
1115                         \r
1116                         reader.MoveToElement();\r
1117                         if(reader.IsEmptyElement)\r
1118                                 return ctype;\r
1119 \r
1120                         //Content: 1. annotation?, \r
1121                         //                 2. simpleContent | 2. complexContent | \r
1122                         //                      (3.(group | all | choice | sequence)?, (4.(attribute | attributeGroup)*, 5.anyAttribute?)))\r
1123                         int level = 1;\r
1124                         while(reader.ReadNextElement())\r
1125                         {\r
1126                                 if(reader.NodeType == XmlNodeType.EndElement)\r
1127                                 {\r
1128                                         if(reader.LocalName != xmlname)\r
1129                                                 error(h,"Should not happen :2: XmlSchemaComplexType.Read, name="+reader.Name,null);\r
1130                                         break;\r
1131                                 }\r
1132                                 if(level <= 1 && reader.LocalName == "annotation")\r
1133                                 {\r
1134                                         level = 2; //Only one annotation\r
1135                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
1136                                         if(annotation != null)\r
1137                                                 ctype.Annotation = annotation;\r
1138                                         continue;\r
1139                                 }\r
1140                                 if(level <=2)\r
1141                                 {\r
1142                                         if(reader.LocalName == "simpleContent")\r
1143                                         {\r
1144                                                 level = 6;\r
1145                                                 XmlSchemaSimpleContent simple = XmlSchemaSimpleContent.Read(reader,h);\r
1146                                                 if(simple != null)\r
1147                                                         ctype.ContentModel = simple;\r
1148                                                 continue;\r
1149                                         }\r
1150                                         if(reader.LocalName == "complexContent")\r
1151                                         {\r
1152                                                 level = 6;\r
1153                                                 XmlSchemaComplexContent complex = XmlSchemaComplexContent.Read(reader,h);\r
1154                                                 if(complex != null)\r
1155                                                         ctype.contentModel = complex;\r
1156                                                 continue;\r
1157                                         }\r
1158                                 }\r
1159                                 if(level <= 3)\r
1160                                 {\r
1161                                         if(reader.LocalName == "group")\r
1162                                         {\r
1163                                                 level = 4;\r
1164                                                 XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);\r
1165                                                 if(group != null)\r
1166                                                         ctype.particle = group;\r
1167                                                 continue;\r
1168                                         }\r
1169                                         if(reader.LocalName == "all")\r
1170                                         {\r
1171                                                 level = 4;\r
1172                                                 XmlSchemaAll all = XmlSchemaAll.Read(reader,h);\r
1173                                                 if(all != null)\r
1174                                                         ctype.particle = all;\r
1175                                                 continue;\r
1176                                         }\r
1177                                         if(reader.LocalName == "choice")\r
1178                                         {\r
1179                                                 level = 4;\r
1180                                                 XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);\r
1181                                                 if(choice != null)\r
1182                                                         ctype.particle = choice;\r
1183                                                 continue;\r
1184                                         }\r
1185                                         if(reader.LocalName == "sequence")\r
1186                                         {\r
1187                                                 level = 4;\r
1188                                                 XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);\r
1189                                                 if(sequence != null)\r
1190                                                         ctype.particle = sequence;\r
1191                                                 continue;\r
1192                                         }\r
1193                                 }\r
1194                                 if(level <= 4)\r
1195                                 {\r
1196                                         if(reader.LocalName == "attribute")\r
1197                                         {\r
1198                                                 level = 4;\r
1199                                                 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);\r
1200                                                 if(attr != null)\r
1201                                                         ctype.Attributes.Add(attr);\r
1202                                                 continue;\r
1203                                         }\r
1204                                         if(reader.LocalName == "attributeGroup")\r
1205                                         {\r
1206                                                 level = 4;\r
1207                                                 XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);\r
1208                                                 if(attr != null)\r
1209                                                         ctype.attributes.Add(attr);\r
1210                                                 continue;\r
1211                                         }\r
1212                                 }\r
1213                                 if(level <= 5 && reader.LocalName == "anyAttribute")\r
1214                                 {\r
1215                                         level = 6;\r
1216                                         XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);\r
1217                                         if(anyattr != null)\r
1218                                                 ctype.AnyAttribute = anyattr;\r
1219                                         continue;\r
1220                                 }\r
1221                                 reader.RaiseInvalidElementError();\r
1222                         }\r
1223                         return ctype;\r
1224                 }\r
1225 #endregion\r
1226         }\r
1227 }\r