/// <summary>
        /// Normalizes named stream specific atom metadata.
        /// </summary>
        /// <param name="payloadElement">The payload element to normalize.</param>
        public override void Visit(NamedStreamInstance payloadElement)
        {
            // Edit Link
            string editRelationValue = TestAtomConstants.ODataStreamPropertyEditMediaRelatedLinkRelationPrefix + payloadElement.Name;
            NamedStreamAtomLinkMetadataAnnotation editLinkAnnotation = payloadElement.Annotations.OfType <NamedStreamAtomLinkMetadataAnnotation>().SingleOrDefault(a => a.Relation == editRelationValue);

            NormalizeNamedStreamLinkValue(
                payloadElement,
                editLinkAnnotation,
                (annotation) => annotation.Href,
                (annotation, value) => annotation.Href = value,
                (namedStream) => namedStream.EditLink,
                (namedStream, value) => namedStream.EditLink = value);

            NormalizeNamedStreamLinkValue(
                payloadElement,
                editLinkAnnotation,
                (annotation) => annotation.Type,
                (annotation, value) => annotation.Type = value,
                (namedStream) => namedStream.EditLinkContentType,
                (namedStream, value) => namedStream.EditLinkContentType = value);

            // Source Link
            string sourceRelationValue = TestAtomConstants.ODataStreamPropertyMediaResourceRelatedLinkRelationPrefix + payloadElement.Name;
            NamedStreamAtomLinkMetadataAnnotation sourceLinkAnnotation = payloadElement.Annotations.OfType <NamedStreamAtomLinkMetadataAnnotation>().SingleOrDefault(a => a.Relation == sourceRelationValue);

            NormalizeNamedStreamLinkValue(
                payloadElement,
                sourceLinkAnnotation,
                (annotation) => annotation.Href,
                (annotation, value) => annotation.Href = value,
                (namedStream) => namedStream.SourceLink,
                (namedStream, value) => namedStream.SourceLink = value);

            NormalizeNamedStreamLinkValue(
                payloadElement,
                sourceLinkAnnotation,
                (annotation) => annotation.Type,
                (annotation, value) => annotation.Type = value,
                (namedStream) => namedStream.SourceLinkContentType,
                (namedStream, value) => namedStream.SourceLinkContentType = value);

            base.Visit(payloadElement);
        }
示例#2
0
        /// <summary>
        /// Computes the XmlTreeAnnotation for a stream property from the <see cref="NamedStreamAtomLinkMetadataAnnotation"/>.
        /// </summary>
        /// <param name="streampProperty">The stream property to compute the Xml annotation for.</param>
        /// <param name="relation">The relation of the link we are converting</param>
        /// <returns>The <see cref="XmlTreeAnnotation"/> for the link with the specified <paramref name="relation"/>.</returns>
        private XmlTreeAnnotation GetStreamPropertyLinkXmlTreeAnnotation(NamedStreamInstance streampProperty, string relation)
        {
            // Look it up again since it was created above.
            NamedStreamAtomLinkMetadataAnnotation linkAnnotation = streampProperty.Annotations.OfType <NamedStreamAtomLinkMetadataAnnotation>().SingleOrDefault(a => a.Relation == relation);

            if (linkAnnotation == null)
            {
                return(null);
            }

            List <XmlTreeAnnotation> attributes = new List <XmlTreeAnnotation>();

            if (linkAnnotation.Href != null)
            {
                attributes.Add(XmlTreeAnnotation.AtomAttribute(TestAtomConstants.AtomLinkHrefAttributeName, linkAnnotation.Href));
            }

            if (linkAnnotation.HrefLang != null)
            {
                attributes.Add(XmlTreeAnnotation.AtomAttribute(TestAtomConstants.AtomLinkHrefLangAttributeName, linkAnnotation.HrefLang));
            }

            if (linkAnnotation.Length != null)
            {
                attributes.Add(XmlTreeAnnotation.AtomAttribute(TestAtomConstants.AtomLinkLengthAttributeName, linkAnnotation.Length));
            }

            if (linkAnnotation.Relation != null)
            {
                attributes.Add(XmlTreeAnnotation.AtomAttribute(TestAtomConstants.AtomLinkRelationAttributeName, linkAnnotation.Relation));
            }

            if (linkAnnotation.Title != null)
            {
                attributes.Add(XmlTreeAnnotation.AtomAttribute(TestAtomConstants.AtomLinkTitleAttributeName, linkAnnotation.Title));
            }

            if (linkAnnotation.Type != null)
            {
                attributes.Add(XmlTreeAnnotation.AtomAttribute(TestAtomConstants.AtomLinkTypeAttributeName, linkAnnotation.Type));
            }

            return(XmlTreeAnnotation.Atom(TestAtomConstants.AtomLinkElementName, null, attributes.ToArray()));
        }
        /// <summary>
        /// Normalizes an named stream's Atom link property value between a NamedStreamAtomLinkMetadataAnnotation and another property.
        /// </summary>
        /// <param name="namedStream">The named stream to normalize.</param>
        /// <param name="annotation">The annotation to use for normalizing.</param>
        /// <param name="getAnnotationValue">Function for retrieving the annotation value for the link property.</param>
        /// <param name="setAnnotationValue">Delegate for setting the annotation value for the link property.</param>
        /// <param name="getPropertyValue">Function for retrieving the property value for the link property.</param>
        /// <param name="setPropertyValue">Delegate for setting the property value for the link property.</param>
        private void NormalizeNamedStreamLinkValue(
            NamedStreamInstance namedStream,
            NamedStreamAtomLinkMetadataAnnotation annotation,
            Func <NamedStreamAtomLinkMetadataAnnotation, string> getAnnotationValue,
            Action <NamedStreamAtomLinkMetadataAnnotation, string> setAnnotationValue,
            Func <NamedStreamInstance, string> getPropertyValue,
            Action <NamedStreamInstance, string> setPropertyValue)
        {
            string propertyValue = getPropertyValue(namedStream);

            if (propertyValue != null)
            {
                if (annotation != null)
                {
                    string annotationValue = getAnnotationValue(annotation);
                    if (annotationValue == null)
                    {
                        setAnnotationValue(annotation, propertyValue);
                    }
                    else
                    {
                        ExceptionUtilities.Assert(propertyValue == annotationValue, "Link '" + annotation.Relation + "' has different values : Property=" + propertyValue + ", Annotation=" + annotationValue);
                    }
                }
                else
                {
                    var newAnnotation = new NamedStreamAtomLinkMetadataAnnotation {
                        Relation = annotation.Relation
                    };
                    setAnnotationValue(newAnnotation, propertyValue);
                    namedStream.Add(newAnnotation);
                }
            }
            else if (annotation != null)
            {
                setPropertyValue(namedStream, getAnnotationValue(annotation));
            }
        }