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; }
259 public DtdProcessing DtdProcessing { get; set; }
263 public override XmlReaderSettings Settings {
264 get { return base.Settings; }
268 public override string Value {
269 get { return Current.Value; }
272 public override string XmlLang {
273 get { return Current.XmlLang; }
276 public override XmlSpace XmlSpace {
277 get { return Current.XmlSpace; }
282 internal bool CharacterChecking {
285 return entity.CharacterChecking;
287 return source.CharacterChecking;
291 entity.CharacterChecking = value;
292 source.CharacterChecking = value;
296 internal bool CloseInput {
299 return entity.CloseInput;
301 return source.CloseInput;
305 entity.CloseInput = value;
306 source.CloseInput = value;
310 internal ConformanceLevel Conformance {
311 get { return source.Conformance; }
314 entity.Conformance = value;
315 source.Conformance = value;
319 internal XmlResolver Resolver {
320 get { return source.Resolver; }
323 private void CopyProperties (XmlTextReader other)
325 CharacterChecking = other.CharacterChecking;
326 CloseInput = other.CloseInput;
327 if (other.Settings != null)
328 Conformance = other.Settings.ConformanceLevel;
329 XmlResolver = other.Resolver;
334 public Encoding Encoding {
337 return entity.Encoding;
339 return source.Encoding;
343 public EntityHandling EntityHandling {
344 get { return source.EntityHandling; }
347 entity.EntityHandling = value;
348 source.EntityHandling = value;
352 public int LineNumber {
355 return entity.LineNumber;
357 return source.LineNumber;
361 public int LinePosition {
364 return entity.LinePosition;
366 return source.LinePosition;
370 public bool Namespaces {
371 get { return source.Namespaces; }
374 entity.Namespaces = value;
375 source.Namespaces = value;
379 public bool Normalization {
380 get { return source.Normalization; }
383 entity.Normalization = value;
384 source.Normalization = value;
388 public bool ProhibitDtd {
389 get { return source.ProhibitDtd; }
392 entity.ProhibitDtd = value;
393 source.ProhibitDtd = value;
397 public WhitespaceHandling WhitespaceHandling {
398 get { return source.WhitespaceHandling; }
401 entity.WhitespaceHandling = value;
402 source.WhitespaceHandling = value;
406 public XmlResolver XmlResolver {
409 entity.XmlResolver = value;
410 source.XmlResolver = value;
418 internal void AdjustLineInfoOffset (int lineNumberOffset, int linePositionOffset)
421 entity.AdjustLineInfoOffset (lineNumberOffset, linePositionOffset);
422 source.AdjustLineInfoOffset (lineNumberOffset, linePositionOffset);
425 internal void SetNameTable (XmlNameTable nameTable)
428 entity.SetNameTable (nameTable);
429 source.SetNameTable (nameTable);
432 internal void SkipTextDeclaration ()
435 entity.SkipTextDeclaration ();
437 source.SkipTextDeclaration ();
442 public override void Close ()
449 public override string GetAttribute (int i)
451 return Current.GetAttribute (i);
454 // MS.NET 1.0 msdn says that this method returns String.Empty
455 // for absent attribute, but in fact it returns null.
456 // This description is corrected in MS.NET 1.1 msdn.
457 public override string GetAttribute (string name)
459 return Current.GetAttribute (name);
462 public override string GetAttribute (string localName, string namespaceURI)
464 return Current.GetAttribute (localName, namespaceURI);
467 public IDictionary<string, string> GetNamespacesInScope (XmlNamespaceScope scope)
469 return ((IXmlNamespaceResolver) Current).GetNamespacesInScope (scope);
472 IDictionary<string, string> IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
474 return GetNamespacesInScope (scope);
477 public override string LookupNamespace (string prefix)
479 return Current.LookupNamespace (prefix);
482 string IXmlNamespaceResolver.LookupPrefix (string ns)
484 return ((IXmlNamespaceResolver) Current).LookupPrefix (ns);
487 public override void MoveToAttribute (int i)
489 if (entity != null && entityInsideAttribute)
491 Current.MoveToAttribute (i);
492 insideAttribute = true;
495 public override bool MoveToAttribute (string name)
497 if (entity != null && !entityInsideAttribute)
498 return entity.MoveToAttribute (name);
499 if (!source.MoveToAttribute (name))
501 if (entity != null && entityInsideAttribute)
503 insideAttribute = true;
507 public override bool MoveToAttribute (string localName, string namespaceURI)
509 if (entity != null && !entityInsideAttribute)
510 return entity.MoveToAttribute (localName, namespaceURI);
511 if (!source.MoveToAttribute (localName, namespaceURI))
513 if (entity != null && entityInsideAttribute)
515 insideAttribute = true;
519 public override bool MoveToElement ()
521 if (entity != null && entityInsideAttribute)
523 if (!Current.MoveToElement ())
525 insideAttribute = false;
529 public override bool MoveToFirstAttribute ()
531 if (entity != null && !entityInsideAttribute)
532 return entity.MoveToFirstAttribute ();
533 if (!source.MoveToFirstAttribute ())
535 if (entity != null && entityInsideAttribute)
537 insideAttribute = true;
541 public override bool MoveToNextAttribute ()
543 if (entity != null && !entityInsideAttribute)
544 return entity.MoveToNextAttribute ();
545 if (!source.MoveToNextAttribute ())
547 if (entity != null && entityInsideAttribute)
549 insideAttribute = true;
553 public override bool Read ()
555 insideAttribute = false;
557 if (entity != null && (entityInsideAttribute || entity.EOF))
559 if (entity != null) {
562 if (EntityHandling == EntityHandling.ExpandEntities) {
563 // EndEntity must be skipped
568 return true; // either success or EndEntity
573 if (EntityHandling == EntityHandling.ExpandEntities
574 && source.NodeType == XmlNodeType.EntityReference) {
582 public override bool ReadAttributeValue ()
584 if (entity != null && entityInsideAttribute) {
589 return true; // either success or EndEntity
592 return Current.ReadAttributeValue ();
595 public override string ReadString ()
597 return base.ReadString ();
600 public void ResetState ()
604 source.ResetState ();
608 void ResolveEntity ()
611 entity.ResolveEntity ();
613 if (source.NodeType != XmlNodeType.EntityReference)
614 throw new InvalidOperationException ("The current node is not an Entity Reference");
615 XmlTextReaderImpl entReader = null;
616 if (ParserContext.Dtd != null)
617 entReader = ParserContext.Dtd.GenerateEntityContentReader (source.Name, ParserContext);
618 if (entReader == null)
619 throw new XmlException (this as IXmlLineInfo, this.BaseURI, String.Format ("Reference to undeclared entity '{0}'.", source.Name));
620 if (entityNameStack == null)
621 entityNameStack = new Stack<string> ();
622 else if (entityNameStack.Contains (Name))
623 throw new XmlException (String.Format ("General entity '{0}' has an invalid recursive reference to itself.", Name));
624 entityNameStack.Push (Name);
625 entity = new XmlTextReader (
626 entReader, insideAttribute);
627 entity.entityNameStack = entityNameStack;
628 entity.CopyProperties (this);
636 entityNameStack.Pop ();
639 public override void Skip ()
644 [MonoTODO] // FIXME: Check how expanded entity is handled here.
645 public TextReader GetRemainder ()
647 if (entity != null) {
650 entityNameStack.Pop ();
652 return source.GetRemainder ();
655 public bool HasLineInfo ()
660 [MonoTODO] // FIXME: Check how expanded entity is handled here.
661 public int ReadBase64 (byte [] array, int offset, int len)
664 return entity.ReadBase64 (array, offset, len);
666 return source.ReadBase64 (array, offset, len);
669 [MonoTODO] // FIXME: Check how expanded entity is handled here.
670 public int ReadBinHex (byte [] array, int offset, int len)
673 return entity.ReadBinHex (array, offset, len);
675 return source.ReadBinHex (array, offset, len);
678 [MonoTODO] // FIXME: Check how expanded entity is handled here.
679 public int ReadChars (char [] buffer, int index, int count)
682 return entity.ReadChars (buffer, index, count);
684 return source.ReadChars (buffer, index, count);
688 [MonoTODO] // FIXME: Check how expanded entity is handled here.
689 public override int ReadContentAsBase64 (byte [] buffer, int index, int count)
692 return entity.ReadContentAsBase64 (buffer, index, count);
694 return source.ReadContentAsBase64 (buffer, index, count);
697 [MonoTODO] // FIXME: Check how expanded entity is handled here.
698 public override int ReadContentAsBinHex (byte [] buffer, int index, int count)
701 return entity.ReadContentAsBinHex (buffer, index, count);
703 return source.ReadContentAsBinHex (buffer, index, count);
706 [MonoTODO] // FIXME: Check how expanded entity is handled here.
707 public override int ReadElementContentAsBase64 (byte [] buffer, int index, int count)
710 return entity.ReadElementContentAsBase64 (buffer, index, count);
712 return source.ReadElementContentAsBase64 (buffer, index, count);
715 [MonoTODO] // FIXME: Check how expanded entity is handled here.
716 public override int ReadElementContentAsBinHex (byte [] buffer, int index, int count)
719 return entity.ReadElementContentAsBinHex (buffer, index, count);
721 return source.ReadElementContentAsBinHex (buffer, index, count);