2 // System.Xml.XmlValidatingReader.cs
5 // Tim Coleman (tim@timcoleman.com)
6 // Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
8 // Copyright (C) Tim Coleman, 2002
9 // (C)2003 Atsushi Enomoto
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections.Generic;
36 using System.Security.Permissions;
38 using System.Xml.Schema;
40 using Mono.Xml.Schema;
44 [PermissionSet (SecurityAction.InheritanceDemand, Unrestricted = true)]
46 [Obsolete("Use XmlReader created by XmlReader.Create() method using"
47 + " appropriate XmlReaderSettings instead.")]
48 public class XmlValidatingReader : XmlReader, IXmlLineInfo, IXmlNamespaceResolver, IHasXmlParserContext
50 public class XmlValidatingReader : XmlReader, IXmlLineInfo, IHasXmlParserContext
56 EntityHandling entityHandling;
57 XmlReader sourceReader;
58 XmlTextReader xmlTextReader;
59 XmlReader validatingReader;
60 XmlResolver resolver; // Only used to non-XmlTextReader XmlReader
61 bool resolverSpecified;
62 ValidationType validationType;
63 // for 2.0: Now it is obsolete. It is allocated only when it is required
64 XmlSchemaCollection schemas;
65 DTDValidatingReader dtdReader;
66 IHasXmlSchemaInfo schemaInfo;
67 StringBuilder storedCharacters;
73 public XmlValidatingReader (XmlReader reader)
75 sourceReader = reader;
76 xmlTextReader = reader as XmlTextReader;
77 if (xmlTextReader == null)
78 resolver = new XmlUrlResolver ();
79 entityHandling = EntityHandling.ExpandEntities;
80 validationType = ValidationType.Auto;
81 storedCharacters = new StringBuilder ();
84 public XmlValidatingReader (Stream xmlFragment, XmlNodeType fragType, XmlParserContext context)
85 : this (new XmlTextReader (xmlFragment, fragType, context))
89 public XmlValidatingReader (string xmlFragment, XmlNodeType fragType, XmlParserContext context)
90 : this (new XmlTextReader (xmlFragment, fragType, context))
94 #endregion // Constructors
98 public override int AttributeCount {
99 get { return validatingReader == null ? 0 : validatingReader.AttributeCount; }
102 public override string BaseURI {
103 get { return validatingReader == null ? sourceReader.BaseURI : validatingReader.BaseURI; }
107 public override bool CanReadBinaryContent {
112 // This property for this class always return true.
113 public override bool CanResolveEntity {
117 public override int Depth {
118 get { return validatingReader == null ? 0 : validatingReader.Depth; }
121 public Encoding Encoding {
123 if (xmlTextReader != null)
124 return xmlTextReader.Encoding;
126 throw new NotSupportedException ("Encoding is supported only for XmlTextReader.");
130 public EntityHandling EntityHandling {
131 get { return entityHandling; }
133 entityHandling = value;
134 if (dtdReader != null)
135 dtdReader.EntityHandling = value;
139 public override bool EOF {
140 get { return validatingReader == null ? false : validatingReader.EOF; }
143 #if DTD_HANDLE_EVENTS
144 internal bool HasValidationEvent {
145 get { return ValidationEventHandler != null; }
149 public override bool HasValue {
150 get { return validatingReader == null ? false : validatingReader.HasValue; }
153 public override bool IsDefault {
154 get { return validatingReader == null ? false : validatingReader.IsDefault; }
157 public override bool IsEmptyElement {
158 get { return validatingReader == null ? false : validatingReader.IsEmptyElement; }
163 public override string this [int i] {
164 get { return GetAttribute (i); }
167 public override string this [string name] {
168 get { return GetAttribute (name); }
171 public override string this [string localName, string namespaceName] {
172 get { return GetAttribute (localName, namespaceName); }
177 public int LineNumber {
179 int IXmlLineInfo.LineNumber {
184 IXmlLineInfo info = validatingReader as IXmlLineInfo;
185 return info != null ? info.LineNumber : 0;
190 public int LinePosition {
192 int IXmlLineInfo.LinePosition {
197 IXmlLineInfo info = validatingReader as IXmlLineInfo;
198 return info != null ? info.LinePosition : 0;
202 public override string LocalName {
204 if (validatingReader == null)
207 return validatingReader.LocalName;
209 return validatingReader.Name;
213 public override string Name {
214 get { return validatingReader == null ? String.Empty : validatingReader.Name; }
217 public bool Namespaces {
219 if (xmlTextReader != null)
220 return xmlTextReader.Namespaces;
225 if (ReadState != ReadState.Initial)
226 throw new InvalidOperationException ("Namespaces have to be set before reading.");
228 if (xmlTextReader != null)
229 xmlTextReader.Namespaces = value;
231 throw new NotSupportedException ("Property 'Namespaces' is supported only for XmlTextReader.");
235 public override string NamespaceURI {
237 if (validatingReader == null)
240 return validatingReader.NamespaceURI;
246 public override XmlNameTable NameTable {
247 get { return validatingReader == null ? sourceReader.NameTable : validatingReader.NameTable; }
250 public override XmlNodeType NodeType {
251 get { return validatingReader == null ? XmlNodeType.None : validatingReader.NodeType; }
254 public override string Prefix {
255 get { return validatingReader == null ? String.Empty : validatingReader.Prefix; }
258 public override char QuoteChar {
259 get { return validatingReader == null ? sourceReader.QuoteChar : validatingReader.QuoteChar; }
262 public XmlReader Reader {
263 get { return sourceReader; }
266 public override ReadState ReadState {
268 if (validatingReader == null)
269 return ReadState.Initial;
270 return validatingReader.ReadState;
274 internal XmlResolver Resolver {
276 // This is special rule... MS.NET shares the
277 // XmlResolver between XmlTextReader and
278 // XmlValidatingReader, so we mimick that
279 // silly behavior here.
280 if (this.xmlTextReader != null)
281 return this.xmlTextReader.Resolver;
282 else if (resolverSpecified)
289 public XmlSchemaCollection Schemas {
292 schemas = new XmlSchemaCollection (NameTable);
297 public object SchemaType {
298 get { return schemaInfo.SchemaType; }
303 public override XmlReaderSettings Settings {
304 get { return validatingReader == null ? sourceReader.Settings : validatingReader.Settings; }
309 // We decided not to support XDR schema; it is obsolete.
310 public ValidationType ValidationType {
311 get { return validationType; }
313 if (ReadState != ReadState.Initial)
314 throw new InvalidOperationException ("ValidationType cannot be set after the first call to Read method.");
315 switch (validationType) {
316 case ValidationType.Auto:
317 case ValidationType.DTD:
318 case ValidationType.None:
319 case ValidationType.Schema:
320 validationType = value;
322 case ValidationType.XDR:
323 throw new NotSupportedException ();
328 public override string Value {
329 get { return validatingReader == null ? String.Empty : validatingReader.Value; }
332 public override string XmlLang {
333 get { return validatingReader == null ? String.Empty : validatingReader.XmlLang; }
336 public XmlResolver XmlResolver {
338 resolverSpecified = true;
340 if (xmlTextReader != null)
341 xmlTextReader.XmlResolver = value;
343 XsdValidatingReader xsvr = validatingReader as XsdValidatingReader;
345 xsvr.XmlResolver = value;
346 DTDValidatingReader dvr = validatingReader as DTDValidatingReader;
348 dvr.XmlResolver = value;
352 public override XmlSpace XmlSpace {
353 get { return validatingReader == null ? XmlSpace.None : validatingReader.XmlSpace; }
356 #endregion // Properties
360 public override void Close ()
362 if (validatingReader == null)
363 sourceReader.Close ();
365 validatingReader.Close ();
368 public override string GetAttribute (int i)
370 if (validatingReader == null)
371 throw new IndexOutOfRangeException ("Reader is not started.");
372 return validatingReader [i];
375 public override string GetAttribute (string name)
377 return validatingReader == null ? null : validatingReader [name];
380 public override string GetAttribute (string localName, string namespaceURI)
382 return validatingReader == null ? null : validatingReader [localName, namespaceURI];
385 XmlParserContext IHasXmlParserContext.ParserContext {
387 if (dtdReader != null)
388 return dtdReader.ParserContext;
389 IHasXmlParserContext i = sourceReader as IHasXmlParserContext;
390 return i != null ? i.ParserContext : null;
395 IDictionary<string, string> IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
397 return ((IHasXmlParserContext) this).ParserContext.NamespaceManager.GetNamespacesInScope (scope);
402 public bool HasLineInfo ()
404 bool IXmlLineInfo.HasLineInfo ()
407 IXmlLineInfo info = validatingReader as IXmlLineInfo;
408 return info != null ? info.HasLineInfo () : false;
411 public override string LookupNamespace (string prefix)
413 if (validatingReader != null)
414 return validatingReader.LookupNamespace (prefix);
416 return sourceReader.LookupNamespace (prefix);
420 string IXmlNamespaceResolver.LookupPrefix (string ns)
422 IXmlNamespaceResolver res = null;
423 if (validatingReader != null)
424 res = sourceReader as IXmlNamespaceResolver;
426 res = validatingReader as IXmlNamespaceResolver;
428 res.LookupNamespace (ns) :
434 public override void MoveToAttribute (int i)
436 if (validatingReader == null)
437 throw new IndexOutOfRangeException ("Reader is not started.");
439 validatingReader.MoveToAttribute (i);
442 public override bool MoveToAttribute (string name)
444 if (validatingReader == null)
446 return validatingReader.MoveToAttribute (name);
449 public override bool MoveToAttribute (string localName, string namespaceURI)
451 if (validatingReader == null)
453 return validatingReader.MoveToAttribute (localName, namespaceURI);
456 public override bool MoveToElement ()
458 if (validatingReader == null)
460 return validatingReader.MoveToElement ();
463 public override bool MoveToFirstAttribute ()
465 if (validatingReader == null)
467 return validatingReader.MoveToFirstAttribute ();
470 public override bool MoveToNextAttribute ()
472 if (validatingReader == null)
474 return validatingReader.MoveToNextAttribute ();
478 // We decided not to support XDR schema; it is obsolete.
479 public override bool Read ()
481 if (validatingReader == null) {
482 switch (ValidationType) {
483 case ValidationType.Auto:
484 case ValidationType.None:
485 goto case ValidationType.Schema; // might be specified by xsi:schemaLocation.
486 case ValidationType.DTD:
487 validatingReader = dtdReader = new DTDValidatingReader (sourceReader, this);
488 dtdReader.XmlResolver = Resolver;
490 case ValidationType.Schema:
491 dtdReader = new DTDValidatingReader (sourceReader, this);
492 XsdValidatingReader xsvr = new XsdValidatingReader (dtdReader);
493 xsvr.ValidationEventHandler +=
494 new ValidationEventHandler (
496 xsvr.ValidationType = ValidationType;
497 xsvr.Schemas = Schemas.SchemaSet;
498 xsvr.XmlResolver = Resolver;
499 validatingReader = xsvr;
500 dtdReader.XmlResolver = Resolver;
502 case ValidationType.XDR:
503 throw new NotSupportedException ();
505 schemaInfo = validatingReader as IHasXmlSchemaInfo;
507 return validatingReader.Read ();
510 public override bool ReadAttributeValue ()
512 if (validatingReader == null)
514 return validatingReader.ReadAttributeValue ();
517 public override string ReadString ()
519 return base.ReadString ();
522 public object ReadTypedValue ()
524 if (dtdReader == null)
526 XmlSchemaDatatype dt = schemaInfo.SchemaType as XmlSchemaDatatype;
528 XmlSchemaType st = schemaInfo.SchemaType as XmlSchemaType;
535 case XmlNodeType.Element:
539 storedCharacters.Length = 0;
544 case XmlNodeType.Whitespace:
545 case XmlNodeType.SignificantWhitespace:
546 case XmlNodeType.Text:
547 case XmlNodeType.CDATA:
548 storedCharacters.Append (Value);
550 case XmlNodeType.Comment:
556 } while (loop && !EOF);
557 return dt.ParseValue (storedCharacters.ToString (), NameTable, dtdReader.ParserContext.NamespaceManager);
558 case XmlNodeType.Attribute:
559 return dt.ParseValue (Value, NameTable, dtdReader.ParserContext.NamespaceManager);
564 public override void ResolveEntity ()
566 validatingReader.ResolveEntity ();
569 // It should be "protected" as usual "event model"
570 // methods are, but validation event is not exposed,
571 // so it is no other way to make it "internal".
572 internal void OnValidationEvent (object o, ValidationEventArgs e)
574 if (ValidationEventHandler != null)
575 ValidationEventHandler (o, e);
576 else if (ValidationType != ValidationType.None && e.Severity == XmlSeverityType.Error)
581 [MonoTODO] // FIXME: Check how expanded entity is handled here.
582 public override int ReadContentAsBase64 (byte [] buffer, int index, int count)
584 if (validatingReader != null)
585 return validatingReader.ReadContentAsBase64 (buffer, index, count);
587 return sourceReader.ReadContentAsBase64 (buffer, index, count);
590 [MonoTODO] // FIXME: Check how expanded entity is handled here.
591 public override int ReadContentAsBinHex (byte [] buffer, int index, int count)
593 if (validatingReader != null)
594 return validatingReader.ReadContentAsBinHex (buffer, index, count);
596 return sourceReader.ReadContentAsBinHex (buffer, index, count);
599 [MonoTODO] // FIXME: Check how expanded entity is handled here.
600 public override int ReadElementContentAsBase64 (byte [] buffer, int index, int count)
602 if (validatingReader != null)
603 return validatingReader.ReadElementContentAsBase64 (buffer, index, count);
605 return sourceReader.ReadElementContentAsBase64 (buffer, index, count);
608 [MonoTODO] // FIXME: Check how expanded entity is handled here.
609 public override int ReadElementContentAsBinHex (byte [] buffer, int index, int count)
611 if (validatingReader != null)
612 return validatingReader.ReadElementContentAsBinHex (buffer, index, count);
614 return sourceReader.ReadElementContentAsBinHex (buffer, index, count);
617 #endregion // Methods
619 #region Events and Delegates
621 public event ValidationEventHandler ValidationEventHandler;
623 #endregion // Events and Delegates