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 private XmlTextReader (XmlTextReaderImpl entityContainer, bool insideAttribute)
130 source = entityContainer;
131 this.entityInsideAttribute = insideAttribute;
136 private XmlReader Current {
137 get { return entity != null && entity.ReadState != ReadState.Initial ? (XmlReader) entity : source; }
140 public override int AttributeCount {
141 get { return Current.AttributeCount; }
144 public override string BaseURI {
145 get { return Current.BaseURI; }
148 public override bool CanReadBinaryContent {
152 public override bool CanReadValueChunk {
156 public override bool CanResolveEntity {
160 public override int Depth {
162 // On EndEntity, depth is the same as that
163 // of EntityReference.
164 if (entity != null && entity.ReadState == ReadState.Interactive)
165 return source.Depth + entity.Depth + 1;
171 public override bool EOF {
172 get { return source.EOF; }
175 public override bool HasValue {
176 get { return Current.HasValue; }
179 public override bool IsDefault {
180 get { return Current.IsDefault; }
183 public override bool IsEmptyElement {
184 get { return Current.IsEmptyElement; }
187 public override string LocalName {
188 get { return Current.LocalName; }
191 public override string Name {
192 get { return Current.Name; }
195 public override string NamespaceURI {
196 get { return Current.NamespaceURI; }
199 public override XmlNameTable NameTable {
200 get { return Current.NameTable; }
203 public override XmlNodeType NodeType {
205 if (Current == entity)
206 return entity.EOF ? XmlNodeType.EndEntity : entity.NodeType;
208 return source.NodeType;
212 internal XmlParserContext ParserContext {
213 get { return ((IHasXmlParserContext) Current).ParserContext; }
216 XmlParserContext IHasXmlParserContext.ParserContext {
217 get { return this.ParserContext; }
220 public override string Prefix {
221 get { return Current.Prefix; }
224 public override char QuoteChar {
225 get { return Current.QuoteChar; }
228 public override ReadState ReadState {
229 get { return entity != null ? ReadState.Interactive : source.ReadState; }
232 public override XmlReaderSettings Settings {
233 get { return base.Settings; }
236 public override string Value {
237 get { return Current.Value; }
240 public override string XmlLang {
241 get { return Current.XmlLang; }
244 public override XmlSpace XmlSpace {
245 get { return Current.XmlSpace; }
250 internal bool CharacterChecking {
253 return entity.CharacterChecking;
255 return source.CharacterChecking;
259 entity.CharacterChecking = value;
260 source.CharacterChecking = value;
264 internal bool CloseInput {
267 return entity.CloseInput;
269 return source.CloseInput;
273 entity.CloseInput = value;
274 source.CloseInput = value;
278 internal ConformanceLevel Conformance {
281 entity.Conformance = value;
282 source.Conformance = value;
286 internal XmlResolver Resolver {
287 get { return source.Resolver; }
290 private void CopyProperties (XmlTextReader other)
292 CharacterChecking = other.CharacterChecking;
293 CloseInput = other.CloseInput;
294 if (other.Settings != null)
295 Conformance = other.Settings.ConformanceLevel;
296 XmlResolver = other.Resolver;
301 public Encoding Encoding {
304 return entity.Encoding;
306 return source.Encoding;
310 public EntityHandling EntityHandling {
311 get { return source.EntityHandling; }
314 entity.EntityHandling = value;
315 source.EntityHandling = value;
319 public int LineNumber {
322 return entity.LineNumber;
324 return source.LineNumber;
328 public int LinePosition {
331 return entity.LinePosition;
333 return source.LinePosition;
337 public bool Namespaces {
338 get { return source.Namespaces; }
341 entity.Namespaces = value;
342 source.Namespaces = value;
346 public bool Normalization {
347 get { return source.Normalization; }
350 entity.Normalization = value;
351 source.Normalization = value;
355 public bool ProhibitDtd {
356 get { return source.ProhibitDtd; }
359 entity.ProhibitDtd = value;
360 source.ProhibitDtd = value;
364 public WhitespaceHandling WhitespaceHandling {
365 get { return source.WhitespaceHandling; }
368 entity.WhitespaceHandling = value;
369 source.WhitespaceHandling = value;
373 public XmlResolver XmlResolver {
376 entity.XmlResolver = value;
377 source.XmlResolver = value;
385 internal void AdjustLineInfoOffset (int lineNumberOffset, int linePositionOffset)
388 entity.AdjustLineInfoOffset (lineNumberOffset, linePositionOffset);
389 source.AdjustLineInfoOffset (lineNumberOffset, linePositionOffset);
392 internal void SetNameTable (XmlNameTable nameTable)
395 entity.SetNameTable (nameTable);
396 source.SetNameTable (nameTable);
401 public override void Close ()
408 public override string GetAttribute (int i)
410 return Current.GetAttribute (i);
413 // MS.NET 1.0 msdn says that this method returns String.Empty
414 // for absent attribute, but in fact it returns null.
415 // This description is corrected in MS.NET 1.1 msdn.
416 public override string GetAttribute (string name)
418 return Current.GetAttribute (name);
421 public override string GetAttribute (string localName, string namespaceURI)
423 return Current.GetAttribute (localName, namespaceURI);
426 public IDictionary<string, string> GetNamespacesInScope (XmlNamespaceScope scope)
428 return ((IXmlNamespaceResolver) Current).GetNamespacesInScope (scope);
431 public override string LookupNamespace (string prefix)
433 return Current.LookupNamespace (prefix);
436 string IXmlNamespaceResolver.LookupPrefix (string ns)
438 return ((IXmlNamespaceResolver) Current).LookupPrefix (ns);
441 public override void MoveToAttribute (int i)
443 if (entity != null && entityInsideAttribute) {
447 Current.MoveToAttribute (i);
448 insideAttribute = true;
451 public override bool MoveToAttribute (string name)
453 if (entity != null && !entityInsideAttribute)
454 return entity.MoveToAttribute (name);
455 if (!source.MoveToAttribute (name))
457 if (entity != null && entityInsideAttribute) {
461 insideAttribute = true;
465 public override bool MoveToAttribute (string localName, string namespaceName)
467 if (entity != null && !entityInsideAttribute)
468 return entity.MoveToAttribute (localName, namespaceName);
469 if (!source.MoveToAttribute (localName, namespaceName))
471 if (entity != null && entityInsideAttribute) {
475 insideAttribute = true;
479 public override bool MoveToElement ()
481 if (entity != null && entityInsideAttribute) {
485 if (!Current.MoveToElement ())
487 insideAttribute = false;
491 public override bool MoveToFirstAttribute ()
493 if (entity != null && !entityInsideAttribute)
494 return entity.MoveToFirstAttribute ();
495 if (!source.MoveToFirstAttribute ())
497 if (entity != null && entityInsideAttribute) {
501 insideAttribute = true;
505 public override bool MoveToNextAttribute ()
507 if (entity != null && !entityInsideAttribute)
508 return entity.MoveToNextAttribute ();
509 if (!source.MoveToNextAttribute ())
511 if (entity != null && entityInsideAttribute) {
515 insideAttribute = true;
519 public override bool Read ()
521 insideAttribute = false;
523 if (entity != null && (entityInsideAttribute || entity.EOF)) {
527 if (entity != null) {
530 if (EntityHandling == EntityHandling.ExpandEntities) {
531 // EndEntity must be skipped
537 return true; // either success or EndEntity
542 if (EntityHandling == EntityHandling.ExpandEntities
543 && source.NodeType == XmlNodeType.EntityReference) {
551 public override bool ReadAttributeValue ()
553 if (entity != null && entityInsideAttribute) {
560 return true; // either success or EndEntity
563 return Current.ReadAttributeValue ();
566 public override string ReadString ()
568 return base.ReadString ();
571 public void ResetState ()
574 entity.ResetState ();
575 source.ResetState ();
578 public override void ResolveEntity ()
581 entity.ResolveEntity ();
583 if (source.NodeType != XmlNodeType.EntityReference)
584 throw new InvalidOperationException ("The current node is not an Entity Reference");
585 XmlTextReaderImpl entReader =
586 ParserContext.Dtd.GenerateEntityContentReader (source.Name, ParserContext);
587 if (entReader == null)
588 throw new XmlException (this as IXmlLineInfo, this.BaseURI, String.Format ("Reference to undeclared entity '{0}'.", source.Name));
589 entity = new XmlTextReader (
590 entReader, insideAttribute);
591 entity.CopyProperties (this);
595 public override void Skip ()
600 [MonoTODO ("Check how expanded entity is handled here.")]
601 public TextReader GetRemainder ()
603 if (entity != null) {
607 return source.GetRemainder ();
610 public bool HasLineInfo ()
615 [MonoTODO ("Check how expanded entity is handled here.")]
616 public int ReadBase64 (byte [] buffer, int offset, int length)
619 return entity.ReadBase64 (buffer, offset, length);
621 return source.ReadBase64 (buffer, offset, length);
624 [MonoTODO ("Check how expanded entity is handled here.")]
625 public int ReadBinHex (byte [] buffer, int offset, int length)
628 return entity.ReadBinHex (buffer, offset, length);
630 return source.ReadBinHex (buffer, offset, length);
633 [MonoTODO ("Check how expanded entity is handled here.")]
634 public int ReadChars (char [] buffer, int offset, int length)
637 return entity.ReadChars (buffer, offset, length);
639 return source.ReadChars (buffer, offset, length);
643 [MonoTODO ("Check how expanded entity is handled here.")]
644 public override int ReadContentAsBase64 (byte [] buffer, int offset, int length)
647 return entity.ReadContentAsBase64 (buffer, offset, length);
649 return source.ReadContentAsBase64 (buffer, offset, length);
652 [MonoTODO ("Check how expanded entity is handled here.")]
653 public override int ReadContentAsBinHex (byte [] buffer, int offset, int length)
656 return entity.ReadContentAsBinHex (buffer, offset, length);
658 return source.ReadContentAsBinHex (buffer, offset, length);
661 [MonoTODO ("Check how expanded entity is handled here.")]
662 public override int ReadElementContentAsBase64 (byte [] buffer, int offset, int length)
665 return entity.ReadElementContentAsBase64 (buffer, offset, length);
667 return source.ReadElementContentAsBase64 (buffer, offset, length);
670 [MonoTODO ("Check how expanded entity is handled here.")]
671 public override int ReadElementContentAsBinHex (byte [] buffer, int offset, int length)
674 return entity.ReadElementContentAsBinHex (buffer, offset, length);
676 return source.ReadElementContentAsBinHex (buffer, offset, length);