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; }
258 public override XmlReaderSettings Settings {
259 get { return base.Settings; }
263 public override string Value {
264 get { return Current.Value; }
267 public override string XmlLang {
268 get { return Current.XmlLang; }
271 public override XmlSpace XmlSpace {
272 get { return Current.XmlSpace; }
277 internal bool CharacterChecking {
280 return entity.CharacterChecking;
282 return source.CharacterChecking;
286 entity.CharacterChecking = value;
287 source.CharacterChecking = value;
291 internal bool CloseInput {
294 return entity.CloseInput;
296 return source.CloseInput;
300 entity.CloseInput = value;
301 source.CloseInput = value;
305 internal ConformanceLevel Conformance {
306 get { return source.Conformance; }
309 entity.Conformance = value;
310 source.Conformance = value;
314 internal XmlResolver Resolver {
315 get { return source.Resolver; }
318 private void CopyProperties (XmlTextReader other)
320 CharacterChecking = other.CharacterChecking;
321 CloseInput = other.CloseInput;
322 if (other.Settings != null)
323 Conformance = other.Settings.ConformanceLevel;
324 XmlResolver = other.Resolver;
329 public Encoding Encoding {
332 return entity.Encoding;
334 return source.Encoding;
338 public EntityHandling EntityHandling {
339 get { return source.EntityHandling; }
342 entity.EntityHandling = value;
343 source.EntityHandling = value;
347 public int LineNumber {
350 return entity.LineNumber;
352 return source.LineNumber;
356 public int LinePosition {
359 return entity.LinePosition;
361 return source.LinePosition;
365 public bool Namespaces {
366 get { return source.Namespaces; }
369 entity.Namespaces = value;
370 source.Namespaces = value;
374 public bool Normalization {
375 get { return source.Normalization; }
378 entity.Normalization = value;
379 source.Normalization = value;
383 public bool ProhibitDtd {
384 get { return source.ProhibitDtd; }
387 entity.ProhibitDtd = value;
388 source.ProhibitDtd = value;
392 public WhitespaceHandling WhitespaceHandling {
393 get { return source.WhitespaceHandling; }
396 entity.WhitespaceHandling = value;
397 source.WhitespaceHandling = value;
401 public XmlResolver XmlResolver {
404 entity.XmlResolver = value;
405 source.XmlResolver = value;
413 internal void AdjustLineInfoOffset (int lineNumberOffset, int linePositionOffset)
416 entity.AdjustLineInfoOffset (lineNumberOffset, linePositionOffset);
417 source.AdjustLineInfoOffset (lineNumberOffset, linePositionOffset);
420 internal void SetNameTable (XmlNameTable nameTable)
423 entity.SetNameTable (nameTable);
424 source.SetNameTable (nameTable);
427 internal void SkipTextDeclaration ()
430 entity.SkipTextDeclaration ();
432 source.SkipTextDeclaration ();
437 public override void Close ()
444 public override string GetAttribute (int i)
446 return Current.GetAttribute (i);
449 // MS.NET 1.0 msdn says that this method returns String.Empty
450 // for absent attribute, but in fact it returns null.
451 // This description is corrected in MS.NET 1.1 msdn.
452 public override string GetAttribute (string name)
454 return Current.GetAttribute (name);
457 public override string GetAttribute (string localName, string namespaceURI)
459 return Current.GetAttribute (localName, namespaceURI);
462 public IDictionary<string, string> GetNamespacesInScope (XmlNamespaceScope scope)
464 return ((IXmlNamespaceResolver) Current).GetNamespacesInScope (scope);
467 IDictionary<string, string> IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
469 return GetNamespacesInScope (scope);
472 public override string LookupNamespace (string prefix)
474 return Current.LookupNamespace (prefix);
477 string IXmlNamespaceResolver.LookupPrefix (string ns)
479 return ((IXmlNamespaceResolver) Current).LookupPrefix (ns);
482 public override void MoveToAttribute (int i)
484 if (entity != null && entityInsideAttribute)
486 Current.MoveToAttribute (i);
487 insideAttribute = true;
490 public override bool MoveToAttribute (string name)
492 if (entity != null && !entityInsideAttribute)
493 return entity.MoveToAttribute (name);
494 if (!source.MoveToAttribute (name))
496 if (entity != null && entityInsideAttribute)
498 insideAttribute = true;
502 public override bool MoveToAttribute (string localName, string namespaceURI)
504 if (entity != null && !entityInsideAttribute)
505 return entity.MoveToAttribute (localName, namespaceURI);
506 if (!source.MoveToAttribute (localName, namespaceURI))
508 if (entity != null && entityInsideAttribute)
510 insideAttribute = true;
514 public override bool MoveToElement ()
516 if (entity != null && entityInsideAttribute)
518 if (!Current.MoveToElement ())
520 insideAttribute = false;
524 public override bool MoveToFirstAttribute ()
526 if (entity != null && !entityInsideAttribute)
527 return entity.MoveToFirstAttribute ();
528 if (!source.MoveToFirstAttribute ())
530 if (entity != null && entityInsideAttribute)
532 insideAttribute = true;
536 public override bool MoveToNextAttribute ()
538 if (entity != null && !entityInsideAttribute)
539 return entity.MoveToNextAttribute ();
540 if (!source.MoveToNextAttribute ())
542 if (entity != null && entityInsideAttribute)
544 insideAttribute = true;
548 public override bool Read ()
550 insideAttribute = false;
552 if (entity != null && (entityInsideAttribute || entity.EOF))
554 if (entity != null) {
557 if (EntityHandling == EntityHandling.ExpandEntities) {
558 // EndEntity must be skipped
563 return true; // either success or EndEntity
568 if (EntityHandling == EntityHandling.ExpandEntities
569 && source.NodeType == XmlNodeType.EntityReference) {
577 public override bool ReadAttributeValue ()
579 if (entity != null && entityInsideAttribute) {
584 return true; // either success or EndEntity
587 return Current.ReadAttributeValue ();
590 public override string ReadString ()
592 return base.ReadString ();
595 public void ResetState ()
599 source.ResetState ();
603 void ResolveEntity ()
606 entity.ResolveEntity ();
608 if (source.NodeType != XmlNodeType.EntityReference)
609 throw new InvalidOperationException ("The current node is not an Entity Reference");
610 XmlTextReaderImpl entReader = null;
611 if (ParserContext.Dtd != null)
612 entReader = ParserContext.Dtd.GenerateEntityContentReader (source.Name, ParserContext);
613 if (entReader == null)
614 throw new XmlException (this as IXmlLineInfo, this.BaseURI, String.Format ("Reference to undeclared entity '{0}'.", source.Name));
615 if (entityNameStack == null)
616 entityNameStack = new Stack<string> ();
617 else if (entityNameStack.Contains (Name))
618 throw new XmlException (String.Format ("General entity '{0}' has an invalid recursive reference to itself.", Name));
619 entityNameStack.Push (Name);
620 entity = new XmlTextReader (
621 entReader, insideAttribute);
622 entity.entityNameStack = entityNameStack;
623 entity.CopyProperties (this);
631 entityNameStack.Pop ();
634 public override void Skip ()
639 [MonoTODO] // FIXME: Check how expanded entity is handled here.
640 public TextReader GetRemainder ()
642 if (entity != null) {
645 entityNameStack.Pop ();
647 return source.GetRemainder ();
650 public bool HasLineInfo ()
655 [MonoTODO] // FIXME: Check how expanded entity is handled here.
656 public int ReadBase64 (byte [] array, int offset, int len)
659 return entity.ReadBase64 (array, offset, len);
661 return source.ReadBase64 (array, offset, len);
664 [MonoTODO] // FIXME: Check how expanded entity is handled here.
665 public int ReadBinHex (byte [] array, int offset, int len)
668 return entity.ReadBinHex (array, offset, len);
670 return source.ReadBinHex (array, offset, len);
673 [MonoTODO] // FIXME: Check how expanded entity is handled here.
674 public int ReadChars (char [] buffer, int index, int count)
677 return entity.ReadChars (buffer, index, count);
679 return source.ReadChars (buffer, index, count);
683 [MonoTODO] // FIXME: Check how expanded entity is handled here.
684 public override int ReadContentAsBase64 (byte [] buffer, int index, int count)
687 return entity.ReadContentAsBase64 (buffer, index, count);
689 return source.ReadContentAsBase64 (buffer, index, count);
692 [MonoTODO] // FIXME: Check how expanded entity is handled here.
693 public override int ReadContentAsBinHex (byte [] buffer, int index, int count)
696 return entity.ReadContentAsBinHex (buffer, index, count);
698 return source.ReadContentAsBinHex (buffer, index, count);
701 [MonoTODO] // FIXME: Check how expanded entity is handled here.
702 public override int ReadElementContentAsBase64 (byte [] buffer, int index, int count)
705 return entity.ReadElementContentAsBase64 (buffer, index, count);
707 return source.ReadElementContentAsBase64 (buffer, index, count);
710 [MonoTODO] // FIXME: Check how expanded entity is handled here.
711 public override int ReadElementContentAsBinHex (byte [] buffer, int index, int count)
714 return entity.ReadElementContentAsBinHex (buffer, index, count);
716 return source.ReadElementContentAsBinHex (buffer, index, count);