| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688 |
- //
- // System.Xml.XmlTextReader2.cs - XmlTextReader for .NET 2.0
- //
- // Author:
- // Atsushi Enomoto ([email protected])
- //
- // Copyright (C) 2004 Novell, Inc.
- //
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- #if NET_2_0
- using XmlTextReaderImpl = Mono.Xml2.XmlTextReader;
- using System;
- using System.Collections;
- using System.Globalization;
- using System.IO;
- using System.Text;
- using System.Xml.Schema;
- using Mono.Xml;
- namespace System.Xml
- {
- public class XmlTextReader : XmlReader,
- IXmlLineInfo, IXmlNamespaceResolver, IHasXmlParserContext
- {
- XmlTextReader entity;
- XmlTextReaderImpl source;
- bool entityInsideAttribute;
- bool insideAttribute;
- string cachedAttributeValue;
- bool attributeValueConsumed;
- protected XmlTextReader ()
- {
- }
- public XmlTextReader (Stream input)
- : this (new XmlStreamReader (input))
- {
- }
- public XmlTextReader (string url)
- : this(url, new NameTable ())
- {
- }
- public XmlTextReader (TextReader input)
- : this (input, new NameTable ())
- {
- }
- protected XmlTextReader (XmlNameTable nt)
- : this (String.Empty, XmlNodeType.Element, null)
- {
- }
- public XmlTextReader (Stream input, XmlNameTable nt)
- : this(new XmlStreamReader (input), nt)
- {
- }
- public XmlTextReader (string url, Stream input)
- : this (url, new XmlStreamReader (input))
- {
- }
- public XmlTextReader (string url, TextReader input)
- : this (url, input, new NameTable ())
- {
- }
- public XmlTextReader (string url, XmlNameTable nt)
- {
- source = new XmlTextReaderImpl (url, nt);
- }
- public XmlTextReader (TextReader input, XmlNameTable nt)
- : this (String.Empty, input, nt)
- {
- }
- public XmlTextReader (Stream xmlFragment, XmlNodeType fragType, XmlParserContext context)
- {
- source = new XmlTextReaderImpl (xmlFragment, fragType, context);
- }
- public XmlTextReader (string url, Stream input, XmlNameTable nt)
- : this (url, new XmlStreamReader (input), nt)
- {
- }
- public XmlTextReader (string url, TextReader input, XmlNameTable nt)
- {
- source = new XmlTextReaderImpl (url, input, nt);
- }
- public XmlTextReader (string xmlFragment, XmlNodeType fragType, XmlParserContext context)
- {
- source = new XmlTextReaderImpl (xmlFragment, fragType, context);
- }
- private XmlTextReader (XmlTextReaderImpl entityContainer, bool insideAttribute)
- {
- source = entityContainer;
- this.entityInsideAttribute = insideAttribute;
- }
- #region Properties
- private XmlReader Current {
- get { return entity != null && entity.ReadState != ReadState.Initial ? (XmlReader) entity : source; }
- }
- public override int AttributeCount {
- get { return Current.AttributeCount; }
- }
- public override string BaseURI {
- get { return Current.BaseURI; }
- }
- public override bool CanReadBinaryContent {
- get { return true; }
- }
- public override bool CanReadValueChunk {
- get { return true; }
- }
- public override bool CanResolveEntity {
- get { return true; }
- }
- public override int Depth {
- get {
- // On EndEntity, depth is the same as that
- // of EntityReference.
- if (entity != null && entity.ReadState == ReadState.Interactive)
- return source.Depth + entity.Depth + 1;
- else
- return source.Depth;
- }
- }
- public override bool EOF {
- get { return source.EOF; }
- }
- public override bool HasValue {
- get { return Current.HasValue; }
- }
- public override bool IsDefault {
- get { return Current.IsDefault; }
- }
- public override bool IsEmptyElement {
- get { return Current.IsEmptyElement; }
- }
- public override string LocalName {
- get { return Current.LocalName; }
- }
- public override string Name {
- get { return Current.Name; }
- }
- public override string NamespaceURI {
- get { return Current.NamespaceURI; }
- }
- public override XmlNameTable NameTable {
- get { return Current.NameTable; }
- }
- public override XmlNodeType NodeType {
- get {
- if (Current == entity)
- return entity.EOF ? XmlNodeType.EndEntity : entity.NodeType;
- else
- return source.NodeType;
- }
- }
- internal XmlParserContext ParserContext {
- get { return ((IHasXmlParserContext) Current).ParserContext; }
- }
- XmlParserContext IHasXmlParserContext.ParserContext {
- get { return this.ParserContext; }
- }
- public override string Prefix {
- get { return Current.Prefix; }
- }
- public override char QuoteChar {
- get { return Current.QuoteChar; }
- }
- public override ReadState ReadState {
- get { return entity != null ? ReadState.Interactive : source.ReadState; }
- }
- public override XmlReaderSettings Settings {
- get { return base.Settings; }
- }
- public override string Value {
- get { return Current.Value; }
- }
- public override string XmlLang {
- get { return Current.XmlLang; }
- }
- public override XmlSpace XmlSpace {
- get { return Current.XmlSpace; }
- }
- // non-overrides
- internal bool CharacterChecking {
- get {
- if (entity != null)
- return entity.CharacterChecking;
- else
- return source.CharacterChecking;
- }
- set {
- if (entity != null)
- entity.CharacterChecking = value;
- source.CharacterChecking = value;
- }
- }
- internal bool CloseInput {
- get {
- if (entity != null)
- return entity.CloseInput;
- else
- return source.CloseInput;
- }
- set {
- if (entity != null)
- entity.CloseInput = value;
- source.CloseInput = value;
- }
- }
- internal ConformanceLevel Conformance {
- set {
- if (entity != null)
- entity.Conformance = value;
- source.Conformance = value;
- }
- }
- internal XmlResolver Resolver {
- get { return source.Resolver; }
- }
- private void CopyProperties (XmlTextReader other)
- {
- CharacterChecking = other.CharacterChecking;
- CloseInput = other.CloseInput;
- if (other.Settings != null)
- Conformance = other.Settings.ConformanceLevel;
- XmlResolver = other.Resolver;
- }
- // public members
- public Encoding Encoding {
- get {
- if (entity != null)
- return entity.Encoding;
- else
- return source.Encoding;
- }
- }
- public EntityHandling EntityHandling {
- get { return source.EntityHandling; }
- set {
- if (entity != null)
- entity.EntityHandling = value;
- source.EntityHandling = value;
- }
- }
- public int LineNumber {
- get {
- if (entity != null)
- return entity.LineNumber;
- else
- return source.LineNumber;
- }
- }
- public int LinePosition {
- get {
- if (entity != null)
- return entity.LinePosition;
- else
- return source.LinePosition;
- }
- }
- public bool Namespaces {
- get { return source.Namespaces; }
- set {
- if (entity != null)
- entity.Namespaces = value;
- source.Namespaces = value;
- }
- }
- public bool Normalization {
- get { return source.Normalization; }
- set {
- if (entity != null)
- entity.Normalization = value;
- source.Normalization = value;
- }
- }
- public bool ProhibitDtd {
- get { return source.ProhibitDtd; }
- set {
- if (entity != null)
- entity.ProhibitDtd = value;
- source.ProhibitDtd = value;
- }
- }
- public WhitespaceHandling WhitespaceHandling {
- get { return source.WhitespaceHandling; }
- set {
- if (entity != null)
- entity.WhitespaceHandling = value;
- source.WhitespaceHandling = value;
- }
- }
- public XmlResolver XmlResolver {
- set {
- if (entity != null)
- entity.XmlResolver = value;
- source.XmlResolver = value;
- }
- }
- #endregion
- #region Methods
- internal void AdjustLineInfoOffset (int lineNumberOffset, int linePositionOffset)
- {
- if (entity != null)
- entity.AdjustLineInfoOffset (lineNumberOffset, linePositionOffset);
- source.AdjustLineInfoOffset (lineNumberOffset, linePositionOffset);
- }
- internal void SetNameTable (XmlNameTable nameTable)
- {
- if (entity != null)
- entity.SetNameTable (nameTable);
- source.SetNameTable (nameTable);
- }
- // overrides
- public override void Close ()
- {
- if (entity != null)
- entity.Close ();
- source.Close ();
- }
- public override string GetAttribute (int i)
- {
- return Current.GetAttribute (i);
- }
- // MS.NET 1.0 msdn says that this method returns String.Empty
- // for absent attribute, but in fact it returns null.
- // This description is corrected in MS.NET 1.1 msdn.
- public override string GetAttribute (string name)
- {
- return Current.GetAttribute (name);
- }
- public override string GetAttribute (string localName, string namespaceURI)
- {
- return Current.GetAttribute (localName, namespaceURI);
- }
- public IDictionary GetNamespacesInScope (XmlNamespaceScope scope)
- {
- return ((IXmlNamespaceResolver) Current).GetNamespacesInScope (scope);
- }
- public override string LookupNamespace (string prefix)
- {
- return Current.LookupNamespace (prefix, false);
- }
- public override string LookupNamespace (string prefix, bool atomizedName)
- {
- return ((IXmlNamespaceResolver) Current).LookupNamespace (prefix, atomizedName);
- }
- string IXmlNamespaceResolver.LookupPrefix (string ns)
- {
- return ((IXmlNamespaceResolver) Current).LookupPrefix (ns, false);
- }
- public string LookupPrefix (string ns, bool atomizedName)
- {
- return ((IXmlNamespaceResolver) Current).LookupPrefix (ns, atomizedName);
- }
- public override void MoveToAttribute (int i)
- {
- if (entity != null && entityInsideAttribute) {
- entity.Close ();
- entity = null;
- }
- Current.MoveToAttribute (i);
- insideAttribute = true;
- }
- public override bool MoveToAttribute (string name)
- {
- if (entity != null && !entityInsideAttribute)
- return entity.MoveToAttribute (name);
- if (!source.MoveToAttribute (name))
- return false;
- if (entity != null && entityInsideAttribute) {
- entity.Close ();
- entity = null;
- }
- insideAttribute = true;
- return true;
- }
- public override bool MoveToAttribute (string localName, string namespaceName)
- {
- if (entity != null && !entityInsideAttribute)
- return entity.MoveToAttribute (localName, namespaceName);
- if (!source.MoveToAttribute (localName, namespaceName))
- return false;
- if (entity != null && entityInsideAttribute) {
- entity.Close ();
- entity = null;
- }
- insideAttribute = true;
- return true;
- }
- public override bool MoveToElement ()
- {
- if (entity != null && entityInsideAttribute) {
- entity.Close ();
- entity = null;
- }
- if (!Current.MoveToElement ())
- return false;
- insideAttribute = false;
- return true;
- }
- public override bool MoveToFirstAttribute ()
- {
- if (entity != null && !entityInsideAttribute)
- return entity.MoveToFirstAttribute ();
- if (!source.MoveToFirstAttribute ())
- return false;
- if (entity != null && entityInsideAttribute) {
- entity.Close ();
- entity = null;
- }
- insideAttribute = true;
- return true;
- }
- public override bool MoveToNextAttribute ()
- {
- if (entity != null && !entityInsideAttribute)
- return entity.MoveToNextAttribute ();
- if (!source.MoveToNextAttribute ())
- return false;
- if (entity != null && entityInsideAttribute) {
- entity.Close ();
- entity = null;
- }
- insideAttribute = true;
- return true;
- }
- public override bool Read ()
- {
- insideAttribute = false;
- if (entity != null && (entityInsideAttribute || entity.EOF)) {
- entity.Close ();
- entity = null;
- }
- if (entity != null) {
- if (entity.Read ())
- return true;
- if (EntityHandling == EntityHandling.ExpandEntities) {
- // EndEntity must be skipped
- entity.Close ();
- entity = null;
- return Read ();
- }
- else
- return true; // either success or EndEntity
- }
- else {
- if (!source.Read ())
- return false;
- if (EntityHandling == EntityHandling.ExpandEntities
- && source.NodeType == XmlNodeType.EntityReference) {
- ResolveEntity ();
- return Read ();
- }
- return true;
- }
- }
- public override bool ReadAttributeValue ()
- {
- if (entity != null && entityInsideAttribute) {
- if (entity.EOF) {
- entity.Close ();
- entity = null;
- }
- else {
- entity.Read ();
- return true; // either success or EndEntity
- }
- }
- return Current.ReadAttributeValue ();
- }
- public override string ReadString ()
- {
- return base.ReadString ();
- }
- public void ResetState ()
- {
- if (entity != null)
- entity.ResetState ();
- source.ResetState ();
- }
- public override void ResolveEntity ()
- {
- if (entity != null)
- entity.ResolveEntity ();
- else {
- if (source.NodeType != XmlNodeType.EntityReference)
- throw new InvalidOperationException ("The current node is not an Entity Reference");
- XmlTextReaderImpl entReader =
- ParserContext.Dtd.GenerateEntityContentReader (source.Name, ParserContext);
- if (entReader == null)
- throw new XmlException (this as IXmlLineInfo, this.BaseURI, String.Format ("Reference to undeclared entity '{0}'.", source.Name));
- entity = new XmlTextReader (
- entReader, insideAttribute);
- entity.CopyProperties (this);
- }
- }
- public override void Skip ()
- {
- base.Skip ();
- }
- [MonoTODO ("Check how expanded entity is handled here.")]
- public TextReader GetRemainder ()
- {
- if (entity != null) {
- entity.Close ();
- entity = null;
- }
- return source.GetRemainder ();
- }
- public bool HasLineInfo ()
- {
- return true;
- }
- [MonoTODO ("Check how expanded entity is handled here.")]
- public int ReadBase64 (byte [] buffer, int offset, int length)
- {
- if (entity != null)
- return entity.ReadBase64 (buffer, offset, length);
- else
- return source.ReadBase64 (buffer, offset, length);
- }
- [MonoTODO ("Check how expanded entity is handled here.")]
- public int ReadBinHex (byte [] buffer, int offset, int length)
- {
- if (entity != null)
- return entity.ReadBinHex (buffer, offset, length);
- else
- return source.ReadBinHex (buffer, offset, length);
- }
- [MonoTODO ("Check how expanded entity is handled here.")]
- public int ReadChars (char [] buffer, int offset, int length)
- {
- if (entity != null)
- return entity.ReadChars (buffer, offset, length);
- else
- return source.ReadChars (buffer, offset, length);
- }
- [MonoTODO ("Check how expanded entity is handled here.")]
- public override int ReadContentAsBase64 (byte [] buffer, int offset, int length)
- {
- if (entity != null)
- return entity.ReadContentAsBase64 (buffer, offset, length);
- else
- return source.ReadContentAsBase64 (buffer, offset, length);
- }
- [MonoTODO ("Check how expanded entity is handled here.")]
- public override int ReadContentAsBinHex (byte [] buffer, int offset, int length)
- {
- if (entity != null)
- return entity.ReadContentAsBinHex (buffer, offset, length);
- else
- return source.ReadContentAsBinHex (buffer, offset, length);
- }
- [MonoTODO ("Check how expanded entity is handled here.")]
- public override int ReadElementContentAsBase64 (byte [] buffer, int offset, int length)
- {
- if (entity != null)
- return entity.ReadElementContentAsBase64 (buffer, offset, length);
- else
- return source.ReadElementContentAsBase64 (buffer, offset, length);
- }
- [MonoTODO ("Check how expanded entity is handled here.")]
- public override int ReadElementContentAsBinHex (byte [] buffer, int offset, int length)
- {
- if (entity != null)
- return entity.ReadElementContentAsBinHex (buffer, offset, length);
- else
- return source.ReadElementContentAsBinHex (buffer, offset, length);
- }
- #endregion
- }
- }
- #endif
|