TypeElement.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / ndp / fx / src / DataEntity / System / Data / EntityModel / SchemaObjectModel / TypeElement.cs / 2 / TypeElement.cs

                            //---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// 
// @owner       [....]
// @backupOwner [....] 
//--------------------------------------------------------------------- 
using System;
using System.Collections.Generic; 
using System.Text;
using System.Data.Metadata.Edm;
using System.Xml;
using System.Data.Common; 
using System.Diagnostics;
 
namespace System.Data.EntityModel.SchemaObjectModel 
{
    ///  
    /// Responsible for parsing Type ProviderManifest
    /// xml elements
    /// 
    internal class TypeElement : SchemaType 
    {
 
        PrimitiveType _primitiveType = new PrimitiveType(); 
        List _facetDescriptions = new List();
 
        public TypeElement(Schema parent)
            : base(parent)
        {
            _primitiveType.NamespaceName = Schema.Namespace; 
        }
 
 
        protected override bool HandleElement(XmlReader reader)
        { 
            if (base.HandleElement(reader))
            {
                return true;
            } 
            else if (CanHandleElement(reader, XmlConstants.FacetDescriptionsElement))
            { 
                SkipThroughElement(reader); 
                return true;
            } 
            else if (CanHandleElement(reader, XmlConstants.PrecisionElement))
            {
                HandlePrecisionElement(reader);
                return true; 
            }
            else if (CanHandleElement(reader, XmlConstants.ScaleElement)) 
            { 
                HandleScaleElement(reader);
                return true; 
            }
            else if (CanHandleElement(reader, XmlConstants.MaxLengthElement))
            {
                HandleMaxLengthElement(reader); 
                return true;
            } 
            else if (CanHandleElement(reader, XmlConstants.UnicodeElement)) 
            {
                HandleUnicodeElement(reader); 
                return true;
            }
            else if (CanHandleElement(reader, XmlConstants.FixedLengthElement))
            { 
                HandleFixedLengthElement(reader);
                return true; 
            } 
            return false;
        } 

        protected override bool HandleAttribute(XmlReader reader)
        {
            if (base.HandleAttribute(reader)) 
            {
                return true; 
            } 
            else if (CanHandleAttribute(reader, XmlConstants.PrimitiveTypeKindAttribute))
            { 
                HandlePrimitiveTypeKindAttribute(reader);
                return true;
            }
 
            return false;
        } 
 
        /////////////////////////////////////////////////////////////////////
        // Element Handlers 

        /// 
        /// Handler for the Precision element
        ///  
        /// xml reader currently positioned at Precision element
        private void HandlePrecisionElement(XmlReader reader) 
        { 
            Debug.Assert(reader != null);
            ByteFacetDescriptionElement facetDescription = new ByteFacetDescriptionElement(this, DbProviderManifest.PrecisionFacetName); 
            facetDescription.Parse(reader);


            // Bug 476451: Preventing decimal precision to be more than 29 since clr 
            // decimal is capable of holding decimals upto 29 precision
            if ((this.PrimitiveType.PrimitiveTypeKind == PrimitiveTypeKind.Decimal) && (facetDescription.MaxValue > EdmProviderManifest.MaximumDecimalPrecision)) 
            { 
                AddError(ErrorCode.PrecisionMoreThanAllowedMax,
                         EdmSchemaErrorSeverity.Error, 
                         System.Data.Entity.Strings.PrecisionMoreThanAllowedMax(facetDescription.MaxValue, EdmProviderManifest.MaximumDecimalPrecision));
            }
            else if (((this.PrimitiveType.PrimitiveTypeKind == PrimitiveTypeKind.DateTime) || (this.PrimitiveType.PrimitiveTypeKind == PrimitiveTypeKind.DateTimeOffset)
                || (this.PrimitiveType.PrimitiveTypeKind == PrimitiveTypeKind.Time)) && (facetDescription.MaxValue > EdmProviderManifest.MaximumDateTimePrecision)) 
            {
                AddError(ErrorCode.PrecisionMoreThanAllowedMax, 
                         EdmSchemaErrorSeverity.Error, 
                         System.Data.Entity.Strings.PrecisionMoreThanAllowedMax(facetDescription.MaxValue, EdmProviderManifest.MaximumDateTimePrecision));
            } 
            else
            {
                _facetDescriptions.Add(facetDescription);
            } 
        }
 
        ///  
        /// Handler for the Scale element
        ///  
        /// xml reader currently positioned at Scale element
        private void HandleScaleElement(XmlReader reader)
        {
            Debug.Assert(reader != null); 
            ByteFacetDescriptionElement facetDescription = new ByteFacetDescriptionElement(this, DbProviderManifest.ScaleFacetName);
            facetDescription.Parse(reader); 
            _facetDescriptions.Add(facetDescription); 
        }
 
        /// 
        /// Handler for the MaxLength element
        /// 
        /// xml reader currently positioned at MaxLength element 
        private void HandleMaxLengthElement(XmlReader reader)
        { 
            Debug.Assert(reader != null); 
            IntegerFacetDescriptionElement facetDescription = new IntegerFacetDescriptionElement(this, DbProviderManifest.MaxLengthFacetName);
            facetDescription.Parse(reader); 
            _facetDescriptions.Add(facetDescription);
        }

        ///  
        /// Handler for the Unicode element
        ///  
        /// xml reader currently positioned at Unicode element 
        private void HandleUnicodeElement(XmlReader reader)
        { 
            Debug.Assert(reader != null);
            BooleanFacetDescriptionElement facetDescription = new BooleanFacetDescriptionElement(this, DbProviderManifest.UnicodeFacetName);
            facetDescription.Parse(reader);
            _facetDescriptions.Add(facetDescription); 
        }
 
        ///  
        /// Handler for the FixedLength element
        ///  
        /// xml reader currently positioned at FixedLength element
        private void HandleFixedLengthElement(XmlReader reader)
        {
            Debug.Assert(reader != null); 
            BooleanFacetDescriptionElement facetDescription = new BooleanFacetDescriptionElement(this, DbProviderManifest.FixedLengthFacetName);
            facetDescription.Parse(reader); 
            _facetDescriptions.Add(facetDescription); 
        }
 
        /////////////////////////////////////////////////////////////////////
        // Attribute Handlers

        ///  
        /// Handler for the PrimitiveTypeKind attribute
        ///  
        /// xml reader currently positioned at Version attribute 
        private void HandlePrimitiveTypeKindAttribute(XmlReader reader)
        { 
            Debug.Assert(reader != null);
            string value = reader.Value;
            try
            { 
                _primitiveType.PrimitiveTypeKind = (PrimitiveTypeKind)Enum.Parse(typeof(PrimitiveTypeKind), value);
                _primitiveType.BaseType = MetadataItem.EdmProviderManifest.GetPrimitiveType(_primitiveType.PrimitiveTypeKind); 
            } 
            catch (ArgumentException)
            { 
                AddError(ErrorCode.InvalidPrimitiveTypeKind, EdmSchemaErrorSeverity.Error,
                    System.Data.Entity.Strings.InvalidPrimitiveTypeKind(value));
            }
        } 

        public override string Name 
        { 
            get
            { 
                return _primitiveType.Name;
            }
            set
            { 
                _primitiveType.Name = value;
            } 
        } 

        public PrimitiveType PrimitiveType 
        {
            get
            {
                return _primitiveType; 
            }
        } 
 
        public IEnumerable FacetDescriptions
        { 
            get
            {
                foreach (FacetDescriptionElement element in _facetDescriptions)
                { 
                    yield return element.FacetDescription;
                } 
            } 
        }
 
        internal override void ResolveTopLevelNames()
        {
            base.ResolveTopLevelNames();
 
            // Call validate on the facet descriptions
            foreach (FacetDescriptionElement facetDescription in _facetDescriptions) 
            { 
                try
                { 
                    facetDescription.CreateAndValidateFacetDescription(this.Name);
                }
                catch (ArgumentException e)
                { 
                    AddError(ErrorCode.InvalidFacetInProviderManifest,
                             EdmSchemaErrorSeverity.Error, 
                             e.Message); 
                }
            } 
            // facet descriptions don't have any names to resolve
        }

        internal override void Validate() 
        {
            base.Validate(); 
 
            if (!ValidateSufficientFacets())
            { 
                // the next checks will ---- up, so get out
                // if we had errors
                return;
            } 

            if (!ValidateInterFacetConsistency()) 
            { 
                return;
            } 
        }

        private bool ValidateInterFacetConsistency()
        { 
            if (PrimitiveType.PrimitiveTypeKind == PrimitiveTypeKind.Decimal)
            { 
                FacetDescription precisionFacetDescription = Helper.GetFacet(FacetDescriptions, EdmProviderManifest.PrecisionFacetName); 
                FacetDescription scaleFacetDescription = Helper.GetFacet(FacetDescriptions, EdmProviderManifest.ScaleFacetName);
 
                if(precisionFacetDescription.MaxValue.Value < scaleFacetDescription.MaxValue.Value)
                {
                    AddError(ErrorCode.BadPrecisionAndScale,
                             EdmSchemaErrorSeverity.Error, 
                             System.Data.Entity.Strings.BadPrecisionAndScale(
                                           precisionFacetDescription.MaxValue.Value, 
                                           scaleFacetDescription.MaxValue.Value)); 
                    return false;
                } 
            }

            return true;
        } 

        private bool ValidateSufficientFacets() 
        { 
            PrimitiveType baseType = _primitiveType.BaseType as PrimitiveType;
            // the base type will be an edm type 
            // the edm type is the athority for which facets are required
            if (baseType == null)
            {
                // an error will already have been added for this 
                return false;
            } 
 
            bool addedErrors = false;
            foreach (FacetDescription systemFacetDescription in baseType.FacetDescriptions) 
            {
                FacetDescription providerFacetDescription = Helper.GetFacet(FacetDescriptions, systemFacetDescription.FacetName);
                if (providerFacetDescription == null)
                { 
                    AddError(ErrorCode.RequiredFacetMissing,
                             EdmSchemaErrorSeverity.Error, 
                             System.Data.Entity.Strings.MissingFacetDescription( 
                                           PrimitiveType.Name,
                                           PrimitiveType.PrimitiveTypeKind, 
                                           systemFacetDescription.FacetName));
                    addedErrors = true;
                }
            } 

            return !addedErrors; 
        } 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// 
// @owner       [....]
// @backupOwner [....] 
//--------------------------------------------------------------------- 
using System;
using System.Collections.Generic; 
using System.Text;
using System.Data.Metadata.Edm;
using System.Xml;
using System.Data.Common; 
using System.Diagnostics;
 
namespace System.Data.EntityModel.SchemaObjectModel 
{
    ///  
    /// Responsible for parsing Type ProviderManifest
    /// xml elements
    /// 
    internal class TypeElement : SchemaType 
    {
 
        PrimitiveType _primitiveType = new PrimitiveType(); 
        List _facetDescriptions = new List();
 
        public TypeElement(Schema parent)
            : base(parent)
        {
            _primitiveType.NamespaceName = Schema.Namespace; 
        }
 
 
        protected override bool HandleElement(XmlReader reader)
        { 
            if (base.HandleElement(reader))
            {
                return true;
            } 
            else if (CanHandleElement(reader, XmlConstants.FacetDescriptionsElement))
            { 
                SkipThroughElement(reader); 
                return true;
            } 
            else if (CanHandleElement(reader, XmlConstants.PrecisionElement))
            {
                HandlePrecisionElement(reader);
                return true; 
            }
            else if (CanHandleElement(reader, XmlConstants.ScaleElement)) 
            { 
                HandleScaleElement(reader);
                return true; 
            }
            else if (CanHandleElement(reader, XmlConstants.MaxLengthElement))
            {
                HandleMaxLengthElement(reader); 
                return true;
            } 
            else if (CanHandleElement(reader, XmlConstants.UnicodeElement)) 
            {
                HandleUnicodeElement(reader); 
                return true;
            }
            else if (CanHandleElement(reader, XmlConstants.FixedLengthElement))
            { 
                HandleFixedLengthElement(reader);
                return true; 
            } 
            return false;
        } 

        protected override bool HandleAttribute(XmlReader reader)
        {
            if (base.HandleAttribute(reader)) 
            {
                return true; 
            } 
            else if (CanHandleAttribute(reader, XmlConstants.PrimitiveTypeKindAttribute))
            { 
                HandlePrimitiveTypeKindAttribute(reader);
                return true;
            }
 
            return false;
        } 
 
        /////////////////////////////////////////////////////////////////////
        // Element Handlers 

        /// 
        /// Handler for the Precision element
        ///  
        /// xml reader currently positioned at Precision element
        private void HandlePrecisionElement(XmlReader reader) 
        { 
            Debug.Assert(reader != null);
            ByteFacetDescriptionElement facetDescription = new ByteFacetDescriptionElement(this, DbProviderManifest.PrecisionFacetName); 
            facetDescription.Parse(reader);


            // Bug 476451: Preventing decimal precision to be more than 29 since clr 
            // decimal is capable of holding decimals upto 29 precision
            if ((this.PrimitiveType.PrimitiveTypeKind == PrimitiveTypeKind.Decimal) && (facetDescription.MaxValue > EdmProviderManifest.MaximumDecimalPrecision)) 
            { 
                AddError(ErrorCode.PrecisionMoreThanAllowedMax,
                         EdmSchemaErrorSeverity.Error, 
                         System.Data.Entity.Strings.PrecisionMoreThanAllowedMax(facetDescription.MaxValue, EdmProviderManifest.MaximumDecimalPrecision));
            }
            else if (((this.PrimitiveType.PrimitiveTypeKind == PrimitiveTypeKind.DateTime) || (this.PrimitiveType.PrimitiveTypeKind == PrimitiveTypeKind.DateTimeOffset)
                || (this.PrimitiveType.PrimitiveTypeKind == PrimitiveTypeKind.Time)) && (facetDescription.MaxValue > EdmProviderManifest.MaximumDateTimePrecision)) 
            {
                AddError(ErrorCode.PrecisionMoreThanAllowedMax, 
                         EdmSchemaErrorSeverity.Error, 
                         System.Data.Entity.Strings.PrecisionMoreThanAllowedMax(facetDescription.MaxValue, EdmProviderManifest.MaximumDateTimePrecision));
            } 
            else
            {
                _facetDescriptions.Add(facetDescription);
            } 
        }
 
        ///  
        /// Handler for the Scale element
        ///  
        /// xml reader currently positioned at Scale element
        private void HandleScaleElement(XmlReader reader)
        {
            Debug.Assert(reader != null); 
            ByteFacetDescriptionElement facetDescription = new ByteFacetDescriptionElement(this, DbProviderManifest.ScaleFacetName);
            facetDescription.Parse(reader); 
            _facetDescriptions.Add(facetDescription); 
        }
 
        /// 
        /// Handler for the MaxLength element
        /// 
        /// xml reader currently positioned at MaxLength element 
        private void HandleMaxLengthElement(XmlReader reader)
        { 
            Debug.Assert(reader != null); 
            IntegerFacetDescriptionElement facetDescription = new IntegerFacetDescriptionElement(this, DbProviderManifest.MaxLengthFacetName);
            facetDescription.Parse(reader); 
            _facetDescriptions.Add(facetDescription);
        }

        ///  
        /// Handler for the Unicode element
        ///  
        /// xml reader currently positioned at Unicode element 
        private void HandleUnicodeElement(XmlReader reader)
        { 
            Debug.Assert(reader != null);
            BooleanFacetDescriptionElement facetDescription = new BooleanFacetDescriptionElement(this, DbProviderManifest.UnicodeFacetName);
            facetDescription.Parse(reader);
            _facetDescriptions.Add(facetDescription); 
        }
 
        ///  
        /// Handler for the FixedLength element
        ///  
        /// xml reader currently positioned at FixedLength element
        private void HandleFixedLengthElement(XmlReader reader)
        {
            Debug.Assert(reader != null); 
            BooleanFacetDescriptionElement facetDescription = new BooleanFacetDescriptionElement(this, DbProviderManifest.FixedLengthFacetName);
            facetDescription.Parse(reader); 
            _facetDescriptions.Add(facetDescription); 
        }
 
        /////////////////////////////////////////////////////////////////////
        // Attribute Handlers

        ///  
        /// Handler for the PrimitiveTypeKind attribute
        ///  
        /// xml reader currently positioned at Version attribute 
        private void HandlePrimitiveTypeKindAttribute(XmlReader reader)
        { 
            Debug.Assert(reader != null);
            string value = reader.Value;
            try
            { 
                _primitiveType.PrimitiveTypeKind = (PrimitiveTypeKind)Enum.Parse(typeof(PrimitiveTypeKind), value);
                _primitiveType.BaseType = MetadataItem.EdmProviderManifest.GetPrimitiveType(_primitiveType.PrimitiveTypeKind); 
            } 
            catch (ArgumentException)
            { 
                AddError(ErrorCode.InvalidPrimitiveTypeKind, EdmSchemaErrorSeverity.Error,
                    System.Data.Entity.Strings.InvalidPrimitiveTypeKind(value));
            }
        } 

        public override string Name 
        { 
            get
            { 
                return _primitiveType.Name;
            }
            set
            { 
                _primitiveType.Name = value;
            } 
        } 

        public PrimitiveType PrimitiveType 
        {
            get
            {
                return _primitiveType; 
            }
        } 
 
        public IEnumerable FacetDescriptions
        { 
            get
            {
                foreach (FacetDescriptionElement element in _facetDescriptions)
                { 
                    yield return element.FacetDescription;
                } 
            } 
        }
 
        internal override void ResolveTopLevelNames()
        {
            base.ResolveTopLevelNames();
 
            // Call validate on the facet descriptions
            foreach (FacetDescriptionElement facetDescription in _facetDescriptions) 
            { 
                try
                { 
                    facetDescription.CreateAndValidateFacetDescription(this.Name);
                }
                catch (ArgumentException e)
                { 
                    AddError(ErrorCode.InvalidFacetInProviderManifest,
                             EdmSchemaErrorSeverity.Error, 
                             e.Message); 
                }
            } 
            // facet descriptions don't have any names to resolve
        }

        internal override void Validate() 
        {
            base.Validate(); 
 
            if (!ValidateSufficientFacets())
            { 
                // the next checks will ---- up, so get out
                // if we had errors
                return;
            } 

            if (!ValidateInterFacetConsistency()) 
            { 
                return;
            } 
        }

        private bool ValidateInterFacetConsistency()
        { 
            if (PrimitiveType.PrimitiveTypeKind == PrimitiveTypeKind.Decimal)
            { 
                FacetDescription precisionFacetDescription = Helper.GetFacet(FacetDescriptions, EdmProviderManifest.PrecisionFacetName); 
                FacetDescription scaleFacetDescription = Helper.GetFacet(FacetDescriptions, EdmProviderManifest.ScaleFacetName);
 
                if(precisionFacetDescription.MaxValue.Value < scaleFacetDescription.MaxValue.Value)
                {
                    AddError(ErrorCode.BadPrecisionAndScale,
                             EdmSchemaErrorSeverity.Error, 
                             System.Data.Entity.Strings.BadPrecisionAndScale(
                                           precisionFacetDescription.MaxValue.Value, 
                                           scaleFacetDescription.MaxValue.Value)); 
                    return false;
                } 
            }

            return true;
        } 

        private bool ValidateSufficientFacets() 
        { 
            PrimitiveType baseType = _primitiveType.BaseType as PrimitiveType;
            // the base type will be an edm type 
            // the edm type is the athority for which facets are required
            if (baseType == null)
            {
                // an error will already have been added for this 
                return false;
            } 
 
            bool addedErrors = false;
            foreach (FacetDescription systemFacetDescription in baseType.FacetDescriptions) 
            {
                FacetDescription providerFacetDescription = Helper.GetFacet(FacetDescriptions, systemFacetDescription.FacetName);
                if (providerFacetDescription == null)
                { 
                    AddError(ErrorCode.RequiredFacetMissing,
                             EdmSchemaErrorSeverity.Error, 
                             System.Data.Entity.Strings.MissingFacetDescription( 
                                           PrimitiveType.Name,
                                           PrimitiveType.PrimitiveTypeKind, 
                                           systemFacetDescription.FacetName));
                    addedErrors = true;
                }
            } 

            return !addedErrors; 
        } 
    }
} 

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