Beispiel #1
0
        /// <summary>
        /// Returns the annotation in the OData metadata namespace with the specified <paramref name="localName" />.
        /// </summary>
        /// <param name="model">The <see cref="IEdmModel"/> containing the annotation.</param>
        /// <param name="annotatable">The <see cref="IEdmElement"/> to get the annotation from.</param>
        /// <param name="localName">The local name of the annotation to find.</param>
        /// <param name="value">The value of the annotation in the OData metadata namespace and with the specified <paramref name="localName"/>.</param>
        /// <returns>true if an annotation with the specified local name was found; otherwise false.</returns>
        internal static bool TryGetAnnotation(this IEdmModel model, IEdmElement annotatable, string localName, out string value)
        {
            Debug.Assert(model != null, "model != null");
            Debug.Assert(annotatable != null, "annotatable != null");
            Debug.Assert(!String.IsNullOrEmpty(localName), "!string.IsNullOrEmpty(localName)");

            object annotationValue = model.GetAnnotationValue(annotatable, CsdlConstants.ODataMetadataNamespace, localName);

            if (annotationValue == null)
            {
                value = null;
                return(false);
            }

            IEdmStringValue annotationStringValue = annotationValue as IEdmStringValue;

            if (annotationStringValue == null)
            {
                // invalid annotation type found
                throw new InvalidOperationException(Strings.EdmUtil_InvalidAnnotationValue(localName, annotationValue.GetType().FullName));
            }

            value = annotationStringValue.Value;
            return(true);
        }
Beispiel #2
0
        private static IEdmDirectValueAnnotationBinding GetODataAnnotationBinding(IEdmElement annotatable, string localName, string value)
        {
            IEdmStringValue value2 = null;

            if (value != null)
            {
                value2 = new EdmStringConstant(EdmCoreModel.Instance.GetString(true), value);
            }
            return(new EdmDirectValueAnnotationBinding(annotatable, "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata", localName, value2));
        }
Beispiel #3
0
        internal static void SetODataAnnotation(this IEdmModel model, IEdmElement annotatable, string localName, string value)
        {
            IEdmStringValue value2 = null;

            if (value != null)
            {
                value2 = new EdmStringConstant(EdmCoreModel.Instance.GetString(true), value);
            }
            model.SetAnnotationValue(annotatable, "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata", localName, value2);
        }
Beispiel #4
0
        /// <summary>
        /// Sets the annotation with the OData metadata namespace and the specified <paramref name="localName" /> on the <paramref name="annotatable"/>.
        /// </summary>
        /// <param name="model">The <see cref="IEdmModel"/> containing the annotations."/></param>
        /// <param name="annotatable">The <see cref="IEdmElement"/> to set the annotation on.</param>
        /// <param name="localName">The local name of the annotation to set.</param>
        /// <param name="value">The value of the annotation to set.</param>
        internal static void SetAnnotation(this IEdmModel model, IEdmElement annotatable, string localName, string value)
        {
            Debug.Assert(model != null, "model != null");
            Debug.Assert(annotatable != null, "annotatable != null");
            Debug.Assert(!String.IsNullOrEmpty(localName), "!string.IsNullOrEmpty(localName)");

            IEdmStringValue stringValue = null;
            if (value != null)
            {
                IEdmStringTypeReference typeReference = EdmCoreModel.Instance.GetString(/*nullable*/true);
                stringValue = new EdmStringConstant(typeReference, value);
            }

            model.SetAnnotationValue(annotatable, CsdlConstants.ODataMetadataNamespace, localName, stringValue);
        }
Beispiel #5
0
        private static string ConvertEdmAnnotationValue(IEdmDirectValueAnnotation annotation)
        {
            object obj2 = annotation.Value;

            if (obj2 == null)
            {
                return(null);
            }
            IEdmStringValue value2 = obj2 as IEdmStringValue;

            if (value2 == null)
            {
                throw new ODataException(Microsoft.Data.OData.Strings.EpmExtensionMethods_CannotConvertEdmAnnotationValue(annotation.NamespaceUri, annotation.Name, annotation.GetType().FullName));
            }
            return(value2.Value);
        }
Beispiel #6
0
        internal static bool TryGetODataAnnotation(this IEdmModel model, IEdmElement annotatable, string localName, out string value)
        {
            object obj2 = model.GetAnnotationValue(annotatable, "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata", localName);

            if (obj2 == null)
            {
                value = null;
                return(false);
            }
            IEdmStringValue value2 = obj2 as IEdmStringValue;

            if (value2 == null)
            {
                throw new ODataException(Microsoft.Data.OData.Strings.ODataAtomWriterMetadataUtils_InvalidAnnotationValue(localName, obj2.GetType().FullName));
            }
            value = value2.Value;
            return(true);
        }
Beispiel #7
0
        internal static bool ValidateValueCanBeWrittenAsXmlElementAnnotation(IEdmValue value, string annotationNamespace, string annotationName, out EdmError error)
        {
            IEdmStringValue edmStringValue = value as IEdmStringValue;

            if (edmStringValue == null)
            {
                error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Edm.Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationNotIEdmStringValue);
                return(false);
            }

            string rawString = edmStringValue.Value;

            XmlReader reader = XmlReader.Create(new StringReader(rawString));

            try
            {
                // Skip to root element.
                if (reader.NodeType != XmlNodeType.Element)
                {
                    while (reader.Read() && reader.NodeType != XmlNodeType.Element)
                    {
                    }
                }

                // The annotation must be an element.
                if (reader.EOF)
                {
                    error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Edm.Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationValueInvalidXml);
                    return(false);
                }

                // The root element must corespond to the term of the annotation
                string elementNamespace = reader.NamespaceURI;
                string elementName      = reader.LocalName;

                if (EdmUtil.IsNullOrWhiteSpaceInternal(elementNamespace) || EdmUtil.IsNullOrWhiteSpaceInternal(elementName))
                {
                    error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Edm.Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationNullNamespaceOrName);
                    return(false);
                }

                if (!((annotationNamespace == null || elementNamespace == annotationNamespace) && (annotationName == null || elementName == annotationName)))
                {
                    error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Edm.Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationMismatchedTerm);
                    return(false);
                }

                // Parse the entire fragment to determine if the XML is valid
                while (reader.Read())
                {
                }

                error = null;
                return(true);
            }
            catch (Exception)
            {
                error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Edm.Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationValueInvalidXml);
                return(false);
            }
        }
Beispiel #8
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);
            }
        }
        /// <summary>
        /// Populates test annotation values from the serializable annotation.
        /// </summary>
        /// <param name="feedMappingAnnotation">Test annotation to populate values for.</param>
        /// <param name="serializableAnnotation">Serializable annotation to populate values from.</param>
        /// <param name="propertyName">The name of the property to create the mapping for; null for mappings on the type.</param>
        private void PopulateTestAnnotationValues(PropertyMappingAnnotation feedMappingAnnotation, IEdmDirectValueAnnotation serializableAnnotation, string propertyName)
        {
            string localName = serializableAnnotation.Name;

            ExceptionUtilities.CheckObjectNotNull(localName, "localName cannot be null");

            if (localName.Count(c => c == UnderScore) == 2)
            {
                localName = localName.Remove(localName.LastIndexOf(UnderScore));
            }

            IEdmStringValue edmStringValue = serializableAnnotation.Value as IEdmStringValue;

            ExceptionUtilities.CheckObjectNotNull(edmStringValue, "edmStringValue cannot be null");
            string value = edmStringValue.Value;

            ExceptionUtilities.CheckObjectNotNull(value, "value cannot be null");

            switch (localName)
            {
            case ODataConstants.TargetPathAttribute:
                feedMappingAnnotation.TargetPath = this.HandleTargetPath(value, feedMappingAnnotation);
                break;

            case ODataConstants.SourcePathAttribute:
                if (value == null)
                {
                    feedMappingAnnotation.SourcePath = propertyName;
                }
                else if (propertyName != null && !value.StartsWith(propertyName))
                {
                    feedMappingAnnotation.SourcePath = propertyName + "/" + value;
                }
                else
                {
                    feedMappingAnnotation.SourcePath = value;
                }

                break;

            case ODataConstants.ContentKindAttribute:
                feedMappingAnnotation.SyndicationTextContentKind = ODataExtensions.FromTextContentKindAttributeString(value);
                break;

            case ODataConstants.KeepInContentAttribute:
                feedMappingAnnotation.KeepInContent = bool.Parse(value);
                break;

            case ODataConstants.NSPrefixAttribute:
                feedMappingAnnotation.TargetNamespacePrefix = value;
                break;

            case ODataConstants.NSUriAttribute:
                feedMappingAnnotation.TargetNamespaceUri = value;
                break;

            default:
                throw new TaupoArgumentException(
                          string.Format(CultureInfo.InvariantCulture, "Attribute Name:{0} does not match one of Attribute names for Customizing feeds", localName));
            }

            // if there is no source path, use the property name
            if (feedMappingAnnotation.SourcePath == null)
            {
                feedMappingAnnotation.SourcePath = propertyName;
            }
        }