XmlWrappingReader.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataWeb / Client / System / Data / Services / Client / Xml / XmlWrappingReader.cs / 1305376 / XmlWrappingReader.cs

                            //---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//  
//      Provides a class used to wrap XmlReader instances.
//  
// 
// @owner  [....]
//--------------------------------------------------------------------- 

namespace System.Data.Services.Client.Xml
{
    #region Namespaces. 

    using System.Xml; 
    using System.Xml.Schema; 
    using System.Collections.Generic;
    using System.Diagnostics; 

    #endregion Namespaces.

    /// Use this class to wrap an existing . 
    internal class XmlWrappingReader : XmlReader, IXmlLineInfo
    { 
        #region Private fields. 

        /// Wrapper reader. 
        private XmlReader reader;

        /// Line information of the wrapper reader (possibly null).
        private IXmlLineInfo readerAsIXmlLineInfo; 

        /// stack to keep track of the base uri. 
        private Stack xmlBaseStack; 

        /// while creating a nested reader, we need to use the base uri from the previous reader. This field is used for that purpose. 
        private string previousReaderBaseUri;

        #endregion Private fields.
 
        /// Initializes a new  instance.
        /// Reader to wrap. 
        internal XmlWrappingReader(XmlReader baseReader) 
        {
            this.Reader = baseReader; 
        }

        #region Properties.
 
        /// Gets the number of attributes on the current node.
        public override int AttributeCount 
        { 
            get
            { 
                return this.reader.AttributeCount;
            }
        }
 
        /// Gets the base URI of the current node.
        public override string BaseURI 
        { 
            get
            { 
                if (this.xmlBaseStack != null && this.xmlBaseStack.Count > 0)
                {
                    return this.xmlBaseStack.Peek().BaseUri.AbsoluteUri;
                } 
                else if (!String.IsNullOrEmpty(this.previousReaderBaseUri))
                { 
                    return this.previousReaderBaseUri; 
                }
 
                return this.reader.BaseURI;
            }
        }
 
        /// Gets a value indicating whether this reader can parse and resolve entities.
        public override bool CanResolveEntity 
        { 
            get
            { 
                return this.reader.CanResolveEntity;
            }
        }
 
        /// Gets the depth of the current node in the XML document.
        public override int Depth 
        { 
            get
            { 
                return this.reader.Depth;
            }
        }
 
        // NOTE: there is no support for wrapping the DtdSchemaInfo property.
 
        /// Gets a value indicating whether the reader is positioned at the end of the stream. 
        public override bool EOF
        { 
            get
            {
                return this.reader.EOF;
            } 
        }
 
        /// Gets a value indicating whether the current node has any attributes. 
        public override bool HasAttributes
        { 
            get
            {
                return this.reader.HasAttributes;
            } 
        }
 
        /// Gets a value indicating whether the current node can have a . 
        public override bool HasValue
        { 
            get
            {
                return this.reader.HasValue;
            } 
        }
 
        ///  
        /// Gets a value indicating whether the current node is an attribute that was generated from the default value
        /// defined in the DTD or schema. 
        /// 
        public override bool IsDefault
        {
            get 
            {
                return this.reader.IsDefault; 
            } 
        }
 
        /// Gets a value indicating whether the current node is an empty element.
        public override bool IsEmptyElement
        {
            get 
            {
                return this.reader.IsEmptyElement; 
            } 
        }
 
        /// Gets the current line number.
        public virtual int LineNumber
        {
            get 
            {
                if (this.readerAsIXmlLineInfo != null) 
                { 
                    return this.readerAsIXmlLineInfo.LineNumber;
                } 

                return 0;
            }
        } 

        /// Gets the current line position. 
        public virtual int LinePosition 
        {
            get 
            {
                if (this.readerAsIXmlLineInfo != null)
                {
                    return this.readerAsIXmlLineInfo.LinePosition; 
                }
 
                return 0; 
            }
        } 

        /// Gets the local name of the current node.
        public override string LocalName
        { 
            get
            { 
                return this.reader.LocalName; 
            }
        } 

        /// Gets the qualified name of the current node.
        public override string Name
        { 
            get
            { 
                return this.reader.Name; 
            }
        } 

        /// Gets the namespace URI (as defined in the W3C Namespace specification) of the node on which the reader is positioned.
        public override string NamespaceURI
        { 
            get
            { 
                return this.reader.NamespaceURI; 
            }
        } 

        /// Gets the XmlNameTable associated with this implementation.
        public override XmlNameTable NameTable
        { 
            get
            { 
                return this.reader.NameTable; 
            }
        } 

        /// Gets the type of the current node.
        public override XmlNodeType NodeType
        { 
            get
            { 
                return this.reader.NodeType; 
            }
        } 

        /// Gets the namespace prefix associated with the current node.
        public override string Prefix
        { 
            get
            { 
                return this.reader.Prefix; 
            }
        } 

#if !ASTORIA_LIGHT

        /// Gets the quotation mark character used to enclose the value of an attribute node. 
        public override char QuoteChar
        { 
            get 
            {
                return this.reader.QuoteChar; 
            }
        }

#endif 

        /// Gets the state of the reader. 
        public override ReadState ReadState 
        {
            get 
            {
                return this.reader.ReadState;
            }
        } 

#if !ASTORIA_LIGHT 
 
        /// Gets the schema information that has been assigned to the current node as a result of schema validation.
        public override IXmlSchemaInfo SchemaInfo 
        {
            get
            {
                return this.reader.SchemaInfo; 
            }
        } 
#endif 

        /// Gets the XmlReaderSettings object used to create this XmlReader instance. 
        public override XmlReaderSettings Settings
        {
            get
            { 
                return this.reader.Settings;
            } 
        } 

        /// Gets the text value of the current node. 
        public override string Value
        {
            get
            { 
                return this.reader.Value;
            } 
        } 

        /// Gets the Common Language Runtime (CLR) type for the current node. 
        public override Type ValueType
        {
            get
            { 
                return this.reader.ValueType;
            } 
        } 

        /// Gets the current xml:lang scope. 
        public override string XmlLang
        {
            get
            { 
                return this.reader.XmlLang;
            } 
        } 

        /// Gets the current xml:space scope. 
        public override XmlSpace XmlSpace
        {
            get
            { 
                return this.reader.XmlSpace;
            } 
        } 

        /// Wrapped reader. 
        protected XmlReader Reader
        {
            get
            { 
                return this.reader;
            } 
 
            set
            { 
                this.reader = value;
                this.readerAsIXmlLineInfo = value as IXmlLineInfo;
            }
        } 

        #endregion Properties. 
 
        #region Methods.
 
        /// Changes the ReadState to Closed.
        public override void Close()
        {
            this.reader.Close(); 
        }
 
        /// Gets the value of the attribute with the specified index. 
        /// The index of the attribute. The index is zero-based. (The first attribute has index 0.)
        /// The value of the specified attribute. This method does not move the reader. 
        public override string GetAttribute(int i)
        {
            return this.reader.GetAttribute(i);
        } 

        /// Gets the value of the attribute with the specified name. 
        /// The qualified name of the attribute. 
        /// 
        /// The value of the specified attribute. If the attribute is not found or the value is String.Empty, 
        /// a null reference is returned.
        /// 
        public override string GetAttribute(string name)
        { 
            return this.reader.GetAttribute(name);
        } 
 
        /// Gets the value of the attribute with the specified index.
        /// The local name of the attribute. 
        /// The namespace URI of the attribute.
        /// 
        /// The value of the specified attribute. If the attribute is not found or the value is String.Empty,
        /// a null reference is returned. 
        /// 
        public override string GetAttribute(string name, string namespaceURI) 
        { 
            return this.reader.GetAttribute(name, namespaceURI);
        } 

        /// Gets a value indicating whether the class can return line information.
        /// true if LineNumber and LinePosition can be provided; otherwise, false.
        public virtual bool HasLineInfo() 
        {
            return ((this.readerAsIXmlLineInfo != null) && this.readerAsIXmlLineInfo.HasLineInfo()); 
        } 

        /// Resolves a namespace prefix in the current element's scope. 
        /// 
        /// The prefix whose namespace URI you want to resolve. To match the default namespace, pass an empty string.
        /// 
        /// The namespace URI to which the prefix maps or a null reference. 
        public override string LookupNamespace(string prefix)
        { 
            return this.reader.LookupNamespace(prefix); 
        }
 
        /// Moves to the attribute with the specified index.
        /// The index of the attribute.
        public override void MoveToAttribute(int i)
        { 
            this.reader.MoveToAttribute(i);
        } 
 
        /// Moves to the attribute with the specified name.
        /// The qualified name of the attribute. 
        /// 
        /// true if the attribute is found; otherwise, false. If false, the reader's position does not change.
        /// 
        public override bool MoveToAttribute(string name) 
        {
            return this.reader.MoveToAttribute(name); 
        } 

        /// Moves to the attribute with the specified name. 
        /// The local name of the attribute.
        /// The namespace URI of the attribute.
        /// 
        /// true if the attribute is found; otherwise, false. If false, the reader's position does not change. 
        /// 
        public override bool MoveToAttribute(string name, string ns) 
        { 
            return this.reader.MoveToAttribute(name, ns);
        } 

        /// Moves to the element that contains the current attribute node.
        /// 
        /// true if the reader is positioned on an attribute (the reader moves to the element that owns the attribute); 
        /// false if the reader is not positioned on an attribute (the position of the reader does not change).
        ///  
        public override bool MoveToElement() 
        {
            return this.reader.MoveToElement(); 
        }

        /// Moves to the first attribute.
        ///  
        /// true if an attribute exists (the reader moves to the first attribute); otherwise, false (the position of
        /// the reader does not change). 
        ///  
        public override bool MoveToFirstAttribute()
        { 
            return this.reader.MoveToFirstAttribute();
        }

        /// Moves to the next attribute. 
        /// true if there is a next attribute; false if there are no more attributes.
        public override bool MoveToNextAttribute() 
        { 
            return this.reader.MoveToNextAttribute();
        } 

        /// Reads the next node from the stream.
        /// true if the next node was read successfully; false if there are no more nodes to read.
        public override bool Read() 
        {
            if (this.reader.NodeType == XmlNodeType.EndElement) 
            { 
                this.PopXmlBase();
            } 
            else
            {
                // If the xmlreader is located at the attributes, IsEmptyElement will always return false.
                // Hence we need to call MoveToElement first, before checking for IsEmptyElement 
                this.reader.MoveToElement();
                if (this.reader.IsEmptyElement) 
                { 
                    this.PopXmlBase();
                } 
            }

            bool result = this.reader.Read();
            if (result) 
            {
                if (this.reader.NodeType == XmlNodeType.Element && 
                    this.reader.HasAttributes) 
                {
                    string baseAttribute = this.reader.GetAttribute(XmlConstants.XmlBaseAttributeNameWithPrefix); 
                    if (String.IsNullOrEmpty(baseAttribute))
                    {
                        // If there is no xml base attribute specified, don't do anything
                        return result; 
                    }
 
                    Uri newBaseUri = null; 
                    newBaseUri = Util.CreateUri(baseAttribute, UriKind.RelativeOrAbsolute);
 
                    // Initialize the stack first time you see the xml base uri
                    if (this.xmlBaseStack == null)
                    {
                        this.xmlBaseStack = new Stack(); 
                    }
 
                    if (this.xmlBaseStack.Count > 0) 
                    {
                        // If there is a xml:base attribute already specified, then the new xml base 
                        // value must be relative to the previous xml base.
                        // For more information, look into section 3 of the following RFC
                        // http://www.w3.org/TR/2001/REC-xmlbase-20010627/
                        newBaseUri = Util.CreateUri(this.xmlBaseStack.Peek().BaseUri, newBaseUri); 
                    }
 
                    // Push current state and allocate new one 
                    this.xmlBaseStack.Push(new XmlBaseState(newBaseUri, this.reader.Depth));
                } 
            }

            return result;
        } 

        /// Parses the attribute value into one or more Text, EntityReference, or EndEntity nodes. 
        /// true if there are nodes to return. 
        public override bool ReadAttributeValue()
        { 
            return this.reader.ReadAttributeValue();
        }

        /// Resolves the entity reference for EntityReference nodes. 
        public override void ResolveEntity()
        { 
            this.reader.ResolveEntity(); 
        }
 
        /// Skips the children of the current node.
        public override void Skip()
        {
            this.reader.Skip(); 
        }
 
        ///  
        /// Creates a new instance of XmlWrappingReader instance which wraps the 
        ///  
        /// current base uri.
        /// xml reader which needs to be wrapped.
        /// a new instance of XmlWrappingReader.
        internal static XmlWrappingReader CreateReader(string currentBaseUri, XmlReader newReader) 
        {
            Debug.Assert(!(newReader is XmlWrappingReader), "The new reader must not be a xmlWrappingReader"); 
            XmlWrappingReader reader = new XmlWrappingReader(newReader); 
            reader.previousReaderBaseUri = currentBaseUri;
            return reader; 
        }

        /// 
        /// Releases the unmanaged resources used by the XmlReader and optionally releases the managed resources. 
        /// 
        ///  
        /// true to release both managed and unmanaged resources; false to release only unmanaged resources. 
        /// 
        protected override void Dispose(bool disposing) 
        {
            if (this.reader != null)
            {
                ((IDisposable)this.reader).Dispose(); 
            }
 
            // XmlReader.Dispose(bool) does nothing, but for completeness w/ FxCop 
            base.Dispose(disposing);
        } 

        /// 
        /// Pops the xml base from the top of the stack, if required.
        ///  
        private void PopXmlBase()
        { 
            if (this.xmlBaseStack != null && this.xmlBaseStack.Count > 0 && this.reader.Depth == this.xmlBaseStack.Peek().Depth) 
            {
                this.xmlBaseStack.Pop(); 
            }
        }

        #endregion Methods. 

        #region Private Class 
 
        /// 
        /// Private class to maintain the state information for keeping track of base uri's. 
        /// 
        private class XmlBaseState
        {
            ///  
            /// Creates a new instance of the XmlBaseState class.
            ///  
            /// base uri for the given element. 
            /// depth of the element.
            internal XmlBaseState(Uri baseUri, int depth) 
            {
                this.BaseUri = baseUri;
                this.Depth = depth;
            } 

            /// base uri as specified in the xmml:base attribute of the given element. 
            public Uri BaseUri 
            {
                get; 
                private set;
            }

            /// depth of the element. 
            public int Depth
            { 
                get; 
                private set;
            } 
        }

        #endregion // Private Class
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK