2 // XmlSchemaValidatingReader.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
35 using System.Xml.Schema;
36 using System.Xml.XPath;
43 using System.Collections.Generic;
44 using QName = System.Xml.XmlQualifiedName;
45 using Form = System.Xml.Schema.XmlSchemaForm;
46 using Use = System.Xml.Schema.XmlSchemaUse;
47 using ContentType = System.Xml.Schema.XmlSchemaContentType;
48 using Validity = System.Xml.Schema.XmlSchemaValidity;
49 using ValidationFlags = System.Xml.Schema.XmlSchemaValidationFlags;
50 using SOMList = System.Xml.Schema.XmlSchemaObjectCollection;
51 using SOMObject = System.Xml.Schema.XmlSchemaObject;
52 using XsElement = System.Xml.Schema.XmlSchemaElement;
53 using XsAttr = System.Xml.Schema.XmlSchemaAttribute;
54 using AttrGroup = System.Xml.Schema.XmlSchemaAttributeGroup;
55 using AttrGroupRef = System.Xml.Schema.XmlSchemaAttributeGroupRef;
56 using XsDatatype = System.Xml.Schema.XmlSchemaDatatype;
57 using SchemaType = System.Xml.Schema.XmlSchemaType;
58 using SimpleType = System.Xml.Schema.XmlSchemaSimpleType;
59 using ComplexType = System.Xml.Schema.XmlSchemaComplexType;
60 using SimpleModel = System.Xml.Schema.XmlSchemaSimpleContent;
61 using SimpleExt = System.Xml.Schema.XmlSchemaSimpleContentExtension;
62 using SimpleRst = System.Xml.Schema.XmlSchemaSimpleContentRestriction;
63 using ComplexModel = System.Xml.Schema.XmlSchemaComplexContent;
64 using ComplexExt = System.Xml.Schema.XmlSchemaComplexContentExtension;
65 using ComplexRst = System.Xml.Schema.XmlSchemaComplexContentRestriction;
66 using SimpleTypeRst = System.Xml.Schema.XmlSchemaSimpleTypeRestriction;
67 using SimpleList = System.Xml.Schema.XmlSchemaSimpleTypeList;
68 using SimpleUnion = System.Xml.Schema.XmlSchemaSimpleTypeUnion;
69 using SchemaFacet = System.Xml.Schema.XmlSchemaFacet;
70 using LengthFacet = System.Xml.Schema.XmlSchemaLengthFacet;
71 using MinLengthFacet = System.Xml.Schema.XmlSchemaMinLengthFacet;
72 using Particle = System.Xml.Schema.XmlSchemaParticle;
73 using Sequence = System.Xml.Schema.XmlSchemaSequence;
74 using Choice = System.Xml.Schema.XmlSchemaChoice;
75 using ValException = System.Xml.Schema.XmlSchemaValidationException;
77 namespace Mono.Xml.Schema
79 internal class XmlSchemaValidatingReader : XmlReader, IXmlLineInfo,
80 IXmlSchemaInfo, IXmlNamespaceResolver, IHasXmlParserContext
82 static readonly XsAttr [] emptyAttributeArray =
85 #region Instance Fields
88 ValidationFlags options;
90 XmlValueGetter getter;
92 IXmlLineInfo readerLineInfo;
93 ValidationType validationType;
94 IXmlNamespaceResolver nsResolver;
96 XsAttr [] defaultAttributes = emptyAttributeArray;
97 int currentDefaultAttribute = -1;
98 ArrayList defaultAttributesCache = new ArrayList ();
99 bool defaultAttributeConsumed;
100 XmlSchemaType currentAttrType;
103 // Extra for XmlSchemaValidtingReader
104 // (not in XsdValidatingReader)
105 //XsElement element; // ... xsinfo.Element?
109 public XmlSchemaValidatingReader (XmlReader reader,
110 XmlReaderSettings settings)
112 IXmlNamespaceResolver nsResolver = reader as IXmlNamespaceResolver;
113 if (nsResolver == null)
114 // throw new ArgumentException ("Argument XmlReader must implement IXmlNamespaceResolver.");
115 nsResolver = new XmlNamespaceManager (reader.NameTable);
117 XmlSchemaSet schemas = settings.Schemas;
119 schemas = new XmlSchemaSet ();
120 options = settings.ValidationFlags;
122 this.reader = reader;
123 v = new XmlSchemaValidator (
128 if (reader.BaseURI != String.Empty)
129 v.SourceUri = new Uri (reader.BaseURI);
131 readerLineInfo = reader as IXmlLineInfo;
132 getter = delegate () {
133 if (v.CurrentAttributeType != null)
134 return v.CurrentAttributeType.ParseValue (Value, NameTable, this);
138 xsinfo = new XmlSchemaInfo (); // transition cache
139 v.LineInfoProvider = this;
140 v.ValidationEventSender = reader;
141 this.nsResolver = nsResolver;
143 ValidationEventHandler += delegate (object o, ValidationEventArgs e) {
144 settings.OnValidationError (o, e);
146 if (settings != null && settings.Schemas != null)
147 v.XmlResolver = settings.Schemas.XmlResolver;
149 v.XmlResolver = new XmlUrlResolver ();
151 v.XmlResolver = new XmlUrlResolver ();
156 public event ValidationEventHandler ValidationEventHandler {
157 add { v.ValidationEventHandler += value; }
158 remove { v.ValidationEventHandler -= value; }
162 public XmlSchemaType ElementSchemaType {
164 return null; // element != null ? element.ElementSchemaType : null;
168 // clear default attributes, MoveTo*Attribute() transitent
170 private void ResetStateOnRead ()
172 currentDefaultAttribute = -1;
173 defaultAttributeConsumed = false;
174 currentAttrType = null;
175 defaultAttributes = emptyAttributeArray;
176 v.CurrentAttributeType = null;
181 public int LineNumber {
182 get { return readerLineInfo != null ? readerLineInfo.LineNumber : 0; }
185 public int LinePosition {
186 get { return readerLineInfo != null ? readerLineInfo.LinePosition : 0; }
189 public XmlSchemaType SchemaType {
191 if (ReadState != ReadState.Interactive)
195 case XmlNodeType.Element:
196 if (ElementSchemaType != null)
197 return ElementSchemaType;
199 return null;//SourceReaderSchemaType;
200 case XmlNodeType.Attribute:
201 if (currentAttrType == null) {
202 ComplexType ct = ElementSchemaType as ComplexType;
204 XsAttr attdef = ct.AttributeUses [new XmlQualifiedName (LocalName, NamespaceURI)] as XsAttr;
206 currentAttrType = attdef.AttributeSchemaType;
207 return currentAttrType;
209 // currentAttrType = SourceReaderSchemaType;
211 return currentAttrType;
213 return null;//SourceReaderSchemaType;
218 public ValidationType ValidationType {
219 get { return validationType; }
221 if (ReadState != ReadState.Initial)
222 throw new InvalidOperationException ("ValidationType must be set before reading.");
223 validationType = value;
227 public IDictionary<string, string> GetNamespacesInScope (XmlNamespaceScope scope)
229 IXmlNamespaceResolver resolver = reader as IXmlNamespaceResolver;
230 if (resolver == null)
231 throw new NotSupportedException ("The input XmlReader does not implement IXmlNamespaceResolver and thus this validating reader cannot collect in-scope namespaces.");
232 return resolver.GetNamespacesInScope (scope);
235 public string LookupPrefix (string ns)
237 return nsResolver.LookupPrefix (ns);
240 // Public Overriden Properties
242 public override int AttributeCount {
244 return reader.AttributeCount + defaultAttributes.Length;
248 public override string BaseURI {
249 get { return reader.BaseURI; }
252 // If this class is used to implement XmlValidatingReader,
253 // it should be left to DTDValidatingReader. In other cases,
254 // it depends on the reader's ability.
255 public override bool CanResolveEntity {
256 get { return reader.CanResolveEntity; }
259 public override int Depth {
261 if (currentDefaultAttribute < 0)
263 if (this.defaultAttributeConsumed)
264 return reader.Depth + 2;
265 return reader.Depth + 1;
269 public override bool EOF {
270 get { return reader.EOF; }
273 public override bool HasValue {
275 if (currentDefaultAttribute < 0)
276 return reader.HasValue;
281 public override bool IsDefault {
283 if (currentDefaultAttribute < 0)
284 return reader.IsDefault;
289 public override bool IsEmptyElement {
291 if (currentDefaultAttribute < 0)
292 return reader.IsEmptyElement;
297 public override string this [int i] {
298 get { return GetAttribute (i); }
301 public override string this [string name] {
302 get { return GetAttribute (name); }
305 public override string this [string localName, string ns] {
306 get { return GetAttribute (localName, ns); }
309 int IXmlLineInfo.LineNumber {
310 get { return readerLineInfo != null ? readerLineInfo.LineNumber : 0; }
313 int IXmlLineInfo.LinePosition {
314 get { return readerLineInfo != null ? readerLineInfo.LinePosition : 0; }
317 public override string LocalName {
319 if (currentDefaultAttribute < 0)
320 return reader.LocalName;
321 if (defaultAttributeConsumed)
323 return defaultAttributes [currentDefaultAttribute].QualifiedName.Name;
327 public override string Name {
329 if (currentDefaultAttribute < 0)
331 if (defaultAttributeConsumed)
334 XmlQualifiedName qname = defaultAttributes [currentDefaultAttribute].QualifiedName;
335 string prefix = Prefix;
336 if (prefix == String.Empty)
339 return String.Concat (prefix, ":", qname.Name);
343 public override string NamespaceURI {
345 if (currentDefaultAttribute < 0)
346 return reader.NamespaceURI;
347 if (defaultAttributeConsumed)
349 return defaultAttributes [currentDefaultAttribute].QualifiedName.Namespace;
353 public override XmlNameTable NameTable {
354 get { return reader.NameTable; }
357 public override XmlNodeType NodeType {
359 if (currentDefaultAttribute < 0)
360 return reader.NodeType;
361 if (defaultAttributeConsumed)
362 return XmlNodeType.Text;
363 return XmlNodeType.Attribute;
367 public XmlParserContext ParserContext {
368 get { return XmlSchemaUtil.GetParserContext (reader); }
371 public override string Prefix {
373 if (currentDefaultAttribute < 0)
374 return reader.Prefix;
375 if (defaultAttributeConsumed)
377 XmlQualifiedName qname = defaultAttributes [currentDefaultAttribute].QualifiedName;
378 string prefix = nsResolver.LookupPrefix (qname.Namespace);
386 public override char QuoteChar {
387 get { return reader.QuoteChar; }
390 public override ReadState ReadState {
391 get { return reader.ReadState; }
394 public override IXmlSchemaInfo SchemaInfo {
396 return new XmlSchemaInfo () {
397 //ContentType = this.ContentType,
398 IsDefault = this.IsDefault,
400 MemberType = this.MemberType,
401 SchemaAttribute = this.SchemaAttribute,
402 SchemaElement = this.SchemaElement,
403 SchemaType = this.SchemaType,
404 Validity = this.Validity
409 public override string Value {
411 if (currentDefaultAttribute < 0)
413 string value = defaultAttributes [currentDefaultAttribute].ValidatedDefaultValue;
415 value = defaultAttributes [currentDefaultAttribute].ValidatedFixedValue;
420 public override string XmlLang {
422 string xmlLang = reader.XmlLang;
425 int idx = this.FindDefaultAttribute ("lang", XmlNamespaceManager.XmlnsXml);
428 xmlLang = defaultAttributes [idx].ValidatedDefaultValue;
430 xmlLang = defaultAttributes [idx].ValidatedFixedValue;
435 public override XmlSpace XmlSpace {
437 XmlSpace space = reader.XmlSpace;
438 if (space != XmlSpace.None)
440 int idx = this.FindDefaultAttribute ("space", XmlNamespaceManager.XmlnsXml);
442 return XmlSpace.None;
443 string spaceSpec = defaultAttributes [idx].ValidatedDefaultValue;
444 if (spaceSpec == null)
445 spaceSpec = defaultAttributes [idx].ValidatedFixedValue;
446 return (XmlSpace) Enum.Parse (typeof (XmlSpace), spaceSpec, false);
451 #region Public Methods
455 public override void Close ()
460 public override string GetAttribute (int i)
462 switch (reader.NodeType) {
463 case XmlNodeType.XmlDeclaration:
464 case XmlNodeType.DocumentType:
465 return reader.GetAttribute (i);
468 if (reader.AttributeCount > i)
469 reader.GetAttribute (i);
470 int defIdx = i - reader.AttributeCount;
471 if (i < AttributeCount)
472 return defaultAttributes [defIdx].DefaultValue;
474 throw new ArgumentOutOfRangeException ("i", i, "Specified attribute index is out of range.");
477 public override string GetAttribute (string name)
479 switch (reader.NodeType) {
480 case XmlNodeType.XmlDeclaration:
481 case XmlNodeType.DocumentType:
482 return reader.GetAttribute (name);
485 string value = reader.GetAttribute (name);
489 XmlQualifiedName qname = SplitQName (name);
490 return GetDefaultAttribute (qname.Name, qname.Namespace);
493 private XmlQualifiedName SplitQName (string name)
495 XmlConvert.VerifyName (name);
498 XmlQualifiedName qname = XmlSchemaUtil.ToQName (reader, name, out ex);
500 return XmlQualifiedName.Empty;
505 public override string GetAttribute (string localName, string ns)
507 switch (reader.NodeType) {
508 case XmlNodeType.XmlDeclaration:
509 case XmlNodeType.DocumentType:
510 return reader.GetAttribute (localName, ns);
513 string value = reader.GetAttribute (localName, ns);
517 return GetDefaultAttribute (localName, ns);
520 private string GetDefaultAttribute (string localName, string ns)
522 int idx = this.FindDefaultAttribute (localName, ns);
525 string value = defaultAttributes [idx].ValidatedDefaultValue;
527 value = defaultAttributes [idx].ValidatedFixedValue;
531 private int FindDefaultAttribute (string localName, string ns)
533 for (int i = 0; i < this.defaultAttributes.Length; i++) {
534 XsAttr attr = defaultAttributes [i];
535 if (attr.QualifiedName.Name == localName &&
536 (ns == null || attr.QualifiedName.Namespace == ns))
542 bool IXmlLineInfo.HasLineInfo ()
544 return readerLineInfo != null && readerLineInfo.HasLineInfo ();
547 public override string LookupNamespace (string prefix)
549 return reader.LookupNamespace (prefix);
552 public override void MoveToAttribute (int i)
554 switch (reader.NodeType) {
555 case XmlNodeType.XmlDeclaration:
556 case XmlNodeType.DocumentType:
557 reader.MoveToAttribute (i);
561 currentAttrType = null;
562 if (i < reader.AttributeCount) {
563 reader.MoveToAttribute (i);
564 this.currentDefaultAttribute = -1;
565 this.defaultAttributeConsumed = false;
568 if (i < AttributeCount) {
569 this.currentDefaultAttribute = i - reader.AttributeCount;
570 this.defaultAttributeConsumed = false;
573 throw new ArgumentOutOfRangeException ("i", i, "Attribute index is out of range.");
576 public override bool MoveToAttribute (string name)
578 switch (reader.NodeType) {
579 case XmlNodeType.XmlDeclaration:
580 case XmlNodeType.DocumentType:
581 return reader.MoveToAttribute (name);
584 currentAttrType = null;
585 bool b = reader.MoveToAttribute (name);
587 this.currentDefaultAttribute = -1;
588 this.defaultAttributeConsumed = false;
592 return MoveToDefaultAttribute (name, null);
595 public override bool MoveToAttribute (string localName, string ns)
597 switch (reader.NodeType) {
598 case XmlNodeType.XmlDeclaration:
599 case XmlNodeType.DocumentType:
600 return reader.MoveToAttribute (localName, ns);
603 currentAttrType = null;
604 bool b = reader.MoveToAttribute (localName, ns);
606 this.currentDefaultAttribute = -1;
607 this.defaultAttributeConsumed = false;
611 return MoveToDefaultAttribute (localName, ns);
614 private bool MoveToDefaultAttribute (string localName, string ns)
616 int idx = this.FindDefaultAttribute (localName, ns);
619 currentDefaultAttribute = idx;
620 defaultAttributeConsumed = false;
624 public override bool MoveToElement ()
626 currentDefaultAttribute = -1;
627 defaultAttributeConsumed = false;
628 currentAttrType = null;
629 return reader.MoveToElement ();
632 public override bool MoveToFirstAttribute ()
634 switch (reader.NodeType) {
635 case XmlNodeType.XmlDeclaration:
636 case XmlNodeType.DocumentType:
637 return reader.MoveToFirstAttribute ();
640 currentAttrType = null;
641 if (reader.AttributeCount > 0) {
642 bool b = reader.MoveToFirstAttribute ();
644 currentDefaultAttribute = -1;
645 defaultAttributeConsumed = false;
650 if (this.defaultAttributes.Length > 0) {
651 currentDefaultAttribute = 0;
652 defaultAttributeConsumed = false;
659 public override bool MoveToNextAttribute ()
661 switch (reader.NodeType) {
662 case XmlNodeType.XmlDeclaration:
663 case XmlNodeType.DocumentType:
664 return reader.MoveToNextAttribute ();
667 currentAttrType = null;
668 if (currentDefaultAttribute >= 0) {
669 if (defaultAttributes.Length == currentDefaultAttribute + 1)
671 currentDefaultAttribute++;
672 defaultAttributeConsumed = false;
676 bool b = reader.MoveToNextAttribute ();
678 currentDefaultAttribute = -1;
679 defaultAttributeConsumed = false;
683 if (defaultAttributes.Length > 0) {
684 currentDefaultAttribute = 0;
685 defaultAttributeConsumed = false;
692 public override bool Read ()
694 if (!reader.Read ()) {
695 if (!validationDone) {
697 validationDone = true;
704 switch (reader.NodeType) {
705 case XmlNodeType.Element:
706 string sl = reader.GetAttribute (
708 XmlSchema.InstanceNamespace);
709 string noNsSL = reader.GetAttribute (
710 "noNamespaceSchemaLocation",
711 XmlSchema.InstanceNamespace);
712 string xsiType = reader.GetAttribute ("type",
713 XmlSchema.InstanceNamespace);
714 string xsiNil = reader.GetAttribute ("nil",
715 XmlSchema.InstanceNamespace);
717 v.ValidateElement (reader.LocalName,
725 if (reader.MoveToFirstAttribute ()) {
727 switch (reader.NamespaceURI) {
728 case XmlSchema.InstanceNamespace:
729 switch (reader.LocalName) {
730 case "schemaLocation":
731 case "noNamespaceSchemaLocation":
737 case XmlNamespaceManager.XmlnsXmlns:
740 v.ValidateAttribute (
745 } while (reader.MoveToNextAttribute ());
746 reader.MoveToElement ();
748 v.GetUnspecifiedDefaultAttributes (
749 defaultAttributesCache);
750 defaultAttributes = (XsAttr [])
751 defaultAttributesCache.ToArray (
753 v.ValidateEndOfAttributes (xsinfo);
754 defaultAttributesCache.Clear ();
756 if (reader.IsEmptyElement)
757 goto case XmlNodeType.EndElement;
759 case XmlNodeType.EndElement:
760 // FIXME: find out what another overload means.
761 v.ValidateEndElement (xsinfo);
763 case XmlNodeType.Text:
764 v.ValidateText (getter);
766 case XmlNodeType.SignificantWhitespace:
767 case XmlNodeType.Whitespace:
768 v.ValidateWhitespace (getter);
775 public override bool ReadAttributeValue ()
777 if (currentDefaultAttribute < 0)
778 return reader.ReadAttributeValue ();
780 if (this.defaultAttributeConsumed)
783 defaultAttributeConsumed = true;
787 // This class itself does not have this feature.
788 public override void ResolveEntity ()
790 reader.ResolveEntity ();
795 #region IXmlSchemaInfo
798 get { return xsinfo.IsNil; }
801 public XmlSchemaSimpleType MemberType {
802 get { return xsinfo.MemberType; }
805 public XmlSchemaAttribute SchemaAttribute {
806 get { return xsinfo.SchemaAttribute; }
809 public XmlSchemaElement SchemaElement {
810 get { return xsinfo.SchemaElement; }
813 public XmlSchemaValidity Validity {
814 get { return xsinfo.Validity; }