2 // System.Xml.XmlTextReader2.cs - XmlTextReader for .NET 2.0
5 // Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
7 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using XmlTextReaderImpl = Mono.Xml2.XmlTextReader;
33 using System.Collections.Generic;
34 using System.Globalization;
36 using System.Security.Permissions;
38 using System.Xml.Schema;
43 [PermissionSet (SecurityAction.InheritanceDemand, Unrestricted = true)]
44 public class XmlTextReader : XmlReader,
45 IXmlLineInfo, IXmlNamespaceResolver, IHasXmlParserContext
48 XmlTextReaderImpl source;
49 bool entityInsideAttribute;
51 string cachedAttributeValue;
52 bool attributeValueConsumed;
54 protected XmlTextReader ()
58 public XmlTextReader (Stream input)
59 : this (new XmlStreamReader (input))
63 public XmlTextReader (string url)
64 : this(url, new NameTable ())
68 public XmlTextReader (TextReader input)
69 : this (input, new NameTable ())
73 protected XmlTextReader (XmlNameTable nt)
74 : this (String.Empty, XmlNodeType.Element, null)
78 public XmlTextReader (Stream input, XmlNameTable nt)
79 : this(new XmlStreamReader (input), nt)
83 public XmlTextReader (string url, Stream input)
84 : this (url, new XmlStreamReader (input))
88 public XmlTextReader (string url, TextReader input)
89 : this (url, input, new NameTable ())
93 public XmlTextReader (string url, XmlNameTable nt)
95 source = new XmlTextReaderImpl (url, nt);
98 public XmlTextReader (TextReader input, XmlNameTable nt)
99 : this (String.Empty, input, nt)
103 public XmlTextReader (Stream xmlFragment, XmlNodeType fragType, XmlParserContext context)
105 source = new XmlTextReaderImpl (xmlFragment, fragType, context);
108 public XmlTextReader (string url, Stream input, XmlNameTable nt)
109 : this (url, new XmlStreamReader (input), nt)
113 public XmlTextReader (string url, TextReader input, XmlNameTable nt)
115 source = new XmlTextReaderImpl (url, input, nt);
118 public XmlTextReader (string xmlFragment, XmlNodeType fragType, XmlParserContext context)
120 source = new XmlTextReaderImpl (xmlFragment, fragType, context);
123 internal XmlTextReader (string baseURI, TextReader xmlFragment, XmlNodeType fragType)
125 source = new XmlTextReaderImpl (baseURI, xmlFragment, fragType);
128 internal XmlTextReader (string baseURI, TextReader xmlFragment, XmlNodeType fragType, XmlParserContext context)
130 source = new XmlTextReaderImpl (baseURI, xmlFragment, fragType, context);
133 internal XmlTextReader (bool dummy, string url, XmlNodeType fragType, XmlParserContext context)
135 source = new XmlTextReaderImpl (dummy, url, fragType, context);
138 private XmlTextReader (XmlTextReaderImpl entityContainer, bool insideAttribute)
140 source = entityContainer;
141 this.entityInsideAttribute = insideAttribute;
146 private XmlReader Current {
147 get { return entity != null && entity.ReadState != ReadState.Initial ? (XmlReader) entity : source; }
150 public override int AttributeCount {
151 get { return Current.AttributeCount; }
154 public override string BaseURI {
155 get { return Current.BaseURI; }
158 public override bool CanReadBinaryContent {
162 public override bool CanReadValueChunk {
166 public override bool CanResolveEntity {
170 public override int Depth {
172 // On EndEntity, depth is the same as that
173 // of EntityReference.
174 if (entity != null && entity.ReadState == ReadState.Interactive)
175 return source.Depth + entity.Depth + 1;
181 public override bool EOF {
182 get { return source.EOF; }
185 public override bool HasValue {
186 get { return Current.HasValue; }
189 public override bool IsDefault {
190 get { return Current.IsDefault; }
193 public override bool IsEmptyElement {
194 get { return Current.IsEmptyElement; }
197 public override string LocalName {
198 get { return Current.LocalName; }
201 public override string Name {
202 get { return Current.Name; }
205 public override string NamespaceURI {
206 get { return Current.NamespaceURI; }
209 public override XmlNameTable NameTable {
210 get { return Current.NameTable; }
213 public override XmlNodeType NodeType {
215 if (Current == entity)
216 return entity.EOF ? XmlNodeType.EndEntity : entity.NodeType;
218 return source.NodeType;
222 internal XmlParserContext ParserContext {
223 get { return ((IHasXmlParserContext) Current).ParserContext; }
226 XmlParserContext IHasXmlParserContext.ParserContext {
227 get { return this.ParserContext; }
230 public override string Prefix {
231 get { return Current.Prefix; }
234 public override char QuoteChar {
235 get { return Current.QuoteChar; }
238 public override ReadState ReadState {
239 get { return entity != null ? ReadState.Interactive : source.ReadState; }
242 public override XmlReaderSettings Settings {
243 get { return base.Settings; }
246 public override string Value {
247 get { return Current.Value; }
250 public override string XmlLang {
251 get { return Current.XmlLang; }
254 public override XmlSpace XmlSpace {
255 get { return Current.XmlSpace; }
260 internal bool CharacterChecking {
263 return entity.CharacterChecking;
265 return source.CharacterChecking;
269 entity.CharacterChecking = value;
270 source.CharacterChecking = value;
274 internal bool CloseInput {
277 return entity.CloseInput;
279 return source.CloseInput;
283 entity.CloseInput = value;
284 source.CloseInput = value;
288 internal ConformanceLevel Conformance {
291 entity.Conformance = value;
292 source.Conformance = value;
296 internal XmlResolver Resolver {
297 get { return source.Resolver; }
300 private void CopyProperties (XmlTextReader other)
302 CharacterChecking = other.CharacterChecking;
303 CloseInput = other.CloseInput;
304 if (other.Settings != null)
305 Conformance = other.Settings.ConformanceLevel;
306 XmlResolver = other.Resolver;
311 public Encoding Encoding {
314 return entity.Encoding;
316 return source.Encoding;
320 public EntityHandling EntityHandling {
321 get { return source.EntityHandling; }
324 entity.EntityHandling = value;
325 source.EntityHandling = value;
329 public int LineNumber {
332 return entity.LineNumber;
334 return source.LineNumber;
338 public int LinePosition {
341 return entity.LinePosition;
343 return source.LinePosition;
347 public bool Namespaces {
348 get { return source.Namespaces; }
351 entity.Namespaces = value;
352 source.Namespaces = value;
356 public bool Normalization {
357 get { return source.Normalization; }
360 entity.Normalization = value;
361 source.Normalization = value;
365 public bool ProhibitDtd {
366 get { return source.ProhibitDtd; }
369 entity.ProhibitDtd = value;
370 source.ProhibitDtd = value;
374 public WhitespaceHandling WhitespaceHandling {
375 get { return source.WhitespaceHandling; }
378 entity.WhitespaceHandling = value;
379 source.WhitespaceHandling = value;
383 public XmlResolver XmlResolver {
386 entity.XmlResolver = value;
387 source.XmlResolver = value;
395 internal void AdjustLineInfoOffset (int lineNumberOffset, int linePositionOffset)
398 entity.AdjustLineInfoOffset (lineNumberOffset, linePositionOffset);
399 source.AdjustLineInfoOffset (lineNumberOffset, linePositionOffset);
402 internal void SetNameTable (XmlNameTable nameTable)
405 entity.SetNameTable (nameTable);
406 source.SetNameTable (nameTable);
411 public override void Close ()
418 public override string GetAttribute (int i)
420 return Current.GetAttribute (i);
423 // MS.NET 1.0 msdn says that this method returns String.Empty
424 // for absent attribute, but in fact it returns null.
425 // This description is corrected in MS.NET 1.1 msdn.
426 public override string GetAttribute (string name)
428 return Current.GetAttribute (name);
431 public override string GetAttribute (string localName, string namespaceURI)
433 return Current.GetAttribute (localName, namespaceURI);
436 public IDictionary<string, string> GetNamespacesInScope (XmlNamespaceScope scope)
438 return ((IXmlNamespaceResolver) Current).GetNamespacesInScope (scope);
441 IDictionary<string, string> IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
443 return GetNamespacesInScope (scope);
446 public override string LookupNamespace (string prefix)
448 return Current.LookupNamespace (prefix);
451 string IXmlNamespaceResolver.LookupPrefix (string ns)
453 return ((IXmlNamespaceResolver) Current).LookupPrefix (ns);
456 public override void MoveToAttribute (int i)
458 if (entity != null && entityInsideAttribute) {
462 Current.MoveToAttribute (i);
463 insideAttribute = true;
466 public override bool MoveToAttribute (string name)
468 if (entity != null && !entityInsideAttribute)
469 return entity.MoveToAttribute (name);
470 if (!source.MoveToAttribute (name))
472 if (entity != null && entityInsideAttribute) {
476 insideAttribute = true;
480 public override bool MoveToAttribute (string localName, string namespaceName)
482 if (entity != null && !entityInsideAttribute)
483 return entity.MoveToAttribute (localName, namespaceName);
484 if (!source.MoveToAttribute (localName, namespaceName))
486 if (entity != null && entityInsideAttribute) {
490 insideAttribute = true;
494 public override bool MoveToElement ()
496 if (entity != null && entityInsideAttribute) {
500 if (!Current.MoveToElement ())
502 insideAttribute = false;
506 public override bool MoveToFirstAttribute ()
508 if (entity != null && !entityInsideAttribute)
509 return entity.MoveToFirstAttribute ();
510 if (!source.MoveToFirstAttribute ())
512 if (entity != null && entityInsideAttribute) {
516 insideAttribute = true;
520 public override bool MoveToNextAttribute ()
522 if (entity != null && !entityInsideAttribute)
523 return entity.MoveToNextAttribute ();
524 if (!source.MoveToNextAttribute ())
526 if (entity != null && entityInsideAttribute) {
530 insideAttribute = true;
534 public override bool Read ()
536 insideAttribute = false;
538 if (entity != null && (entityInsideAttribute || entity.EOF)) {
542 if (entity != null) {
545 if (EntityHandling == EntityHandling.ExpandEntities) {
546 // EndEntity must be skipped
552 return true; // either success or EndEntity
557 if (EntityHandling == EntityHandling.ExpandEntities
558 && source.NodeType == XmlNodeType.EntityReference) {
566 public override bool ReadAttributeValue ()
568 if (entity != null && entityInsideAttribute) {
575 return true; // either success or EndEntity
578 return Current.ReadAttributeValue ();
581 public override string ReadString ()
583 return base.ReadString ();
586 public void ResetState ()
589 entity.ResetState ();
590 source.ResetState ();
593 public override void ResolveEntity ()
596 entity.ResolveEntity ();
598 if (source.NodeType != XmlNodeType.EntityReference)
599 throw new InvalidOperationException ("The current node is not an Entity Reference");
600 XmlTextReaderImpl entReader =
601 ParserContext.Dtd.GenerateEntityContentReader (source.Name, ParserContext);
602 if (entReader == null)
603 throw new XmlException (this as IXmlLineInfo, this.BaseURI, String.Format ("Reference to undeclared entity '{0}'.", source.Name));
604 entity = new XmlTextReader (
605 entReader, insideAttribute);
606 entity.CopyProperties (this);
610 public override void Skip ()
615 [MonoTODO ("Check how expanded entity is handled here.")]
616 public TextReader GetRemainder ()
618 if (entity != null) {
622 return source.GetRemainder ();
625 public bool HasLineInfo ()
630 [MonoTODO ("Check how expanded entity is handled here.")]
631 public int ReadBase64 (byte [] buffer, int offset, int length)
634 return entity.ReadBase64 (buffer, offset, length);
636 return source.ReadBase64 (buffer, offset, length);
639 [MonoTODO ("Check how expanded entity is handled here.")]
640 public int ReadBinHex (byte [] buffer, int offset, int length)
643 return entity.ReadBinHex (buffer, offset, length);
645 return source.ReadBinHex (buffer, offset, length);
648 [MonoTODO ("Check how expanded entity is handled here.")]
649 public int ReadChars (char [] buffer, int offset, int length)
652 return entity.ReadChars (buffer, offset, length);
654 return source.ReadChars (buffer, offset, length);
658 [MonoTODO ("Check how expanded entity is handled here.")]
659 public override int ReadContentAsBase64 (byte [] buffer, int offset, int length)
662 return entity.ReadContentAsBase64 (buffer, offset, length);
664 return source.ReadContentAsBase64 (buffer, offset, length);
667 [MonoTODO ("Check how expanded entity is handled here.")]
668 public override int ReadContentAsBinHex (byte [] buffer, int offset, int length)
671 return entity.ReadContentAsBinHex (buffer, offset, length);
673 return source.ReadContentAsBinHex (buffer, offset, length);
676 [MonoTODO ("Check how expanded entity is handled here.")]
677 public override int ReadElementContentAsBase64 (byte [] buffer, int offset, int length)
680 return entity.ReadElementContentAsBase64 (buffer, offset, length);
682 return source.ReadElementContentAsBase64 (buffer, offset, length);
685 [MonoTODO ("Check how expanded entity is handled here.")]
686 public override int ReadElementContentAsBinHex (byte [] buffer, int offset, int length)
689 return entity.ReadElementContentAsBinHex (buffer, offset, length);
691 return source.ReadElementContentAsBinHex (buffer, offset, length);