Beispiel #1
0
        internal static bool TryWriteCsdl(IEdmModel model, Func<string, XmlWriter> writerProvider, bool singleFileExpected, out IEnumerable<EdmError> errors)
        {
            EdmUtil.CheckArgumentNull(model, "model");
            EdmUtil.CheckArgumentNull(writerProvider, "writerProvider");

            errors = model.GetSerializationErrors();
            if (errors.FirstOrDefault() != null)
            {
                return false;
            }

            IEnumerable<EdmSchema> schemas = new EdmModelSchemaSeparationSerializationVisitor(model).GetSchemas();
            if (schemas.Count() > 1 && singleFileExpected)
            {
                errors = new EdmError[] { new EdmError(new CsdlLocation(0, 0), EdmErrorCode.SingleFileExpected, Edm.Strings.Serializer_SingleFileExpected) };
                return false;
            }

            if (schemas.Count() == 0)
            {
                errors = new EdmError[] { new EdmError(new CsdlLocation(0, 0), EdmErrorCode.NoSchemasProduced, Edm.Strings.Serializer_NoSchemasProduced) };
                return false;
            }

            WriteSchemas(model, schemas, writerProvider);

            errors = Enumerable.Empty<EdmError>();
            return true;
        }
Beispiel #2
0
        /// <summary>
        /// Outputs an EDMX artifact to the provided XmlWriter.
        /// </summary>
        /// <param name="model">Model to be written.</param>
        /// <param name="writer">XmlWriter the generated EDMX will be written to.</param>
        /// <param name="target">Target implementation of the EDMX being generated.</param>
        /// <param name="errors">Errors that prevented successful serialization, or no errors if serialization was successfull. </param>
        /// <returns>A value indicating whether serialization was successful.</returns>
        public static bool TryWriteEdmx(IEdmModel model, XmlWriter writer, EdmxTarget target, out IEnumerable<EdmError> errors)
        {
            EdmUtil.CheckArgumentNull(model, "model");
            EdmUtil.CheckArgumentNull(writer, "writer");

            errors = model.GetSerializationErrors();
            if (errors.FirstOrDefault() != null)
            {
                return false;
            }

            Version edmxVersion = model.GetEdmxVersion();

            if (edmxVersion != null)
            {
                if (!CsdlConstants.SupportedEdmxVersions.ContainsKey(edmxVersion))
                {
                    errors = new EdmError[] { new EdmError(new CsdlLocation(0, 0), EdmErrorCode.UnknownEdmxVersion, Edm.Strings.Serializer_UnknownEdmxVersion) };
                    return false;
                }
            }
            else if (!CsdlConstants.EdmToEdmxVersions.TryGetValue(model.GetEdmVersion() ?? EdmConstants.EdmVersionLatest, out edmxVersion))
            {
                errors = new EdmError[] { new EdmError(new CsdlLocation(0, 0), EdmErrorCode.UnknownEdmVersion, Edm.Strings.Serializer_UnknownEdmVersion) };
                return false;
            }

            IEnumerable<EdmSchema> schemas = new EdmModelSchemaSeparationSerializationVisitor(model).GetSchemas();

            EdmxWriter edmxWriter = new EdmxWriter(model, schemas, writer, edmxVersion, target);
            edmxWriter.WriteEdmx();

            errors = Enumerable.Empty<EdmError>();
            return true;
        }
Beispiel #3
0
		private static bool TestNullabilityMatch(this IEdmTypeReference expressionType, IEdmTypeReference assertedType, EdmLocation location, out IEnumerable<EdmError> discoveredErrors)
		{
			if (assertedType.IsNullable || !expressionType.IsNullable)
			{
				discoveredErrors = Enumerable.Empty<EdmError>();
				return true;
			}
			else
			{
				EdmError[] edmError = new EdmError[1];
				edmError[0] = new EdmError(location, EdmErrorCode.CannotAssertNullableTypeAsNonNullableType, Strings.EdmModel_Validator_Semantic_CannotAssertNullableTypeAsNonNullableType(expressionType.FullName()));
				discoveredErrors = edmError;
				return false;
			}
		}
Beispiel #4
0
        public static bool TryParse(IEnumerable<XmlReader> csdlReaders, out CsdlModel entityModel, out IEnumerable<EdmError> errors)
        {
            EdmUtil.CheckArgumentNull(csdlReaders, "csdlReaders");
            CsdlParser parser = new CsdlParser();
            int readerCount = 0;
            foreach (var inputReader in csdlReaders)
            {
                if (inputReader != null)
                {
                    try
                    {
                        parser.AddReader(inputReader);
                    }
                    catch (XmlException e)
                    {
                        entityModel = null;
                        errors = new EdmError[] { new EdmError(new CsdlLocation(e.LineNumber, e.LinePosition), EdmErrorCode.XmlError, e.Message) };

                        return false;
                    }
                }
                else
                {
                    entityModel = null;
                    errors = new EdmError[] { new EdmError(null, EdmErrorCode.NullXmlReader, Edm.Strings.CsdlParser_NullXmlReader) };

                    return false;
                }

                readerCount++;
            }

            if (readerCount == 0)
            {
                entityModel = null;
                errors = new EdmError[] { new EdmError(null, EdmErrorCode.NoReadersProvided, Edm.Strings.CsdlParser_NoReadersProvided) };

                return false;
            }

            bool success = parser.GetResult(out entityModel, out errors);
            if (!success)
            {
                entityModel = null;
            }

            return success;
        }
Beispiel #5
0
		public static bool TryWriteEdmx(IEdmModel model, XmlWriter writer, EdmxTarget target, out IEnumerable<EdmError> errors)
		{
			EdmUtil.CheckArgumentNull<IEdmModel>(model, "model");
			EdmUtil.CheckArgumentNull<XmlWriter>(writer, "writer");
			errors = model.GetSerializationErrors();
			if (errors.FirstOrDefault<EdmError>() == null)
			{
				Version edmxVersion = model.GetEdmxVersion();
				if (edmxVersion == null)
				{
					Dictionary<Version, Version> edmToEdmxVersions = CsdlConstants.EdmToEdmxVersions;
					Version edmVersion = model.GetEdmVersion();
					Version edmVersionLatest = edmVersion;
					if (edmVersion == null)
					{
						edmVersionLatest = EdmConstants.EdmVersionLatest;
					}
					if (!edmToEdmxVersions.TryGetValue(edmVersionLatest, out edmxVersion))
					{
						EdmError[] edmError = new EdmError[1];
						edmError[0] = new EdmError(new CsdlLocation(0, 0), EdmErrorCode.UnknownEdmVersion, Strings.Serializer_UnknownEdmVersion);
						errors = edmError;
						return false;
					}
				}
				else
				{
					if (!CsdlConstants.SupportedEdmxVersions.ContainsKey(edmxVersion))
					{
						EdmError[] edmErrorArray = new EdmError[1];
						edmErrorArray[0] = new EdmError(new CsdlLocation(0, 0), EdmErrorCode.UnknownEdmxVersion, Strings.Serializer_UnknownEdmxVersion);
						errors = edmErrorArray;
						return false;
					}
				}
				IEnumerable<EdmSchema> schemas = (new EdmModelSchemaSeparationSerializationVisitor(model)).GetSchemas();
				EdmxWriter edmxWriter = new EdmxWriter(model, schemas, writer, edmxVersion, target);
				edmxWriter.WriteEdmx();
				errors = Enumerable.Empty<EdmError>();
				return true;
			}
			else
			{
				return false;
			}
		}
Beispiel #6
0
        private static bool TryAssertStringConstantAsType(IEdmStringConstantExpression expression, IEdmTypeReference type, out IEnumerable <EdmError> discoveredErrors)
        {
            if (!type.IsString())
            {
                discoveredErrors = new EdmError[] { new EdmError(expression.Location(), EdmErrorCode.ExpressionPrimitiveKindNotValidForAssertedType, Edm.Strings.EdmModel_Validator_Semantic_ExpressionPrimitiveKindNotValidForAssertedType) };
                return(false);
            }

            IEdmStringTypeReference stringType = type.AsString();

            if (stringType.MaxLength.HasValue && expression.Value.Length > stringType.MaxLength.Value)
            {
                discoveredErrors = new EdmError[] { new EdmError(expression.Location(), EdmErrorCode.StringConstantLengthOutOfRange, Edm.Strings.EdmModel_Validator_Semantic_StringConstantLengthOutOfRange(expression.Value.Length, stringType.MaxLength.Value)) };
                return(false);
            }

            discoveredErrors = Enumerable.Empty <EdmError>();
            return(true);
        }
Beispiel #7
0
        private static bool TestTypeMatch(this IEdmType expressionType, IEdmType assertedType, EdmLocation location, bool matchExactly, out IEnumerable <EdmError> discoveredErrors)
        {
            if (matchExactly)
            {
                if (!expressionType.IsEquivalentTo(assertedType))
                {
                    discoveredErrors = new EdmError[] { new EdmError(location, EdmErrorCode.ExpressionNotValidForTheAssertedType, Edm.Strings.EdmModel_Validator_Semantic_ExpressionNotValidForTheAssertedType) };
                    return(false);
                }
            }
            else
            {
                // A bad type matches anything (so as to avoid generating spurious errors).
                if (expressionType.TypeKind == EdmTypeKind.None || expressionType.IsBad())
                {
                    discoveredErrors = Enumerable.Empty <EdmError>();
                    return(true);
                }

                if (expressionType.TypeKind == EdmTypeKind.Primitive && assertedType.TypeKind == EdmTypeKind.Primitive)
                {
                    IEdmPrimitiveType primitiveExpressionType = expressionType as IEdmPrimitiveType;
                    IEdmPrimitiveType primitiveAssertedType   = assertedType as IEdmPrimitiveType;
                    if (!primitiveExpressionType.PrimitiveKind.PromotesTo(primitiveAssertedType.PrimitiveKind))
                    {
                        discoveredErrors = new EdmError[] { new EdmError(location, EdmErrorCode.ExpressionPrimitiveKindNotValidForAssertedType, Edm.Strings.EdmModel_Validator_Semantic_ExpressionPrimitiveKindCannotPromoteToAssertedType(expressionType.ToTraceString(), assertedType.ToTraceString())) };
                        return(false);
                    }
                }
                else
                {
                    if (!expressionType.IsOrInheritsFrom(assertedType))
                    {
                        discoveredErrors = new EdmError[] { new EdmError(location, EdmErrorCode.ExpressionNotValidForTheAssertedType, Edm.Strings.EdmModel_Validator_Semantic_ExpressionNotValidForTheAssertedType) };
                        return(false);
                    }
                }
            }

            discoveredErrors = Enumerable.Empty <EdmError>();
            return(true);
        }
Beispiel #8
0
 private static bool TestTypeMatch(this IEdmTypeReference expressionType, IEdmTypeReference assertedType, EdmLocation location, out IEnumerable <EdmError> discoveredErrors)
 {
     if (expressionType.TestNullabilityMatch(assertedType, location, out discoveredErrors))
     {
         if (expressionType.TypeKind() == EdmTypeKind.None || expressionType.IsBad())
         {
             discoveredErrors = Enumerable.Empty <EdmError>();
             return(true);
         }
         else
         {
             if (!expressionType.IsPrimitive() || !assertedType.IsPrimitive())
             {
                 if (!expressionType.Definition.IsEquivalentTo(assertedType.Definition))
                 {
                     EdmError[] edmError = new EdmError[1];
                     edmError[0]      = new EdmError(location, EdmErrorCode.ExpressionNotValidForTheAssertedType, Strings.EdmModel_Validator_Semantic_ExpressionNotValidForTheAssertedType);
                     discoveredErrors = edmError;
                     return(false);
                 }
             }
             else
             {
                 if (!expressionType.PrimitiveKind().PromotesTo(assertedType.AsPrimitive().PrimitiveKind()))
                 {
                     EdmError[] edmErrorArray = new EdmError[1];
                     edmErrorArray[0] = new EdmError(location, EdmErrorCode.ExpressionPrimitiveKindNotValidForAssertedType, Strings.EdmModel_Validator_Semantic_ExpressionPrimitiveKindCannotPromoteToAssertedType(expressionType.FullName(), assertedType.FullName()));
                     discoveredErrors = edmErrorArray;
                     return(false);
                 }
             }
             discoveredErrors = Enumerable.Empty <EdmError>();
             return(true);
         }
     }
     else
     {
         return(false);
     }
 }
Beispiel #9
0
        internal static bool TryAssertCollectionAsType(this IEdmCollectionExpression expression, IEdmTypeReference type, IEdmType context, bool matchExactly, out IEnumerable <EdmError> discoveredErrors)
        {
            if (!type.IsCollection())
            {
                discoveredErrors = new EdmError[] { new EdmError(expression.Location(), EdmErrorCode.CollectionExpressionNotValidForNonCollectionType, Edm.Strings.EdmModel_Validator_Semantic_CollectionExpressionNotValidForNonCollectionType) };
                return(false);
            }

            IEdmTypeReference collectionElementType = type.AsCollection().ElementType();
            bool                   success          = true;
            List <EdmError>        errors           = new List <EdmError>();
            IEnumerable <EdmError> recursiveErrors;

            foreach (IEdmExpression element in expression.Elements)
            {
                success = TryAssertType(element, collectionElementType, context, matchExactly, out recursiveErrors) && success;
                errors.AddRange(recursiveErrors);
            }

            discoveredErrors = errors;
            return(success);
        }
Beispiel #10
0
		internal static bool TryWriteCsdl(IEdmModel model, Func<string, XmlWriter> writerProvider, bool singleFileExpected, out IEnumerable<EdmError> errors)
		{
			EdmUtil.CheckArgumentNull<IEdmModel>(model, "model");
			EdmUtil.CheckArgumentNull<Func<string, XmlWriter>>(writerProvider, "writerProvider");
			errors = model.GetSerializationErrors();
			if (errors.FirstOrDefault<EdmError>() == null)
			{
				IEnumerable<EdmSchema> schemas = (new EdmModelSchemaSeparationSerializationVisitor(model)).GetSchemas();
				if (schemas.Count<EdmSchema>() <= 1 || !singleFileExpected)
				{
					if (schemas.Count<EdmSchema>() != 0)
					{
						CsdlWriter.WriteSchemas(model, schemas, writerProvider);
						errors = Enumerable.Empty<EdmError>();
						return true;
					}
					else
					{
						EdmError[] edmError = new EdmError[1];
						edmError[0] = new EdmError(new CsdlLocation(0, 0), EdmErrorCode.NoSchemasProduced, Strings.Serializer_NoSchemasProduced);
						errors = edmError;
						return false;
					}
				}
				else
				{
					EdmError[] edmErrorArray = new EdmError[1];
					edmErrorArray[0] = new EdmError(new CsdlLocation(0, 0), EdmErrorCode.SingleFileExpected, Strings.Serializer_SingleFileExpected);
					errors = edmErrorArray;
					return false;
				}
			}
			else
			{
				return false;
			}
		}
		private IEdmAssociationEnd ComputeTo()
		{
			string toRole = this.navigationProperty.ToRole;
			string fromRole = this.navigationProperty.FromRole;
			this.Model.SetAssociationEndName(this, fromRole);
			IEdmAssociation association = this.Association;
			if (toRole != fromRole)
			{
				if (association.End1.Name != toRole)
				{
					if (association.End2.Name != toRole)
					{
						EdmError[] edmError = new EdmError[1];
						edmError[0] = new EdmError(base.Location, EdmErrorCode.BadNavigationProperty, Strings.EdmModel_Validator_Semantic_BadNavigationPropertyUndefinedRole(this.Name, toRole, association.Name));
						return new CsdlSemanticsNavigationProperty.BadCsdlSemanticsNavigationPropertyToEnd(this.Association, toRole, edmError);
					}
					else
					{
						return association.End2;
					}
				}
				else
				{
					return association.End1;
				}
			}
			else
			{
				EdmError[] edmErrorArray = new EdmError[1];
				edmErrorArray[0] = new EdmError(base.Location, EdmErrorCode.BadNavigationProperty, Strings.EdmModel_Validator_Semantic_BadNavigationPropertyRolesCannotBeTheSame(this.Name));
				return new CsdlSemanticsNavigationProperty.BadCsdlSemanticsNavigationPropertyToEnd(association, toRole, edmErrorArray);
			}
		}
Beispiel #12
0
        public static bool TryAssertType(this IEdmExpression expression, IEdmTypeReference type, out IEnumerable <EdmError> discoveredErrors)
        {
            EdmError[] edmError;
            EdmUtil.CheckArgumentNull <IEdmExpression>(expression, "expression");
            if (type == null || type.TypeKind() == EdmTypeKind.None)
            {
                discoveredErrors = Enumerable.Empty <EdmError>();
                return(true);
            }
            else
            {
                EdmExpressionKind expressionKind = expression.ExpressionKind;
                switch (expressionKind)
                {
                case EdmExpressionKind.BinaryConstant:
                case EdmExpressionKind.BooleanConstant:
                case EdmExpressionKind.DateTimeConstant:
                case EdmExpressionKind.DateTimeOffsetConstant:
                case EdmExpressionKind.DecimalConstant:
                case EdmExpressionKind.FloatingConstant:
                case EdmExpressionKind.GuidConstant:
                case EdmExpressionKind.IntegerConstant:
                case EdmExpressionKind.StringConstant:
                case EdmExpressionKind.TimeConstant:
                {
                    IEdmPrimitiveValue edmPrimitiveValue = (IEdmPrimitiveValue)expression;
                    if (edmPrimitiveValue.Type == null)
                    {
                        return(edmPrimitiveValue.TryAssertPrimitiveAsType(type, out discoveredErrors));
                    }
                    else
                    {
                        return(edmPrimitiveValue.Type.TestTypeMatch(type, expression.Location(), out discoveredErrors));
                    }
                }

                case EdmExpressionKind.Null:
                {
                    return(((IEdmNullExpression)expression).TryAssertNullAsType(type, out discoveredErrors));
                }

                case EdmExpressionKind.Record:
                {
                    IEdmRecordExpression edmRecordExpression = (IEdmRecordExpression)expression;
                    if (edmRecordExpression.DeclaredType == null)
                    {
                        return(edmRecordExpression.TryAssertRecordAsType(type, out discoveredErrors));
                    }
                    else
                    {
                        return(edmRecordExpression.DeclaredType.TestTypeMatch(type, expression.Location(), out discoveredErrors));
                    }
                }

                case EdmExpressionKind.Collection:
                {
                    IEdmCollectionExpression edmCollectionExpression = (IEdmCollectionExpression)expression;
                    if (edmCollectionExpression.DeclaredType == null)
                    {
                        return(edmCollectionExpression.TryAssertCollectionAsType(type, out discoveredErrors));
                    }
                    else
                    {
                        return(edmCollectionExpression.DeclaredType.TestTypeMatch(type, expression.Location(), out discoveredErrors));
                    }
                }

                case EdmExpressionKind.Path:
                {
                    return(((IEdmPathExpression)expression).TryAssertPathAsType(type, out discoveredErrors));
                }

                case EdmExpressionKind.ParameterReference:
                case EdmExpressionKind.FunctionReference:
                case EdmExpressionKind.PropertyReference:
                case EdmExpressionKind.ValueTermReference:
                case EdmExpressionKind.EntitySetReference:
                case EdmExpressionKind.EnumMemberReference:
                {
                    edmError         = new EdmError[1];
                    edmError[0]      = new EdmError(expression.Location(), EdmErrorCode.ExpressionNotValidForTheAssertedType, Strings.EdmModel_Validator_Semantic_ExpressionNotValidForTheAssertedType);
                    discoveredErrors = edmError;
                    return(false);
                }

                case EdmExpressionKind.If:
                {
                    return(((IEdmIfExpression)expression).TryAssertIfAsType(type, out discoveredErrors));
                }

                case EdmExpressionKind.AssertType:
                {
                    return(((IEdmAssertTypeExpression)expression).Type.TestTypeMatch(type, expression.Location(), out discoveredErrors));
                }

                case EdmExpressionKind.IsType:
                {
                    return(EdmCoreModel.Instance.GetBoolean(false).TestTypeMatch(type, expression.Location(), out discoveredErrors));
                }

                case EdmExpressionKind.FunctionApplication:
                {
                    IEdmApplyExpression edmApplyExpression = (IEdmApplyExpression)expression;
                    if (edmApplyExpression.AppliedFunction != null)
                    {
                        IEdmFunctionBase appliedFunction = edmApplyExpression.AppliedFunction as IEdmFunctionBase;
                        if (appliedFunction != null)
                        {
                            return(appliedFunction.ReturnType.TestTypeMatch(type, expression.Location(), out discoveredErrors));
                        }
                    }
                    discoveredErrors = Enumerable.Empty <EdmError>();
                    return(true);
                }

                case EdmExpressionKind.LabeledExpressionReference:
                {
                    return(((IEdmLabeledExpressionReferenceExpression)expression).ReferencedLabeledExpression.TryAssertType(type, out discoveredErrors));
                }

                case EdmExpressionKind.Labeled:
                {
                    return(((IEdmLabeledExpression)expression).Expression.TryAssertType(type, out discoveredErrors));
                }

                default:
                {
                    edmError         = new EdmError[1];
                    edmError[0]      = new EdmError(expression.Location(), EdmErrorCode.ExpressionNotValidForTheAssertedType, Strings.EdmModel_Validator_Semantic_ExpressionNotValidForTheAssertedType);
                    discoveredErrors = edmError;
                    return(false);
                }
                }
            }
        }
Beispiel #13
0
        internal static bool TryAssertRecordAsType(this IEdmRecordExpression expression, IEdmTypeReference type, out IEnumerable <EdmError> discoveredErrors)
        {
            IEnumerable <EdmError> edmErrors = null;

            EdmUtil.CheckArgumentNull <IEdmRecordExpression>(expression, "expression");
            EdmUtil.CheckArgumentNull <IEdmTypeReference>(type, "type");
            if (type.IsStructured())
            {
                HashSetInternal <string>    strs       = new HashSetInternal <string>();
                List <EdmError>             edmErrors1 = new List <EdmError>();
                IEdmStructuredTypeReference edmStructuredTypeReference = type.AsStructured();
                IEnumerator <IEdmProperty>  enumerator = edmStructuredTypeReference.StructuredDefinition().Properties().GetEnumerator();
                using (enumerator)
                {
                    Func <IEdmPropertyConstructor, bool> func = null;
                    while (enumerator.MoveNext())
                    {
                        IEdmProperty current = enumerator.Current;
                        IEnumerable <IEdmPropertyConstructor> properties = expression.Properties;
                        if (func == null)
                        {
                            func = (IEdmPropertyConstructor p) => p.Name == current.Name;
                        }
                        IEdmPropertyConstructor edmPropertyConstructor = properties.FirstOrDefault <IEdmPropertyConstructor>(func);
                        if (edmPropertyConstructor != null)
                        {
                            if (!edmPropertyConstructor.Value.TryAssertType(current.Type, out edmErrors))
                            {
                                IEnumerator <EdmError> enumerator1 = edmErrors.GetEnumerator();
                                using (enumerator1)
                                {
                                    while (enumerator1.MoveNext())
                                    {
                                        EdmError edmError = enumerator1.Current;
                                        edmErrors1.Add(edmError);
                                    }
                                }
                            }
                            strs.Add(current.Name);
                        }
                        else
                        {
                            edmErrors1.Add(new EdmError(expression.Location(), EdmErrorCode.RecordExpressionMissingRequiredProperty, Strings.EdmModel_Validator_Semantic_RecordExpressionMissingProperty(current.Name)));
                        }
                    }
                }
                if (!edmStructuredTypeReference.IsOpen())
                {
                    foreach (IEdmPropertyConstructor property in expression.Properties)
                    {
                        if (strs.Contains(property.Name))
                        {
                            continue;
                        }
                        edmErrors1.Add(new EdmError(expression.Location(), EdmErrorCode.RecordExpressionHasExtraProperties, Strings.EdmModel_Validator_Semantic_RecordExpressionHasExtraProperties(property.Name)));
                    }
                }
                if (edmErrors1.FirstOrDefault <EdmError>() == null)
                {
                    discoveredErrors = Enumerable.Empty <EdmError>();
                    return(true);
                }
                else
                {
                    discoveredErrors = edmErrors1;
                    return(false);
                }
            }
            else
            {
                EdmError[] edmErrorArray = new EdmError[1];
                edmErrorArray[0] = new EdmError(expression.Location(), EdmErrorCode.RecordExpressionNotValidForNonStructuredType, Strings.EdmModel_Validator_Semantic_RecordExpressionNotValidForNonStructuredType);
                discoveredErrors = edmErrorArray;
                return(false);
            }
        }
Beispiel #14
0
        internal static bool TryAssertPrimitiveAsType(this IEdmPrimitiveValue expression, IEdmTypeReference type, out IEnumerable <EdmError> discoveredErrors)
        {
            EdmError[] edmError;
            if (type.IsPrimitive())
            {
                EdmValueKind valueKind = expression.ValueKind;
                switch (valueKind)
                {
                case EdmValueKind.Binary:
                {
                    return(ExpressionTypeChecker.TryAssertBinaryConstantAsType((IEdmBinaryConstantExpression)expression, type, out discoveredErrors));
                }

                case EdmValueKind.Boolean:
                {
                    return(ExpressionTypeChecker.TryAssertBooleanConstantAsType((IEdmBooleanConstantExpression)expression, type, out discoveredErrors));
                }

                case EdmValueKind.Collection:
                case EdmValueKind.Enum:
                case EdmValueKind.Null:
                case EdmValueKind.Structured:
                {
                    edmError         = new EdmError[1];
                    edmError[0]      = new EdmError(expression.Location(), EdmErrorCode.ExpressionPrimitiveKindNotValidForAssertedType, Strings.EdmModel_Validator_Semantic_ExpressionPrimitiveKindNotValidForAssertedType);
                    discoveredErrors = edmError;
                    return(false);
                }

                case EdmValueKind.DateTimeOffset:
                {
                    return(ExpressionTypeChecker.TryAssertDateTimeOffsetConstantAsType((IEdmDateTimeOffsetConstantExpression)expression, type, out discoveredErrors));
                }

                case EdmValueKind.DateTime:
                {
                    return(ExpressionTypeChecker.TryAssertDateTimeConstantAsType((IEdmDateTimeConstantExpression)expression, type, out discoveredErrors));
                }

                case EdmValueKind.Decimal:
                {
                    return(ExpressionTypeChecker.TryAssertDecimalConstantAsType((IEdmDecimalConstantExpression)expression, type, out discoveredErrors));
                }

                case EdmValueKind.Floating:
                {
                    return(ExpressionTypeChecker.TryAssertFloatingConstantAsType((IEdmFloatingConstantExpression)expression, type, out discoveredErrors));
                }

                case EdmValueKind.Guid:
                {
                    return(ExpressionTypeChecker.TryAssertGuidConstantAsType((IEdmGuidConstantExpression)expression, type, out discoveredErrors));
                }

                case EdmValueKind.Integer:
                {
                    return(ExpressionTypeChecker.TryAssertIntegerConstantAsType((IEdmIntegerConstantExpression)expression, type, out discoveredErrors));
                }

                case EdmValueKind.String:
                {
                    return(ExpressionTypeChecker.TryAssertStringConstantAsType((IEdmStringConstantExpression)expression, type, out discoveredErrors));
                }

                case EdmValueKind.Time:
                {
                    return(ExpressionTypeChecker.TryAssertTimeConstantAsType((IEdmTimeConstantExpression)expression, type, out discoveredErrors));
                }

                default:
                {
                    edmError         = new EdmError[1];
                    edmError[0]      = new EdmError(expression.Location(), EdmErrorCode.ExpressionPrimitiveKindNotValidForAssertedType, Strings.EdmModel_Validator_Semantic_ExpressionPrimitiveKindNotValidForAssertedType);
                    discoveredErrors = edmError;
                    return(false);
                }
                }
            }
            else
            {
                EdmError[] edmErrorArray = new EdmError[1];
                edmErrorArray[0] = new EdmError(expression.Location(), EdmErrorCode.PrimitiveConstantExpressionNotValidForNonPrimitiveType, Strings.EdmModel_Validator_Semantic_PrimitiveConstantExpressionNotValidForNonPrimitiveType);
                discoveredErrors = edmErrorArray;
                return(false);
            }
        }
Beispiel #15
0
        private bool TryParse(IEnumerable<IEdmModel> references, out IEdmModel model, out IEnumerable<EdmError> parsingErrors)
        {
            Version edmxVersion;
            try
            {
                this.ParseEdmxFile(out edmxVersion);
            }
            catch (XmlException e)
            {
                model = null;
                parsingErrors = new EdmError[] { new EdmError(new CsdlLocation(e.LineNumber, e.LinePosition), EdmErrorCode.XmlError, e.Message) };
                return false;
            }

            if (this.errors.Count == 0)
            {
                CsdlModel astModel;
                IEnumerable<EdmError> csdlErrors;
                if (this.csdlParser.GetResult(out astModel, out csdlErrors))
                {
                    model = new CsdlSemanticsModel(astModel, new CsdlSemanticsDirectValueAnnotationsManager(), references);
                    
                    Debug.Assert(edmxVersion != null, "edmxVersion != null");
                    model.SetEdmxVersion(edmxVersion);
                    
                    if (this.dataServiceVersion != null)
                    {
                        model.SetDataServiceVersion(this.dataServiceVersion);
                    }

                    if (this.maxDataServiceVersion != null)
                    {
                        model.SetMaxDataServiceVersion(this.maxDataServiceVersion);
                    }
                }
                else
                {
                    Debug.Assert(csdlErrors != null && csdlErrors.Count() > 0, "csdlErrors != null && csdlErrors.Count() > 0");
                    this.errors.AddRange(csdlErrors);
                    model = null;
                }
            }
            else
            {
                model = null;
            }

            parsingErrors = this.errors;
            return this.errors.Count == 0;
        }
Beispiel #16
0
		private static bool TryAssertTimeConstantAsType(IEdmTimeConstantExpression expression, IEdmTypeReference type, out IEnumerable<EdmError> discoveredErrors)
		{
			if (type.IsTime())
			{
				discoveredErrors = Enumerable.Empty<EdmError>();
				return true;
			}
			else
			{
				EdmError[] edmError = new EdmError[1];
				edmError[0] = new EdmError(expression.Location(), EdmErrorCode.ExpressionPrimitiveKindNotValidForAssertedType, Strings.EdmModel_Validator_Semantic_ExpressionPrimitiveKindNotValidForAssertedType);
				discoveredErrors = edmError;
				return false;
			}
		}
		private IEnumerable<EdmError> ComputeErrors()
		{
			byte[] numArray = null;
			if (EdmValueParser.TryParseBinary(this.expression.Value, out numArray))
			{
				return Enumerable.Empty<EdmError>();
			}
			else
			{
				EdmError[] edmError = new EdmError[1];
				edmError[0] = new EdmError(base.Location, EdmErrorCode.InvalidBinary, Strings.ValueParser_InvalidBinary(this.expression.Value));
				return edmError;
			}
		}
Beispiel #18
0
		private bool TryParse(IEnumerable<IEdmModel> references, out IEdmModel model, out IEnumerable<EdmError> parsingErrors)
		{
			Version version = null;
			CsdlModel csdlModel = null;
			IEnumerable<EdmError> edmErrors = null;
			bool flag;
			try
			{
				this.ParseEdmxFile(out version);
				if (this.errors.Count != 0)
				{
					model = null;
				}
				else
				{
					if (!this.csdlParser.GetResult(out csdlModel, out edmErrors))
					{
						this.errors.AddRange(edmErrors);
						model = null;
					}
					else
					{
						model = new CsdlSemanticsModel(csdlModel, new CsdlSemanticsDirectValueAnnotationsManager(), references);
						model.SetEdmxVersion(version);
						if (this.dataServiceVersion != null)
						{
							model.SetDataServiceVersion(this.dataServiceVersion);
						}
						if (this.maxDataServiceVersion != null)
						{
							model.SetMaxDataServiceVersion(this.maxDataServiceVersion);
						}
					}
				}
				parsingErrors = this.errors;
				return this.errors.Count == 0;
			}
			catch (XmlException xmlException1)
			{
				XmlException xmlException = xmlException1;
				model = null;
				EdmError[] edmError = new EdmError[1];
				edmError[0] = new EdmError(new CsdlLocation(xmlException.LineNumber, xmlException.LinePosition), EdmErrorCode.XmlError, xmlException.Message);
				parsingErrors = edmError;
				flag = false;
			}
			return flag;
		}
		private IEnumerable<IEdmEnumMember> ComputeMembers()
		{
			IEdmEnumMember csdlSemanticsEnumMember;
			List<IEdmEnumMember> edmEnumMembers = new List<IEdmEnumMember>();
			long value = (long)-1;
			foreach (CsdlEnumMember member in this.enumeration.Members)
			{
				long? nullable = member.Value;
				if (nullable.HasValue)
				{
					long? value1 = member.Value;
					value = value1.Value;
					csdlSemanticsEnumMember = new CsdlSemanticsEnumMember(this, member);
					csdlSemanticsEnumMember.SetIsValueExplicit(this.Model, new bool?(true));
				}
				else
				{
					if (value >= 0x7fffffffffffffffL)
					{
						CsdlSemanticsEnumTypeDefinition csdlSemanticsEnumTypeDefinition = this;
						string name = member.Name;
						EdmError[] edmErrorArray = new EdmError[1];
						EdmError[] edmError = edmErrorArray;
						int num = 0;
						EdmLocation location = member.Location;
						EdmLocation edmLocation = location;
						if (location == null)
						{
							edmLocation = base.Location;
						}
						edmError[num] = new EdmError(edmLocation, EdmErrorCode.EnumMemberValueOutOfRange, Strings.CsdlSemantics_EnumMemberValueOutOfRange);
						csdlSemanticsEnumMember = new BadEnumMember(csdlSemanticsEnumTypeDefinition, name, edmErrorArray);
					}
					else
					{
						long? nullable1 = new long?(value + (long)1);
						value = nullable1.Value;
						member.Value = nullable1;
						csdlSemanticsEnumMember = new CsdlSemanticsEnumMember(this, member);
					}
					csdlSemanticsEnumMember.SetIsValueExplicit(this.Model, new bool?(false));
				}
				edmEnumMembers.Add(csdlSemanticsEnumMember);
			}
			return edmEnumMembers;
		}
		private IEnumerable<EdmError> ComputeErrors()
		{
			bool? nullable;
			if (EdmValueParser.TryParseBool(this.expression.Value, out nullable))
			{
				return Enumerable.Empty<EdmError>();
			}
			else
			{
				EdmError[] edmError = new EdmError[1];
				edmError[0] = new EdmError(base.Location, EdmErrorCode.InvalidBoolean, Strings.ValueParser_InvalidBoolean(this.expression.Value));
				return edmError;
			}
		}
Beispiel #21
0
		public static bool TryAssertType(this IEdmExpression expression, IEdmTypeReference type, out IEnumerable<EdmError> discoveredErrors)
		{
			EdmError[] edmError;
			EdmUtil.CheckArgumentNull<IEdmExpression>(expression, "expression");
			if (type == null || type.TypeKind() == EdmTypeKind.None)
			{
				discoveredErrors = Enumerable.Empty<EdmError>();
				return true;
			}
			else
			{
				EdmExpressionKind expressionKind = expression.ExpressionKind;
				switch (expressionKind)
				{
					case EdmExpressionKind.BinaryConstant:
					case EdmExpressionKind.BooleanConstant:
					case EdmExpressionKind.DateTimeConstant:
					case EdmExpressionKind.DateTimeOffsetConstant:
					case EdmExpressionKind.DecimalConstant:
					case EdmExpressionKind.FloatingConstant:
					case EdmExpressionKind.GuidConstant:
					case EdmExpressionKind.IntegerConstant:
					case EdmExpressionKind.StringConstant:
					case EdmExpressionKind.TimeConstant:
					{
						IEdmPrimitiveValue edmPrimitiveValue = (IEdmPrimitiveValue)expression;
						if (edmPrimitiveValue.Type == null)
						{
							return edmPrimitiveValue.TryAssertPrimitiveAsType(type, out discoveredErrors);
						}
						else
						{
							return edmPrimitiveValue.Type.TestTypeMatch(type, expression.Location(), out discoveredErrors);
						}
					}
					case EdmExpressionKind.Null:
					{
						return ((IEdmNullExpression)expression).TryAssertNullAsType(type, out discoveredErrors);
					}
					case EdmExpressionKind.Record:
					{
						IEdmRecordExpression edmRecordExpression = (IEdmRecordExpression)expression;
						if (edmRecordExpression.DeclaredType == null)
						{
							return edmRecordExpression.TryAssertRecordAsType(type, out discoveredErrors);
						}
						else
						{
							return edmRecordExpression.DeclaredType.TestTypeMatch(type, expression.Location(), out discoveredErrors);
						}
					}
					case EdmExpressionKind.Collection:
					{
						IEdmCollectionExpression edmCollectionExpression = (IEdmCollectionExpression)expression;
						if (edmCollectionExpression.DeclaredType == null)
						{
							return edmCollectionExpression.TryAssertCollectionAsType(type, out discoveredErrors);
						}
						else
						{
							return edmCollectionExpression.DeclaredType.TestTypeMatch(type, expression.Location(), out discoveredErrors);
						}
					}
					case EdmExpressionKind.Path:
					{
						return ((IEdmPathExpression)expression).TryAssertPathAsType(type, out discoveredErrors);
					}
					case EdmExpressionKind.ParameterReference:
					case EdmExpressionKind.FunctionReference:
					case EdmExpressionKind.PropertyReference:
					case EdmExpressionKind.ValueTermReference:
					case EdmExpressionKind.EntitySetReference:
					case EdmExpressionKind.EnumMemberReference:
					{
						edmError = new EdmError[1];
						edmError[0] = new EdmError(expression.Location(), EdmErrorCode.ExpressionNotValidForTheAssertedType, Strings.EdmModel_Validator_Semantic_ExpressionNotValidForTheAssertedType);
						discoveredErrors = edmError;
						return false;
					}
					case EdmExpressionKind.If:
					{
						return ((IEdmIfExpression)expression).TryAssertIfAsType(type, out discoveredErrors);
					}
					case EdmExpressionKind.AssertType:
					{
						return ((IEdmAssertTypeExpression)expression).Type.TestTypeMatch(type, expression.Location(), out discoveredErrors);
					}
					case EdmExpressionKind.IsType:
					{
						return EdmCoreModel.Instance.GetBoolean(false).TestTypeMatch(type, expression.Location(), out discoveredErrors);
					}
					case EdmExpressionKind.FunctionApplication:
					{
						IEdmApplyExpression edmApplyExpression = (IEdmApplyExpression)expression;
						if (edmApplyExpression.AppliedFunction != null)
						{
							IEdmFunctionBase appliedFunction = edmApplyExpression.AppliedFunction as IEdmFunctionBase;
							if (appliedFunction != null)
							{
								return appliedFunction.ReturnType.TestTypeMatch(type, expression.Location(), out discoveredErrors);
							}
						}
						discoveredErrors = Enumerable.Empty<EdmError>();
						return true;
					}
					case EdmExpressionKind.LabeledExpressionReference:
					{
						return ((IEdmLabeledExpressionReferenceExpression)expression).ReferencedLabeledExpression.TryAssertType(type, out discoveredErrors);
					}
					case EdmExpressionKind.Labeled:
					{
						return ((IEdmLabeledExpression)expression).Expression.TryAssertType(type, out discoveredErrors);
					}
					default:
					{
						edmError = new EdmError[1];
						edmError[0] = new EdmError(expression.Location(), EdmErrorCode.ExpressionNotValidForTheAssertedType, Strings.EdmModel_Validator_Semantic_ExpressionNotValidForTheAssertedType);
						discoveredErrors = edmError;
						return false;
					}
				}
			}
		}
Beispiel #22
0
		private static bool TestTypeMatch(this IEdmTypeReference expressionType, IEdmTypeReference assertedType, EdmLocation location, out IEnumerable<EdmError> discoveredErrors)
		{
			if (expressionType.TestNullabilityMatch(assertedType, location, out discoveredErrors))
			{
				if (expressionType.TypeKind() == EdmTypeKind.None || expressionType.IsBad())
				{
					discoveredErrors = Enumerable.Empty<EdmError>();
					return true;
				}
				else
				{
					if (!expressionType.IsPrimitive() || !assertedType.IsPrimitive())
					{
						if (!expressionType.Definition.IsEquivalentTo(assertedType.Definition))
						{
							EdmError[] edmError = new EdmError[1];
							edmError[0] = new EdmError(location, EdmErrorCode.ExpressionNotValidForTheAssertedType, Strings.EdmModel_Validator_Semantic_ExpressionNotValidForTheAssertedType);
							discoveredErrors = edmError;
							return false;
						}
					}
					else
					{
						if (!expressionType.PrimitiveKind().PromotesTo(assertedType.AsPrimitive().PrimitiveKind()))
						{
							EdmError[] edmErrorArray = new EdmError[1];
							edmErrorArray[0] = new EdmError(location, EdmErrorCode.ExpressionPrimitiveKindNotValidForAssertedType, Strings.EdmModel_Validator_Semantic_ExpressionPrimitiveKindCannotPromoteToAssertedType(expressionType.FullName(), assertedType.FullName()));
							discoveredErrors = edmErrorArray;
							return false;
						}
					}
					discoveredErrors = Enumerable.Empty<EdmError>();
					return true;
				}
			}
			else
			{
				return false;
			}
		}
		private TupleInternal<IEdmAssociationEnd, IEdmAssociationEnd> ComputeEnds()
		{
			IEdmAssociationEnd csdlSemanticsAssociationEnd;
			IEdmAssociationEnd edmAssociationEnd;
			if (this.association.End1 != null)
			{
				csdlSemanticsAssociationEnd = new CsdlSemanticsAssociationEnd(this.context, this, this.association.End1);
			}
			else
			{
				EdmError[] edmError = new EdmError[1];
				edmError[0] = new EdmError(base.Location, EdmErrorCode.InvalidAssociation, Strings.CsdlParser_InvalidAssociationIncorrectNumberOfEnds(string.Concat(this.Namespace, ".", this.Name)));
				IEdmAssociationEnd badAssociationEnd = new BadAssociationEnd(this, "End1", edmError);
				csdlSemanticsAssociationEnd = badAssociationEnd;
			}
			if (this.association.End2 != null)
			{
				edmAssociationEnd = new CsdlSemanticsAssociationEnd(this.context, this, this.association.End2);
			}
			else
			{
				EdmError[] edmErrorArray = new EdmError[1];
				edmErrorArray[0] = new EdmError(base.Location, EdmErrorCode.InvalidAssociation, Strings.CsdlParser_InvalidAssociationIncorrectNumberOfEnds(string.Concat(this.Namespace, ".", this.Name)));
				IEdmAssociationEnd badAssociationEnd1 = new BadAssociationEnd(this, "End2", edmErrorArray);
				edmAssociationEnd = badAssociationEnd1;
			}
			return TupleInternal.Create<IEdmAssociationEnd, IEdmAssociationEnd>(csdlSemanticsAssociationEnd, edmAssociationEnd);
		}
		private IEdmAssociationEnd ComputePrincipal()
		{
			IEdmAssociationEnd end1 = this.context.End1;
			if (end1.Name != this.constraint.Principal.Role)
			{
				end1 = this.context.End2;
			}
			if (end1.Name != this.constraint.Principal.Role)
			{
				EdmError[] edmError = new EdmError[1];
				edmError[0] = new EdmError(base.Location, EdmErrorCode.BadNonComputableAssociationEnd, Strings.Bad_UncomputableAssociationEnd(this.constraint.Principal.Role));
				end1 = new BadAssociationEnd(this.context, this.constraint.Principal.Role, edmError);
			}
			return end1;
		}
		private IEnumerable<IEdmStructuralProperty> ComputeDependentProperties()
		{
			List<IEdmStructuralProperty> edmStructuralProperties = new List<IEdmStructuralProperty>();
			IEdmEntityType entityType = this.DependentEnd.EntityType;
			var principalRoleType = this.PrincipalEnd.EntityType;
			CsdlReferentialConstraintRole principal = this.constraint.Principal;
			CsdlReferentialConstraintRole dependent = this.constraint.Dependent;
			if (principalRoleType.Key().Count<IEdmStructuralProperty>() != principal.Properties.Count<CsdlPropertyReference>() || principal.Properties.Count<CsdlPropertyReference>() != dependent.Properties.Count<CsdlPropertyReference>() || this.PrincipalKeyPropertiesNotFoundInPrincipalProperties.Count<string>() != 0 || this.DependentPropertiesNotFoundInDependentType.Count<string>() != 0)
			{
				edmStructuralProperties = new List<IEdmStructuralProperty>();
				foreach (CsdlPropertyReference property in dependent.Properties)
				{
					EdmError[] edmError = new EdmError[1];
					edmError[0] = new EdmError(base.Location, EdmErrorCode.TypeMismatchRelationshipConstraint, Strings.CsdlSemantics_ReferentialConstraintMismatch);
					edmStructuralProperties.Add(new BadProperty(entityType, property.PropertyName, edmError));
				}
			}
			else
			{
				IEnumerator<IEdmStructuralProperty> enumerator = this.PrincipalEnd.EntityType.Key().GetEnumerator();
				using (enumerator)
				{
					Func<CsdlPropertyReference, bool> func = null;
					while (enumerator.MoveNext())
					{
						IEdmStructuralProperty current = enumerator.Current;
						IEnumerable<CsdlPropertyReference> properties = principal.Properties;
						if (func == null)
						{
							func = (CsdlPropertyReference reference) => principalRoleType.FindProperty(reference.PropertyName).Equals(current);
						}
						CsdlPropertyReference csdlPropertyReference = properties.Where<CsdlPropertyReference>(func).FirstOrDefault<CsdlPropertyReference>();
						int num = principal.IndexOf(csdlPropertyReference);
						CsdlPropertyReference csdlPropertyReference1 = dependent.Properties.ElementAt<CsdlPropertyReference>(num);
						IEdmStructuralProperty edmStructuralProperty = entityType.FindProperty(csdlPropertyReference1.PropertyName) as IEdmStructuralProperty;
						edmStructuralProperties.Add(edmStructuralProperty);
					}
				}
			}
			return edmStructuralProperties;
		}
 private IEdmEntitySet ComputeEntitySet()
 {
     if (this.end != null)
     {
         return this.context.Container.FindEntitySet(this.end.EntitySet) ?? new UnresolvedEntitySet(this.end.EntitySet, this.context.Container, this.Location);
     }
     else
     {
         IEnumerable<EdmError> errors = new EdmError[]
         {
             new EdmError(
                 this.Location,
                 EdmErrorCode.NoEntitySetsFoundForType,
                 Strings.EdmModel_Validator_Semantic_NoEntitySetsFoundForType(this.context.Container.FullName() + this.context.Name, this.role.EntityType.FullName(), this.Role.Name))
         };
         return this.context.Container.EntitySets().Where(set => set.ElementType == this.role.EntityType).FirstOrDefault() ?? new BadEntitySet("UnresolvedEntitySet", this.context.Container, errors);
     }
 }
Beispiel #27
0
        /// <summary>
        /// Determines if the type of an expression is compatible with the provided type
        /// </summary>
        /// <param name="expression">The expression to assert the type of.</param>
        /// <param name="type">The type to assert the expression as.</param>
        /// <param name="context">The context paths are to be evaluated in.</param>
        /// <param name="matchExactly">A value indicating whether the expression must match the asserted type exactly, or simply be compatible.</param>
        /// <param name="discoveredErrors">Errors produced if the expression does not match the specified type.</param>
        /// <returns>A value indicating whether the expression is valid for the given type or not.</returns>
        /// <remarks>If the expression has an associated type, this function will check that it matches the expected type and stop looking further.
        /// If an expression claims a type, it must be validated that the type is valid for the expression. If the expression does not claim a type
        /// this method will attempt to check the validity of the expression itself with the asserted type.</remarks>
        public static bool TryAssertType(this IEdmExpression expression, IEdmTypeReference type, IEdmType context, bool matchExactly, out IEnumerable <EdmError> discoveredErrors)
        {
            EdmUtil.CheckArgumentNull(expression, "expression");

            // If we don't have a type to assert this passes vacuously.
            if (type == null || type.TypeKind() == EdmTypeKind.None)
            {
                discoveredErrors = Enumerable.Empty <EdmError>();
                return(true);
            }

            switch (expression.ExpressionKind)
            {
            case EdmExpressionKind.IntegerConstant:
            case EdmExpressionKind.StringConstant:
            case EdmExpressionKind.BinaryConstant:
            case EdmExpressionKind.BooleanConstant:
            case EdmExpressionKind.DateTimeConstant:
            case EdmExpressionKind.DateTimeOffsetConstant:
            case EdmExpressionKind.DecimalConstant:
            case EdmExpressionKind.FloatingConstant:
            case EdmExpressionKind.GuidConstant:
            case EdmExpressionKind.TimeConstant:
                IEdmPrimitiveValue primitiveValue = (IEdmPrimitiveValue)expression;
                if (primitiveValue.Type != null)
                {
                    return(TestTypeReferenceMatch(primitiveValue.Type, type, expression.Location(), matchExactly, out discoveredErrors));
                }

                return(TryAssertPrimitiveAsType(primitiveValue, type, out discoveredErrors));

            case EdmExpressionKind.Null:
                return(TryAssertNullAsType((IEdmNullExpression)expression, type, out discoveredErrors));

            case EdmExpressionKind.Path:
                return(TryAssertPathAsType((IEdmPathExpression)expression, type, context, matchExactly, out discoveredErrors));

            case EdmExpressionKind.FunctionApplication:
                IEdmApplyExpression applyExpression = (IEdmApplyExpression)expression;
                if (applyExpression.AppliedFunction != null)
                {
                    IEdmFunctionBase function = applyExpression.AppliedFunction as IEdmFunctionBase;
                    if (function != null)
                    {
                        return(TestTypeReferenceMatch(function.ReturnType, type, expression.Location(), matchExactly, out discoveredErrors));
                    }
                }

                // If we don't have the applied function we just assume that it will work.
                discoveredErrors = Enumerable.Empty <EdmError>();
                return(true);

            case EdmExpressionKind.If:
                return(TryAssertIfAsType((IEdmIfExpression)expression, type, context, matchExactly, out discoveredErrors));

            case EdmExpressionKind.IsType:
                return(TestTypeReferenceMatch(EdmCoreModel.Instance.GetBoolean(false), type, expression.Location(), matchExactly, out discoveredErrors));

            case EdmExpressionKind.Record:
                IEdmRecordExpression recordExpression = (IEdmRecordExpression)expression;
                if (recordExpression.DeclaredType != null)
                {
                    return(TestTypeReferenceMatch(recordExpression.DeclaredType, type, expression.Location(), matchExactly, out discoveredErrors));
                }

                return(TryAssertRecordAsType(recordExpression, type, context, matchExactly, out discoveredErrors));

            case EdmExpressionKind.Collection:
                IEdmCollectionExpression collectionExpression = (IEdmCollectionExpression)expression;
                if (collectionExpression.DeclaredType != null)
                {
                    return(TestTypeReferenceMatch(collectionExpression.DeclaredType, type, expression.Location(), matchExactly, out discoveredErrors));
                }

                return(TryAssertCollectionAsType(collectionExpression, type, context, matchExactly, out discoveredErrors));

            case EdmExpressionKind.Labeled:
                return(TryAssertType(((IEdmLabeledExpression)expression).Expression, type, context, matchExactly, out discoveredErrors));

            case EdmExpressionKind.AssertType:
                return(TestTypeReferenceMatch(((IEdmAssertTypeExpression)expression).Type, type, expression.Location(), matchExactly, out discoveredErrors));

            case EdmExpressionKind.LabeledExpressionReference:
                return(TryAssertType(((IEdmLabeledExpressionReferenceExpression)expression).ReferencedLabeledExpression, type, out discoveredErrors));

            default:
                discoveredErrors = new EdmError[] { new EdmError(expression.Location(), EdmErrorCode.ExpressionNotValidForTheAssertedType, Edm.Strings.EdmModel_Validator_Semantic_ExpressionNotValidForTheAssertedType) };
                return(false);
            }
        }
Beispiel #28
0
		internal static bool SignificantToSerialization(EdmError error)
		{
			if (!ValidationHelper.IsInterfaceCritical(error))
			{
				EdmErrorCode errorCode = error.ErrorCode;
				if (errorCode > EdmErrorCode.RowTypeMustNotHaveBaseType)
				{
					if (errorCode > EdmErrorCode.EnumMemberTypeMustMatchEnumUnderlyingType)
					{
						if (errorCode != EdmErrorCode.ReferencedTypeMustHaveValidName)
						{
							if (errorCode == EdmErrorCode.InvalidFunctionImportParameterMode || errorCode == EdmErrorCode.TypeMustNotHaveKindOfNone || errorCode == EdmErrorCode.PrimitiveTypeMustNotHaveKindOfNone || errorCode == EdmErrorCode.PropertyMustNotHaveKindOfNone || errorCode == EdmErrorCode.TermMustNotHaveKindOfNone || errorCode == EdmErrorCode.SchemaElementMustNotHaveKindOfNone || errorCode == EdmErrorCode.EntityContainerElementMustNotHaveKindOfNone || errorCode == EdmErrorCode.BinaryValueCannotHaveEmptyValue)
							{
								return true;
							}
							if (errorCode != EdmErrorCode.EnumMustHaveIntegerUnderlyingType)
							{
								return false;
							}
						}
					}
					else
					{
						if (errorCode == EdmErrorCode.OnlyInputParametersAllowedInFunctions || errorCode == EdmErrorCode.FunctionImportParameterIncorrectType)
						{
							return true;
						}
						else if (errorCode == EdmErrorCode.ComplexTypeMustHaveProperties)
						{
							return false;
						}
						if (errorCode == EdmErrorCode.EnumMemberTypeMustMatchEnumUnderlyingType)
						{
							return true;
						}
						return false;
					}
				}
				else
				{
					if (errorCode > EdmErrorCode.NameTooLong)
					{
						if (errorCode != EdmErrorCode.FunctionImportEntitySetExpressionIsInvalid)
						{
							if (errorCode == EdmErrorCode.SystemNamespaceEncountered || errorCode == EdmErrorCode.InvalidNamespaceName)
							{
								return true;
							}
							else if ((int)errorCode == 162)
							{
								return false;
							}
							if (errorCode == EdmErrorCode.RowTypeMustNotHaveBaseType)
							{
								return true;
							}
							return false;
						}
					}
					else
					{
						if (errorCode == EdmErrorCode.InvalidName || errorCode == EdmErrorCode.NameTooLong)
						{
							return true;
						}
						return false;
					}
				}
				return true;
			}
			else
			{
				return true;
			}
		}
Beispiel #29
0
		private static IEnumerable<EdmError> ConversionError(EdmLocation location, string typeName, string typeKindName)
		{
			EdmError[] edmErrorArray = new EdmError[1];
			EdmError[] edmError = edmErrorArray;
			int num = 0;
			EdmLocation edmLocation = location;
			int num1 = 230;
			string str = typeName;
			object obj = str;
			if (str == null)
			{
				obj = "UnnamedType";
			}
			edmError[num] = new EdmError(edmLocation, (EdmErrorCode)num1, Strings.TypeSemantics_CouldNotConvertTypeReference(obj, typeKindName));
			return edmErrorArray;
		}
 /// <summary>
 /// Register an error with the validation context.
 /// </summary>
 /// <param name="error">Error to register.</param>
 public void AddError(EdmError error)
 {
     this.errors.Add(error);
 }
		private IEdmEntitySet ComputeEntitySet()
		{
			Func<IEdmEntitySet, bool> func = null;
			if (this.end == null)
			{
				EdmError[] edmError = new EdmError[1];
				edmError[0] = new EdmError(base.Location, EdmErrorCode.NoEntitySetsFoundForType, Strings.EdmModel_Validator_Semantic_NoEntitySetsFoundForType(string.Concat(this.context.Container.FullName(), this.context.Name), this.role.EntityType.FullName(), this.Role.Name));
				IEnumerable<EdmError> edmErrors = edmError;
				IEnumerable<IEdmEntitySet> edmEntitySets = this.context.Container.EntitySets();
				if (func == null)
				{
					func = (IEdmEntitySet set) => set.ElementType == this.role.EntityType;
				}
				IEdmEntitySet edmEntitySet = edmEntitySets.Where<IEdmEntitySet>(func).FirstOrDefault<IEdmEntitySet>();
				IEdmEntitySet badEntitySet = edmEntitySet;
				if (edmEntitySet == null)
				{
					badEntitySet = new BadEntitySet("UnresolvedEntitySet", this.context.Container, edmErrors);
				}
				return badEntitySet;
			}
			else
			{
				IEdmEntitySet edmEntitySet1 = this.context.Container.FindEntitySet(this.end.EntitySet);
				IEdmEntitySet unresolvedEntitySet = edmEntitySet1;
				if (edmEntitySet1 == null)
				{
					unresolvedEntitySet = new UnresolvedEntitySet(this.end.EntitySet, this.context.Container, base.Location);
				}
				return unresolvedEntitySet;
			}
		}
		private IEdmAssociationEnd ComputeFrom()
		{
			IEdmAssociation association = this.Association;
			string fromRole = this.navigationProperty.FromRole;
			if (association.End1.Name != fromRole)
			{
				if (association.End2.Name != fromRole)
				{
					EdmError[] edmError = new EdmError[1];
					edmError[0] = new EdmError(base.Location, EdmErrorCode.BadNavigationProperty, Strings.EdmModel_Validator_Semantic_BadNavigationPropertyUndefinedRole(this.Name, fromRole, association.Name));
					return new CsdlSemanticsNavigationProperty.BadCsdlSemanticsNavigationPropertyToEnd(this.Association, fromRole, edmError);
				}
				else
				{
					return association.End2;
				}
			}
			else
			{
				return association.End1;
			}
		}
        internal static bool SignificantToSerialization(EdmError error)
        {
            if (ValidationHelper.IsInterfaceCritical(error))
            {
                return true;
            }

            switch (error.ErrorCode)
            {
                case EdmErrorCode.InvalidName:
                case EdmErrorCode.NameTooLong:
                case EdmErrorCode.InvalidNamespaceName:
                case EdmErrorCode.SystemNamespaceEncountered:
                case EdmErrorCode.RowTypeMustNotHaveBaseType:
                case EdmErrorCode.ReferencedTypeMustHaveValidName:
                case EdmErrorCode.FunctionImportEntitySetExpressionIsInvalid:
                case EdmErrorCode.FunctionImportParameterIncorrectType:
                case EdmErrorCode.OnlyInputParametersAllowedInFunctions:
                case EdmErrorCode.InvalidFunctionImportParameterMode:
                case EdmErrorCode.TypeMustNotHaveKindOfNone:
                case EdmErrorCode.PrimitiveTypeMustNotHaveKindOfNone:
                case EdmErrorCode.PropertyMustNotHaveKindOfNone:
                case EdmErrorCode.TermMustNotHaveKindOfNone:
                case EdmErrorCode.SchemaElementMustNotHaveKindOfNone:
                case EdmErrorCode.EntityContainerElementMustNotHaveKindOfNone:
                case EdmErrorCode.BinaryValueCannotHaveEmptyValue:
                case EdmErrorCode.EnumMustHaveIntegerUnderlyingType:
                case EdmErrorCode.EnumMemberTypeMustMatchEnumUnderlyingType:
                    return true;
            }

            return false;
        }
			private IEdmTypeReference ComputeType()
			{
				EdmMultiplicity multiplicity = this.partner.From.Multiplicity;
				switch (multiplicity)
				{
					case EdmMultiplicity.ZeroOrOne:
					{
						return new EdmEntityTypeReference(this.partner.DeclaringEntityType(), true);
					}
					case EdmMultiplicity.One:
					{
						return new EdmEntityTypeReference(this.partner.DeclaringEntityType(), false);
					}
					case EdmMultiplicity.Many:
					{
						return new EdmCollectionTypeReference(new EdmCollectionType(new EdmEntityTypeReference(this.partner.DeclaringEntityType(), false)), false);
					}
				}
				EdmError[] edmError = new EdmError[1];
				edmError[0] = new EdmError(this.partner.To.Location(), EdmErrorCode.NavigationPropertyTypeInvalidBecauseOfBadAssociation, Strings.EdmModel_Validator_Semantic_BadNavigationPropertyCouldNotDetermineType(this.partner.DeclaringEntityType().Name));
				return new BadEntityTypeReference(this.partner.DeclaringEntityType().FullName(), false, edmError);
			}
Beispiel #35
0
		public static bool TryParse(IEnumerable<XmlReader> csdlReaders, out CsdlModel entityModel, out IEnumerable<EdmError> errors)
		{
			bool flag;
			EdmUtil.CheckArgumentNull<IEnumerable<XmlReader>>(csdlReaders, "csdlReaders");
			CsdlParser csdlParser = new CsdlParser();
			int num = 0;
			IEnumerator<XmlReader> enumerator = csdlReaders.GetEnumerator();
			using (enumerator)
			{
				while (enumerator.MoveNext())
				{
					XmlReader current = enumerator.Current;
					if (current == null)
					{
						entityModel = null;
						EdmError[] edmError = new EdmError[1];
						edmError[0] = new EdmError(null, EdmErrorCode.NullXmlReader, Strings.CsdlParser_NullXmlReader);
						errors = edmError;
						flag = false;
						return flag;
					}
					else
					{
						try
						{
							csdlParser.AddReader(current);
						}
						catch (XmlException xmlException1)
						{
							XmlException xmlException = xmlException1;
							entityModel = null;
							EdmError[] edmErrorArray = new EdmError[1];
							edmErrorArray[0] = new EdmError(new CsdlLocation(xmlException.LineNumber, xmlException.LinePosition), EdmErrorCode.XmlError, xmlException.Message);
							errors = edmErrorArray;
							flag = false;
							return flag;
						}
						num++;
					}
				}
				goto Label0;
			}
			return flag;
		Label0:
			if (num != 0)
			{
				bool result = csdlParser.GetResult(out entityModel, out errors);
				if (!result)
				{
					entityModel = null;
				}
				return result;
			}
			else
			{
				entityModel = null;
				EdmError[] edmError1 = new EdmError[1];
				edmError1[0] = new EdmError(null, EdmErrorCode.NoReadersProvided, Strings.CsdlParser_NoReadersProvided);
				errors = edmError1;
				return false;
			}
		}
Beispiel #36
0
		internal static bool IsInterfaceCritical(EdmError error)
		{
			if (error.ErrorCode < EdmErrorCode.InterfaceCriticalPropertyValueMustNotBeNull)
			{
				return false;
			}
			else
			{
				return error.ErrorCode <= EdmErrorCode.InterfaceCriticalCycleInTypeHierarchy;
			}
		}
Beispiel #37
0
		internal static bool ValidateValueCanBeWrittenAsXmlElementAnnotation(IEdmValue value, string annotationNamespace, string annotationName, out EdmError error)
		{
			bool flag;
			IEdmStringValue edmStringValue = value as IEdmStringValue;
			if (edmStringValue != null)
			{
				string str = edmStringValue.Value;
				XmlReader xmlReader = XmlReader.Create(new StringReader(str));
				try
				{
					if (xmlReader.NodeType != XmlNodeType.Element)
					{
						while (xmlReader.Read() && xmlReader.NodeType != XmlNodeType.Element)
						{
						}
					}
					if (!xmlReader.EOF)
					{
						string namespaceURI = xmlReader.NamespaceURI;
						string localName = xmlReader.LocalName;
						if (EdmUtil.IsNullOrWhiteSpaceInternal(namespaceURI) || EdmUtil.IsNullOrWhiteSpaceInternal(localName))
						{
							error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationNullNamespaceOrName);
							flag = false;
						}
						else
						{
							if ((annotationNamespace == null || namespaceURI == annotationNamespace) && (annotationName == null || localName == annotationName))
							{
								while (xmlReader.Read())
								{
								}
								error = null;
								flag = true;
							}
							else
							{
								error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationMismatchedTerm);
								flag = false;
							}
						}
					}
					else
					{
						error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationValueInvalidXml);
						flag = false;
					}
				}
				catch (Exception exception)
				{
					error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationValueInvalidXml);
					flag = false;
				}
				return flag;
			}
			else
			{
				error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationNotIEdmStringValue);
				return false;
			}
		}
Beispiel #38
0
 /// <summary>
 /// Register an error with the validation context.
 /// </summary>
 /// <param name="error">Error to register.</param>
 public void AddError(EdmError error)
 {
     this.errors.Add(error);
 }
Beispiel #39
0
		private static bool TryAssertStringConstantAsType(IEdmStringConstantExpression expression, IEdmTypeReference type, out IEnumerable<EdmError> discoveredErrors)
		{
			if (type.IsString())
			{
				IEdmStringTypeReference edmStringTypeReference = type.AsString();
				int? maxLength = edmStringTypeReference.MaxLength;
				if (maxLength.HasValue)
				{
					int? nullable = edmStringTypeReference.MaxLength;
					if (expression.Value.Length > nullable.Value)
					{
						EdmError[] edmError = new EdmError[1];
						int? maxLength1 = edmStringTypeReference.MaxLength;
						edmError[0] = new EdmError(expression.Location(), EdmErrorCode.StringConstantLengthOutOfRange, Strings.EdmModel_Validator_Semantic_StringConstantLengthOutOfRange(expression.Value.Length, maxLength1.Value));
						discoveredErrors = edmError;
						return false;
					}
				}
				discoveredErrors = Enumerable.Empty<EdmError>();
				return true;
			}
			else
			{
				EdmError[] edmErrorArray = new EdmError[1];
				edmErrorArray[0] = new EdmError(expression.Location(), EdmErrorCode.ExpressionPrimitiveKindNotValidForAssertedType, Strings.EdmModel_Validator_Semantic_ExpressionPrimitiveKindNotValidForAssertedType);
				discoveredErrors = edmErrorArray;
				return false;
			}
		}