2004-06-03 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / Mono.Xml.Schema / XsdValidatingReader.cs
1 //
2 // Mono.Xml.Schema.XsdValidatingReader.cs
3 //
4 // Author:
5 //      Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
6 //
7 //      (C)2003 Atsushi Enomoto
8 //
9 // Note:
10 //
11 // This class doesn't support set_XmlResolver, since it isn't common to XmlReader interface. 
12 // Try to set that of xml reader which is used to construct this object.
13 //
14 using System;
15 using System.Collections;
16 using System.Collections.Specialized;
17 using System.Text;
18 using System.Xml;
19 using System.Xml.Schema;
20 using Mono.Xml;
21
22 namespace Mono.Xml.Schema
23 {
24         internal class XsdValidatingReader : XmlReader, IXmlLineInfo, IHasXmlSchemaInfo, IHasXmlParserContext
25         {
26                 static char [] wsChars = new char [] {' ', '\t', '\n', '\r'};
27
28                 XmlReader reader;
29                 XmlValidatingReader xvReader;
30                 XmlResolver resolver;
31                 IHasXmlSchemaInfo sourceReaderSchemaInfo;
32                 IXmlLineInfo readerLineInfo;
33                 bool laxElementValidation = true;
34                 bool reportNoValidationError;
35                 XmlSchemaCollection schemas = new XmlSchemaCollection ();
36                 bool namespaces = true;
37
38                 Hashtable idList = new Hashtable ();
39                 ArrayList missingIDReferences = new ArrayList ();
40                 string thisElementId;
41
42                 ArrayList keyTables = new ArrayList ();
43                 ArrayList currentKeyFieldConsumers = new ArrayList ();
44
45                 XsdValidationStateManager stateManager = new XsdValidationStateManager ();
46                 XsdValidationContext context = new XsdValidationContext ();
47                 XsdValidationState childParticleState;
48
49                 int xsiNilDepth = -1;
50                 StringBuilder storedCharacters = new StringBuilder ();
51                 bool shouldValidateCharacters;
52                 int skipValidationDepth = -1;
53
54                 XmlSchemaAttribute [] defaultAttributes = new XmlSchemaAttribute [0];
55                 int currentDefaultAttribute = -1;
56                 XmlQualifiedName currentQName;
57
58                 ArrayList elementQNameStack = new ArrayList ();
59                 bool popContext;
60
61                 // Property Cache.
62                 int nonDefaultAttributeCount;
63                 bool defaultAttributeConsumed;
64
65                 // Validation engine cached object
66                 ArrayList defaultAttributesCache = new ArrayList ();
67                 ArrayList tmpKeyrefPool = new ArrayList ();
68
69 #region .ctor
70                 public XsdValidatingReader (XmlReader reader)
71                         : this (reader, null)
72                 {
73                 }
74                 
75                 public XsdValidatingReader (XmlReader reader, XmlReader validatingReader)
76                 {
77                         this.reader = reader;
78                         xvReader = validatingReader as XmlValidatingReader;
79                         if (xvReader != null) {
80                                 if (xvReader.ValidationType == ValidationType.None)
81                                         reportNoValidationError = true;
82                         }
83                         readerLineInfo = reader as IXmlLineInfo;
84                         sourceReaderSchemaInfo = reader as IHasXmlSchemaInfo;
85                 }
86 #endregion
87                 // Provate Properties
88                 private XmlQualifiedName CurrentQName {
89                         get {
90                                 if (currentQName == null)
91                                         currentQName = new XmlQualifiedName (LocalName, NamespaceURI);
92                                 return currentQName;
93                         }
94                 }
95
96                 internal ArrayList CurrentKeyFieldConsumers {
97                         get { return currentKeyFieldConsumers; }
98                 }
99
100                 // Public Non-overrides
101
102                 public int XsiNilDepth {
103                         get { return xsiNilDepth; }
104                 }
105
106                 public bool Namespaces {
107                         get { return namespaces; }
108                         set { namespaces = value; }
109                 }
110
111                 public XmlReader Reader {
112                         get { return reader; }
113                 }
114
115                 // This is required to resolve xsi:schemaLocation
116                 public XmlResolver XmlResolver {
117                         set {
118                                 resolver = value;
119                         }
120                 }
121
122                 // This should be changed before the first Read() call.
123                 public XmlSchemaCollection Schemas {
124                         get { return schemas; }
125                 }
126
127                 public object SchemaType {
128                         get {
129                                 if (ReadState != ReadState.Interactive)
130                                         return null;
131
132                                 switch (NodeType) {
133                                 case XmlNodeType.Element:
134                                         if (context.ActualType != null)
135                                                 return context.ActualType;
136                                         else if (context.Element != null)
137                                                 return context.Element.ElementType;
138                                         else
139                                                 return SourceReaderSchemaType;
140                                 case XmlNodeType.Attribute:
141                                         XmlSchemaComplexType ct = context.ActualType as XmlSchemaComplexType;
142                                         if (ct != null) {
143                                                 XmlSchemaAttribute attdef = ct.AttributeUses [CurrentQName] as XmlSchemaAttribute;
144                                                 if (attdef != null)
145                                                         return attdef.AttributeType;
146                                         }
147                                         return SourceReaderSchemaType;
148                                 default:
149                                         return SourceReaderSchemaType;
150                                 }
151                         }
152                 }
153
154                 private object SourceReaderSchemaType {
155                         get { return this.sourceReaderSchemaInfo != null ? sourceReaderSchemaInfo.SchemaType : null; }
156                 }
157
158                 // This property is never used in Mono.
159                 public ValidationType ValidationType {
160                         get {
161                                 if (reportNoValidationError)
162                                         return ValidationType.None;
163                                 else
164                                         return ValidationType.Schema;
165                         }
166                 }
167
168                 // It is used only for independent XmlReader use, not for XmlValidatingReader.
169                 public object ReadTypedValue ()
170                 {
171                         XmlSchemaDatatype dt = SchemaType as XmlSchemaDatatype;
172                         XmlSchemaSimpleType st = SchemaType as XmlSchemaSimpleType;
173                         if (st != null)
174                                 dt = st.Datatype;
175                         if (dt == null)
176                                 return null;
177
178                         switch (NodeType) {
179                         case XmlNodeType.Element:
180                                 if (IsEmptyElement)
181                                         return null;
182
183                                 storedCharacters.Length = 0;
184                                 bool loop = true;
185                                 do {
186                                         Read ();
187                                         switch (NodeType) {
188                                         case XmlNodeType.SignificantWhitespace:
189                                         case XmlNodeType.Text:
190                                         case XmlNodeType.CDATA:
191                                                 storedCharacters.Append (Value);
192                                                 break;
193                                         case XmlNodeType.Comment:
194                                                 break;
195                                         default:
196                                                 loop = false;
197                                                 break;
198                                         }
199                                 } while (loop && !EOF);
200                                 return dt.ParseValue (storedCharacters.ToString (), NameTable, ParserContext.NamespaceManager);
201                         case XmlNodeType.Attribute:
202                                 return dt.ParseValue (Value, NameTable, ParserContext.NamespaceManager);
203                         }
204                         return null;
205                 }
206
207                 /*
208                 public ValueType ReadTypedValueType ()
209                 {
210                         XmlSchemaDatatype dt = SchemaType as XmlSchemaDatatype;
211                         XmlSchemaSimpleType st = SchemaType as XmlSchemaSimpleType;
212                         if (st != null)
213                                 dt = st.Datatype;
214                         if (dt == null)
215                                 return null;
216
217                         switch (NodeType) {
218                         case XmlNodeType.Element:
219                                 if (IsEmptyElement)
220                                         return null;
221
222                                 storedCharacters.Length = 0;
223                                 bool loop = true;
224                                 do {
225                                         Read ();
226                                         switch (NodeType) {
227                                         case XmlNodeType.SignificantWhitespace:
228                                         case XmlNodeType.Text:
229                                         case XmlNodeType.CDATA:
230                                                 storedCharacters.Append (Value);
231                                                 break;
232                                         case XmlNodeType.Comment:
233                                                 break;
234                                         default:
235                                                 loop = false;
236                                                 break;
237                                         }
238                                 } while (loop && !EOF);
239                                 return dt.ParseValueType (storedCharacters.ToString (), NameTable, ParserContext.NamespaceManager);
240                         case XmlNodeType.Attribute:
241                                 return dt.ParseValueType (Value, NameTable, ParserContext.NamespaceManager);
242                         }
243                         return null;
244                 }
245                 */
246
247                 public ValidationEventHandler ValidationEventHandler;
248
249                 // Public Overrided Properties
250
251                 public override int AttributeCount {
252                         get {
253                                 /*
254                                 if (NodeType == XmlNodeType.Element)
255                                         return attributeCount;
256                                 else if (IsDefault)
257                                         return 0;
258                                 else
259                                         return reader.AttributeCount;
260                                 */
261                                 return nonDefaultAttributeCount + defaultAttributes.Length;
262                         }
263                 }
264
265                 public override string BaseURI {
266                         get { return reader.BaseURI; }
267                 }
268
269                 // If this class is used to implement XmlValidatingReader,
270                 // it should be left to DTDValidatingReader. In other cases,
271                 // it depends on the reader's ability.
272                 public override bool CanResolveEntity {
273                         get { return reader.CanResolveEntity; }
274                 }
275
276                 public override int Depth {
277                         get {
278                                 if (currentDefaultAttribute < 0)
279                                         return reader.Depth;
280                                 if (this.defaultAttributeConsumed)
281                                         return reader.Depth + 2;
282                                 return reader.Depth + 1;
283                         }
284                 }
285
286                 public override bool EOF {
287                         get { return reader.EOF; }
288                 }
289
290                 public override bool HasValue {
291                         get {
292                                 if (currentDefaultAttribute < 0)
293                                         return reader.HasValue;
294                                 return true;
295                         }
296                 }
297
298                 public override bool IsDefault {
299                         get {
300                                 if (currentDefaultAttribute < 0)
301                                         return reader.IsDefault;
302                                 return true;
303                         }
304                 }
305
306                 public override bool IsEmptyElement {
307                         get {
308                                 if (currentDefaultAttribute < 0)
309                                         return reader.IsEmptyElement;
310                                 return false;
311                         }
312                 }
313
314                 public override string this [int i] {
315                         get { return GetAttribute (i); }
316                 }
317
318                 public override string this [string name] {
319                         get { return GetAttribute (name); }
320                 }
321
322                 public override string this [string localName, string ns] {
323                         get { return GetAttribute (localName, ns); }
324                 }
325
326                 int IXmlLineInfo.LineNumber {
327                         get { return readerLineInfo != null ? readerLineInfo.LineNumber : 0; }
328                 }
329
330                 int IXmlLineInfo.LinePosition {
331                         get { return readerLineInfo != null ? readerLineInfo.LinePosition : 0; }
332                 }
333
334                 public override string LocalName {
335                         get {
336                                 if (currentDefaultAttribute < 0)
337                                         return reader.LocalName;
338                                 if (defaultAttributeConsumed)
339                                         return String.Empty;
340                                 return defaultAttributes [currentDefaultAttribute].QualifiedName.Name;
341                         }
342                 }
343
344                 public override string Name {
345                         get {
346                                 if (currentDefaultAttribute < 0)
347                                         return reader.Name;
348                                 if (defaultAttributeConsumed)
349                                         return String.Empty;
350
351                                 XmlQualifiedName qname = defaultAttributes [currentDefaultAttribute].QualifiedName;
352                                 string prefix = Prefix;
353                                 if (prefix == String.Empty)
354                                         return qname.Name;
355                                 else
356                                         return String.Concat (prefix, ":", qname.Name);
357                         }
358                 }
359
360                 public override string NamespaceURI {
361                         get {
362                                 if (currentDefaultAttribute < 0)
363                                         return reader.NamespaceURI;
364                                 if (defaultAttributeConsumed)
365                                         return String.Empty;
366                                 return defaultAttributes [currentDefaultAttribute].QualifiedName.Namespace;
367                         }
368                 }
369
370                 public override XmlNameTable NameTable {
371                         get { return reader.NameTable; }
372                 }
373
374                 public override XmlNodeType NodeType {
375                         get {
376                                 if (currentDefaultAttribute < 0)
377                                         return reader.NodeType;
378                                 if (defaultAttributeConsumed)
379                                         return XmlNodeType.Text;
380                                 return XmlNodeType.Attribute;
381                         }
382                 }
383
384                 public XmlParserContext ParserContext {
385                         get { return XmlSchemaUtil.GetParserContext (reader); }
386                 }
387
388                 public override string Prefix {
389                         get {
390                                 if (currentDefaultAttribute < 0)
391                                         return reader.Prefix;
392                                 if (defaultAttributeConsumed)
393                                         return String.Empty;
394                                 XmlQualifiedName qname = defaultAttributes [currentDefaultAttribute].QualifiedName;
395                                 string prefix = this.ParserContext.NamespaceManager.LookupPrefix (qname.Namespace, false);
396                                 if (prefix == null)
397                                         return String.Empty;
398                                 else
399                                         return prefix;
400                         }
401                 }
402
403                 public override char QuoteChar {
404                         get { return reader.QuoteChar; }
405                 }
406
407                 public override ReadState ReadState {
408                         get { return reader.ReadState; }
409                 }
410
411                 public override string Value {
412                         get {
413                                 if (currentDefaultAttribute < 0)
414                                         return reader.Value;
415                                 string value = defaultAttributes [currentDefaultAttribute].ValidatedDefaultValue;
416                                 if (value == null)
417                                         value = defaultAttributes [currentDefaultAttribute].ValidatedFixedValue;
418                                 return value;
419                         }
420                 }
421
422                 XmlQualifiedName qnameXmlLang = new XmlQualifiedName ("lang", XmlNamespaceManager.XmlnsXml);
423
424                 public override string XmlLang {
425                         get {
426                                 string xmlLang = reader.XmlLang;
427                                 if (xmlLang != null)
428                                         return xmlLang;
429                                 int idx = this.FindDefaultAttribute ("lang", XmlNamespaceManager.XmlnsXml);
430                                 if (idx < 0)
431                                         return null;
432                                 xmlLang = defaultAttributes [idx].ValidatedDefaultValue;
433                                 if (xmlLang == null)
434                                         xmlLang = defaultAttributes [idx].ValidatedFixedValue;
435                                 return xmlLang;
436                         }
437                 }
438
439                 public override XmlSpace XmlSpace {
440                         get {
441                                 XmlSpace space = reader.XmlSpace;
442                                 if (space != XmlSpace.None)
443                                         return space;
444                                 int idx = this.FindDefaultAttribute ("space", XmlNamespaceManager.XmlnsXml);
445                                 if (idx < 0)
446                                         return XmlSpace.None;
447                                 string spaceSpec = defaultAttributes [idx].ValidatedDefaultValue;
448                                 if (spaceSpec == null)
449                                         spaceSpec = defaultAttributes [idx].ValidatedFixedValue;
450                                 return (XmlSpace) Enum.Parse (typeof (XmlSpace), spaceSpec, false);
451                         }
452                 }
453
454                 // Private Methods
455
456                 private XmlQualifiedName QualifyName (string name)
457                 {
458                         int colonAt = name.IndexOf (':');
459                         if (colonAt < 0)
460                                 return new XmlQualifiedName (name, null);
461                         else
462                                 return new XmlQualifiedName (name.Substring (colonAt + 1),
463                                         LookupNamespace (name.Substring (0, colonAt)));
464                 }
465
466                 private void HandleError (string error)
467                 {
468                         HandleError (error, null);
469                 }
470
471                 private void HandleError (string error, Exception innerException)
472                 {
473                         HandleError (error, innerException, false);
474                 }
475
476                 private void HandleError (string error, Exception innerException, bool isWarning)
477                 {
478                         if (reportNoValidationError)    // extra quick check
479                                 return;
480
481                         XmlSchemaException schemaException = new XmlSchemaException (error, 
482                                         this, this.BaseURI, null, innerException);
483                         HandleError (schemaException, isWarning);
484                 }
485
486                 private void HandleError (XmlSchemaException schemaException)
487                 {
488                         HandleError (schemaException, false);
489                 }
490
491                 private void HandleError (XmlSchemaException schemaException, bool isWarning)
492                 {
493                         if (reportNoValidationError)
494                                 return;
495
496                         ValidationEventArgs e = new ValidationEventArgs (schemaException,
497                                 schemaException.Message, isWarning ? XmlSeverityType.Warning : XmlSeverityType.Error);
498
499                         if (this.ValidationEventHandler != null)
500                                 this.ValidationEventHandler (this, e);
501                         else if (xvReader != null)
502                                 xvReader.OnValidationEvent (this, e);
503                         else if (e.Severity == XmlSeverityType.Error)
504 #if NON_MONO_ENV
505                                 this.xvReader.OnValidationEvent (this, e);
506 #else
507                                 throw e.Exception;
508 #endif
509                 }
510
511                 private XmlSchemaElement FindElement (string name, string ns)
512                 {
513                         foreach (XmlSchema target in schemas) {
514                                 XmlSchema matches = target.Schemas [ns];
515                                 if (matches != null) {
516                                         XmlSchemaElement result = target.Elements [new XmlQualifiedName (name, ns)] as XmlSchemaElement;
517                                         if (result != null)
518                                                 return result;
519                                 }
520                         }
521                         return null;
522                 }
523
524                 private XmlSchemaType FindType (XmlQualifiedName qname)
525                 {
526                         foreach (XmlSchema target in schemas) {
527                                 XmlSchemaType type = target.SchemaTypes [qname] as XmlSchemaType;
528                                 if (type != null)
529                                         return type;
530                         }
531                         return null;
532                 }
533
534                 private void ValidateStartElementParticle ()
535                 {
536                         stateManager.CurrentElement = null;
537                         context.ParticleState = context.ParticleState.EvaluateStartElement (reader.LocalName, reader.NamespaceURI);
538                         if (context.ParticleState == XsdValidationState.Invalid)
539                                 HandleError ("Invalid start element: " + reader.NamespaceURI + ":" + reader.LocalName);
540
541                         context.Element = stateManager.CurrentElement;
542                         if (context.Element != null)
543                                 context.SchemaType = context.Element.ElementType;
544                 }
545
546                 private void ValidateEndElementParticle ()
547                 {
548                         if (childParticleState != null) {
549                                 if (!childParticleState.EvaluateEndElement ()) {
550                                         HandleError ("Invalid end element: " + reader.Name);
551                                 }
552                         }
553                         context.PopScope (reader.Depth);
554                 }
555
556                 // Utility for missing validation completion related to child items.
557                 private void ValidateCharacters ()
558                 {
559                         if (xsiNilDepth >= 0 && xsiNilDepth < reader.Depth)
560                                 HandleError ("Element item appeared, while current element context is nil.");
561
562                         storedCharacters.Append (reader.Value);
563                 }
564
565                 // Utility for missing validation completion related to child items.
566                 private void ValidateEndCharacters ()
567                 {
568                         if (context.ActualType == null)
569                                 return;
570
571                         string value = storedCharacters.ToString ();
572
573                         if (storedCharacters.Length == 0) {
574                                 // 3.3.4 Element Locally Valid (Element) 5.1.2
575                                 if (context.Element != null) {
576                                         if (context.Element.ValidatedDefaultValue != null)
577                                                 value = context.Element.ValidatedDefaultValue;
578                                 }                                       
579                         }
580
581                         XmlSchemaDatatype dt = context.ActualType as XmlSchemaDatatype;
582                         XmlSchemaSimpleType st = context.ActualType as XmlSchemaSimpleType;
583                         if (dt == null) {
584                                 if (st != null) {
585                                         dt = st.Datatype;
586                                 } else {
587                                         XmlSchemaComplexType ct = context.ActualType as XmlSchemaComplexType;
588                                         dt = ct.Datatype;
589                                         switch (ct.ContentType) {
590                                         case XmlSchemaContentType.ElementOnly:
591                                         case XmlSchemaContentType.Empty:
592                                                 if (storedCharacters.Length > 0)
593                                                         HandleError ("Character content not allowed.");
594                                                 break;
595                                         }
596                                 }
597                         }
598                         if (dt != null) {
599                                 // 3.3.4 Element Locally Valid (Element) :: 5.2.2.2. Fixed value constraints
600                                 if (context.Element != null && context.Element.ValidatedFixedValue != null)
601                                         if (value != context.Element.ValidatedFixedValue)
602                                                 HandleError ("Fixed value constraint was not satisfied.");
603                                 AssessStringValid (st, dt, value);
604                         }
605
606                         // Identity field value
607                         while (this.currentKeyFieldConsumers.Count > 0) {
608                                 XsdKeyEntryField field = this.currentKeyFieldConsumers [0] as XsdKeyEntryField;
609                                 if (field.Identity != null)
610                                         HandleError ("Two or more identical field was found. Former value is '" + field.Identity + "' .");
611                                 object identity = null; // This means empty value
612                                 if (dt != null) {
613                                         try {
614                                                 identity = dt.ParseValue (value, NameTable, ParserContext.NamespaceManager);
615                                         } catch (Exception ex) { // FIXME: (wishlist) This is bad manner ;-(
616                                                 HandleError ("Identity value is invalid against its data type " + dt.TokenizedType, ex);
617                                         }
618                                 }
619                                 if (identity == null)
620                                         identity = value;
621
622                                 if (!field.SetIdentityField (identity, reader.Depth == xsiNilDepth, dt as XsdAnySimpleType, this))
623                                         HandleError ("Two or more identical key value was found: '" + value + "' .");
624                                 this.currentKeyFieldConsumers.RemoveAt (0);
625                         }
626
627                         shouldValidateCharacters = false;
628                 }
629
630                 // 3.14.4 String Valid 
631                 private void AssessStringValid (XmlSchemaSimpleType st,
632                         XmlSchemaDatatype dt, string value)
633                 {
634                         XmlSchemaDatatype validatedDatatype = dt;
635                         if (st != null) {
636                                 string normalized = validatedDatatype.Normalize (value);
637                                 string [] values;
638                                 XmlSchemaDatatype itemDatatype;
639                                 XmlSchemaSimpleType itemSimpleType;
640                                 switch (st.DerivedBy) {
641                                 case XmlSchemaDerivationMethod.List:
642                                         XmlSchemaSimpleTypeList listContent = st.Content as XmlSchemaSimpleTypeList;
643                                         values = normalized.Split (wsChars);
644                                         itemDatatype = listContent.ValidatedListItemType as XmlSchemaDatatype;
645                                         itemSimpleType = listContent.ValidatedListItemType as XmlSchemaSimpleType;
646                                         for (int vi = 0; vi < values.Length; vi++) {
647                                                 string each = values [vi];
648                                                 if (each == String.Empty)
649                                                         continue;
650                                                 // validate against ValidatedItemType
651                                                 if (itemDatatype != null) {
652                                                         try {
653                                                                 itemDatatype.ParseValue (each, NameTable, ParserContext.NamespaceManager);
654                                                         } catch (Exception ex) { // FIXME: (wishlist) better exception handling ;-(
655                                                                 HandleError ("List type value contains one or more invalid values.", ex);
656                                                                 break;
657                                                         }
658                                                 }
659                                                 else
660                                                         AssessStringValid (itemSimpleType, itemSimpleType.Datatype, each);
661                                         }
662                                         break;
663                                 case XmlSchemaDerivationMethod.Union:
664                                         XmlSchemaSimpleTypeUnion union = st.Content as XmlSchemaSimpleTypeUnion;
665                                         {
666                                                 string each = normalized;
667                                                 // validate against ValidatedItemType
668                                                 bool passed = false;
669                                                 foreach (object eachType in union.ValidatedTypes) {
670                                                         itemDatatype = eachType as XmlSchemaDatatype;
671                                                         itemSimpleType = eachType as XmlSchemaSimpleType;
672                                                         if (itemDatatype != null) {
673                                                                 try {
674                                                                         itemDatatype.ParseValue (each, NameTable, ParserContext.NamespaceManager);
675                                                                 } catch (Exception) { // FIXME: (wishlist) better exception handling ;-(
676                                                                         continue;
677                                                                 }
678                                                         }
679                                                         else {
680                                                                 try {
681                                                                         AssessStringValid (itemSimpleType, itemSimpleType.Datatype, each);
682                                                                 } catch (XmlSchemaException) {
683                                                                         continue;
684                                                                 }
685                                                         }
686                                                         passed = true;
687                                                         break;
688                                                 }
689                                                 if (!passed) {
690                                                         HandleError ("Union type value contains one or more invalid values.");
691                                                         break;
692                                                 }
693                                         }
694                                         break;
695                                 case XmlSchemaDerivationMethod.Restriction:
696                                         XmlSchemaSimpleTypeRestriction str = st.Content as XmlSchemaSimpleTypeRestriction;
697                                         // facet validation
698                                         if (str != null) {
699                                                 /* Don't forget to validate against inherited type's facets 
700                                                  * Could we simplify this by assuming that the basetype will also
701                                                  * be restriction?
702                                                  * */
703                                                  // mmm, will check later.
704                                                 XmlSchemaSimpleType baseType = st.BaseXmlSchemaType as XmlSchemaSimpleType;
705                                                 if (baseType != null) {
706                                                          AssessStringValid(baseType, dt, normalized);
707                                                 }
708                                                 if (!str.ValidateValueWithFacets (normalized, NameTable)) {
709                                                         HandleError ("Specified value was invalid against the facets.");
710                                                         break;
711                                                 }
712                                         }
713                                         validatedDatatype = st.Datatype;
714                                         break;
715                                 }
716                         }
717                         if (validatedDatatype != null) {
718                                 try {
719                                         validatedDatatype.ParseValue (value, NameTable, ParserContext.NamespaceManager);
720                                 } catch (Exception ex) {        // FIXME: (wishlist) It is bad manner ;-(
721                                         HandleError ("Invalidly typed data was specified.", ex);
722                                 }
723                         }
724                 }
725
726                 private object GetLocalTypeDefinition (string name)
727                 {
728                         object xsiType = null;
729                         XmlQualifiedName typeQName = QualifyName (name);
730                         if (typeQName == XmlSchemaComplexType.AnyTypeName)
731                                 xsiType = XmlSchemaComplexType.AnyType;
732                         else if (XmlSchemaUtil.IsBuiltInDatatypeName (typeQName))
733                                 xsiType = XmlSchemaDatatype.FromName (typeQName);
734                         else
735                                 xsiType = FindType (typeQName);
736                         return xsiType;
737                 }
738
739                 // It is common to ElementLocallyValid::4 and SchemaValidityAssessment::1.2.1.2.4
740                 private void AssessLocalTypeDerivationOK (object xsiType, object baseType, XmlSchemaDerivationMethod flag)
741                 {
742                         XmlSchemaType xsiSchemaType = xsiType as XmlSchemaType;
743                         XmlSchemaComplexType baseComplexType = baseType as XmlSchemaComplexType;
744                         XmlSchemaComplexType xsiComplexType = xsiSchemaType as XmlSchemaComplexType;
745                         if (xsiType != baseType) {
746                                 // Extracted (not extraneous) check for 3.4.6 TypeDerivationOK.
747                                 if (baseComplexType != null)
748                                         flag |= baseComplexType.BlockResolved;
749                                 if (flag == XmlSchemaDerivationMethod.All) {
750                                         HandleError ("Prohibited element type substitution.");
751                                         return;
752                                 } else if (xsiSchemaType != null && (flag & xsiSchemaType.DerivedBy) != 0) {
753                                         HandleError ("Prohibited element type substitution.");
754                                         return;
755                                 }
756                         }
757
758                         if (xsiComplexType != null)
759                                 try {
760                                         xsiComplexType.ValidateTypeDerivationOK (baseType, null, null);
761                                 } catch (XmlSchemaException ex) {
762 //                                      HandleError ("Locally specified schema complex type derivation failed. " + ex.Message, ex);
763                                         HandleError (ex);
764                                 }
765                         else {
766                                 XmlSchemaSimpleType xsiSimpleType = xsiType as XmlSchemaSimpleType;
767                                 if (xsiSimpleType != null) {
768                                         try {
769                                                 xsiSimpleType.ValidateTypeDerivationOK (baseType, null, null, true);
770                                         } catch (XmlSchemaException ex) {
771 //                                              HandleError ("Locally specified schema simple type derivation failed. " + ex.Message, ex);
772                                                 HandleError (ex);
773                                         }
774                                 }
775                                 else if (xsiType is XmlSchemaDatatype) {
776                                         // do nothing
777                                 }
778                                 else
779                                         HandleError ("Primitive data type cannot be derived type using xsi:type specification.");
780                         }
781                 }
782
783                 // Section 3.3.4 of the spec.
784                 private void AssessStartElementSchemaValidity ()
785                 {
786                         // If the reader is inside xsi:nil (and failed on validation),
787                         // then simply skip its content.
788                         if (xsiNilDepth >= 0 && xsiNilDepth < reader.Depth)
789                                 HandleError ("Element item appeared, while current element context is nil.");
790
791                         context.Load (reader.Depth);
792                         if (childParticleState != null) {
793                                 context.ParticleState = childParticleState;
794                                 childParticleState = null;
795                         }
796
797                         // If validation state exists, then first assess particle validity.
798                         context.SchemaType = null;
799                         if (context.ParticleState != null) {
800                                 ValidateStartElementParticle ();
801                         }
802
803                         string xsiNilValue = reader.GetAttribute ("nil", XmlSchema.InstanceNamespace);
804                         if (xsiNilValue != null)
805                                 xsiNilValue = xsiNilValue.Trim (XmlChar.WhitespaceChars);
806                         bool isXsiNil = xsiNilValue == "true";
807                         if (isXsiNil && this.xsiNilDepth < 0)
808                                 xsiNilDepth = reader.Depth;
809
810                         // [Schema Validity Assessment (Element) 1.2]
811                         // Evaluate "local type definition" from xsi:type.
812                         // (See spec 3.3.4 Schema Validity Assessment (Element) 1.2.1.2.3.
813                         // Note that Schema Validity Assessment(Element) 1.2 takes
814                         // precedence than 1.1 of that.
815
816                         string xsiTypeName = reader.GetAttribute ("type", XmlSchema.InstanceNamespace);
817                         if (xsiTypeName != null) {
818                                 xsiTypeName = xsiTypeName.Trim (XmlChar.WhitespaceChars);
819                                 object xsiType = GetLocalTypeDefinition (xsiTypeName);
820                                 if (xsiType == null)
821                                         HandleError ("The instance type was not found: " + xsiTypeName + " .");
822                                 else {
823                                         XmlSchemaType xsiSchemaType = xsiType as XmlSchemaType;
824                                         if (xsiSchemaType != null && this.context.Element != null) {
825                                                 XmlSchemaType elemBaseType = context.Element.ElementType as XmlSchemaType;
826                                                 if (elemBaseType != null && (xsiSchemaType.DerivedBy & elemBaseType.FinalResolved) != 0)
827                                                         HandleError ("The instance type is prohibited by the type of the context element.");
828                                                 if (elemBaseType != xsiType && (xsiSchemaType.DerivedBy & this.context.Element.BlockResolved) != 0)
829                                                         HandleError ("The instance type is prohibited by the context element.");
830                                         }
831                                         XmlSchemaComplexType xsiComplexType = xsiType as XmlSchemaComplexType;
832                                         if (xsiComplexType != null && xsiComplexType.IsAbstract)
833                                                 HandleError ("The instance type is abstract: " + xsiTypeName + " .");
834                                         else {
835                                                 // If current schema type exists, then this xsi:type must be
836                                                 // valid extension of that type. See 1.2.1.2.4.
837                                                 if (context.Element != null) {
838                                                         AssessLocalTypeDerivationOK (xsiType, context.Element.ElementType, context.Element.BlockResolved);
839                                                 }
840                                                 AssessStartElementLocallyValidType (xsiType);   // 1.2.2:
841                                                 context.LocalTypeDefinition = xsiType;
842                                         }
843                                 }
844                         }
845                         else
846                                 context.LocalTypeDefinition = null;
847
848                         // Create Validation Root, if not exist.
849                         // [Schema Validity Assessment (Element) 1.1]
850                         if (context.Element == null)
851                                 context.Element = FindElement (reader.LocalName, reader.NamespaceURI);
852                         if (context.Element != null) {
853                                 if (xsiTypeName == null) {
854                                         context.SchemaType = context.Element.ElementType;
855                                         AssessElementLocallyValidElement (context.Element, xsiNilValue);        // 1.1.2
856                                 }
857                         } else {
858                                 XmlSchema schema;
859                                 switch (stateManager.ProcessContents) {
860                                 case XmlSchemaContentProcessing.Skip:
861                                         break;
862                                 case XmlSchemaContentProcessing.Lax:
863                                         /*
864                                         schema = schemas [reader.NamespaceURI];
865                                         if (schema != null && !schema.missedSubComponents)
866                                                 HandleError ("Element declaration for " + reader.LocalName + " is missing.");
867                                         */
868                                         break;
869                                 default:
870                                         schema = schemas [reader.NamespaceURI];
871                                         if (xsiTypeName == null && (schema == null || !schema.missedSubComponents))
872                                                 HandleError ("Element declaration for " + reader.LocalName + " is missing.");
873                                         break;
874                                 }
875                         }
876
877                         if (stateManager.ProcessContents == XmlSchemaContentProcessing.Skip)
878                                 skipValidationDepth = reader.Depth;
879
880                         // Finally, create child particle state.
881                         XmlSchemaComplexType xsComplexType = SchemaType as XmlSchemaComplexType;
882                         if (xsComplexType != null)
883                                 childParticleState = stateManager.Create (xsComplexType.ValidatableParticle);
884                         else if (stateManager.ProcessContents == XmlSchemaContentProcessing.Lax)
885                                 childParticleState = stateManager.Create (XmlSchemaAny.AnyTypeContent);
886                         else
887                                 childParticleState = stateManager.Create (XmlSchemaParticle.Empty);
888
889                         AssessStartIdentityConstraints ();
890
891                         context.PushScope (reader.Depth);
892                 }
893
894                 // 3.3.4 Element Locally Valid (Element)
895                 private void AssessElementLocallyValidElement (XmlSchemaElement element, string xsiNilValue)
896                 {
897                         XmlQualifiedName qname = new XmlQualifiedName (reader.LocalName, reader.NamespaceURI);
898                         // 1.
899                         if (element == null)
900                                 HandleError ("Element declaration is required for " + qname);
901                         // 2.
902                         if (element.ActualIsAbstract)
903                                 HandleError ("Abstract element declaration was specified for " + qname);
904                         // 3.1.
905                         if (!element.ActualIsNillable && xsiNilValue != null)
906                                 HandleError ("This element declaration is not nillable: " + qname);
907                         // 3.2.
908                         // Note that 3.2.1 xsi:nil constraints are to be validated in
909                         else if (xsiNilValue == "true") {
910                                 // AssessElementSchemaValidity() and ValidateCharacters()
911
912                                 if (element.ValidatedFixedValue != null)
913                                         HandleError ("Schema instance nil was specified, where the element declaration for " + qname + "has fixed value constraints.");
914                         }
915                         // 4.
916                         string xsiType = reader.GetAttribute ("type", XmlSchema.InstanceNamespace);
917                         if (xsiType != null) {
918                                 context.LocalTypeDefinition = GetLocalTypeDefinition (xsiType);
919                                 AssessLocalTypeDerivationOK (context.LocalTypeDefinition, element.ElementType, element.BlockResolved);
920                         }
921                         else
922                                 context.LocalTypeDefinition = null;
923
924                         // 5 Not all things cannot be assessed here.
925                         // It is common to 5.1 and 5.2
926                         if (element.ElementType != null)
927                                 AssessStartElementLocallyValidType (SchemaType);
928
929                         // 6. should be out from here.
930                         // See invokation of AssessStartIdentityConstraints().
931
932                         // 7 is going to be validated in Read() (in case of xmlreader's EOF).
933                 }
934
935                 // 3.3.4 Element Locally Valid (Type)
936                 private void AssessStartElementLocallyValidType (object schemaType)
937                 {
938                         if (schemaType == null) {       // 1.
939                                 HandleError ("Schema type does not exist.");
940                                 return;
941                         }
942                         XmlSchemaComplexType cType = schemaType as XmlSchemaComplexType;
943                         XmlSchemaSimpleType sType = schemaType as XmlSchemaSimpleType;
944                         if (sType != null) {
945                                 // 3.1.1.
946                                 while (reader.MoveToNextAttribute ()) {
947                                         if (reader.NamespaceURI == XmlNamespaceManager.XmlnsXmlns)
948                                                 continue;
949                                         if (reader.NamespaceURI != XmlSchema.InstanceNamespace)
950                                                 HandleError ("Current simple type cannot accept attributes other than schema instance namespace.");
951                                         switch (reader.LocalName) {
952                                         case "type":
953                                         case "nil":
954                                         case "schemaLocation":
955                                         case "noNamespaceSchemaLocation":
956                                                 break;
957                                         default:
958                                                 HandleError ("Unknown schema instance namespace attribute: " + reader.LocalName);
959                                                 break;
960                                         }
961                                 }
962                                 reader.MoveToElement ();
963                                 // 3.1.2 and 3.1.3 cannot be assessed here.
964                         } else if (cType != null) {
965                                 if (cType.IsAbstract) { // 2.
966                                         HandleError ("Target complex type is abstract.");
967                                         return;
968                                 }
969                                 // 3.2
970                                 AssessElementLocallyValidComplexType (cType);
971                         }
972                 }
973
974                 // 3.4.4 Element Locally Valid (Complex Type)
975                 private void AssessElementLocallyValidComplexType (XmlSchemaComplexType cType)
976                 {
977                         // 1.
978                         if (cType.IsAbstract)
979                                 HandleError ("Target complex type is abstract.");
980
981                         // 2 (xsi:nil and content prohibition)
982                         // See AssessStartElementSchemaValidity() and ValidateCharacters()
983
984                         string elementNs = reader.NamespaceURI;
985                         // 3. attribute uses and 
986                         // 5. wild IDs
987                         while (reader.MoveToNextAttribute ()) {
988                                 if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
989                                         continue;
990                                 else if (reader.NamespaceURI == XmlSchema.InstanceNamespace)
991                                         continue;
992                                 XmlQualifiedName qname = new XmlQualifiedName (reader.LocalName, reader.NamespaceURI);
993                                 object attMatch = FindAttributeDeclaration (cType, qname, elementNs);
994                                 if (attMatch == null)
995                                         HandleError ("Attribute declaration was not found for " + qname);
996                                 else {
997                                         XmlSchemaAttribute attdecl = attMatch as XmlSchemaAttribute;
998                                         if (attdecl == null) { // i.e. anyAttribute
999                                                 XmlSchemaAnyAttribute anyAttrMatch = attMatch as XmlSchemaAnyAttribute;
1000                                         } else {
1001                                                 AssessAttributeLocallyValidUse (attdecl);
1002                                                 AssessAttributeLocallyValid (attdecl, true);
1003                                         }
1004                                 }
1005                         }
1006                         reader.MoveToElement ();
1007
1008                         // Collect default attributes.
1009                         // 4.
1010                         foreach (DictionaryEntry entry in cType.AttributeUses) {
1011                                 XmlSchemaAttribute attr = (XmlSchemaAttribute) entry.Value;
1012                                 if (reader [attr.QualifiedName.Name, attr.QualifiedName.Namespace] == null) {
1013                                         if (attr.ValidatedUse == XmlSchemaUse.Required && 
1014                                                 attr.ValidatedFixedValue == null)
1015                                                 HandleError ("Required attribute " + attr.QualifiedName + " was not found.");
1016                                         else if (attr.ValidatedDefaultValue != null)
1017                                                 defaultAttributesCache.Add (attr);
1018                                         else if (attr.ValidatedFixedValue != null)
1019                                                 defaultAttributesCache.Add (attr);
1020                                 }
1021                         }
1022                         defaultAttributes = (XmlSchemaAttribute []) 
1023                                 defaultAttributesCache.ToArray (typeof (XmlSchemaAttribute));
1024                         context.DefaultAttributes = defaultAttributes;
1025                         defaultAttributesCache.Clear ();
1026                         // 5. wild IDs was already checked above.
1027                 }
1028
1029                 // Spec 3.10.4 Item Valid (Wildcard)
1030                 private bool AttributeWildcardItemValid (XmlSchemaAnyAttribute anyAttr, XmlQualifiedName qname)
1031                 {
1032                         if (anyAttr.HasValueAny)
1033                                 return true;
1034                         if (anyAttr.HasValueOther && (anyAttr.TargetNamespace == "" || reader.NamespaceURI != anyAttr.TargetNamespace))
1035                                 return true;
1036                         if (anyAttr.HasValueTargetNamespace && reader.NamespaceURI == anyAttr.TargetNamespace)
1037                                 return true;
1038                         if (anyAttr.HasValueLocal && reader.NamespaceURI == "")
1039                                 return true;
1040                         for (int i = 0; i < anyAttr.ResolvedNamespaces.Count; i++)
1041                                 if (anyAttr.ResolvedNamespaces [i] == reader.NamespaceURI)
1042                                         return true;
1043                         return false;
1044                 }
1045
1046                 private XmlSchemaObject FindAttributeDeclaration (XmlSchemaComplexType cType,
1047                         XmlQualifiedName qname, string elementNs)
1048                 {
1049                         XmlSchemaObject result = cType.AttributeUses [qname];
1050                         if (result != null)
1051                                 return result;
1052                         if (cType.AttributeWildcard == null)
1053                                 return null;
1054
1055                         if (!AttributeWildcardItemValid (cType.AttributeWildcard, qname))
1056                                 return null;
1057
1058                         if (cType.AttributeWildcard.ResolvedProcessContents == XmlSchemaContentProcessing.Skip)
1059                                 return cType.AttributeWildcard;
1060                         foreach (XmlSchema schema in schemas) {
1061                                 foreach (DictionaryEntry entry in schema.Attributes) {
1062                                         XmlSchemaAttribute attr = (XmlSchemaAttribute) entry.Value;
1063                                         if (attr.QualifiedName == qname)
1064                                                 return attr;
1065                                 }
1066                         }
1067                         if (cType.AttributeWildcard.ResolvedProcessContents == XmlSchemaContentProcessing.Lax)
1068                                 return cType.AttributeWildcard;
1069                         else
1070                                 return null;
1071                 }
1072
1073                 // 3.2.4 Attribute Locally Valid and 3.4.4 - 5.wildIDs
1074                 private void AssessAttributeLocallyValid (XmlSchemaAttribute attr, bool checkWildIDs)
1075                 {
1076                         // 1.
1077                         switch (reader.NamespaceURI) {
1078                         case XmlNamespaceManager.XmlnsXml:
1079                         case XmlNamespaceManager.XmlnsXmlns:
1080                         case XmlSchema.InstanceNamespace:
1081                                 break;
1082                         }
1083                         // 2. - 4.
1084                         if (attr.AttributeType == null)
1085                                 HandleError ("Attribute type is missing for " + attr.QualifiedName);
1086                         XmlSchemaDatatype dt = attr.AttributeType as XmlSchemaDatatype;
1087                         if (dt == null)
1088                                 dt = ((XmlSchemaSimpleType) attr.AttributeType).Datatype;
1089                         // It is a bit heavy process, so let's omit as long as possible ;-)
1090                         if (dt != XmlSchemaSimpleType.AnySimpleType || attr.ValidatedFixedValue != null) {
1091                                 string normalized = dt.Normalize (reader.Value);
1092                                 object parsedValue = null;
1093                                 try {
1094                                         parsedValue = dt.ParseValue (normalized, reader.NameTable, this.ParserContext.NamespaceManager);
1095                                 } catch (Exception ex) { // FIXME: (wishlist) It is bad manner ;-(
1096                                         HandleError ("Attribute value is invalid against its data type " + dt.TokenizedType, ex);
1097                                 }
1098                                 if (attr.ValidatedFixedValue != null && attr.ValidatedFixedValue != normalized)
1099                                         HandleError ("The value of the attribute " + attr.QualifiedName + " does not match with its fixed value.");
1100                                 if (checkWildIDs)
1101                                         AssessEachAttributeIdentityConstraint (dt, normalized, parsedValue);
1102                         }
1103                 }
1104
1105                 private void AssessEachAttributeIdentityConstraint (XmlSchemaDatatype dt,
1106                         string normalized, object parsedValue)
1107                 {
1108                         // Get normalized value and (if required) parsedValue if missing.
1109                         switch (dt.TokenizedType) {
1110                         case XmlTokenizedType.IDREFS:
1111                                 if (normalized == null)
1112                                         normalized = dt.Normalize (reader.Value);
1113                                 if (parsedValue == null)
1114                                         parsedValue = dt.ParseValue (normalized, reader.NameTable, ParserContext.NamespaceManager);
1115                                 break;
1116                         case XmlTokenizedType.ID:
1117                         case XmlTokenizedType.IDREF:
1118                                 if (normalized == null)
1119                                         normalized = dt.Normalize (reader.Value);
1120                                 break;
1121                         }
1122
1123                         // Validate identity constraints.
1124                         switch (dt.TokenizedType) {
1125                         case XmlTokenizedType.ID:
1126                                 if (thisElementId != null)
1127                                         HandleError ("ID type attribute was already assigned in the containing element.");
1128                                 thisElementId = normalized;
1129                                 if (idList.Contains (normalized))
1130                                         HandleError ("Duplicate ID value was found.");
1131                                 else
1132                                         idList.Add (normalized, normalized);
1133                                 break;
1134                         case XmlTokenizedType.IDREF:
1135                                 if (missingIDReferences.Contains (normalized))
1136                                         missingIDReferences.Remove (normalized);
1137                                 else
1138                                         missingIDReferences.Add (normalized);
1139                                 break;
1140                         case XmlTokenizedType.IDREFS:
1141                                 string [] idrefs = (string []) parsedValue;
1142                                 for (int i = 0; i < idrefs.Length; i++) {
1143                                         string id = idrefs [i];
1144                                         if (missingIDReferences.Contains (id))
1145                                                 missingIDReferences.Remove (id);
1146                                         else
1147                                                 missingIDReferences.Add (id);
1148                                 }
1149                                 break;
1150                         }
1151                 }
1152
1153                 private void AssessAttributeLocallyValidUse (XmlSchemaAttribute attr)
1154                 {
1155                         // This is extra check than spec 3.5.4
1156                         if (attr.ValidatedUse == XmlSchemaUse.Prohibited)
1157                                 HandleError ("Attribute " + attr.QualifiedName + " is prohibited in this context.");
1158                 }
1159
1160                 private void AssessEndElementSchemaValidity ()
1161                 {
1162                         if (childParticleState == null)
1163                                 childParticleState = context.ParticleState;
1164                         ValidateEndElementParticle ();  // validate against childrens' state.
1165
1166                         if (shouldValidateCharacters) {
1167                                 ValidateEndCharacters ();
1168                                 shouldValidateCharacters = false;
1169                         }
1170
1171                         // 3.3.4 Assess ElementLocallyValidElement 5: value constraints.
1172                         // 3.3.4 Assess ElementLocallyValidType 3.1.3. = StringValid(3.14.4)
1173                         // => ValidateEndCharacters().
1174
1175                         // Reset Identity constraints.
1176                         for (int i = 0; i < keyTables.Count; i++) {
1177                                 XsdKeyTable keyTable = this.keyTables [i] as XsdKeyTable;
1178                                 if (keyTable.StartDepth == reader.Depth) {
1179                                         EndIdentityValidation (keyTable);
1180                                 } else {
1181                                         for (int k = 0; k < keyTable.Entries.Count; k++) {
1182                                                 XsdKeyEntry entry = keyTable.Entries [k] as XsdKeyEntry;
1183                                                 // Remove finished (maybe key not found) entries.
1184                                                 if (entry.StartDepth == reader.Depth) {
1185                                                         if (entry.KeyFound)
1186                                                                 keyTable.FinishedEntries.Add (entry);
1187                                                         else if (entry.KeySequence.SourceSchemaIdentity is XmlSchemaKey)
1188                                                                 HandleError ("Key sequence is missing.");
1189                                                         keyTable.Entries.RemoveAt (k);
1190                                                         k--;
1191                                                 }
1192                                                 // Pop validated key depth to find two or more fields.
1193                                                 else {
1194                                                         for (int j = 0; j < entry.KeyFields.Count; j++) {
1195                                                                 XsdKeyEntryField kf = entry.KeyFields [j];
1196                                                                 if (!kf.FieldFound && kf.FieldFoundDepth == reader.Depth) {
1197                                                                         kf.FieldFoundDepth = 0;
1198                                                                         kf.FieldFoundPath = null;
1199                                                                 }
1200                                                         }
1201                                                 }
1202                                         }
1203                                 }
1204                         }
1205                         for (int i = 0; i < keyTables.Count; i++) {
1206                                 XsdKeyTable keyseq = this.keyTables [i] as XsdKeyTable;
1207                                 if (keyseq.StartDepth == reader.Depth) {
1208                                         keyTables.RemoveAt (i);
1209                                         i--;
1210                                 }
1211                         }
1212
1213                         // Reset xsi:nil, if required.
1214                         if (xsiNilDepth == reader.Depth)
1215                                 xsiNilDepth = -1;
1216                 }
1217
1218                 // 3.11.4 Identity Constraint Satisfied
1219                 private void AssessStartIdentityConstraints ()
1220                 {
1221                         tmpKeyrefPool.Clear ();
1222                         if (context.Element != null && context.Element.Constraints.Count > 0) {
1223                                 // (a) Create new key sequences, if required.
1224                                 for (int i = 0; i < context.Element.Constraints.Count; i++) {
1225                                         XmlSchemaIdentityConstraint ident = (XmlSchemaIdentityConstraint) context.Element.Constraints [i];
1226                                         XsdKeyTable seq = CreateNewKeyTable (ident);
1227                                         if (ident is XmlSchemaKeyref)
1228                                                 tmpKeyrefPool.Add (seq);
1229                                 }
1230                         }
1231
1232                         // (b) Evaluate current key sequences.
1233                         for (int i = 0; i < keyTables.Count; i++) {
1234                                 XsdKeyTable seq  = (XsdKeyTable) keyTables [i];
1235                                 if (seq.SelectorMatches (this.elementQNameStack, reader) != null) {
1236                                         // creates and registers new entry.
1237                                         XsdKeyEntry entry = new XsdKeyEntry (seq, reader);
1238                                         seq.Entries.Add (entry);
1239                                 }
1240                         }
1241
1242                         // (c) Evaluate field paths.
1243                         for (int i = 0; i < keyTables.Count; i++) {
1244                                 XsdKeyTable seq  = (XsdKeyTable) keyTables [i];
1245                                 // If possible, create new field entry candidates.
1246                                 for (int j = 0; j < seq.Entries.Count; j++) {
1247                                         XsdKeyEntry entry = seq.Entries [j] as XsdKeyEntry;
1248                                         try {
1249                                                 entry.FieldMatches (this.elementQNameStack, this);
1250                                         } catch (Exception ex) { // FIXME: (wishlist) It is bad manner ;-(
1251                                                 HandleError ("Identity field value is invalid against its data type.", ex);
1252                                         }
1253                                 }
1254                         }
1255                 }
1256
1257                 private XsdKeyTable CreateNewKeyTable (XmlSchemaIdentityConstraint ident)
1258                 {
1259                         XsdKeyTable seq = new XsdKeyTable (ident, this);
1260                         seq.StartDepth = reader.Depth;
1261                         XmlSchemaKeyref keyref = ident as XmlSchemaKeyref;
1262                         this.keyTables.Add (seq);
1263                         return seq;
1264                 }
1265
1266                 private void EndIdentityValidation (XsdKeyTable seq)
1267                 {
1268                         ArrayList errors = new ArrayList ();
1269                         for (int i = 0; i < seq.Entries.Count; i++) {
1270                                 XsdKeyEntry entry = (XsdKeyEntry) seq.Entries [i];
1271                                 if (entry.KeyFound)
1272                                         continue;
1273                                 if (seq.SourceSchemaIdentity is XmlSchemaKey)
1274                                         errors.Add ("line " + entry.SelectorLineNumber + "position " + entry.SelectorLinePosition);
1275                         }
1276                         if (errors.Count > 0)
1277                                 HandleError ("Invalid identity constraints were found. Key was not found. "
1278                                         + String.Join (", ", errors.ToArray (typeof (string)) as string []));
1279
1280                         errors.Clear ();
1281                         // Find reference target
1282                         XmlSchemaKeyref xsdKeyref = seq.SourceSchemaIdentity as XmlSchemaKeyref;
1283                         if (xsdKeyref != null) {
1284                                 for (int i = this.keyTables.Count - 1; i >= 0; i--) {
1285                                         XsdKeyTable target = this.keyTables [i] as XsdKeyTable;
1286                                         if (target.SourceSchemaIdentity == xsdKeyref.Target) {
1287                                                 seq.ReferencedKey = target;
1288                                                 for (int j = 0; j < seq.FinishedEntries.Count; j++) {
1289                                                         XsdKeyEntry entry = (XsdKeyEntry) seq.FinishedEntries [j];
1290                                                         for (int k = 0; k < target.FinishedEntries.Count; k++) {
1291                                                                 XsdKeyEntry targetEntry = (XsdKeyEntry) target.FinishedEntries [k];
1292                                                                 if (entry.CompareIdentity (targetEntry)) {
1293                                                                         entry.KeyRefFound = true;
1294                                                                         break;
1295                                                                 }
1296                                                         }
1297                                                 }
1298                                         }
1299                                 }
1300                                 if (seq.ReferencedKey == null)
1301                                         HandleError ("Target key was not found.");
1302                                 for (int i = 0; i < seq.FinishedEntries.Count; i++) {
1303                                         XsdKeyEntry entry = (XsdKeyEntry) seq.FinishedEntries [i];
1304                                         if (!entry.KeyRefFound)
1305                                                 errors.Add (" line " + entry.SelectorLineNumber + ", position " + entry.SelectorLinePosition);
1306                                 }
1307                                 if (errors.Count > 0)
1308                                         HandleError ("Invalid identity constraints were found. Referenced key was not found: "
1309                                                 + String.Join (" / ", errors.ToArray (typeof (string)) as string []));
1310                         }
1311                 }
1312
1313                 // Overrided Methods
1314
1315                 public override void Close ()
1316                 {
1317                         reader.Close ();
1318                 }
1319
1320                 public override string GetAttribute (int i)
1321                 {
1322                         switch (reader.NodeType) {
1323                         case XmlNodeType.XmlDeclaration:
1324                         case XmlNodeType.DocumentType:
1325                                 return reader.GetAttribute (i);
1326                         }
1327
1328                         if (reader.AttributeCount > i)
1329                                 reader.GetAttribute (i);
1330                         int defIdx = i - nonDefaultAttributeCount;
1331                         if (i < AttributeCount)
1332                                 return defaultAttributes [defIdx].DefaultValue;
1333
1334                         throw new ArgumentOutOfRangeException ("i", i, "Specified attribute index is out of range.");
1335                 }
1336
1337                 public override string GetAttribute (string name)
1338                 {
1339                         switch (reader.NodeType) {
1340                         case XmlNodeType.XmlDeclaration:
1341                         case XmlNodeType.DocumentType:
1342                                 return reader.GetAttribute (name);
1343                         }
1344
1345                         string value = reader.GetAttribute (name);
1346                         if (value != null)
1347                                 return value;
1348
1349                         XmlQualifiedName qname = SplitQName (name);
1350                         return GetDefaultAttribute (qname.Name, qname.Namespace);
1351                 }
1352
1353                 private XmlQualifiedName SplitQName (string name)
1354                 {
1355                         if (!XmlChar.IsName (name))
1356                                 throw new ArgumentException ("Invalid name was specified.", "name");
1357
1358                         Exception ex = null;
1359                         XmlQualifiedName qname = XmlSchemaUtil.ToQName (reader, name, out ex);
1360                         if (ex != null)
1361                                 return XmlQualifiedName.Empty;
1362                         else
1363                                 return qname;
1364                 }
1365
1366                 public override string GetAttribute (string localName, string ns)
1367                 {
1368                         switch (reader.NodeType) {
1369                         case XmlNodeType.XmlDeclaration:
1370                         case XmlNodeType.DocumentType:
1371                                 return reader.GetAttribute (localName, ns);
1372                         }
1373
1374                         string value = reader.GetAttribute (localName, ns);
1375                         if (value != null)
1376                                 return value;
1377
1378                         return GetDefaultAttribute (localName, ns);
1379                 }
1380
1381                 private string GetDefaultAttribute (string localName, string ns)
1382                 {
1383                         int idx = this.FindDefaultAttribute (localName, ns);
1384                         if (idx < 0)
1385                                 return null;
1386                         string value = defaultAttributes [idx].ValidatedDefaultValue;
1387                         if (value == null)
1388                                 value = defaultAttributes [idx].ValidatedFixedValue;
1389                         return value;
1390                 }
1391
1392                 private int FindDefaultAttribute (string localName, string ns)
1393                 {
1394                         for (int i = 0; i < this.defaultAttributes.Length; i++) {
1395                                 XmlSchemaAttribute attr = defaultAttributes [i];
1396                                 if (attr.QualifiedName.Name == localName &&
1397                                         (ns == null || attr.QualifiedName.Namespace == ns))
1398                                         return i;
1399                         }
1400                         return -1;
1401                 }
1402
1403                 bool IXmlLineInfo.HasLineInfo ()
1404                 {
1405                         return readerLineInfo != null && readerLineInfo.HasLineInfo ();
1406                 }
1407
1408                 public override string LookupNamespace (string prefix)
1409                 {
1410                         return reader.LookupNamespace (prefix);
1411                 }
1412
1413                 public override void MoveToAttribute (int i)
1414                 {
1415                         switch (reader.NodeType) {
1416                         case XmlNodeType.XmlDeclaration:
1417                         case XmlNodeType.DocumentType:
1418                                 reader.MoveToAttribute (i);
1419                                 return;
1420                         }
1421
1422                         currentQName = null;
1423                         if (i < this.nonDefaultAttributeCount) {
1424                                 reader.MoveToAttribute (i);
1425                                 this.currentDefaultAttribute = -1;
1426                                 this.defaultAttributeConsumed = false;
1427                         }
1428
1429                         if (i < AttributeCount) {
1430                                 this.currentDefaultAttribute = i - nonDefaultAttributeCount;
1431                                 this.defaultAttributeConsumed = false;
1432                         }
1433                         else
1434                                 throw new ArgumentOutOfRangeException ("i", i, "Attribute index is out of range.");
1435                 }
1436
1437                 public override bool MoveToAttribute (string name)
1438                 {
1439                         switch (reader.NodeType) {
1440                         case XmlNodeType.XmlDeclaration:
1441                         case XmlNodeType.DocumentType:
1442                                 return reader.MoveToAttribute (name);
1443                         }
1444
1445                         currentQName = null;
1446                         bool b = reader.MoveToAttribute (name);
1447                         if (b) {
1448                                 this.currentDefaultAttribute = -1;
1449                                 this.defaultAttributeConsumed = false;
1450                                 return true;
1451                         }
1452
1453                         return MoveToDefaultAttribute (name, null);
1454                 }
1455
1456                 public override bool MoveToAttribute (string localName, string ns)
1457                 {
1458                         switch (reader.NodeType) {
1459                         case XmlNodeType.XmlDeclaration:
1460                         case XmlNodeType.DocumentType:
1461                                 return reader.MoveToAttribute (localName, ns);
1462                         }
1463
1464                         currentQName = null;
1465                         bool b = reader.MoveToAttribute (localName, ns);
1466                         if (b) {
1467                                 this.currentDefaultAttribute = -1;
1468                                 this.defaultAttributeConsumed = false;
1469                                 return true;
1470                         }
1471
1472                         return MoveToDefaultAttribute (localName, ns);
1473                 }
1474
1475                 private bool MoveToDefaultAttribute (string localName, string ns)
1476                 {
1477                         int idx = this.FindDefaultAttribute (localName, ns);
1478                         if (idx < 0)
1479                                 return false;
1480                         currentDefaultAttribute = idx;
1481                         defaultAttributeConsumed = false;
1482                         return true;
1483                 }
1484
1485                 public override bool MoveToElement ()
1486                 {
1487                         currentDefaultAttribute = -1;
1488                         defaultAttributeConsumed = false;
1489                         currentQName = null;
1490                         return reader.MoveToElement ();
1491                 }
1492
1493                 public override bool MoveToFirstAttribute ()
1494                 {
1495                         switch (reader.NodeType) {
1496                         case XmlNodeType.XmlDeclaration:
1497                         case XmlNodeType.DocumentType:
1498                                 return reader.MoveToFirstAttribute ();
1499                         }
1500
1501                         currentQName = null;
1502                         if (this.nonDefaultAttributeCount > 0) {
1503                                 bool b = reader.MoveToFirstAttribute ();
1504                                 if (b) {
1505                                         currentDefaultAttribute = -1;
1506                                         defaultAttributeConsumed = false;
1507                                 }
1508                                 return b;
1509                         }
1510
1511                         if (this.defaultAttributes.Length > 0) {
1512                                 currentDefaultAttribute = 0;
1513                                 defaultAttributeConsumed = false;
1514                                 return true;
1515                         }
1516                         else
1517                                 return false;
1518                 }
1519
1520                 public override bool MoveToNextAttribute ()
1521                 {
1522                         switch (reader.NodeType) {
1523                         case XmlNodeType.XmlDeclaration:
1524                         case XmlNodeType.DocumentType:
1525                                 return reader.MoveToNextAttribute ();
1526                         }
1527
1528                         currentQName = null;
1529                         if (currentDefaultAttribute >= 0) {
1530                                 if (defaultAttributes.Length == currentDefaultAttribute + 1)
1531                                         return false;
1532                                 currentDefaultAttribute++;
1533                                 defaultAttributeConsumed = false;
1534                                 return true;
1535                         }
1536
1537                         bool b = reader.MoveToNextAttribute ();
1538                         if (b) {
1539                                 currentDefaultAttribute = -1;
1540                                 defaultAttributeConsumed = false;
1541                                 return true;
1542                         }
1543
1544                         if (defaultAttributes.Length > 0) {
1545                                 currentDefaultAttribute = 0;
1546                                 defaultAttributeConsumed = false;
1547                                 return true;
1548                         }
1549                         else
1550                                 return false;
1551                 }
1552
1553                 private void ExamineAdditionalSchema ()
1554                 {
1555                         XmlSchema schema = null;
1556                         string schemaLocation = reader.GetAttribute ("schemaLocation", XmlSchema.InstanceNamespace);
1557                         if (schemaLocation != null) {
1558                                 string [] tmp = null;
1559                                 try {
1560                                         schemaLocation = XmlSchemaDatatype.FromName ("token").Normalize (schemaLocation);
1561                                         tmp = schemaLocation.Split (XmlChar.WhitespaceChars);
1562                                 } catch (Exception ex) {
1563                                         HandleError ("Invalid schemaLocation attribute format.", ex, true);
1564                                         tmp = new string [0];
1565                                 }
1566                                 if (tmp.Length % 2 != 0)
1567                                         HandleError ("Invalid schemaLocation attribute format.");
1568                                 for (int i = 0; i < tmp.Length; i += 2) {
1569                                         Uri absUri = null;
1570                                         try {
1571                                                 absUri = new Uri ((this.BaseURI != "" ? new Uri (BaseURI) : null), tmp [i + 1]);
1572                                                 XmlTextReader xtr = new XmlTextReader (absUri.ToString ());
1573                                                 schema = XmlSchema.Read (xtr, null);
1574                                         } catch (Exception) { // FIXME: (wishlist) It is bad manner ;-(
1575                                                 HandleError ("Could not resolve schema location URI: " + absUri, null, true);\r
1576                                                 continue;
1577                                         }
1578                                         if (schema.TargetNamespace == null)
1579                                                 schema.TargetNamespace = tmp [i];
1580                                         else if (schema.TargetNamespace != tmp [i])
1581                                                 HandleError ("Specified schema has different target namespace.");
1582                                 }
1583                         }
1584                         if (schema != null) {
1585                                 try {
1586                                         schemas.Add (schema, resolver);
1587                                 } catch (XmlSchemaException ex) {
1588                                         HandleError (ex);
1589                                 }
1590                         }
1591                         schema = null;
1592                         string noNsSchemaLocation = reader.GetAttribute ("noNamespaceSchemaLocation", XmlSchema.InstanceNamespace);
1593                         if (noNsSchemaLocation != null) {
1594                                 Uri absUri = null;
1595                                 try {
1596                                         absUri = new Uri ((this.BaseURI != "" ? new Uri (BaseURI) : null), noNsSchemaLocation);
1597                                         XmlTextReader xtr = new XmlTextReader (absUri.ToString ());
1598                                         schema = XmlSchema.Read (xtr, null);
1599                                 } catch (Exception) { // FIXME: (wishlist) It is bad manner ;-(
1600                                         HandleError ("Could not resolve schema location URI: " + absUri, null, true);\r
1601                                 }
1602                                 if (schema != null && schema.TargetNamespace != null)
1603                                         HandleError ("Specified schema has different target namespace.");
1604                         }
1605                         if (schema != null) {
1606                                 try {
1607                                         schema.Compile (ValidationEventHandler, resolver);
1608                                         schemas.Add (schema);
1609                                 } catch (XmlSchemaException ex) {
1610                                         HandleError (ex);
1611                                 }
1612                         }
1613                 }
1614
1615                 public override bool Read ()
1616                 {
1617                         nonDefaultAttributeCount = 0;
1618                         currentDefaultAttribute = -1;
1619                         defaultAttributeConsumed = false;
1620                         currentQName = null;
1621                         thisElementId = null;
1622                         defaultAttributes = new XmlSchemaAttribute [0];
1623                         if (popContext) {
1624                                 elementQNameStack.RemoveAt (elementQNameStack.Count - 1);
1625                                 popContext = false;
1626                         }
1627
1628                         bool result = reader.Read ();
1629                         // 3.3.4 ElementLocallyValidElement 7 = Root Valid.
1630                         if (!result && missingIDReferences.Count > 0)
1631                                 HandleError ("There are missing ID references: " +
1632                                         String.Join (" ",
1633                                         this.missingIDReferences.ToArray (typeof (string)) as string []));
1634
1635                         switch (reader.NodeType) {
1636                         case XmlNodeType.XmlDeclaration:
1637                                 this.nonDefaultAttributeCount = reader.AttributeCount;
1638                                 break;
1639                         case XmlNodeType.Element:
1640                                 nonDefaultAttributeCount = reader.AttributeCount;
1641
1642                                 if (reader.Depth == 0)
1643                                         ExamineAdditionalSchema ();
1644
1645                                 this.elementQNameStack.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI));
1646
1647                                 // If there is no schema information, then no validation is performed.
1648                                 if (schemas.Count == 0)
1649                                         break;
1650
1651                                 if (skipValidationDepth < 0 || reader.Depth <= skipValidationDepth) {
1652                                         if (shouldValidateCharacters) {
1653                                                 ValidateEndCharacters ();
1654                                                 shouldValidateCharacters = false;
1655                                         }
1656                                         AssessStartElementSchemaValidity ();
1657                                         storedCharacters.Length = 0;
1658                                 } else {
1659                                         context.Clear ();
1660                                 }
1661
1662                                 if (reader.IsEmptyElement)
1663                                         goto case XmlNodeType.EndElement;
1664                                 else
1665                                         shouldValidateCharacters = true;
1666                                 break;
1667                         case XmlNodeType.EndElement:
1668                                 if (reader.Depth == skipValidationDepth) {
1669                                         skipValidationDepth = -1;
1670                                         context.Clear ();
1671                                 }
1672                                 else
1673                                         AssessEndElementSchemaValidity ();
1674
1675                                 storedCharacters.Length = 0;
1676                                 childParticleState = null;
1677                                 popContext = true;
1678                                 break;
1679
1680                         case XmlNodeType.CDATA:
1681                         case XmlNodeType.SignificantWhitespace:
1682                         case XmlNodeType.Text:
1683                                 XmlSchemaComplexType ct = context.ActualType as XmlSchemaComplexType;
1684                                 if (ct != null && storedCharacters.Length > 0) {
1685                                         switch (ct.ContentType) {
1686                                         case XmlSchemaContentType.ElementOnly:
1687                                         case XmlSchemaContentType.Empty:
1688                                                 HandleError ("Not allowed character content was found.");
1689                                                 break;
1690                                         }
1691                                 }
1692
1693                                 ValidateCharacters ();
1694                                 break;
1695                         }
1696
1697                         return result;
1698                 }
1699
1700                 public override bool ReadAttributeValue ()
1701                 {
1702                         if (currentDefaultAttribute < 0)
1703                                 return reader.ReadAttributeValue ();
1704
1705                         if (this.defaultAttributeConsumed)
1706                                 return false;
1707
1708                         defaultAttributeConsumed = true;
1709                         return true;
1710                 }
1711
1712 #if NET_1_0
1713                 public override string ReadInnerXml ()
1714                 {
1715                         // MS.NET 1.0 has a serious bug here. It skips validation.
1716                         return reader.ReadInnerXml ();
1717                 }
1718
1719                 public override string ReadOuterXml ()
1720                 {
1721                         // MS.NET 1.0 has a serious bug here. It skips validation.
1722                         return reader.ReadOuterXml ();
1723                 }
1724 #endif
1725
1726                 // XmlReader.ReadString() should call derived this.Read().
1727                 public override string ReadString ()
1728                 {
1729 #if NET_1_0
1730                         return reader.ReadString ();
1731 #else
1732                         return base.ReadString ();
1733 #endif
1734                 }
1735
1736                 // This class itself does not have this feature.
1737                 public override void ResolveEntity ()
1738                 {
1739                         reader.ResolveEntity ();
1740                 }
1741
1742                 internal class XsdValidationContext
1743                 {
1744                         Hashtable contextStack;
1745
1746                         public XsdValidationContext ()
1747                         {
1748                                 contextStack = new Hashtable ();
1749                         }
1750
1751                         // Some of them might be missing (See the spec section 5.3, and also 3.3.4).
1752                         public XmlSchemaElement Element;
1753                         public XsdValidationState ParticleState;
1754                         public XmlSchemaAttribute [] DefaultAttributes;
1755
1756                         // Some of them might be missing (See the spec section 5.3).
1757                         public object SchemaType;
1758
1759                         public object LocalTypeDefinition;
1760
1761                         public object ActualType {
1762                                 get {
1763                                         if (LocalTypeDefinition != null)
1764                                                 return LocalTypeDefinition;
1765                                         else
1766                                                 return SchemaType;
1767                                 }
1768                         }
1769
1770                         public void Clear ()
1771                         {
1772                                 Element = null;
1773                                 SchemaType = null;
1774                                 ParticleState = null;
1775                                 LocalTypeDefinition = null;
1776                         }
1777
1778                         public void PushScope (int depth)
1779                         {
1780                                 contextStack [depth] = this.MemberwiseClone ();
1781                         }
1782
1783                         public void PopScope (int depth)
1784                         {
1785                                 Load (depth);
1786                                 contextStack.Remove (depth + 1);
1787                         }
1788
1789                         public void Load (int depth)
1790                         {
1791                                 Clear ();
1792                                 XsdValidationContext restored = (XsdValidationContext) contextStack [depth];
1793                                 if (restored != null) {
1794                                         this.Element = restored.Element;
1795                                         this.ParticleState = restored.ParticleState;
1796                                         this.SchemaType = restored.SchemaType;
1797                                         this.LocalTypeDefinition = restored.LocalTypeDefinition;
1798                                 }
1799                         }
1800                 }
1801
1802                 /*
1803                 internal class XsdValidityState
1804                 {
1805                         ArrayList currentParticles = new ArrayList ();
1806                         ArrayList occured = new ArrayList ();
1807                         Hashtable xsAllConsumed = new Hashtable ();
1808                         XmlSchemaParticle parciele;
1809                         int particleDepth;
1810
1811                         public XsdValidityState (XmlSchemaParticle particle)
1812                         {
1813                                 this.parciele = particle;
1814                                 currentParticles.Add (particle);
1815                         }
1816
1817                 }
1818                 */
1819         }
1820
1821 }