constify
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaElement.cs
1 //\r
2 // System.Xml.Schema.XmlSchemaElement.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 using System;\r
9 using System.Collections;\r
10 using System.Xml;\r
11 using System.Xml.Serialization;\r
12 using System.ComponentModel;\r
13 \r
14 namespace System.Xml.Schema\r
15 {\r
16         /// <summary>\r
17         /// Summary description for XmlSchemaElement.\r
18         /// </summary>\r
19         public class XmlSchemaElement : XmlSchemaParticle\r
20         {\r
21                 private XmlSchemaDerivationMethod block;\r
22                 private XmlSchemaDerivationMethod blockResolved;\r
23                 private XmlSchemaObjectCollection constraints;\r
24                 private string defaultValue;\r
25                 private object elementType;\r
26                 private XmlSchemaDerivationMethod final;\r
27                 private XmlSchemaDerivationMethod finalResolved;\r
28                 private string fixedValue;\r
29                 private XmlSchemaForm form;\r
30                 private bool isAbstract;\r
31                 private bool isNillable;\r
32                 private string name;\r
33                 private XmlQualifiedName qName;\r
34                 private XmlQualifiedName refName;\r
35                 private XmlSchemaType schemaType;\r
36                 private XmlQualifiedName schemaTypeName;\r
37                 private XmlQualifiedName substitutionGroup;\r
38                 internal bool parentIsSchema = false;\r
39                 private string validatedDefaultValue;\r
40                 private string validatedFixedValue;\r
41                 private bool actualIsAbstract;\r
42                 private bool actualIsNillable;\r
43                 private XmlSchemaElement substitutionGroupElement;\r
44                 private ArrayList substitutingElements = new ArrayList ();\r
45                 private XmlSchemaElement referencedElement;\r
46 \r
47                 // Post compilation items. It should be added on all schema components.\r
48                 XmlSchema schema;\r
49 \r
50                 const string xmlname = "element";\r
51 \r
52                 public XmlSchemaElement()\r
53                 {\r
54                         block = XmlSchemaDerivationMethod.None;\r
55                         final = XmlSchemaDerivationMethod.None;\r
56                         constraints = new XmlSchemaObjectCollection();\r
57                         qName = XmlQualifiedName.Empty;\r
58                         refName = XmlQualifiedName.Empty;\r
59                         schemaTypeName = XmlQualifiedName.Empty;\r
60                         substitutionGroup = XmlQualifiedName.Empty;\r
61                 }\r
62 \r
63                 #region Attributes\r
64                 \r
65                 [DefaultValue(false)]\r
66                 [System.Xml.Serialization.XmlAttribute("abstract")]\r
67                 public bool IsAbstract \r
68                 {\r
69                         get{ return  isAbstract; }\r
70                         set{ isAbstract = value; }\r
71                 }\r
72 \r
73                 [DefaultValue(XmlSchemaDerivationMethod.None)]\r
74                 [System.Xml.Serialization.XmlAttribute("block")]\r
75                 public XmlSchemaDerivationMethod Block \r
76                 {\r
77                         get{ return  block; }\r
78                         set{ block = value; }\r
79                 }\r
80                 \r
81                 [DefaultValue(null)]\r
82                 [System.Xml.Serialization.XmlAttribute("default")]\r
83                 public string DefaultValue \r
84                 {\r
85                         get{ return  defaultValue; }\r
86                         set{ defaultValue = value; }\r
87                 }\r
88                 \r
89                 [DefaultValue(XmlSchemaDerivationMethod.None)]\r
90                 [System.Xml.Serialization.XmlAttribute("final")]\r
91                 public XmlSchemaDerivationMethod Final \r
92                 {\r
93                         get{ return  final; }\r
94                         set{ final = value; }\r
95                 }\r
96 \r
97                 [DefaultValue(null)]\r
98                 [System.Xml.Serialization.XmlAttribute("fixed")]\r
99                 public string FixedValue \r
100                 {\r
101                         get{ return  fixedValue; }\r
102                         set{ fixedValue = value; }\r
103                 }\r
104                 [DefaultValue(XmlSchemaForm.None)]\r
105                 [System.Xml.Serialization.XmlAttribute("form")]\r
106                 public XmlSchemaForm Form \r
107                 {\r
108                         get{ return  form; }\r
109                         set{ form = value; }\r
110                 }\r
111 \r
112                 [DefaultValue(null)]\r
113                 [System.Xml.Serialization.XmlAttribute("name")]\r
114                 public string Name \r
115                 {\r
116                         get{ return  name; }\r
117                         set{ name = value; }\r
118                 }\r
119 \r
120                 [DefaultValue(false)]\r
121                 [System.Xml.Serialization.XmlAttribute("nillable")]\r
122                 public bool IsNillable \r
123                 {\r
124                         get{ return  isNillable; }\r
125                         set{ isNillable = value; }\r
126                 }\r
127 \r
128                 [System.Xml.Serialization.XmlAttribute("ref")]\r
129                 public XmlQualifiedName RefName \r
130                 {\r
131                         get{ return  refName; }\r
132                         set{ refName = value;}\r
133                 }\r
134 \r
135                 [System.Xml.Serialization.XmlAttribute("substitutionGroup")]\r
136                 public XmlQualifiedName SubstitutionGroup\r
137                 {\r
138                         get{ return  substitutionGroup; }\r
139                         set{ substitutionGroup = value; }\r
140                 }\r
141                 \r
142                 [System.Xml.Serialization.XmlAttribute("type")]\r
143                 public XmlQualifiedName SchemaTypeName \r
144                 {\r
145                         get{ return  schemaTypeName; }\r
146                         set{ schemaTypeName = value; }\r
147                 }\r
148                 #endregion\r
149 \r
150                 #region Elements\r
151 \r
152                 [XmlElement("simpleType",typeof(XmlSchemaSimpleType),Namespace="http://www.w3.org/2001/XMLSchema")]\r
153                 [XmlElement("complexType",typeof(XmlSchemaComplexType),Namespace="http://www.w3.org/2001/XMLSchema")]\r
154                 public XmlSchemaType SchemaType \r
155                 {\r
156                         get{ return  schemaType; }\r
157                         set{ schemaType = value; }\r
158                 }\r
159 \r
160                 [XmlElement("unique",typeof(XmlSchemaUnique),Namespace="http://www.w3.org/2001/XMLSchema")]\r
161                 [XmlElement("key",typeof(XmlSchemaKey),Namespace="http://www.w3.org/2001/XMLSchema")]\r
162                 [XmlElement("keyref",typeof(XmlSchemaKeyref),Namespace="http://www.w3.org/2001/XMLSchema")]\r
163                 public XmlSchemaObjectCollection Constraints \r
164                 {\r
165                         get{ return constraints; }\r
166                 }\r
167                 #endregion\r
168 \r
169                 #region Post Compilation Schema Info\r
170                 [XmlIgnore]\r
171                 public XmlQualifiedName QualifiedName \r
172                 {\r
173                         get{ return qName; }\r
174                 }\r
175 \r
176                 [XmlIgnore]\r
177                 public object ElementType \r
178                 {\r
179                         get {\r
180                                 if (referencedElement != null)\r
181                                         return referencedElement.ElementType;\r
182                                 else\r
183                                         return elementType;\r
184                         }\r
185                 }\r
186 \r
187                 [XmlIgnore]\r
188                 public XmlSchemaDerivationMethod BlockResolved \r
189                 {\r
190                         get{\r
191                                 if (referencedElement != null)\r
192                                         return referencedElement.BlockResolved;\r
193                                 else\r
194                                         return blockResolved;\r
195                         }\r
196                 }\r
197                 \r
198                 [XmlIgnore]\r
199                 public XmlSchemaDerivationMethod FinalResolved \r
200                 {\r
201                         get{\r
202                                 if (referencedElement != null)\r
203                                         return referencedElement.FinalResolved;\r
204                                 else\r
205                                         return finalResolved;\r
206                         }\r
207                 }\r
208 \r
209                 internal bool ActualIsNillable {\r
210                         get {\r
211                                 if (referencedElement != null)\r
212                                         return referencedElement.ActualIsNillable;\r
213                                 else\r
214                                         return actualIsNillable;\r
215                         }\r
216                 }\r
217                 \r
218                 internal bool ActualIsAbstract {\r
219                         get {\r
220                                 if (referencedElement != null)\r
221                                         return referencedElement.ActualIsAbstract;\r
222                                 else\r
223                                         return actualIsAbstract;\r
224                         }\r
225                 }\r
226                 \r
227                 // Post compilation default value (normalized)\r
228                 internal string ValidatedDefaultValue {\r
229                         get{\r
230                                 if (referencedElement != null)\r
231                                         return referencedElement.ValidatedDefaultValue;\r
232                                 else\r
233                                         return validatedDefaultValue;\r
234                         }\r
235                 }\r
236 \r
237                 // Post compilation fixed value (normalized)\r
238                 internal string ValidatedFixedValue {\r
239                         get{\r
240                                 if (referencedElement != null)\r
241                                         return referencedElement.ValidatedFixedValue;\r
242                                 else\r
243                                         return validatedFixedValue;\r
244                         }\r
245                 }\r
246 \r
247                 internal ArrayList SubstitutingElements {\r
248                         get {\r
249                                 if (referencedElement != null)\r
250                                         return referencedElement.SubstitutingElements;\r
251                                 else\r
252                                         return this.substitutingElements;\r
253                         }\r
254                 }\r
255 \r
256                 internal XmlSchemaElement SubstitutionGroupElement {\r
257                         get {\r
258                                 if (referencedElement != null)\r
259                                         return referencedElement.SubstitutionGroupElement;\r
260                                 else\r
261                                         return substitutionGroupElement;\r
262                         }\r
263                 }\r
264 \r
265                 #endregion\r
266 \r
267                 private XmlSchemaParticle substChoice;\r
268 \r
269                 /// <remarks>\r
270                 /// a) If Element has parent as schema:\r
271                 ///             1. name must be present and of type NCName.\r
272                 ///             2. ref must be absent\r
273                 ///             3. form must be absent\r
274                 ///             4. minOccurs must be absent\r
275                 ///             5. maxOccurs must be absent\r
276                 ///     b) If Element has parent is not schema and ref is absent\r
277                 ///             1. name must be present and of type NCName.\r
278                 ///             2. if form equals qualified or form is absent and schema's formdefault is qualifed,\r
279                 ///                targetNamespace is schema's targetnamespace else empty.\r
280                 ///             3. type and either <simpleType> or <complexType> are mutually exclusive\r
281                 ///             4. default and fixed must not both be present.\r
282                 ///             5. substitutiongroup must be absent\r
283                 ///             6. final must be absent\r
284                 ///             7. abstract must be absent\r
285                 ///     c) if the parent is not schema and ref is set\r
286                 ///             1. name must not be present\r
287                 ///             2. all of <simpleType>,<complexType>,  <key>, <keyref>, <unique>, nillable, \r
288                 ///                default, fixed, form, block and type,  must be absent.\r
289                 ///         3. substitutiongroup is prohibited\r
290                 ///             4. final is prohibited\r
291                 ///             5. abstract is prohibited\r
292                 ///             6. default and fixed must not both be present.(Actually both are absent)\r
293                 /// </remarks>  \r
294                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)\r
295                 {\r
296                         // If this is already compiled this time, simply skip.\r
297                         if (this.IsComplied (schema.CompilationId))\r
298                                 return 0;\r
299                         this.schema = schema;\r
300 \r
301                         if(this.defaultValue != null && this.fixedValue != null)\r
302                                 error(h,"both default and fixed can't be present");\r
303 \r
304                         if(parentIsSchema || isRedefineChild)\r
305                         {\r
306                                 if(this.refName != null && !RefName.IsEmpty)\r
307                                         error(h,"ref must be absent");\r
308 \r
309                                 if(this.name == null)   //b1\r
310                                         error(h,"Required attribute name must be present");\r
311                                 else if(!XmlSchemaUtil.CheckNCName(this.name)) // b1.2\r
312                                         error(h,"attribute name must be NCName");\r
313                                 else\r
314                                         this.qName = new XmlQualifiedName (this.name, schema.TargetNamespace);\r
315 \r
316                                 if(form != XmlSchemaForm.None)\r
317                                         error(h,"form must be absent");\r
318                                 if(MinOccursString != null)\r
319                                         error(h,"minOccurs must be absent");\r
320                                 if(MaxOccursString != null)\r
321                                         error(h,"maxOccurs must be absent");\r
322 \r
323                                 XmlSchemaDerivationMethod allfinal = (XmlSchemaDerivationMethod.Extension | XmlSchemaDerivationMethod.Restriction);\r
324                                 if(final == XmlSchemaDerivationMethod.All)\r
325                                         finalResolved = allfinal;\r
326                                 else if(final == XmlSchemaDerivationMethod.None)\r
327                                         finalResolved = XmlSchemaDerivationMethod.Empty;\r
328                                 else \r
329                                 {\r
330 //                                      if((final & ~allfinal) != 0)\r
331                                         if ((final | XmlSchemaUtil.FinalAllowed) != XmlSchemaUtil.FinalAllowed)\r
332                                                 error (h,"some values for final are invalid in this context");\r
333                                         finalResolved = final & allfinal;\r
334                                 }\r
335 \r
336                                 if(schemaType != null && schemaTypeName != null && !schemaTypeName.IsEmpty)\r
337                                 {\r
338                                         error(h,"both schemaType and content can't be present");\r
339                                 }\r
340 \r
341                                 //Even if both are present, read both of them.\r
342                                 if(schemaType != null)\r
343                                 {\r
344                                         if(schemaType is XmlSchemaSimpleType)\r
345                                         {\r
346                                                 errorCount += ((XmlSchemaSimpleType)schemaType).Compile(h,schema);\r
347                                         }\r
348                                         else if(schemaType is XmlSchemaComplexType)\r
349                                         {\r
350                                                 errorCount += ((XmlSchemaComplexType)schemaType).Compile(h,schema);\r
351                                         }\r
352                                         else\r
353                                                 error(h,"only simpletype or complextype is allowed");\r
354                                 }\r
355                                 if(schemaTypeName != null && !schemaTypeName.IsEmpty)\r
356                                 {\r
357                                         if(!XmlSchemaUtil.CheckQName(SchemaTypeName))\r
358                                                 error(h,"SchemaTypeName must be an XmlQualifiedName");\r
359                                 }\r
360                                 if(SubstitutionGroup != null && !SubstitutionGroup.IsEmpty)\r
361                                 {\r
362                                         if(!XmlSchemaUtil.CheckQName(SubstitutionGroup))\r
363                                                 error(h,"SubstitutionGroup must be a valid XmlQualifiedName");\r
364                                 }\r
365 \r
366                                 foreach(XmlSchemaObject obj in constraints)\r
367                                 {\r
368                                         if(obj is XmlSchemaUnique)\r
369                                                 errorCount += ((XmlSchemaUnique)obj).Compile(h,schema);\r
370                                         else if(obj is XmlSchemaKey)\r
371                                                 errorCount += ((XmlSchemaKey)obj).Compile(h,schema);\r
372                                         else if(obj is XmlSchemaKeyref)\r
373                                                 errorCount += ((XmlSchemaKeyref)obj).Compile(h,schema);\r
374                                 }\r
375                         }\r
376                         else\r
377                         {\r
378                                 if(substitutionGroup != null && !substitutionGroup.IsEmpty)\r
379                                         error(h,"substitutionGroup must be absent");\r
380                                 if(final != XmlSchemaDerivationMethod.None)\r
381                                         error(h,"final must be absent");\r
382 \r
383                                 CompileOccurence (h, schema);\r
384 \r
385                                 if(refName == null || RefName.IsEmpty)\r
386                                 {\r
387                                         string targetNamespace = String.Empty;\r
388 \r
389                                         if(form == XmlSchemaForm.Qualified || (form == XmlSchemaForm.None && schema.ElementFormDefault == XmlSchemaForm.Qualified))\r
390                                                 targetNamespace = schema.TargetNamespace;\r
391 \r
392                                         if(this.name == null)   //b1\r
393                                                 error(h,"Required attribute name must be present");\r
394                                         else if(!XmlSchemaUtil.CheckNCName(this.name)) // b1.2\r
395                                                 error(h,"attribute name must be NCName");\r
396                                         else\r
397                                                 this.qName = new XmlQualifiedName(this.name, targetNamespace);\r
398                                 \r
399                                         if(schemaType != null && schemaTypeName != null && !schemaTypeName.IsEmpty)\r
400                                         {\r
401                                                 error(h,"both schemaType and content can't be present");\r
402                                         }\r
403 \r
404                                         //Even if both are present, read both of them.\r
405                                         if(schemaType != null)\r
406                                         {\r
407                                                 if(schemaType is XmlSchemaSimpleType)\r
408                                                 {\r
409                                                         errorCount += ((XmlSchemaSimpleType)schemaType).Compile(h,schema);\r
410                                                 }\r
411                                                 else if(schemaType is XmlSchemaComplexType)\r
412                                                 {\r
413                                                         errorCount += ((XmlSchemaComplexType)schemaType).Compile(h,schema);\r
414                                                 }\r
415                                                 else\r
416                                                         error(h,"only simpletype or complextype is allowed");\r
417                                         }\r
418                                         if(schemaTypeName != null && !schemaTypeName.IsEmpty)\r
419                                         {\r
420                                                 if(!XmlSchemaUtil.CheckQName(SchemaTypeName))\r
421                                                         error(h,"SchemaTypeName must be an XmlQualifiedName");\r
422                                         }\r
423                                         if(SubstitutionGroup != null && !SubstitutionGroup.IsEmpty)\r
424                                         {\r
425                                                 if(!XmlSchemaUtil.CheckQName(SubstitutionGroup))\r
426                                                         error(h,"SubstitutionGroup must be a valid XmlQualifiedName");\r
427                                         }\r
428 \r
429                                         foreach(XmlSchemaObject obj in constraints)\r
430                                         {\r
431                                                 if(obj is XmlSchemaUnique)\r
432                                                         errorCount += ((XmlSchemaUnique)obj).Compile(h,schema);\r
433                                                 else if(obj is XmlSchemaKey)\r
434                                                         errorCount += ((XmlSchemaKey)obj).Compile(h,schema);\r
435                                                 else if(obj is XmlSchemaKeyref)\r
436                                                         errorCount += ((XmlSchemaKeyref)obj).Compile(h,schema);\r
437                                         }\r
438                                 }\r
439                                 else\r
440                                 {\r
441                                         if(!XmlSchemaUtil.CheckQName(RefName))\r
442                                                 error(h,"RefName must be a XmlQualifiedName");\r
443 \r
444                                         if(name != null)\r
445                                                 error(h,"name must not be present when ref is present");\r
446                                         if(Constraints.Count != 0)\r
447                                                 error(h,"key, keyref and unique must be absent");\r
448                                         if(isNillable)\r
449                                                 error(h,"nillable must be absent");\r
450                                         if(defaultValue != null)\r
451                                                 error(h,"default must be absent");\r
452                                         if(fixedValue != null)\r
453                                                 error(h,"fixed must be null");\r
454                                         if(form != XmlSchemaForm.None)\r
455                                                 error(h,"form must be absent");\r
456                                         if(block != XmlSchemaDerivationMethod.None)\r
457                                                 error(h,"block must be absent");\r
458                                         if(schemaTypeName != null && !schemaTypeName.IsEmpty)\r
459                                                 error(h,"type must be absent");\r
460                                         if(SchemaType != null)\r
461                                                 error(h,"simpleType or complexType must be absent");\r
462 \r
463                                         qName = RefName;\r
464                                 }\r
465                         }\r
466 \r
467                         switch (block) {\r
468                         case XmlSchemaDerivationMethod.All:\r
469                                 blockResolved = XmlSchemaDerivationMethod.All;\r
470                                 break;\r
471                         case XmlSchemaDerivationMethod.None:\r
472                                 blockResolved = XmlSchemaDerivationMethod.Empty;\r
473                                 break;\r
474                         default:\r
475                                 if ((block | XmlSchemaUtil.ElementBlockAllowed) != XmlSchemaUtil.ElementBlockAllowed)\r
476                                         error (h,"Some of the values for block are invalid in this context");\r
477                                 blockResolved = block;\r
478                                 break;\r
479                         }\r
480 \r
481                         if (Constraints != null) {\r
482                                 XmlSchemaObjectTable table = new XmlSchemaObjectTable ();\r
483                                 foreach (XmlSchemaIdentityConstraint c in Constraints) {\r
484                                         XmlSchemaUtil.AddToTable (table, c, c.QualifiedName, h);\r
485                                 }\r
486                         }\r
487 \r
488                         XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);\r
489 \r
490                         this.CompilationId = schema.CompilationId;\r
491                         return errorCount;\r
492                 }\r
493 \r
494                 [MonoTODO ("Return clone in case when it returns itself")]\r
495                 internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)\r
496                 {\r
497                         if (OptimizedParticle != null)\r
498                                 return OptimizedParticle;\r
499                         if (RefName != null && RefName != XmlQualifiedName.Empty) {\r
500                                 referencedElement = schema.Elements [RefName] as XmlSchemaElement;\r
501                         }\r
502 \r
503 //                      if (this.referencedElement != null)\r
504 //                              OptimizedParticle = referencedElement.GetOptimizedParticle (isTop);\r
505 //                      else \r
506                         if (ValidatedMaxOccurs == 0)\r
507                                 OptimizedParticle = XmlSchemaParticle.Empty;\r
508                         // Substitution Group\r
509                         else if (SubstitutingElements != null && SubstitutingElements.Count > 0) {\r
510                                 XmlSchemaChoice choice = new XmlSchemaChoice ();\r
511                                 choice.MinOccurs = MinOccurs;\r
512                                 choice.MaxOccurs = MaxOccurs;\r
513                                 substChoice = choice;\r
514                                 choice.Compile (null, schema); // compute Validated Min/Max Occurs.\r
515                                 XmlSchemaElement item = this.MemberwiseClone () as XmlSchemaElement;\r
516                                 item.MinOccurs = 1;\r
517                                 item.MaxOccurs = 1;\r
518                                 item.substitutionGroupElement = null;\r
519                                 item.substitutingElements = null;\r
520                                 for (int i = 0; i < SubstitutingElements.Count; i++) {\r
521                                         XmlSchemaElement se = SubstitutingElements [i] as XmlSchemaElement;\r
522 //                                      choice.Items.Add (se);\r
523 //                                      choice.CompiledItems.Add (se);\r
524                                         this.AddSubstElementRecursively (choice.Items, se);\r
525                                         this.AddSubstElementRecursively (choice.CompiledItems, se);\r
526                                 }\r
527                                 if (!choice.Items.Contains (item)) {\r
528                                         choice.Items.Add (item);\r
529                                         choice.CompiledItems.Add (item);\r
530                                 }\r
531                                 OptimizedParticle = choice;\r
532                         }\r
533                         else\r
534                                 OptimizedParticle = this;//.MemberwiseClone () as XmlSchemaElement;\r
535                         return OptimizedParticle;\r
536                 }\r
537 \r
538                 private void AddSubstElementRecursively (XmlSchemaObjectCollection col, XmlSchemaElement el)\r
539                 {\r
540                         if (el.SubstitutingElements != null)\r
541                                 for (int i = 0; i < el.SubstitutingElements.Count; i++)\r
542                                         this.AddSubstElementRecursively (col, el.SubstitutingElements [i] as XmlSchemaElement);\r
543                         if (!col.Contains (el))\r
544                                 col.Add (el);\r
545                 }\r
546 \r
547                 internal void FillSubstitutionElementInfo ()\r
548                 {\r
549                         if (this.substitutionGroupElement != null)\r
550                                 return;\r
551 \r
552                         if (this.SubstitutionGroup != XmlQualifiedName.Empty) {\r
553                                 XmlSchemaElement substElem = schema.Elements [SubstitutionGroup] as XmlSchemaElement;\r
554                                 this.substitutionGroupElement = substElem;\r
555                                 if (substElem != null)\r
556                                         substElem.substitutingElements.Add (this);\r
557                         }\r
558                 }\r
559 \r
560                 internal override int Validate(ValidationEventHandler h, XmlSchema schema)\r
561                 {\r
562                         if (IsValidated (schema.CompilationId))\r
563                                 return errorCount;\r
564 \r
565                         // See XML Schema Structures 3.6 for the complete description.\r
566 \r
567                         // Element Declaration Properties Correct\r
568                         // 1. = 3.3.1 (modulo 5.3)\r
569 \r
570                         // 3.3.1:\r
571                         // {annotation} is as is.\r
572                         // {name}, {target namespace}, {scope}, {disallowed substitution},\r
573                         // {substitution group exclusions} (handled the same as 'disallowed substitution')\r
574                         // and {identity-constraint-definitions} are Compile()d.\r
575                         // {value constraint} is going to be filled in step 2.\r
576 \r
577                         // actual {nillable}, {abstract} \r
578                         this.actualIsNillable = IsNillable;\r
579                         this.actualIsAbstract = IsAbstract;\r
580 \r
581                         // Before determining element type, we need to validate substituting element\r
582                         if (this.SubstitutionGroup != XmlQualifiedName.Empty) {\r
583                                 XmlSchemaElement substElem = substitutionGroupElement;\r
584                                 if (substElem != null)\r
585                                         substElem.Validate (h, schema);\r
586                         }\r
587 \r
588                         // {type} from here\r
589                         XmlSchemaDatatype datatype = null;\r
590                         if (schemaType != null)\r
591                                 elementType = schemaType;\r
592                         else if (SchemaTypeName != XmlQualifiedName.Empty) {\r
593                                 XmlSchemaType type = schema.SchemaTypes [SchemaTypeName] as XmlSchemaType;\r
594                                 // If el is null, then it is missing sub components .\r
595                                 if (type != null) {\r
596                                         type.Validate (h, schema);\r
597                                         elementType = type;\r
598                                 }\r
599                                 else if (SchemaTypeName == XmlSchemaComplexType.AnyTypeName)\r
600                                         elementType = XmlSchemaComplexType.AnyType;
601                                 else if (XmlSchemaUtil.IsBuiltInDatatypeName (SchemaTypeName)) {\r
602                                         datatype = XmlSchemaDatatype.FromName (SchemaTypeName);\r
603                                         if (datatype == null)\r
604                                                 error (h, "Invalid schema datatype was specified.");\r
605                                         else\r
606                                                 elementType = datatype;\r
607                                 }\r
608                                 // otherwise, it might be missing sub components.\r
609                                 else if (!schema.IsNamespaceAbsent (SchemaTypeName.Namespace))\r
610                                         error (h, "Referenced element schema type " + SchemaTypeName + " was not found in the corresponding schema.");\r
611                         }\r
612                         else if (RefName != XmlQualifiedName.Empty)\r
613                         {\r
614                                 XmlSchemaElement refElem = schema.Elements [RefName] as XmlSchemaElement;\r
615                                 // If el is null, then it is missing sub components .\r
616                                 if (refElem != null) {\r
617                                         this.referencedElement = refElem;\r
618                                         errorCount += refElem.Validate (h, schema);\r
619                                 }\r
620                                 // otherwise, it might be missing sub components.\r
621                                 else if (!schema.IsNamespaceAbsent (RefName.Namespace))\r
622                                         error (h, "Referenced element " + RefName + " was not found in the corresponding schema.");\r
623                         }\r
624                         \r
625                         // Otherwise if there are substitution group, then the type of the substitution group element.\r
626                         if (referencedElement == null) {\r
627                                 if (elementType == null && this.substitutionGroupElement != null)\r
628                                         elementType = substitutionGroupElement.ElementType;\r
629                                 // Otherwise, the -ur type- definition.\r
630                                 if (elementType == null)\r
631                                         elementType = XmlSchemaComplexType.AnyType;\r
632                         }\r
633 \r
634                         XmlSchemaType xsType = elementType as XmlSchemaType;\r
635                         if (xsType != null) {\r
636                                 errorCount += xsType.Validate (h, schema);\r
637                                 datatype = xsType.Datatype;\r
638                         }\r
639                         // basic {type} is now filled, except for derivation by {substitution group}.\r
640 \r
641                         // {substitution group affiliation}\r
642                         // 3. subsitution group's type derivation check.\r
643                         if (this.SubstitutionGroup != XmlQualifiedName.Empty) {\r
644                                 XmlSchemaElement substElem = schema.Elements [SubstitutionGroup] as XmlSchemaElement;\r
645                                 // If el is null, then it is missing sub components .\r
646                                 if (substElem != null) {\r
647                                         XmlSchemaType substSchemaType = substElem.ElementType as XmlSchemaType;\r
648                                         if (substSchemaType != null) {\r
649                                                 // 3.3.6 Properties Correct 3.\r
650                                                 if ((substElem.FinalResolved & XmlSchemaDerivationMethod.Substitution) != 0)\r
651                                                         error (h, "Substituted element blocks substitution.");\r
652                                                 if (xsType != null && (substElem.FinalResolved & xsType.DerivedBy) != 0)\r
653                                                         error (h, "Invalid derivation was found. Substituted element prohibits this derivation method: " + xsType.DerivedBy + ".");\r
654                                         }\r
655                                         XmlSchemaComplexType xsComplexType = xsType as XmlSchemaComplexType;\r
656                                         if (xsComplexType != null)\r
657                                                 xsComplexType.ValidateTypeDerivationOK (substElem.ElementType, h, schema);\r
658                                         else {\r
659                                                 XmlSchemaSimpleType xsSimpleType = xsType as XmlSchemaSimpleType;\r
660                                                 if (xsSimpleType != null)\r
661                                                         xsSimpleType.ValidateTypeDerivationOK (substElem.ElementType, h, schema, true);\r
662                                         }\r
663 \r
664                                 }\r
665                                 // otherwise, it might be missing sub components.\r
666                                 else if (!schema.IsNamespaceAbsent (SubstitutionGroup.Namespace))\r
667                                         error (h, "Referenced element type " + SubstitutionGroup + " was not found in the corresponding schema.");\r
668                         }\r
669 \r
670                         // 2. ElementDefaultValid\r
671                         // 4. ID with {value constraint} is prohibited.\r
672                         if (defaultValue != null || fixedValue != null) {\r
673                                 ValidateElementDefaultValidImmediate (h, schema);\r
674                                 if (datatype != null && // Such situation is basically an error. For ValidationEventHandler.\r
675                                         datatype.TokenizedType == XmlTokenizedType.ID)\r
676                                         error (h, "Element type is ID, which does not allows default or fixed values.");\r
677                         }\r
678 \r
679                         // Identity constraints (3.11.3 / 3.11.6)\r
680                         foreach (XmlSchemaIdentityConstraint ident in Constraints)\r
681                                 ident.Validate (h, schema);\r
682 \r
683                         ValidationId = schema.ValidationId;\r
684                         return errorCount;\r
685                 }\r
686 \r
687                 internal override bool ParticleEquals (XmlSchemaParticle other)\r
688                 {\r
689                         XmlSchemaElement element = other as XmlSchemaElement;\r
690                         if (element == null)\r
691                                 return false;\r
692                         if (this.ValidatedMaxOccurs != element.ValidatedMaxOccurs ||\r
693                                 this.ValidatedMinOccurs != element.ValidatedMinOccurs)\r
694                                 return false;\r
695                         if (this.QualifiedName != element.QualifiedName ||\r
696                                 this.ElementType != element.ElementType ||\r
697                                 this.Constraints.Count != element.Constraints.Count)\r
698                                 return false;\r
699                         for (int i = 0; i < this.Constraints.Count; i++) {\r
700                                 XmlSchemaIdentityConstraint c1 = Constraints [i] as XmlSchemaIdentityConstraint;\r
701                                 XmlSchemaIdentityConstraint c2 = element.Constraints [i] as XmlSchemaIdentityConstraint;\r
702                                 if (c1.QualifiedName != c2.QualifiedName ||\r
703                                         c1.Selector.XPath != c2.Selector.XPath ||\r
704                                         c1.Fields.Count != c2.Fields.Count)\r
705                                         return false;\r
706                                 for (int f = 0; f < c1.Fields.Count; f++) {\r
707                                         XmlSchemaXPath f1 = c1.Fields [f] as XmlSchemaXPath;\r
708                                         XmlSchemaXPath f2 = c2.Fields [f] as XmlSchemaXPath;\r
709                                         if (f1.XPath != f2.XPath)\r
710                                                 return false;\r
711                                 }\r
712                         }\r
713                         if (this.BlockResolved != element.BlockResolved ||\r
714                                 this.FinalResolved != element.FinalResolved ||\r
715                                 this.ValidatedDefaultValue != element.ValidatedDefaultValue ||\r
716                                 this.ValidatedFixedValue != element.ValidatedFixedValue)\r
717                                 return false;\r
718                         return true;\r
719                 }\r
720 \r
721                 internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,\r
722                         ValidationEventHandler h, XmlSchema schema, bool raiseError)\r
723                 {\r
724                         // element - NameAndTypeOK\r
725                         XmlSchemaElement baseElement = baseParticle as XmlSchemaElement;\r
726                         if (baseElement != null) {\r
727                                 return ValidateDerivationByRestrictionNameAndTypeOK (baseElement, h, schema, raiseError);\r
728                         }\r
729 \r
730                         // any - NSCompat\r
731                         XmlSchemaAny baseAny = baseParticle as XmlSchemaAny;\r
732                         if (baseAny != null) {\r
733                                 // NSCompat\r
734                                 if (!baseAny.ValidateWildcardAllowsNamespaceName (this.QualifiedName.Namespace, h, schema, raiseError))\r
735                                         return false;\r
736                                 return ValidateOccurenceRangeOK (baseAny, h, schema, raiseError);\r
737                         }\r
738 \r
739 //*\r
740                         // choice - RecurseAsIfGroup\r
741                         XmlSchemaGroupBase gb = null;\r
742                         if (baseParticle is XmlSchemaSequence)\r
743                                 gb = new XmlSchemaSequence ();\r
744                         else if (baseParticle is XmlSchemaChoice)\r
745                                 gb = new XmlSchemaChoice ();\r
746                         else if (baseParticle is XmlSchemaAll)\r
747                                 gb = new XmlSchemaAll ();\r
748 \r
749                         if (gb != null) {\r
750                                 gb.Items.Add (this);\r
751                                 gb.Compile (h, schema);\r
752                                 gb.Validate (h, schema);\r
753                                 // It looks weird, but here we never think about \r
754                                 // _pointlessness_ of this groupbase particle.\r
755                                 return gb.ValidateDerivationByRestriction (baseParticle, h, schema, raiseError);\r
756                         }\r
757 //*/\r
758                         return true;\r
759                 }\r
760 \r
761                 private bool ValidateDerivationByRestrictionNameAndTypeOK (XmlSchemaElement baseElement,\r
762                         ValidationEventHandler h, XmlSchema schema, bool raiseError)\r
763                 {\r
764                         // 1.\r
765                         if (this.QualifiedName != baseElement.QualifiedName) {\r
766                                 if (raiseError)\r
767                                         error (h, "Invalid derivation by restriction of particle was found. Both elements must have the same name.");\r
768                                 return false;\r
769                         }\r
770                         // 2.\r
771                         if (this.isNillable && !baseElement.isNillable) {\r
772                                 if (raiseError)\r
773                                         error (h, "Invalid element derivation by restriction of particle was found. Base element is not nillable and derived type is nillable.");\r
774                                 return false;\r
775                         }\r
776                         // 3.\r
777                         if (!ValidateOccurenceRangeOK (baseElement, h, schema, raiseError))\r
778                                 return false;\r
779                         // 4.\r
780                         if (baseElement.ValidatedFixedValue != null &&\r
781                                 baseElement.ValidatedFixedValue != this.ValidatedFixedValue) {\r
782                                 if (raiseError)\r
783                                         error (h, "Invalid element derivation by restriction of particle was found. Both fixed value must be the same.");\r
784                                 return false;\r
785                         }\r
786                         // 5. TODO: What is "identity constraints subset" ???\r
787 \r
788                         // 6. \r
789                         if ((baseElement.BlockResolved | this.BlockResolved) != this.BlockResolved) {\r
790                                 if (raiseError)\r
791                                         error (h, "Invalid derivation by restriction of particle was found. Derived element must contain all of the base element's block value.");\r
792                                 return false;\r
793                         }\r
794                         // 7.\r
795                         if (baseElement.ElementType != null) {\r
796                                 XmlSchemaComplexType derivedCType = this.ElementType as XmlSchemaComplexType;\r
797                                 if (derivedCType != null) {\r
798                                         // FIXME: W3C REC says that it is Type Derivation OK to be check, but\r
799                                         // in fact it should be DerivationValid (Restriction, Complex).\r
800                                         derivedCType.ValidateDerivationValidRestriction (\r
801                                                 baseElement.ElementType as XmlSchemaComplexType, h, schema);\r
802                                         derivedCType.ValidateTypeDerivationOK (baseElement.ElementType, h, schema);\r
803                                 } else {\r
804                                         XmlSchemaSimpleType derivedSType = this.ElementType as XmlSchemaSimpleType;\r
805                                         if (derivedSType != null)\r
806                                                 derivedSType.ValidateTypeDerivationOK (baseElement.ElementType, h, schema, true);\r
807                                         else if (baseElement.ElementType != XmlSchemaComplexType.AnyType && baseElement.ElementType != this.ElementType) {\r
808                                                 if (raiseError)\r
809                                                         error (h, "Invalid element derivation by restriction of particle was found. Both primitive types differ.");\r
810                                                 return false;\r
811                                         }\r
812                                 }\r
813                         }\r
814                         return true;\r
815                 }\r
816 \r
817                 internal override void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema)\r
818                 {\r
819                         XmlSchemaComplexType ct = this.ElementType as XmlSchemaComplexType;\r
820                         if (ct == null || ct.Particle == null)\r
821                                 return;\r
822                         ct.Particle.CheckRecursion (depth + 1, h, schema);\r
823                 }\r
824 \r
825                 internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,\r
826                         ValidationEventHandler h, XmlSchema schema)\r
827                 {\r
828                         if (qnames.Contains (this.QualifiedName))// && !this.ParticleEquals ((XmlSchemaParticle) qnames [this.QualifiedName]))\r
829                                 error (h, "Ambiguous element label was detected: " + this.QualifiedName);\r
830                         else {\r
831                                 foreach (XmlSchemaAny any in nsNames) {\r
832                                         if (any.ValidatedMaxOccurs == 0)\r
833                                                 continue;\r
834                                         if (any.HasValueAny ||\r
835                                                 any.HasValueLocal && this.QualifiedName.Namespace == "" ||\r
836                                                 any.HasValueOther && this.QualifiedName.Namespace != this.QualifiedName.Namespace ||\r
837                                                 any.HasValueTargetNamespace && this.QualifiedName.Namespace == this.QualifiedName.Namespace) {\r
838                                                 error (h, "Ambiguous element label which is contained by -any- particle was detected: " + this.QualifiedName);\r
839                                                 break;\r
840                                         } else if (!any.HasValueOther) {\r
841                                                 bool bad = false;\r
842                                                 foreach (string ns in any.ResolvedNamespaces) {\r
843                                                         if (ns == this.QualifiedName.Namespace) {\r
844                                                                 bad = true;\r
845                                                                 break;\r
846                                                         }\r
847                                                 }\r
848                                                 if (bad) {\r
849                                                         error (h, "Ambiguous element label which is contained by -any- particle was detected: " + this.QualifiedName);\r
850                                                         break;\r
851                                                 }\r
852                                         } else {\r
853                                                 if (any.TargetNamespace.Length == 0 ||\r
854                                                         any.TargetNamespace != this.QualifiedName.Namespace)\r
855                                                         error (h, "Ambiguous element label which is contained by -any- particle with ##other value was detected: " + this.QualifiedName);\r
856                                         }\r
857                                 }\r
858                                 qnames.Add (this.QualifiedName, this);\r
859                         }\r
860                 }\r
861 \r
862                 internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,\r
863                         ValidationEventHandler h, XmlSchema schema)\r
864                 {\r
865                         XmlSchemaElement labeled = labels [this.QualifiedName] as XmlSchemaElement;\r
866                         if (labeled == null)\r
867                                 labels.Add (this.QualifiedName, this);\r
868                         else if (labeled.ElementType != this.ElementType)\r
869                                 error (h, "Different types are specified on the same named elements in the same sequence. Element name is " + QualifiedName);\r
870                 }\r
871 \r
872 \r
873                 // 3.3.6 Element Default Valid (Immediate)\r
874                 private void ValidateElementDefaultValidImmediate (ValidationEventHandler h, XmlSchema schema)\r
875                 {\r
876                         // This presumes that ElementType is already filled.\r
877 \r
878                         XmlSchemaDatatype datatype = elementType as XmlSchemaDatatype;\r
879                         XmlSchemaSimpleType simpleType = elementType as XmlSchemaSimpleType;\r
880                         if (simpleType != null)\r
881                                 datatype = simpleType.Datatype;\r
882 \r
883                         if (datatype == null) {\r
884                                 XmlSchemaComplexType complexType = elementType as XmlSchemaComplexType;\r
885                                 switch (complexType.ContentType) {\r
886                                 case XmlSchemaContentType.Empty:\r
887                                 case XmlSchemaContentType.ElementOnly:\r
888                                         error (h, "Element content type must be simple type or mixed.");\r
889                                         break;\r
890                                 }\r
891                                 datatype = XmlSchemaSimpleType.AnySimpleType;\r
892                         }\r
893 \r
894                         XmlNamespaceManager nsmgr = null;\r
895                         if (datatype.TokenizedType == XmlTokenizedType.QName) {\r
896                                 if (this.Namespaces != null)\r
897                                         foreach (XmlQualifiedName qname in Namespaces.ToArray ())\r
898                                                 nsmgr.AddNamespace (qname.Name, qname.Namespace);\r
899                         }\r
900 \r
901                         try {\r
902                                 if (defaultValue != null) {\r
903                                         validatedDefaultValue = datatype.Normalize (defaultValue);\r
904                                         datatype.ParseValue (validatedDefaultValue, null, nsmgr);\r
905                                 }\r
906                         } catch (Exception ex) {\r
907                                 // FIXME: This is not a good way to handle exception, but\r
908                                 // I think there is no remedy for such Framework specification.\r
909                                 error (h, "The Element's default value is invalid with respect to its type definition.", ex);\r
910                         }\r
911                         try {\r
912                                 if (fixedValue != null) {\r
913                                         validatedFixedValue = datatype.Normalize (fixedValue);\r
914                                         datatype.ParseValue (validatedFixedValue, null, nsmgr);\r
915                                 }\r
916                         } catch (Exception ex) {\r
917                                 // FIXME: This is not a good way to handle exception.\r
918                                 error (h, "The Element's fixed value is invalid with its type definition.", ex);\r
919                         }\r
920                 }\r
921 \r
922                 //<element\r
923                 //  abstract = boolean : false\r
924                 //  block = (#all | List of (extension | restriction | substitution)) \r
925                 //  default = string\r
926                 //  final = (#all | List of (extension | restriction)) \r
927                 //  fixed = string\r
928                 //  form = (qualified | unqualified)\r
929                 //  id = ID\r
930                 //  maxOccurs = (nonNegativeInteger | unbounded)  : 1\r
931                 //  minOccurs = nonNegativeInteger : 1\r
932                 //  name = NCName\r
933                 //  nillable = boolean : false\r
934                 //  ref = QName\r
935                 //  substitutionGroup = QName\r
936                 //  type = QName\r
937                 //  {any attributes with non-schema namespace . . .}>\r
938                 //  Content: (annotation?, ((simpleType | complexType)?, (unique | key | keyref)*))\r
939                 //</element>\r
940 \r
941                 internal static XmlSchemaElement Read(XmlSchemaReader reader, ValidationEventHandler h)\r
942                 {\r
943                         XmlSchemaElement element = new XmlSchemaElement();\r
944                         Exception innerex;\r
945                         reader.MoveToElement();\r
946 \r
947                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
948                         {\r
949                                 error(h,"Should not happen :1: XmlSchemaElement.Read, name="+reader.Name,null);\r
950                                 reader.Skip();\r
951                                 return null;\r
952                         }\r
953 \r
954                         element.LineNumber = reader.LineNumber;\r
955                         element.LinePosition = reader.LinePosition;\r
956                         element.SourceUri = reader.BaseURI;\r
957 \r
958                         while(reader.MoveToNextAttribute())\r
959                         {\r
960                                 if(reader.Name == "abstract")\r
961                                 {\r
962                                         element.IsAbstract = XmlSchemaUtil.ReadBoolAttribute(reader,out innerex);\r
963                                         if(innerex != null)\r
964                                                 error(h,reader.Value + " is invalid value for abstract",innerex);\r
965                                 }\r
966                                 else if(reader.Name == "block")\r
967                                 {\r
968                                         element.block = XmlSchemaUtil.ReadDerivationAttribute(reader,out innerex, "block",\r
969                                                 XmlSchemaUtil.ElementBlockAllowed);\r
970                                         if(innerex != null)\r
971                                                 error (h,"some invalid values for block attribute were found",innerex);\r
972                                 }\r
973                                 else if(reader.Name == "default")\r
974                                 {\r
975                                         element.defaultValue = reader.Value;\r
976                                 }\r
977                                 else if(reader.Name == "final")\r
978                                 {\r
979                                         element.Final = XmlSchemaUtil.ReadDerivationAttribute(reader,out innerex, "final",\r
980                                                 XmlSchemaUtil.FinalAllowed);\r
981                                         if(innerex != null)\r
982                                                 error (h,"some invalid values for final attribute were found",innerex);\r
983                                 }\r
984                                 else if(reader.Name == "fixed")\r
985                                 {\r
986                                         element.fixedValue = reader.Value;\r
987                                 }\r
988                                 else if(reader.Name == "form")\r
989                                 {\r
990                                         element.form = XmlSchemaUtil.ReadFormAttribute(reader,out innerex);\r
991                                         if(innerex != null)\r
992                                                 error(h,reader.Value + " is an invalid value for form attribute",innerex);\r
993                                 }\r
994                                 else if(reader.Name == "id")\r
995                                 {\r
996                                         element.Id = reader.Value;\r
997                                 }\r
998                                 else if(reader.Name == "maxOccurs")\r
999                                 {\r
1000                                         try\r
1001                                         {\r
1002                                                 element.MaxOccursString = reader.Value;\r
1003                                         }\r
1004                                         catch(Exception e)\r
1005                                         {\r
1006                                                 error(h,reader.Value + " is an invalid value for maxOccurs",e);\r
1007                                         }\r
1008                                 }\r
1009                                 else if(reader.Name == "minOccurs")\r
1010                                 {\r
1011                                         try\r
1012                                         {\r
1013                                                 element.MinOccursString = reader.Value;\r
1014                                         }\r
1015                                         catch(Exception e)\r
1016                                         {\r
1017                                                 error(h,reader.Value + " is an invalid value for minOccurs",e);\r
1018                                         }\r
1019                                 }\r
1020                                 else if(reader.Name == "name")\r
1021                                 {\r
1022                                         element.Name = reader.Value;\r
1023                                 }\r
1024                                 else if(reader.Name == "nillable")\r
1025                                 {\r
1026                                         element.IsNillable = XmlSchemaUtil.ReadBoolAttribute(reader,out innerex);\r
1027                                         if(innerex != null)\r
1028                                                 error(h,reader.Value + "is not a valid value for nillable",innerex);\r
1029                                 }\r
1030                                 else if(reader.Name == "ref")\r
1031                                 {\r
1032                                         element.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);\r
1033                                         if(innerex != null)\r
1034                                                 error(h, reader.Value + " is not a valid value for ref attribute",innerex);\r
1035                                 }\r
1036                                 else if(reader.Name == "substitutionGroup")\r
1037                                 {\r
1038                                         element.substitutionGroup = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);\r
1039                                         if(innerex != null)\r
1040                                                 error(h, reader.Value + " is not a valid value for substitutionGroup attribute",innerex);\r
1041                                 }\r
1042                                 else if(reader.Name == "type")\r
1043                                 {\r
1044                                         element.SchemaTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);\r
1045                                         if(innerex != null)\r
1046                                                 error(h, reader.Value + " is not a valid value for type attribute",innerex);\r
1047                                 }\r
1048                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
1049                                 {\r
1050                                         error(h,reader.Name + " is not a valid attribute for element",null);\r
1051                                 }\r
1052                                 else\r
1053                                 {\r
1054                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,element);\r
1055                                 }\r
1056                         }\r
1057                         \r
1058                         reader.MoveToElement();\r
1059                         if(reader.IsEmptyElement)\r
1060                                 return element;\r
1061 \r
1062                         //  Content: annotation?, \r
1063                         //                      (simpleType | complexType)?, \r
1064                         //                      (unique | key | keyref)*\r
1065                         int level = 1;\r
1066                         while(reader.ReadNextElement())\r
1067                         {\r
1068                                 if(reader.NodeType == XmlNodeType.EndElement)\r
1069                                 {\r
1070                                         if(reader.LocalName != xmlname)\r
1071                                                 error(h,"Should not happen :2: XmlSchemaElement.Read, name="+reader.Name,null);\r
1072                                         break;\r
1073                                 }\r
1074                                 if(level <= 1 && reader.LocalName == "annotation")\r
1075                                 {\r
1076                                         level = 2; //Only one annotation\r
1077                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
1078                                         if(annotation != null)\r
1079                                                 element.Annotation = annotation;\r
1080                                         continue;\r
1081                                 }\r
1082                                 if(level <= 2)\r
1083                                 {\r
1084                                         if(reader.LocalName == "simpleType")\r
1085                                         {\r
1086                                                 level = 3;\r
1087                                                 XmlSchemaSimpleType simple = XmlSchemaSimpleType.Read(reader,h);\r
1088                                                 if(simple != null)\r
1089                                                         element.SchemaType = simple;\r
1090                                                 continue;\r
1091                                         }\r
1092                                         if(reader.LocalName == "complexType")\r
1093                                         {\r
1094                                                 level = 3;\r
1095                                                 XmlSchemaComplexType complex = XmlSchemaComplexType.Read(reader,h);\r
1096                                                 if(complex != null)\r
1097                                                 {\r
1098                                                         element.SchemaType = complex;\r
1099                                                 }\r
1100                                                 continue;\r
1101                                         }\r
1102                                 }\r
1103                                 if(level <= 3)\r
1104                                 {\r
1105                                         if(reader.LocalName == "unique")\r
1106                                         {\r
1107                                                 level = 3;\r
1108                                                 XmlSchemaUnique unique = XmlSchemaUnique.Read(reader,h);\r
1109                                                 if(unique != null)\r
1110                                                         element.constraints.Add(unique);\r
1111                                                 continue;\r
1112                                         }\r
1113                                         else if(reader.LocalName == "key")\r
1114                                         {\r
1115                                                 level = 3;\r
1116                                                 XmlSchemaKey key = XmlSchemaKey.Read(reader,h);\r
1117                                                 if(key != null)\r
1118                                                         element.constraints.Add(key);\r
1119                                                 continue;\r
1120                                         }\r
1121                                         else if(reader.LocalName == "keyref")\r
1122                                         {\r
1123                                                 level = 3;\r
1124                                                 XmlSchemaKeyref keyref = XmlSchemaKeyref.Read(reader,h);\r
1125                                                 if(keyref != null)\r
1126                                                         element.constraints.Add(keyref);\r
1127                                                 continue;\r
1128                                         }\r
1129                                 }\r
1130                                 reader.RaiseInvalidElementError();\r
1131                         }\r
1132                         return element;\r
1133                 }\r
1134         }\r
1135 }\r