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 // FIXME: this implementation requires somewhat significant change
44 // to expand entities and merge sequential text and entity references
45 // especially to handle whitespace-only entities (such as bug #372839).
47 // To do it, we have to read ahead the next node when the input is
48 // text, whitespace or significant whitespace and check if the next
49 // node is EntityReference. If it is entref, then it have to merge
50 // the input entity if it is a text.
52 // This "read ahead" operation may result in proceeding to the next
53 // element, which badly affects IXmlNamespaceResolverimplementation.
54 // So we cannot fix this in simple way.
56 [PermissionSet (SecurityAction.InheritanceDemand, Unrestricted = true)]
57 public class XmlTextReader : XmlReader,
58 IXmlLineInfo, IXmlNamespaceResolver, IHasXmlParserContext
61 XmlTextReaderImpl source; // dtd2xsd expects this field's existence.
62 bool entityInsideAttribute;
64 Stack<string> entityNameStack;
66 protected XmlTextReader ()
70 public XmlTextReader (Stream input)
71 : this (new XmlStreamReader (input))
75 public XmlTextReader (string url)
76 : this(url, new NameTable ())
80 public XmlTextReader (TextReader input)
81 : this (input, new NameTable ())
85 protected XmlTextReader (XmlNameTable nt)
86 : this (String.Empty, XmlNodeType.Element, null)
90 public XmlTextReader (Stream input, XmlNameTable nt)
91 : this(new XmlStreamReader (input), nt)
95 public XmlTextReader (string url, Stream input)
96 : this (url, new XmlStreamReader (input))
100 public XmlTextReader (string url, TextReader input)
101 : this (url, input, new NameTable ())
105 public XmlTextReader (string url, XmlNameTable nt)
107 source = new XmlTextReaderImpl (url, nt);
110 public XmlTextReader (TextReader input, XmlNameTable nt)
111 : this (String.Empty, input, nt)
115 public XmlTextReader (Stream xmlFragment, XmlNodeType fragType, XmlParserContext context)
117 source = new XmlTextReaderImpl (xmlFragment, fragType, context);
120 public XmlTextReader (string url, Stream input, XmlNameTable nt)
121 : this (url, new XmlStreamReader (input), nt)
125 public XmlTextReader (string url, TextReader input, XmlNameTable nt)
127 source = new XmlTextReaderImpl (url, input, nt);
130 public XmlTextReader (string xmlFragment, XmlNodeType fragType, XmlParserContext context)
132 source = new XmlTextReaderImpl (xmlFragment, fragType, context);
135 internal XmlTextReader (string baseURI, TextReader xmlFragment, XmlNodeType fragType)
137 source = new XmlTextReaderImpl (baseURI, xmlFragment, fragType);
140 internal XmlTextReader (string baseURI, TextReader xmlFragment, XmlNodeType fragType, XmlParserContext context)
142 source = new XmlTextReaderImpl (baseURI, xmlFragment, fragType, context);
145 internal XmlTextReader (bool dummy, XmlResolver resolver, string url, XmlNodeType fragType, XmlParserContext context)
147 source = new XmlTextReaderImpl (dummy, resolver, url, fragType, context);
150 private XmlTextReader (XmlTextReaderImpl entityContainer, bool insideAttribute)
152 source = entityContainer;
153 this.entityInsideAttribute = insideAttribute;
158 private XmlReader Current {
159 get { return entity != null && entity.ReadState != ReadState.Initial ? (XmlReader) entity : source; }
162 public override int AttributeCount {
163 get { return Current.AttributeCount; }
166 public override string BaseURI {
167 get { return Current.BaseURI; }
170 public override bool CanReadBinaryContent {
174 public override bool CanReadValueChunk {
178 public override bool CanResolveEntity {
182 public override int Depth {
184 // On EndEntity, depth is the same as that
185 // of EntityReference.
186 if (entity != null && entity.ReadState == ReadState.Interactive)
187 return source.Depth + entity.Depth + 1;
193 public override bool EOF {
194 get { return source.EOF; }
197 public override bool HasValue {
198 get { return Current.HasValue; }
201 public override bool IsDefault {
202 get { return Current.IsDefault; }
205 public override bool IsEmptyElement {
206 get { return Current.IsEmptyElement; }
209 public override string LocalName {
210 get { return Current.LocalName; }
213 public override string Name {
214 get { return Current.Name; }
217 public override string NamespaceURI {
218 get { return Current.NamespaceURI; }
221 public override XmlNameTable NameTable {
222 get { return Current.NameTable; }
225 public override XmlNodeType NodeType {
228 return entity.ReadState == ReadState.Initial ?
230 entity.EOF ? XmlNodeType.EndEntity :
233 return source.NodeType;
237 internal XmlParserContext ParserContext {
238 get { return ((IHasXmlParserContext) Current).ParserContext; }
241 XmlParserContext IHasXmlParserContext.ParserContext {
242 get { return this.ParserContext; }
245 public override string Prefix {
246 get { return Current.Prefix; }
249 public override char QuoteChar {
250 get { return Current.QuoteChar; }
253 public override ReadState ReadState {
254 get { return entity != null ? ReadState.Interactive : source.ReadState; }
257 public override XmlReaderSettings Settings {
258 get { return base.Settings; }
261 public override string Value {
262 get { return Current.Value; }
265 public override string XmlLang {
266 get { return Current.XmlLang; }
269 public override XmlSpace XmlSpace {
270 get { return Current.XmlSpace; }
275 internal bool CharacterChecking {
278 return entity.CharacterChecking;
280 return source.CharacterChecking;
284 entity.CharacterChecking = value;
285 source.CharacterChecking = value;
289 internal bool CloseInput {
292 return entity.CloseInput;
294 return source.CloseInput;
298 entity.CloseInput = value;
299 source.CloseInput = value;
303 internal ConformanceLevel Conformance {
304 get { return source.Conformance; }
307 entity.Conformance = value;
308 source.Conformance = value;
312 internal XmlResolver Resolver {
313 get { return source.Resolver; }
316 private void CopyProperties (XmlTextReader other)
318 CharacterChecking = other.CharacterChecking;
319 CloseInput = other.CloseInput;
320 if (other.Settings != null)
321 Conformance = other.Settings.ConformanceLevel;
322 XmlResolver = other.Resolver;
327 public Encoding Encoding {
330 return entity.Encoding;
332 return source.Encoding;
336 public EntityHandling EntityHandling {
337 get { return source.EntityHandling; }
340 entity.EntityHandling = value;
341 source.EntityHandling = value;
345 public int LineNumber {
348 return entity.LineNumber;
350 return source.LineNumber;
354 public int LinePosition {
357 return entity.LinePosition;
359 return source.LinePosition;
363 public bool Namespaces {
364 get { return source.Namespaces; }
367 entity.Namespaces = value;
368 source.Namespaces = value;
372 public bool Normalization {
373 get { return source.Normalization; }
376 entity.Normalization = value;
377 source.Normalization = value;
381 public bool ProhibitDtd {
382 get { return source.ProhibitDtd; }
385 entity.ProhibitDtd = value;
386 source.ProhibitDtd = value;
390 public WhitespaceHandling WhitespaceHandling {
391 get { return source.WhitespaceHandling; }
394 entity.WhitespaceHandling = value;
395 source.WhitespaceHandling = value;
399 public XmlResolver XmlResolver {
402 entity.XmlResolver = value;
403 source.XmlResolver = value;
411 internal void AdjustLineInfoOffset (int lineNumberOffset, int linePositionOffset)
414 entity.AdjustLineInfoOffset (lineNumberOffset, linePositionOffset);
415 source.AdjustLineInfoOffset (lineNumberOffset, linePositionOffset);
418 internal void SetNameTable (XmlNameTable nameTable)
421 entity.SetNameTable (nameTable);
422 source.SetNameTable (nameTable);
425 internal void SkipTextDeclaration ()
428 entity.SkipTextDeclaration ();
430 source.SkipTextDeclaration ();
435 public override void Close ()
442 public override string GetAttribute (int i)
444 return Current.GetAttribute (i);
447 // MS.NET 1.0 msdn says that this method returns String.Empty
448 // for absent attribute, but in fact it returns null.
449 // This description is corrected in MS.NET 1.1 msdn.
450 public override string GetAttribute (string name)
452 return Current.GetAttribute (name);
455 public override string GetAttribute (string localName, string namespaceURI)
457 return Current.GetAttribute (localName, namespaceURI);
460 public IDictionary<string, string> GetNamespacesInScope (XmlNamespaceScope scope)
462 return ((IXmlNamespaceResolver) Current).GetNamespacesInScope (scope);
465 IDictionary<string, string> IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
467 return GetNamespacesInScope (scope);
470 public override string LookupNamespace (string prefix)
472 return Current.LookupNamespace (prefix);
475 string IXmlNamespaceResolver.LookupPrefix (string ns)
477 return ((IXmlNamespaceResolver) Current).LookupPrefix (ns);
480 public override void MoveToAttribute (int i)
482 if (entity != null && entityInsideAttribute)
484 Current.MoveToAttribute (i);
485 insideAttribute = true;
488 public override bool MoveToAttribute (string name)
490 if (entity != null && !entityInsideAttribute)
491 return entity.MoveToAttribute (name);
492 if (!source.MoveToAttribute (name))
494 if (entity != null && entityInsideAttribute)
496 insideAttribute = true;
500 public override bool MoveToAttribute (string localName, string namespaceName)
502 if (entity != null && !entityInsideAttribute)
503 return entity.MoveToAttribute (localName, namespaceName);
504 if (!source.MoveToAttribute (localName, namespaceName))
506 if (entity != null && entityInsideAttribute)
508 insideAttribute = true;
512 public override bool MoveToElement ()
514 if (entity != null && entityInsideAttribute)
516 if (!Current.MoveToElement ())
518 insideAttribute = false;
522 public override bool MoveToFirstAttribute ()
524 if (entity != null && !entityInsideAttribute)
525 return entity.MoveToFirstAttribute ();
526 if (!source.MoveToFirstAttribute ())
528 if (entity != null && entityInsideAttribute)
530 insideAttribute = true;
534 public override bool MoveToNextAttribute ()
536 if (entity != null && !entityInsideAttribute)
537 return entity.MoveToNextAttribute ();
538 if (!source.MoveToNextAttribute ())
540 if (entity != null && entityInsideAttribute)
542 insideAttribute = true;
546 public override bool Read ()
548 insideAttribute = false;
550 if (entity != null && (entityInsideAttribute || entity.EOF))
552 if (entity != null) {
555 if (EntityHandling == EntityHandling.ExpandEntities) {
556 // EndEntity must be skipped
561 return true; // either success or EndEntity
566 if (EntityHandling == EntityHandling.ExpandEntities
567 && source.NodeType == XmlNodeType.EntityReference) {
575 public override bool ReadAttributeValue ()
577 if (entity != null && entityInsideAttribute) {
582 return true; // either success or EndEntity
585 return Current.ReadAttributeValue ();
588 public override string ReadString ()
590 return base.ReadString ();
593 public void ResetState ()
597 source.ResetState ();
601 void ResolveEntity ()
604 entity.ResolveEntity ();
606 if (source.NodeType != XmlNodeType.EntityReference)
607 throw new InvalidOperationException ("The current node is not an Entity Reference");
608 XmlTextReaderImpl entReader = null;
609 if (ParserContext.Dtd != null)
610 entReader = ParserContext.Dtd.GenerateEntityContentReader (source.Name, ParserContext);
611 if (entReader == null)
612 throw new XmlException (this as IXmlLineInfo, this.BaseURI, String.Format ("Reference to undeclared entity '{0}'.", source.Name));
613 if (entityNameStack == null)
614 entityNameStack = new Stack<string> ();
615 else if (entityNameStack.Contains (Name))
616 throw new XmlException (String.Format ("General entity '{0}' has an invalid recursive reference to itself.", Name));
617 entityNameStack.Push (Name);
618 entity = new XmlTextReader (
619 entReader, insideAttribute);
620 entity.entityNameStack = entityNameStack;
621 entity.CopyProperties (this);
629 entityNameStack.Pop ();
632 public override void Skip ()
637 [MonoTODO] // FIXME: Check how expanded entity is handled here.
638 public TextReader GetRemainder ()
640 if (entity != null) {
643 entityNameStack.Pop ();
645 return source.GetRemainder ();
648 public bool HasLineInfo ()
653 [MonoTODO] // FIXME: Check how expanded entity is handled here.
654 public int ReadBase64 (byte [] buffer, int offset, int length)
657 return entity.ReadBase64 (buffer, offset, length);
659 return source.ReadBase64 (buffer, offset, length);
662 [MonoTODO] // FIXME: Check how expanded entity is handled here.
663 public int ReadBinHex (byte [] buffer, int offset, int length)
666 return entity.ReadBinHex (buffer, offset, length);
668 return source.ReadBinHex (buffer, offset, length);
671 [MonoTODO] // FIXME: Check how expanded entity is handled here.
672 public int ReadChars (char [] buffer, int offset, int length)
675 return entity.ReadChars (buffer, offset, length);
677 return source.ReadChars (buffer, offset, length);
681 [MonoTODO] // FIXME: Check how expanded entity is handled here.
682 public override int ReadContentAsBase64 (byte [] buffer, int offset, int length)
685 return entity.ReadContentAsBase64 (buffer, offset, length);
687 return source.ReadContentAsBase64 (buffer, offset, length);
690 [MonoTODO] // FIXME: Check how expanded entity is handled here.
691 public override int ReadContentAsBinHex (byte [] buffer, int offset, int length)
694 return entity.ReadContentAsBinHex (buffer, offset, length);
696 return source.ReadContentAsBinHex (buffer, offset, length);
699 [MonoTODO] // FIXME: Check how expanded entity is handled here.
700 public override int ReadElementContentAsBase64 (byte [] buffer, int offset, int length)
703 return entity.ReadElementContentAsBase64 (buffer, offset, length);
705 return source.ReadElementContentAsBase64 (buffer, offset, length);
708 [MonoTODO] // FIXME: Check how expanded entity is handled here.
709 public override int ReadElementContentAsBinHex (byte [] buffer, int offset, int length)
712 return entity.ReadElementContentAsBinHex (buffer, offset, length);
714 return source.ReadElementContentAsBinHex (buffer, offset, length);