Пример #1
0
        private static EntityPropertyMappingAttribute ValidateAnnotationValues(EpmAnnotationValues annotationValues, string typeName, IEdmProperty property)
        {
            if (annotationValues.TargetPath == null)
            {
                string str     = "FC_TargetPath" + annotationValues.AttributeSuffix;
                string message = (property == null) ? Microsoft.Data.OData.Strings.EpmExtensionMethods_MissingAttributeOnType(str, typeName) : Microsoft.Data.OData.Strings.EpmExtensionMethods_MissingAttributeOnProperty(str, property.Name, typeName);
                throw new ODataException(message);
            }
            bool result = true;

            if ((annotationValues.KeepInContent != null) && !bool.TryParse(annotationValues.KeepInContent, out result))
            {
                string str3 = "FC_KeepInContent" + annotationValues.AttributeSuffix;
                throw new InvalidOperationException((property == null) ? Microsoft.Data.OData.Strings.EpmExtensionMethods_InvalidKeepInContentOnType(str3, typeName) : Microsoft.Data.OData.Strings.EpmExtensionMethods_InvalidKeepInContentOnProperty(str3, property.Name, typeName));
            }
            SyndicationItemProperty targetSyndicationItem = MapTargetPathToSyndicationProperty(annotationValues.TargetPath);

            if (targetSyndicationItem == SyndicationItemProperty.CustomProperty)
            {
                if (annotationValues.ContentKind != null)
                {
                    string str4 = "FC_ContentKind" + annotationValues.AttributeSuffix;
                    string str5 = (property == null) ? Microsoft.Data.OData.Strings.EpmExtensionMethods_AttributeNotAllowedForCustomMappingOnType(str4, typeName) : Microsoft.Data.OData.Strings.EpmExtensionMethods_AttributeNotAllowedForCustomMappingOnProperty(str4, property.Name, typeName);
                    throw new ODataException(str5);
                }
                return(new EntityPropertyMappingAttribute(annotationValues.SourcePath, annotationValues.TargetPath, annotationValues.NamespacePrefix, annotationValues.NamespaceUri, result));
            }
            if (annotationValues.NamespaceUri != null)
            {
                string str6 = "FC_NsUri" + annotationValues.AttributeSuffix;
                string str7 = (property == null) ? Microsoft.Data.OData.Strings.EpmExtensionMethods_AttributeNotAllowedForAtomPubMappingOnType(str6, typeName) : Microsoft.Data.OData.Strings.EpmExtensionMethods_AttributeNotAllowedForAtomPubMappingOnProperty(str6, property.Name, typeName);
                throw new ODataException(str7);
            }
            if (annotationValues.NamespacePrefix != null)
            {
                string str8 = "FC_NsPrefix" + annotationValues.AttributeSuffix;
                string str9 = (property == null) ? Microsoft.Data.OData.Strings.EpmExtensionMethods_AttributeNotAllowedForAtomPubMappingOnType(str8, typeName) : Microsoft.Data.OData.Strings.EpmExtensionMethods_AttributeNotAllowedForAtomPubMappingOnProperty(str8, property.Name, typeName);
                throw new ODataException(str9);
            }
            SyndicationTextContentKind plaintext = SyndicationTextContentKind.Plaintext;

            if (annotationValues.ContentKind != null)
            {
                plaintext = MapContentKindToSyndicationTextContentKind(annotationValues.ContentKind, annotationValues.AttributeSuffix, typeName, (property == null) ? null : property.Name);
            }
            return(new EntityPropertyMappingAttribute(annotationValues.SourcePath, targetSyndicationItem, plaintext, result));
        }
Пример #2
0
        private static IEnumerable <EpmAnnotationValues> ParseSerializableEpmAnnotations(this IEdmModel model, IEdmElement annotatable, string typeName, IEdmProperty property)
        {
            Dictionary <string, EpmAnnotationValues> dictionary       = null;
            IEnumerable <IEdmDirectValueAnnotation>  oDataAnnotations = model.GetODataAnnotations(annotatable);

            if (oDataAnnotations != null)
            {
                foreach (IEdmDirectValueAnnotation annotation in oDataAnnotations)
                {
                    string str;
                    string str2;
                    if (annotation.IsEpmAnnotation(out str2, out str))
                    {
                        EpmAnnotationValues values;
                        string str3 = ConvertEdmAnnotationValue(annotation);
                        if (dictionary == null)
                        {
                            dictionary = new Dictionary <string, EpmAnnotationValues>(StringComparer.Ordinal);
                        }
                        if (!dictionary.TryGetValue(str, out values))
                        {
                            values = new EpmAnnotationValues {
                                AttributeSuffix = str
                            };
                            dictionary[str] = values;
                        }
                        if (!NamesMatchByReference("FC_TargetPath", str2))
                        {
                            if (!NamesMatchByReference("FC_SourcePath", str2))
                            {
                                if (!NamesMatchByReference("FC_KeepInContent", str2))
                                {
                                    if (!NamesMatchByReference("FC_ContentKind", str2))
                                    {
                                        if (!NamesMatchByReference("FC_NsUri", str2))
                                        {
                                            if (!NamesMatchByReference("FC_NsPrefix", str2))
                                            {
                                                throw new ODataException(Microsoft.Data.OData.Strings.General_InternalError(InternalErrorCodes.ODataUtils_ParseSerializableEpmAnnotations_UnreachableCodePath));
                                            }
                                            values.NamespacePrefix = str3;
                                        }
                                        else
                                        {
                                            values.NamespaceUri = str3;
                                        }
                                    }
                                    else
                                    {
                                        values.ContentKind = str3;
                                    }
                                }
                                else
                                {
                                    values.KeepInContent = str3;
                                }
                            }
                            else
                            {
                                values.SourcePath = str3;
                            }
                        }
                        else
                        {
                            values.TargetPath = str3;
                        }
                    }
                }
                if (dictionary != null)
                {
                    foreach (EpmAnnotationValues values3 in dictionary.Values)
                    {
                        string sourcePath = values3.SourcePath;
                        if (sourcePath == null)
                        {
                            if (property == null)
                            {
                                throw new ODataException(Microsoft.Data.OData.Strings.EpmExtensionMethods_MissingAttributeOnType("FC_SourcePath" + values3.AttributeSuffix, typeName));
                            }
                            values3.SourcePath = property.Name;
                        }
                        else if ((property != null) && !property.Type.IsODataPrimitiveTypeKind())
                        {
                            values3.SourcePath = property.Name + "/" + sourcePath;
                        }
                    }
                }
            }
            if (dictionary != null)
            {
                return(dictionary.Values);
            }
            return(null);
        }
Пример #3
0
        /// <summary>
        /// Validates the annotation values parsed for an EPM mapping.
        /// </summary>
        /// <param name="annotationValues">The <see cref="EpmAnnotationValues"/> to validate.</param>
        /// <param name="typeName">The name of the type for which the annotations are validated or that declares the <paramref name="property"/>. Only used in error messages.</param>
        /// <param name="property">The property for which the annotations are validated; null if the annotations are for a type.</param>
        /// <returns>An <see cref="EntityPropertyMappingAttribute"/> instance that represents the mapping created from the <paramref name="annotationValues"/>.</returns>
        private static EntityPropertyMappingAttribute ValidateAnnotationValues(EpmAnnotationValues annotationValues, string typeName, IEdmProperty property)
        {
            Debug.Assert(annotationValues != null, "annotationValues != null");
            Debug.Assert(annotationValues.AttributeSuffix != null, "annotationValues.AttributeSuffix != null");
            Debug.Assert(!string.IsNullOrEmpty(typeName), "!string.IsNullOrEmpty(typeName)");

            //// Conditions for EPM annotation values to represent a valid mapping:
            ////   1. must have target path
            ////   2. can have keep-in-content (default is 'true')
            ////   3a. if custom mapping: target path must map to custom property, content kind must be null
            ////   3b. if syndication mapping: content kind (optional; default: plain text), no ns uri, no ns prefix

            if (annotationValues.TargetPath == null)
            {
                string attributeName = EpmConstants.ODataEpmTargetPath + annotationValues.AttributeSuffix;
                string errorMessage = property == null
                    ? o.Strings.EpmExtensionMethods_MissingAttributeOnType(attributeName, typeName)
                    : o.Strings.EpmExtensionMethods_MissingAttributeOnProperty(attributeName, property.Name, typeName);
                throw new ODataException(errorMessage);
            }

            EntityPropertyMappingAttribute mapping;

            bool keepInContent = true;
            if (annotationValues.KeepInContent != null)
            {
                if (!bool.TryParse(annotationValues.KeepInContent, out keepInContent))
                {
                    string attributeName = EpmConstants.ODataEpmKeepInContent + annotationValues.AttributeSuffix;
                    throw new InvalidOperationException(property == null 
                        ? o.Strings.EpmExtensionMethods_InvalidKeepInContentOnType(attributeName, typeName) 
                        : o.Strings.EpmExtensionMethods_InvalidKeepInContentOnProperty(attributeName, property.Name, typeName));
                }
            }

            // figure out whether this is a custom mapping or not
            SyndicationItemProperty targetSyndicationItem = MapTargetPathToSyndicationProperty(annotationValues.TargetPath);
            if (targetSyndicationItem == SyndicationItemProperty.CustomProperty)
            {
                if (annotationValues.ContentKind != null)
                {
                    string attributeName = EpmConstants.ODataEpmContentKind + annotationValues.AttributeSuffix;
                    string errorMessage = property == null
                        ? o.Strings.EpmExtensionMethods_AttributeNotAllowedForCustomMappingOnType(attributeName, typeName)
                        : o.Strings.EpmExtensionMethods_AttributeNotAllowedForCustomMappingOnProperty(attributeName, property.Name, typeName);
                    throw new ODataException(errorMessage);
                }

                mapping = new EntityPropertyMappingAttribute(
                    annotationValues.SourcePath,
                    annotationValues.TargetPath,
                    annotationValues.NamespacePrefix,
                    annotationValues.NamespaceUri,
                    keepInContent);
            }
            else
            {
                if (annotationValues.NamespaceUri != null)
                {
                    string attributeName = EpmConstants.ODataEpmNsUri + annotationValues.AttributeSuffix;
                    string errorMessage = property == null
                        ? o.Strings.EpmExtensionMethods_AttributeNotAllowedForAtomPubMappingOnType(attributeName, typeName)
                        : o.Strings.EpmExtensionMethods_AttributeNotAllowedForAtomPubMappingOnProperty(attributeName, property.Name, typeName);
                    throw new ODataException(errorMessage);
                }

                if (annotationValues.NamespacePrefix != null)
                {
                    string attributeName = EpmConstants.ODataEpmNsPrefix + annotationValues.AttributeSuffix;
                    string errorMessage = property == null
                        ? o.Strings.EpmExtensionMethods_AttributeNotAllowedForAtomPubMappingOnType(attributeName, typeName)
                        : o.Strings.EpmExtensionMethods_AttributeNotAllowedForAtomPubMappingOnProperty(attributeName, property.Name, typeName);
                    throw new ODataException(errorMessage);
                }

                SyndicationTextContentKind contentKind = SyndicationTextContentKind.Plaintext;
                if (annotationValues.ContentKind != null)
                {
                    contentKind = MapContentKindToSyndicationTextContentKind(
                        annotationValues.ContentKind, 
                        annotationValues.AttributeSuffix, 
                        typeName, 
                        property == null ? null : property.Name);
                }

                mapping = new EntityPropertyMappingAttribute(
                    annotationValues.SourcePath,
                    targetSyndicationItem,
                    contentKind,
                    keepInContent);
            }

            Debug.Assert(mapping != null, "mapping != null");
            return mapping;
        }
Пример #4
0
        /// <summary>
        /// Parses the serializable EPM annotations of the <paramref name="annotatable"/>, groups them by suffix
        /// and translates them into a set of structs.
        /// </summary>
        /// <param name="model">The <see cref="IEdmModel"/> containing the annotations.</param>
        /// <param name="annotatable">The <see cref="IEdmElement"/> to parse the EPM annotations for.</param>
        /// <param name="typeName">The name of the type for which the annotations are parsed or that declares the <paramref name="property"/>. Only used in error messages.</param>
        /// <param name="property">The property to parse the EPM annotations for.</param>
        /// <returns>An enumerable of <see cref="EpmAnnotationValues"/> that represents all the parsed annotations grouped by their suffix.</returns>
        private static IEnumerable<EpmAnnotationValues> ParseSerializableEpmAnnotations(this IEdmModel model, IEdmElement annotatable, string typeName, IEdmProperty property)
        {
            Debug.Assert(model != null, "model != null");
            Debug.Assert(annotatable != null, "annotatable != null");
            Debug.Assert(typeName != null, "typeName != null");

            Dictionary<string, EpmAnnotationValues> allAnnotationValues = null;

            IEnumerable<IEdmDirectValueAnnotation> annotations = model.GetODataAnnotations(annotatable);
            if (annotations != null)
            {
                foreach (IEdmDirectValueAnnotation annotation in annotations)
                {
                    string suffix;
                    string baseName;
                    if (annotation.IsEpmAnnotation(out baseName, out suffix))
                    {
                        Debug.Assert(baseName != null, "baseName != null");
                        Debug.Assert(suffix != null, "suffix != null");

                        string annotationValue = ConvertEdmAnnotationValue(annotation);

                        if (allAnnotationValues == null)
                        {
                            allAnnotationValues = new Dictionary<string, EpmAnnotationValues>(StringComparer.Ordinal);
                        }

                        EpmAnnotationValues annotationValues;
                        if (!allAnnotationValues.TryGetValue(suffix, out annotationValues))
                        {
                            annotationValues = new EpmAnnotationValues
                            {
                                AttributeSuffix = suffix
                            };
                            allAnnotationValues[suffix] = annotationValues;
                        }

                        // NOTE: we don't have to check for duplicate definitions since the Xml attribute
                        //       nature of the annotations prevents that.
                        if (NamesMatchByReference(EpmConstants.ODataEpmTargetPath, baseName))
                        {
                            Debug.Assert(annotationValues.TargetPath == null, "Can have only a single target path annotation per suffix.");
                            annotationValues.TargetPath = annotationValue;
                        }
                        else if (NamesMatchByReference(EpmConstants.ODataEpmSourcePath, baseName))
                        {
                            Debug.Assert(annotationValues.SourcePath == null, "Can have only a single source path annotation per suffix.");
                            annotationValues.SourcePath = annotationValue;
                        }
                        else if (NamesMatchByReference(EpmConstants.ODataEpmKeepInContent, baseName))
                        {
                            Debug.Assert(annotationValues.KeepInContent == null, "Can have only a single keep-in-content annotation per suffix.");
                            annotationValues.KeepInContent = annotationValue;
                        }
                        else if (NamesMatchByReference(EpmConstants.ODataEpmContentKind, baseName))
                        {
                            Debug.Assert(annotationValues.ContentKind == null, "Can have only a single content kind annotation per suffix.");
                            annotationValues.ContentKind = annotationValue;
                        }
                        else if (NamesMatchByReference(EpmConstants.ODataEpmNsUri, baseName))
                        {
                            Debug.Assert(annotationValues.NamespaceUri == null, "Can have only a single namespace URI annotation per suffix.");
                            annotationValues.NamespaceUri = annotationValue;
                        }
                        else if (NamesMatchByReference(EpmConstants.ODataEpmNsPrefix, baseName))
                        {
                            Debug.Assert(annotationValues.NamespacePrefix == null, "Can have only a single namespace prefix annotation per suffix.");
                            annotationValues.NamespacePrefix = annotationValue;
                        }
                        else
                        {
                            throw new ODataException(o.Strings.General_InternalError(InternalErrorCodes.ODataUtils_ParseSerializableEpmAnnotations_UnreachableCodePath));
                        }
                    }
                }

                // Fix up the source path values
                if (allAnnotationValues != null)
                {
                    foreach (EpmAnnotationValues annotationValues in allAnnotationValues.Values)
                    {
                        string sourcePath = annotationValues.SourcePath;

                        // set the default source path based on the property name if none was found in the annotations
                        if (sourcePath == null)
                        {
                            if (property == null)
                            {
                                string attributeName = EpmConstants.ODataEpmSourcePath + annotationValues.AttributeSuffix;
                                throw new ODataException(o.Strings.EpmExtensionMethods_MissingAttributeOnType(attributeName, typeName));
                            }

                            annotationValues.SourcePath = property.Name;
                        }
                        else
                        {
                            // For non-primitive properties we strip the property name from the source path if the 
                            // source path is not the same as the property name; re-add the property name here.
                            if (property != null && !property.Type.IsODataPrimitiveTypeKind())
                            {
                                annotationValues.SourcePath = property.Name + "/" + sourcePath;
                            }
                        }
                    }
                }
            }

            return allAnnotationValues == null ? null : allAnnotationValues.Values;
        }
Пример #5
0
 private static EntityPropertyMappingAttribute ValidateAnnotationValues(EpmAnnotationValues annotationValues, string typeName, IEdmProperty property)
 {
     if (annotationValues.TargetPath == null)
     {
         string str = "FC_TargetPath" + annotationValues.AttributeSuffix;
         string message = (property == null) ? Microsoft.Data.OData.Strings.EpmExtensionMethods_MissingAttributeOnType(str, typeName) : Microsoft.Data.OData.Strings.EpmExtensionMethods_MissingAttributeOnProperty(str, property.Name, typeName);
         throw new ODataException(message);
     }
     bool result = true;
     if ((annotationValues.KeepInContent != null) && !bool.TryParse(annotationValues.KeepInContent, out result))
     {
         string str3 = "FC_KeepInContent" + annotationValues.AttributeSuffix;
         throw new InvalidOperationException((property == null) ? Microsoft.Data.OData.Strings.EpmExtensionMethods_InvalidKeepInContentOnType(str3, typeName) : Microsoft.Data.OData.Strings.EpmExtensionMethods_InvalidKeepInContentOnProperty(str3, property.Name, typeName));
     }
     SyndicationItemProperty targetSyndicationItem = MapTargetPathToSyndicationProperty(annotationValues.TargetPath);
     if (targetSyndicationItem == SyndicationItemProperty.CustomProperty)
     {
         if (annotationValues.ContentKind != null)
         {
             string str4 = "FC_ContentKind" + annotationValues.AttributeSuffix;
             string str5 = (property == null) ? Microsoft.Data.OData.Strings.EpmExtensionMethods_AttributeNotAllowedForCustomMappingOnType(str4, typeName) : Microsoft.Data.OData.Strings.EpmExtensionMethods_AttributeNotAllowedForCustomMappingOnProperty(str4, property.Name, typeName);
             throw new ODataException(str5);
         }
         return new EntityPropertyMappingAttribute(annotationValues.SourcePath, annotationValues.TargetPath, annotationValues.NamespacePrefix, annotationValues.NamespaceUri, result);
     }
     if (annotationValues.NamespaceUri != null)
     {
         string str6 = "FC_NsUri" + annotationValues.AttributeSuffix;
         string str7 = (property == null) ? Microsoft.Data.OData.Strings.EpmExtensionMethods_AttributeNotAllowedForAtomPubMappingOnType(str6, typeName) : Microsoft.Data.OData.Strings.EpmExtensionMethods_AttributeNotAllowedForAtomPubMappingOnProperty(str6, property.Name, typeName);
         throw new ODataException(str7);
     }
     if (annotationValues.NamespacePrefix != null)
     {
         string str8 = "FC_NsPrefix" + annotationValues.AttributeSuffix;
         string str9 = (property == null) ? Microsoft.Data.OData.Strings.EpmExtensionMethods_AttributeNotAllowedForAtomPubMappingOnType(str8, typeName) : Microsoft.Data.OData.Strings.EpmExtensionMethods_AttributeNotAllowedForAtomPubMappingOnProperty(str8, property.Name, typeName);
         throw new ODataException(str9);
     }
     SyndicationTextContentKind plaintext = SyndicationTextContentKind.Plaintext;
     if (annotationValues.ContentKind != null)
     {
         plaintext = MapContentKindToSyndicationTextContentKind(annotationValues.ContentKind, annotationValues.AttributeSuffix, typeName, (property == null) ? null : property.Name);
     }
     return new EntityPropertyMappingAttribute(annotationValues.SourcePath, targetSyndicationItem, plaintext, result);
 }
Пример #6
0
 private static IEnumerable<EpmAnnotationValues> ParseSerializableEpmAnnotations(this IEdmModel model, IEdmElement annotatable, string typeName, IEdmProperty property)
 {
     Dictionary<string, EpmAnnotationValues> dictionary = null;
     IEnumerable<IEdmDirectValueAnnotation> oDataAnnotations = model.GetODataAnnotations(annotatable);
     if (oDataAnnotations != null)
     {
         foreach (IEdmDirectValueAnnotation annotation in oDataAnnotations)
         {
             string str;
             string str2;
             if (annotation.IsEpmAnnotation(out str2, out str))
             {
                 EpmAnnotationValues values;
                 string str3 = ConvertEdmAnnotationValue(annotation);
                 if (dictionary == null)
                 {
                     dictionary = new Dictionary<string, EpmAnnotationValues>(StringComparer.Ordinal);
                 }
                 if (!dictionary.TryGetValue(str, out values))
                 {
                     values = new EpmAnnotationValues {
                         AttributeSuffix = str
                     };
                     dictionary[str] = values;
                 }
                 if (!NamesMatchByReference("FC_TargetPath", str2))
                 {
                     if (!NamesMatchByReference("FC_SourcePath", str2))
                     {
                         if (!NamesMatchByReference("FC_KeepInContent", str2))
                         {
                             if (!NamesMatchByReference("FC_ContentKind", str2))
                             {
                                 if (!NamesMatchByReference("FC_NsUri", str2))
                                 {
                                     if (!NamesMatchByReference("FC_NsPrefix", str2))
                                     {
                                         throw new ODataException(Microsoft.Data.OData.Strings.General_InternalError(InternalErrorCodes.ODataUtils_ParseSerializableEpmAnnotations_UnreachableCodePath));
                                     }
                                     values.NamespacePrefix = str3;
                                 }
                                 else
                                 {
                                     values.NamespaceUri = str3;
                                 }
                             }
                             else
                             {
                                 values.ContentKind = str3;
                             }
                         }
                         else
                         {
                             values.KeepInContent = str3;
                         }
                     }
                     else
                     {
                         values.SourcePath = str3;
                     }
                 }
                 else
                 {
                     values.TargetPath = str3;
                 }
             }
         }
         if (dictionary != null)
         {
             foreach (EpmAnnotationValues values3 in dictionary.Values)
             {
                 string sourcePath = values3.SourcePath;
                 if (sourcePath == null)
                 {
                     if (property == null)
                     {
                         throw new ODataException(Microsoft.Data.OData.Strings.EpmExtensionMethods_MissingAttributeOnType("FC_SourcePath" + values3.AttributeSuffix, typeName));
                     }
                     values3.SourcePath = property.Name;
                 }
                 else if ((property != null) && !property.Type.IsODataPrimitiveTypeKind())
                 {
                     values3.SourcePath = property.Name + "/" + sourcePath;
                 }
             }
         }
     }
     if (dictionary != null)
     {
         return dictionary.Values;
     }
     return null;
 }