TypeElement.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 / DataEntity / System / Data / EntityModel / SchemaObjectModel / TypeElement.cs / 1305376 / 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);

            _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 barf, 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