DictionaryContent.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataWeb / Server / System / Data / Services / Serializers / DictionaryContent.cs / 1305376 / DictionaryContent.cs

                            //---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//  
//      Provides a syndication content that holds name/value pairs.
//  
// 
// @owner [....]
//--------------------------------------------------------------------- 

namespace System.Data.Services.Serializers
{
    #region Namespaces. 

    using System.Collections.Generic; 
    using System.Diagnostics; 
    using System.ServiceModel.Syndication;
    using System.Xml; 

    #endregion Namespaces.

    ///  
    /// Use this class to hold the name/value pairs of content that will be written
    /// to the content element of a syndication item. 
    ///  
    internal class DictionaryContent : SyndicationContent
    { 
        #region Private fields.

        /// Content for a property value: one of a string, dictionary or null.
        private List valueContents; 

        /// Names for property values. 
        private List valueNames; 

        /// Declared type names for property values. 
        private List valueTypes;

        #endregion Private fields.
 
        #region Constructors.
 
        /// Initializes a new DictionaryContent instance. 
        public DictionaryContent()
        { 
            this.valueContents = new List();
            this.valueTypes = new List();
            this.valueNames = new List();
        } 

        /// Initializes a new DictionaryContent instance. 
        /// Initial capacity for entries. 
        public DictionaryContent(int capacity)
        { 
            this.valueContents = new List(capacity);
            this.valueTypes = new List(capacity);
            this.valueNames = new List(capacity);
        } 

        ///  
        /// Initializes a new DictionaryContent instance by copying values from 
        /// the specified one.
        ///  
        /// Dictionary to copy content from.
        /// This produces a shallow copy only.
        private DictionaryContent(DictionaryContent other)
        { 
            Debug.Assert(other != null, "other != null");
            this.valueContents = other.valueContents; 
            this.valueTypes = other.valueTypes; 
            this.valueNames = other.valueNames;
        } 

        #endregion Constructors.

        #region Properties. 

        /// The MIME type of this content. 
        public override string Type 
        {
            get { return XmlConstants.MimeApplicationXml; } 
        }

        /// True if there is no properties to serialize out.
        public bool IsEmpty 
        {
            get { return this.valueNames.Count == 0; } 
        } 

        #endregion Properties. 

        #region Methods.

        /// Creates a shallow copy of this content. 
        /// A shallow copy of this content.
        public override SyndicationContent Clone() 
        { 
            return new DictionaryContent(this);
        } 

        /// Adds the specified property.
        /// Property name.
        /// Expected type name for value. 
        /// Property value in text form.
        internal void Add(string name, string expectedTypeName, object value) 
        { 
            Debug.Assert(value != null, "value != null -- otherwise AddNull should have been called.");
            Debug.Assert( 
                value is DictionaryContent || value is string,
                "value is DictionaryContent || value is string -- only sub-dictionaries and formatted strings are allowed.");
            Debug.Assert(!this.valueNames.Contains(name), "!this.valueNames.Contains(name) -- otherwise repeated property set.");
            this.valueNames.Add(name); 
            this.valueTypes.Add(expectedTypeName);
            this.valueContents.Add(value); 
            Debug.Assert(this.valueNames.Count == this.valueTypes.Count, "this.valueNames.Count == this.valueTypes.Count"); 
            Debug.Assert(this.valueNames.Count == this.valueContents.Count, "this.valueNames.Count == this.valueContents.Count");
        } 

        /// Adds a property with a null value.
        /// Type for the property.
        /// Property name. 
        internal void AddNull(string expectedTypeName, string name)
        { 
            Debug.Assert(!this.valueNames.Contains(name), "!this.valueNames.Contains(name) -- otherwise repeated property set."); 
            this.valueNames.Add(name);
            this.valueTypes.Add(expectedTypeName); 
            this.valueContents.Add(null);
            Debug.Assert(this.valueNames.Count == this.valueTypes.Count, "this.valueNames.Count == this.valueTypes.Count");
            Debug.Assert(this.valueNames.Count == this.valueContents.Count, "this.valueNames.Count == this.valueContents.Count");
        } 

        ///  
        /// Gets a XmlReader to the property contents. 
        /// 
        /// XmlReader for the property contents. 
        internal XmlReader GetPropertyContentsReader()
        {
            Debug.Assert(!this.IsEmpty, "!this.IsEmpty -- calling with 0 properties is disallowed.");
 
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            using (XmlWriter writer = XmlWriter.Create(stream)) 
            { 
                writer.WriteStartElement(XmlConstants.DataWebMetadataNamespacePrefix, XmlConstants.AtomPropertiesElementName, XmlConstants.DataWebMetadataNamespace);
                writer.WriteAttributeString(XmlConstants.XmlnsNamespacePrefix, XmlConstants.DataWebNamespacePrefix, null, XmlConstants.DataWebNamespace); 
                this.WritePropertyContentsTo(writer);
                writer.WriteEndElement();
                writer.Flush();
                stream.Position = 0; 
                return XmlReader.Create(stream);
            } 
        } 

        ///  
        /// Looks up the dictionary content value for the property bag for complex property
        /// 
        /// Name of property to lookup
        /// Was content found 
        /// The property bag or null if the property never existed
        internal DictionaryContent Lookup(string propertyName, out bool found) 
        { 
            int index = this.valueNames.IndexOf(propertyName);
            if (index >= 0) 
            {
                found = true;
                object value = this.valueContents[index];
                if (value != null) 
                {
                    Debug.Assert(value is DictionaryContent, "Must only look for complex properties, primitives must not be found"); 
                    return value as DictionaryContent; 
                }
                else 
                {
                    // It might have been the case that the complex property was itself null
                    return null;
                } 
            }
            else 
            { 
                found = false;
                return null; 
            }
        }

        /// Writes the contents of this SyndicationContent object to the specified XmlWriter. 
        /// The XmlWriter to write to.
        protected override void WriteContentsTo(XmlWriter writer) 
        { 
            if (0 < this.valueNames.Count)
            { 
                writer.WriteStartElement(XmlConstants.AtomPropertiesElementName, XmlConstants.DataWebMetadataNamespace);
                this.WritePropertyContentsTo(writer);
                writer.WriteEndElement();
            } 
        }
 
        /// Writes the contents of this SyndicationContent object to the specified XmlWriter. 
        /// The XmlWriter to write to.
        private void WritePropertyContentsTo(XmlWriter writer) 
        {
            for (int i = 0; i < this.valueNames.Count; i++)
            {
                string propertyName = this.valueNames[i]; 
                string propertyTypeName = this.valueTypes[i];
                object propertyValue = this.valueContents[i]; 
                if (propertyValue == null) 
                {
                    PlainXmlSerializer.WriteNullValue(writer, propertyName, propertyTypeName); 
                }
                else if (propertyValue is DictionaryContent)
                {
                    PlainXmlSerializer.WriteStartElementWithType(writer, propertyName, propertyTypeName); 
                    ((DictionaryContent)propertyValue).WritePropertyContentsTo(writer);
                    writer.WriteEndElement(); 
                } 
                else
                { 
                    string propertyText = (string)propertyValue;
                    PlainXmlSerializer.WriteTextValue(writer, propertyName, propertyTypeName, propertyText);
                }
            } 
        }
 
        #endregion Methods. 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//  
//      Provides a syndication content that holds name/value pairs.
//  
// 
// @owner [....]
//--------------------------------------------------------------------- 

namespace System.Data.Services.Serializers
{
    #region Namespaces. 

    using System.Collections.Generic; 
    using System.Diagnostics; 
    using System.ServiceModel.Syndication;
    using System.Xml; 

    #endregion Namespaces.

    ///  
    /// Use this class to hold the name/value pairs of content that will be written
    /// to the content element of a syndication item. 
    ///  
    internal class DictionaryContent : SyndicationContent
    { 
        #region Private fields.

        /// Content for a property value: one of a string, dictionary or null.
        private List valueContents; 

        /// Names for property values. 
        private List valueNames; 

        /// Declared type names for property values. 
        private List valueTypes;

        #endregion Private fields.
 
        #region Constructors.
 
        /// Initializes a new DictionaryContent instance. 
        public DictionaryContent()
        { 
            this.valueContents = new List();
            this.valueTypes = new List();
            this.valueNames = new List();
        } 

        /// Initializes a new DictionaryContent instance. 
        /// Initial capacity for entries. 
        public DictionaryContent(int capacity)
        { 
            this.valueContents = new List(capacity);
            this.valueTypes = new List(capacity);
            this.valueNames = new List(capacity);
        } 

        ///  
        /// Initializes a new DictionaryContent instance by copying values from 
        /// the specified one.
        ///  
        /// Dictionary to copy content from.
        /// This produces a shallow copy only.
        private DictionaryContent(DictionaryContent other)
        { 
            Debug.Assert(other != null, "other != null");
            this.valueContents = other.valueContents; 
            this.valueTypes = other.valueTypes; 
            this.valueNames = other.valueNames;
        } 

        #endregion Constructors.

        #region Properties. 

        /// The MIME type of this content. 
        public override string Type 
        {
            get { return XmlConstants.MimeApplicationXml; } 
        }

        /// True if there is no properties to serialize out.
        public bool IsEmpty 
        {
            get { return this.valueNames.Count == 0; } 
        } 

        #endregion Properties. 

        #region Methods.

        /// Creates a shallow copy of this content. 
        /// A shallow copy of this content.
        public override SyndicationContent Clone() 
        { 
            return new DictionaryContent(this);
        } 

        /// Adds the specified property.
        /// Property name.
        /// Expected type name for value. 
        /// Property value in text form.
        internal void Add(string name, string expectedTypeName, object value) 
        { 
            Debug.Assert(value != null, "value != null -- otherwise AddNull should have been called.");
            Debug.Assert( 
                value is DictionaryContent || value is string,
                "value is DictionaryContent || value is string -- only sub-dictionaries and formatted strings are allowed.");
            Debug.Assert(!this.valueNames.Contains(name), "!this.valueNames.Contains(name) -- otherwise repeated property set.");
            this.valueNames.Add(name); 
            this.valueTypes.Add(expectedTypeName);
            this.valueContents.Add(value); 
            Debug.Assert(this.valueNames.Count == this.valueTypes.Count, "this.valueNames.Count == this.valueTypes.Count"); 
            Debug.Assert(this.valueNames.Count == this.valueContents.Count, "this.valueNames.Count == this.valueContents.Count");
        } 

        /// Adds a property with a null value.
        /// Type for the property.
        /// Property name. 
        internal void AddNull(string expectedTypeName, string name)
        { 
            Debug.Assert(!this.valueNames.Contains(name), "!this.valueNames.Contains(name) -- otherwise repeated property set."); 
            this.valueNames.Add(name);
            this.valueTypes.Add(expectedTypeName); 
            this.valueContents.Add(null);
            Debug.Assert(this.valueNames.Count == this.valueTypes.Count, "this.valueNames.Count == this.valueTypes.Count");
            Debug.Assert(this.valueNames.Count == this.valueContents.Count, "this.valueNames.Count == this.valueContents.Count");
        } 

        ///  
        /// Gets a XmlReader to the property contents. 
        /// 
        /// XmlReader for the property contents. 
        internal XmlReader GetPropertyContentsReader()
        {
            Debug.Assert(!this.IsEmpty, "!this.IsEmpty -- calling with 0 properties is disallowed.");
 
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            using (XmlWriter writer = XmlWriter.Create(stream)) 
            { 
                writer.WriteStartElement(XmlConstants.DataWebMetadataNamespacePrefix, XmlConstants.AtomPropertiesElementName, XmlConstants.DataWebMetadataNamespace);
                writer.WriteAttributeString(XmlConstants.XmlnsNamespacePrefix, XmlConstants.DataWebNamespacePrefix, null, XmlConstants.DataWebNamespace); 
                this.WritePropertyContentsTo(writer);
                writer.WriteEndElement();
                writer.Flush();
                stream.Position = 0; 
                return XmlReader.Create(stream);
            } 
        } 

        ///  
        /// Looks up the dictionary content value for the property bag for complex property
        /// 
        /// Name of property to lookup
        /// Was content found 
        /// The property bag or null if the property never existed
        internal DictionaryContent Lookup(string propertyName, out bool found) 
        { 
            int index = this.valueNames.IndexOf(propertyName);
            if (index >= 0) 
            {
                found = true;
                object value = this.valueContents[index];
                if (value != null) 
                {
                    Debug.Assert(value is DictionaryContent, "Must only look for complex properties, primitives must not be found"); 
                    return value as DictionaryContent; 
                }
                else 
                {
                    // It might have been the case that the complex property was itself null
                    return null;
                } 
            }
            else 
            { 
                found = false;
                return null; 
            }
        }

        /// Writes the contents of this SyndicationContent object to the specified XmlWriter. 
        /// The XmlWriter to write to.
        protected override void WriteContentsTo(XmlWriter writer) 
        { 
            if (0 < this.valueNames.Count)
            { 
                writer.WriteStartElement(XmlConstants.AtomPropertiesElementName, XmlConstants.DataWebMetadataNamespace);
                this.WritePropertyContentsTo(writer);
                writer.WriteEndElement();
            } 
        }
 
        /// Writes the contents of this SyndicationContent object to the specified XmlWriter. 
        /// The XmlWriter to write to.
        private void WritePropertyContentsTo(XmlWriter writer) 
        {
            for (int i = 0; i < this.valueNames.Count; i++)
            {
                string propertyName = this.valueNames[i]; 
                string propertyTypeName = this.valueTypes[i];
                object propertyValue = this.valueContents[i]; 
                if (propertyValue == null) 
                {
                    PlainXmlSerializer.WriteNullValue(writer, propertyName, propertyTypeName); 
                }
                else if (propertyValue is DictionaryContent)
                {
                    PlainXmlSerializer.WriteStartElementWithType(writer, propertyName, propertyTypeName); 
                    ((DictionaryContent)propertyValue).WritePropertyContentsTo(writer);
                    writer.WriteEndElement(); 
                } 
                else
                { 
                    string propertyText = (string)propertyValue;
                    PlainXmlSerializer.WriteTextValue(writer, propertyName, propertyTypeName, propertyText);
                }
            } 
        }
 
        #endregion Methods. 
    }
} 

// 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