internal void ReadAcceptElementInCollection(AtomResourceCollectionMetadata collectionMetadata)
 {
     if (collectionMetadata.Accept != null)
     {
         throw new ODataException(Strings.ODataAtomServiceDocumentMetadataDeserializer_MultipleAcceptElementsFoundInCollection);
     }
     collectionMetadata.Accept = base.XmlReader.ReadElementValue();
 }
 internal void ReadTitleElementInCollection(AtomResourceCollectionMetadata collectionMetadata)
 {
     if (collectionMetadata.Title != null)
     {
         throw new ODataException(Strings.ODataAtomServiceDocumentMetadataDeserializer_MultipleTitleElementsFound("collection"));
     }
     collectionMetadata.Title = base.ReadTitleElement();
 }
 internal void ReadAcceptElementInCollection(AtomResourceCollectionMetadata collectionMetadata)
 {
     if (collectionMetadata.Accept != null)
     {
         throw new ODataException(Strings.ODataAtomServiceDocumentMetadataDeserializer_MultipleAcceptElementsFoundInCollection);
     }
     collectionMetadata.Accept = base.XmlReader.ReadElementValue();
 }
Beispiel #4
0
 public static AtomResourceCollectionMetadata Atom(this ODataResourceCollectionInfo collection)
 {
     ExceptionUtils.CheckArgumentNotNull<ODataResourceCollectionInfo>(collection, "collection");
     AtomResourceCollectionMetadata annotation = collection.GetAnnotation<AtomResourceCollectionMetadata>();
     if (annotation == null)
     {
         annotation = new AtomResourceCollectionMetadata();
         collection.SetAnnotation<AtomResourceCollectionMetadata>(annotation);
     }
     return annotation;
 }
Beispiel #5
0
        public static AtomResourceCollectionMetadata Atom(this ODataResourceCollectionInfo collection)
        {
            ExceptionUtils.CheckArgumentNotNull <ODataResourceCollectionInfo>(collection, "collection");
            AtomResourceCollectionMetadata annotation = collection.GetAnnotation <AtomResourceCollectionMetadata>();

            if (annotation == null)
            {
                annotation = new AtomResourceCollectionMetadata();
                collection.SetAnnotation <AtomResourceCollectionMetadata>(annotation);
            }
            return(annotation);
        }
Beispiel #6
0
        /// <summary>
        /// Reads an atom:title element and adds the new information to <paramref name="collectionMetadata"/>.
        /// </summary>
        /// <param name="collectionMetadata">The non-null collection metadata object to augment.</param>
        /// <remarks>
        /// Pre-Condition:  XmlNodeType.Element - The start of the title element.
        /// Post-Condition: Any                 - The next node after the title element.
        /// </remarks>
        internal void ReadTitleElementInCollection(AtomResourceCollectionMetadata collectionMetadata)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(collectionMetadata != null, "collectionMetadata != null");
            this.AssertXmlCondition(XmlNodeType.Element);
            Debug.Assert(this.XmlReader.LocalName == AtomConstants.AtomTitleElementName, "Expected element named 'title'.");
            Debug.Assert(this.XmlReader.NamespaceURI == AtomConstants.AtomNamespace, "Element 'title' should be in the atom namespace.");

            if (collectionMetadata.Title != null)
            {
                throw new ODataException(Strings.ODataAtomServiceDocumentMetadataDeserializer_MultipleTitleElementsFound(AtomConstants.AtomPublishingCollectionElementName));
            }

            collectionMetadata.Title = this.ReadTitleElement();
        }
Beispiel #7
0
        /// <summary>
        /// Reads an "app:accept" element and adds the new information to <paramref name="collectionMetadata"/>.
        /// </summary>
        /// <param name="collectionMetadata">The non-null collection metadata object to augment.</param>
        /// <remarks>
        /// Pre-Condition:  XmlNodeType.Element - The start of the app:accept element.
        /// Post-Condition: Any                 - The next node after the app:accept element.
        /// </remarks>
        internal void ReadAcceptElementInCollection(AtomResourceCollectionMetadata collectionMetadata)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(collectionMetadata != null, "collectionMetadata != null");
            this.AssertXmlCondition(XmlNodeType.Element);
            Debug.Assert(this.XmlReader.LocalName == AtomConstants.AtomPublishingAcceptElementName, "Expected element named 'accept'.");
            Debug.Assert(this.XmlReader.NamespaceURI == AtomConstants.AtomPublishingNamespace, "Element 'accept' should be in the atom publishing namespace.");

            if (collectionMetadata.Accept != null)
            {
                throw new ODataException(Strings.ODataAtomServiceDocumentMetadataDeserializer_MultipleAcceptElementsFoundInCollection);
            }

            collectionMetadata.Accept = this.XmlReader.ReadElementValue();
        }
 internal void WriteServiceDocument(DataServiceProviderWrapper provider)
 {
     ODataWorkspace defaultWorkspace = new ODataWorkspace {
         Collections = provider.GetResourceSets().Select<ResourceSetWrapper, ODataResourceCollectionInfo>(delegate (ResourceSetWrapper rs) {
             ODataResourceCollectionInfo info = new ODataResourceCollectionInfo {
                 Url = new Uri(rs.Name, UriKind.RelativeOrAbsolute)
             };
             AtomResourceCollectionMetadata annotation = new AtomResourceCollectionMetadata();
             AtomTextConstruct construct = new AtomTextConstruct {
                 Text = rs.Name
             };
             annotation.Title = construct;
             info.SetAnnotation<AtomResourceCollectionMetadata>(annotation);
             return info;
         })
     };
     this.writer.WriteServiceDocument(defaultWorkspace);
 }
Beispiel #9
0
        /// <summary>
        /// Reads an atom:title element and adds the new information to <paramref name="collectionInfo"/> and (if ATOM metadata reading is on) <paramref name="collectionMetadata"/>.
        /// </summary>
        /// <param name="collectionMetadata">The collection metadata object to augment, or null if metadata reading is not on.</param>
        /// <param name="collectionInfo">The non-null collection info object being populated.</param>
        /// <remarks>
        /// Pre-Condition:  XmlNodeType.Element - The start of the title element.
        /// Post-Condition: Any                 - The next node after the title element.
        /// </remarks>
        internal void ReadTitleElementInCollection(AtomResourceCollectionMetadata collectionMetadata, ODataResourceCollectionInfo collectionInfo)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(!this.ReadAtomMetadata || collectionMetadata != null, "collectionMetadata parameter should be non-null when ATOM metadata reading is enabled.");
            Debug.Assert(collectionInfo != null, "collectionInfo != null");
            this.AssertXmlCondition(XmlNodeType.Element);
            Debug.Assert(this.XmlReader.LocalName == AtomConstants.AtomTitleElementName, "Expected element named 'title'.");
            Debug.Assert(this.XmlReader.NamespaceURI == AtomConstants.AtomNamespace, "Element 'title' should be in the atom namespace.");

            if (collectionInfo.Name != null)
            {
                throw new ODataException(Strings.ODataAtomServiceDocumentMetadataDeserializer_MultipleTitleElementsFound(AtomConstants.AtomPublishingCollectionElementName));
            }

            AtomTextConstruct titleTextConstruct = this.ReadTitleElement();

            collectionInfo.Name = titleTextConstruct.Text;

            if (this.ReadAtomMetadata)
            {
                collectionMetadata.Title = titleTextConstruct;
            }
        }
 internal void ReadTitleElementInCollection(AtomResourceCollectionMetadata collectionMetadata)
 {
     if (collectionMetadata.Title != null)
     {
         throw new ODataException(Strings.ODataAtomServiceDocumentMetadataDeserializer_MultipleTitleElementsFound("collection"));
     }
     collectionMetadata.Title = base.ReadTitleElement();
 }
 public void WriteServiceDocument()
 {
     var msgWriter = new ODataMessageWriter(_response, _writerSettings, _map.Model);
     var collections = new List<ODataResourceCollectionInfo>();
     foreach (
         var entityContainer in
             _map.Model.EntityContainers().Where(ec => _map.Model.IsDefaultEntityContainer(ec)))
     {
         foreach (var es in entityContainer.EntitySets())
         {
             var collectionInfo = new ODataResourceCollectionInfo {Url = new Uri(es.Name, UriKind.Relative)};
             var metadata = new AtomResourceCollectionMetadata {Title = es.Name};
             collectionInfo.SetAnnotation(metadata);
             collections.Add(collectionInfo);
         }
     }
     var workspace = new ODataWorkspace {Collections = collections};
     msgWriter.WriteServiceDocument(workspace);
 }
        /// <summary>
        /// Reads an "app:accept" element and adds the new information to <paramref name="collectionMetadata"/>.
        /// </summary>
        /// <param name="collectionMetadata">The non-null collection metadata object to augment.</param>
        /// <remarks>
        /// Pre-Condition:  XmlNodeType.Element - The start of the app:accept element.
        /// Post-Condition: Any                 - The next node after the app:accept element. 
        /// </remarks>
        internal void ReadAcceptElementInCollection(AtomResourceCollectionMetadata collectionMetadata)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(collectionMetadata != null, "collectionMetadata != null");
            this.AssertXmlCondition(XmlNodeType.Element);
            Debug.Assert(this.XmlReader.LocalName == AtomConstants.AtomPublishingAcceptElementName, "Expected element named 'accept'.");
            Debug.Assert(this.XmlReader.NamespaceURI == AtomConstants.AtomPublishingNamespace, "Element 'accept' should be in the atom publishing namespace.");

            if (collectionMetadata.Accept != null)
            {
                throw new ODataException(Strings.ODataAtomServiceDocumentMetadataDeserializer_MultipleAcceptElementsFoundInCollection);
            }

            collectionMetadata.Accept = this.XmlReader.ReadElementValue();
        }
        /// <summary>
        /// Reads an app:categories element as well as each atom:category element contained within it, and adds the new information to <paramref name="collectionMetadata"/>.
        /// </summary>
        /// <param name="collectionMetadata">The non-null collection metadata object to augment.</param>
        /// <remarks>
        /// Pre-Condition:  XmlNodeType.Element - The start of the app:categories element.
        /// Post-Condition: Any                 - The next node after the app:categories element. 
        /// </remarks>
        internal void ReadCategoriesElementInCollection(AtomResourceCollectionMetadata collectionMetadata)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(collectionMetadata != null, "collectionMetadata != null");
            this.AssertXmlCondition(XmlNodeType.Element);
            Debug.Assert(this.XmlReader.LocalName == AtomConstants.AtomPublishingCategoriesElementName, "Expected element named 'categories'.");
            Debug.Assert(this.XmlReader.NamespaceURI == AtomConstants.AtomPublishingNamespace, "Element 'categories' should be in the atom publishing namespace.");

            AtomCategoriesMetadata categoriesMetadata = new AtomCategoriesMetadata();
            List<AtomCategoryMetadata> categoryList = new List<AtomCategoryMetadata>();

            while (this.XmlReader.MoveToNextAttribute())
            {
                string attributeValue = this.XmlReader.Value;

                if (this.XmlReader.NamespaceEquals(this.EmptyNamespace))
                {
                    if (this.XmlReader.LocalNameEquals(this.AtomHRefAttributeName))
                    {
                        categoriesMetadata.Href = this.ProcessUriFromPayload(attributeValue, this.XmlReader.XmlBaseUri);
                    }
                    else if (this.XmlReader.LocalNameEquals(this.AtomPublishingFixedAttributeName))
                    {
                        if (String.CompareOrdinal(attributeValue, AtomConstants.AtomPublishingFixedYesValue) == 0)
                        {
                            categoriesMetadata.Fixed = true;
                        }
                        else if (String.CompareOrdinal(attributeValue, AtomConstants.AtomPublishingFixedNoValue) == 0)
                        {
                            categoriesMetadata.Fixed = false;
                        }
                        else
                        {
                            throw new ODataException(Strings.ODataAtomServiceDocumentMetadataDeserializer_InvalidFixedAttributeValue(attributeValue));
                        }
                    }
                    else if (this.XmlReader.LocalNameEquals(this.AtomCategorySchemeAttributeName))
                    {
                        categoriesMetadata.Scheme = attributeValue;
                    }
                }
            }

            this.XmlReader.MoveToElement();

            if (!this.XmlReader.IsEmptyElement)
            {
                // read over the categories element
                this.XmlReader.ReadStartElement();

                do
                {
                    switch (this.XmlReader.NodeType)
                    {
                        case XmlNodeType.Element:
                            if (this.XmlReader.NamespaceEquals(this.AtomNamespace) && this.XmlReader.LocalNameEquals(this.AtomCategoryElementName))
                            {
                                categoryList.Add(this.ReadCategoryElementInCollection());
                            }

                            break;
                        case XmlNodeType.EndElement:
                            // end of 'categories' element.
                            break;
                        default:
                            // ignore all other nodes.
                            this.XmlReader.Skip();
                            break;
                    }
                }
                while (this.XmlReader.NodeType != XmlNodeType.EndElement);
            } // if (!this.XmlReader.IsEmptyElement)

            // read over the end tag of the categories element or the start tag if the categories element is empty.
            this.XmlReader.Read();

            categoriesMetadata.Categories = new ReadOnlyEnumerable<AtomCategoryMetadata>(categoryList);
            collectionMetadata.Categories = categoriesMetadata;
        }
        /// <summary>
        /// Reads an atom:title element and adds the new information to <paramref name="collectionMetadata"/>.
        /// </summary>
        /// <param name="collectionMetadata">The non-null collection metadata object to augment.</param>
        /// <remarks>
        /// Pre-Condition:  XmlNodeType.Element - The start of the title element.
        /// Post-Condition: Any                 - The next node after the title element. 
        /// </remarks>
        internal void ReadTitleElementInCollection(AtomResourceCollectionMetadata collectionMetadata)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(collectionMetadata != null, "collectionMetadata != null");
            this.AssertXmlCondition(XmlNodeType.Element);
            Debug.Assert(this.XmlReader.LocalName == AtomConstants.AtomTitleElementName, "Expected element named 'title'.");
            Debug.Assert(this.XmlReader.NamespaceURI == AtomConstants.AtomNamespace, "Element 'title' should be in the atom namespace.");

            if (collectionMetadata.Title != null)
            {
                throw new ODataException(Strings.ODataAtomServiceDocumentMetadataDeserializer_MultipleTitleElementsFound(AtomConstants.AtomPublishingCollectionElementName));
            }

            collectionMetadata.Title = this.ReadTitleElement();
        }
        internal void WriteResourceCollectionMetadata(ODataResourceCollectionInfo collection)
        {
            AtomResourceCollectionMetadata annotation = collection.GetAnnotation <AtomResourceCollectionMetadata>();
            AtomTextConstruct textConstruct           = null;

            if (annotation != null)
            {
                textConstruct = annotation.Title;
            }
            if (base.UseServerFormatBehavior && (textConstruct.Kind == AtomTextConstructKind.Text))
            {
                base.WriteElementWithTextContent("atom", "title", "http://www.w3.org/2005/Atom", textConstruct.Text);
            }
            else
            {
                base.WriteTextConstruct("atom", "title", "http://www.w3.org/2005/Atom", textConstruct);
            }
            if (annotation != null)
            {
                string accept = annotation.Accept;
                if (accept != null)
                {
                    base.WriteElementWithTextContent(string.Empty, "accept", "http://www.w3.org/2007/app", accept);
                }
                AtomCategoriesMetadata categories = annotation.Categories;
                if (categories != null)
                {
                    base.XmlWriter.WriteStartElement(string.Empty, "categories", "http://www.w3.org/2007/app");
                    Uri    href   = categories.Href;
                    bool?  @fixed = categories.Fixed;
                    string scheme = categories.Scheme;
                    IEnumerable <AtomCategoryMetadata> source = categories.Categories;
                    if (href != null)
                    {
                        if ((@fixed.HasValue || (scheme != null)) || ((source != null) && source.Any <AtomCategoryMetadata>()))
                        {
                            throw new ODataException(Microsoft.Data.OData.Strings.ODataAtomWriterMetadataUtils_CategoriesHrefWithOtherValues);
                        }
                        base.XmlWriter.WriteAttributeString("href", base.UriToUrlAttributeValue(href));
                    }
                    else
                    {
                        if (@fixed.HasValue)
                        {
                            base.XmlWriter.WriteAttributeString("fixed", @fixed.Value ? "yes" : "no");
                        }
                        if (scheme != null)
                        {
                            base.XmlWriter.WriteAttributeString("scheme", scheme);
                        }
                        if (source != null)
                        {
                            foreach (AtomCategoryMetadata metadata3 in source)
                            {
                                base.WriteCategory("atom", metadata3.Term, metadata3.Scheme, metadata3.Label);
                            }
                        }
                    }
                    base.XmlWriter.WriteEndElement();
                }
            }
        }
        /// <summary>
        /// Writes the ATOM metadata for a single (resource) collection element.
        /// </summary>
        /// <param name="collection">The collection element to get the metadata for and write it.</param>
        internal void WriteResourceCollectionMetadata(ODataResourceCollectionInfo collection)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(collection != null, "collection != null");
            Debug.Assert(collection.Url != null, "collection.Url should have been validated at this point");

            AtomResourceCollectionMetadata metadata = collection.GetAnnotation <AtomResourceCollectionMetadata>();

            AtomTextConstruct title = null;

            if (metadata != null)
            {
                title = metadata.Title;
            }

            // The ATOMPUB specification requires a title.
            // <atom:title>title</atom:title>
            // Note that this will write an empty atom:title element even if the title is null.
            if (this.UseServerFormatBehavior && title.Kind == AtomTextConstructKind.Text)
            {
                // For WCF DS server we must not write the type attribute, just a simple <atom:title>title<atom:title>
                this.WriteElementWithTextContent(
                    AtomConstants.NonEmptyAtomNamespacePrefix,
                    AtomConstants.AtomTitleElementName,
                    AtomConstants.AtomNamespace,
                    title.Text);
            }
            else
            {
                this.WriteTextConstruct(AtomConstants.NonEmptyAtomNamespacePrefix, AtomConstants.AtomTitleElementName, AtomConstants.AtomNamespace, title);
            }

            if (metadata != null)
            {
                string accept = metadata.Accept;
                if (accept != null)
                {
                    // <app:accept>accept</app:accept>
                    this.WriteElementWithTextContent(
                        string.Empty,
                        AtomConstants.AtomPublishingAcceptElementName,
                        AtomConstants.AtomPublishingNamespace,
                        accept);
                }

                AtomCategoriesMetadata categories = metadata.Categories;
                if (categories != null)
                {
                    // <app:categories>
                    this.XmlWriter.WriteStartElement(string.Empty, AtomConstants.AtomPublishingCategoriesElementName, AtomConstants.AtomPublishingNamespace);

                    Uri    href       = categories.Href;
                    bool?  fixedValue = categories.Fixed;
                    string scheme     = categories.Scheme;
                    IEnumerable <AtomCategoryMetadata> categoriesCollection = categories.Categories;
                    if (href != null)
                    {
                        // Out of line categories document
                        if (fixedValue.HasValue || scheme != null ||
                            (categoriesCollection != null && categoriesCollection.Any()))
                        {
                            throw new ODataException(o.Strings.ODataAtomWriterMetadataUtils_CategoriesHrefWithOtherValues);
                        }

                        this.XmlWriter.WriteAttributeString(AtomConstants.AtomHRefAttributeName, this.UriToUrlAttributeValue(href));
                    }
                    else
                    {
                        // Inline categories document

                        // fixed='yes|no'
                        if (fixedValue.HasValue)
                        {
                            this.XmlWriter.WriteAttributeString(
                                AtomConstants.AtomPublishingFixedAttributeName,
                                fixedValue.Value ? AtomConstants.AtomPublishingFixedYesValue : AtomConstants.AtomPublishingFixedNoValue);
                        }

                        // scheme='scheme'
                        if (scheme != null)
                        {
                            this.XmlWriter.WriteAttributeString(AtomConstants.AtomCategorySchemeAttributeName, scheme);
                        }

                        if (categoriesCollection != null)
                        {
                            foreach (AtomCategoryMetadata category in categoriesCollection)
                            {
                                // <atom:category/>
                                this.WriteCategory(AtomConstants.NonEmptyAtomNamespacePrefix, category.Term, category.Scheme, category.Label);
                            }
                        }
                    }

                    // </app:categories>
                    this.XmlWriter.WriteEndElement();
                }
            }
        }
Beispiel #17
0
        private ODataResourceCollectionInfo ReadCollectionElement()
        {
            ODataResourceCollectionInfo info = new ODataResourceCollectionInfo();
            string attribute = base.XmlReader.GetAttribute(this.AtomHRefAttributeName, this.EmptyNamespace);

            ValidationUtils.ValidateResourceCollectionInfoUrl(attribute);
            info.Url = base.ProcessUriFromPayload(attribute, base.XmlReader.XmlBaseUri);
            bool enableAtomMetadataReading = base.MessageReaderSettings.EnableAtomMetadataReading;
            AtomResourceCollectionMetadata collectionMetadata = null;

            if (enableAtomMetadataReading)
            {
                collectionMetadata = new AtomResourceCollectionMetadata();
            }
            if (!base.XmlReader.IsEmptyElement)
            {
                base.XmlReader.ReadStartElement();
                do
                {
                    switch (base.XmlReader.NodeType)
                    {
                    case XmlNodeType.Element:
                        if (base.XmlReader.NamespaceEquals(this.AtomPublishingNamespace))
                        {
                            if (base.XmlReader.LocalNameEquals(this.AtomPublishingCategoriesElementName))
                            {
                                if (enableAtomMetadataReading)
                                {
                                    this.ServiceDocumentMetadataDeserializer.ReadCategoriesElementInCollection(collectionMetadata);
                                }
                                else
                                {
                                    base.XmlReader.Skip();
                                }
                            }
                            else
                            {
                                if (!base.XmlReader.LocalNameEquals(this.AtomPublishingAcceptElementName))
                                {
                                    throw new ODataException(Strings.ODataAtomServiceDocumentDeserializer_UnexpectedElementInResourceCollection(base.XmlReader.LocalName));
                                }
                                if (enableAtomMetadataReading)
                                {
                                    this.ServiceDocumentMetadataDeserializer.ReadAcceptElementInCollection(collectionMetadata);
                                }
                                else
                                {
                                    base.XmlReader.Skip();
                                }
                            }
                        }
                        else if (base.XmlReader.NamespaceEquals(this.AtomNamespace))
                        {
                            if (enableAtomMetadataReading && base.XmlReader.LocalNameEquals(this.AtomTitleElementName))
                            {
                                this.ServiceDocumentMetadataDeserializer.ReadTitleElementInCollection(collectionMetadata);
                            }
                            else
                            {
                                base.XmlReader.Skip();
                            }
                        }
                        else
                        {
                            base.XmlReader.Skip();
                        }
                        break;

                    case XmlNodeType.EndElement:
                        break;

                    default:
                        base.XmlReader.Skip();
                        break;
                    }
                }while (base.XmlReader.NodeType != XmlNodeType.EndElement);
            }
            base.XmlReader.Read();
            if (enableAtomMetadataReading)
            {
                info.SetAnnotation <AtomResourceCollectionMetadata>(collectionMetadata);
            }
            return(info);
        }
        internal void ReadCategoriesElementInCollection(AtomResourceCollectionMetadata collectionMetadata)
        {
            AtomCategoriesMetadata      metadata   = new AtomCategoriesMetadata();
            List <AtomCategoryMetadata> sourceList = new List <AtomCategoryMetadata>();

            while (base.XmlReader.MoveToNextAttribute())
            {
                string uriFromPayload = base.XmlReader.Value;
                if (base.XmlReader.NamespaceEquals(this.EmptyNamespace))
                {
                    if (base.XmlReader.LocalNameEquals(this.AtomHRefAttributeName))
                    {
                        metadata.Href = base.ProcessUriFromPayload(uriFromPayload, base.XmlReader.XmlBaseUri);
                    }
                    else
                    {
                        if (base.XmlReader.LocalNameEquals(this.AtomPublishingFixedAttributeName))
                        {
                            if (string.CompareOrdinal(uriFromPayload, "yes") != 0)
                            {
                                if (string.CompareOrdinal(uriFromPayload, "no") != 0)
                                {
                                    throw new ODataException(Strings.ODataAtomServiceDocumentMetadataDeserializer_InvalidFixedAttributeValue(uriFromPayload));
                                }
                                metadata.Fixed = false;
                            }
                            else
                            {
                                metadata.Fixed = true;
                            }
                            continue;
                        }
                        if (base.XmlReader.LocalNameEquals(this.AtomCategorySchemeAttributeName))
                        {
                            metadata.Scheme = uriFromPayload;
                        }
                    }
                }
            }
            base.XmlReader.MoveToElement();
            if (!base.XmlReader.IsEmptyElement)
            {
                base.XmlReader.ReadStartElement();
                do
                {
                    switch (base.XmlReader.NodeType)
                    {
                    case XmlNodeType.Element:
                        if (base.XmlReader.NamespaceEquals(this.AtomNamespace) && base.XmlReader.LocalNameEquals(this.AtomCategoryElementName))
                        {
                            sourceList.Add(this.ReadCategoryElementInCollection());
                        }
                        break;

                    case XmlNodeType.EndElement:
                        break;

                    default:
                        base.XmlReader.Skip();
                        break;
                    }
                }while (base.XmlReader.NodeType != XmlNodeType.EndElement);
            }
            base.XmlReader.Read();
            metadata.Categories           = new ReadOnlyEnumerable <AtomCategoryMetadata>(sourceList);
            collectionMetadata.Categories = metadata;
        }
        private ODataResourceCollectionInfo ReadCollectionElement()
        {
            ODataResourceCollectionInfo info = new ODataResourceCollectionInfo();
            string attribute = base.XmlReader.GetAttribute(this.AtomHRefAttributeName, this.EmptyNamespace);
            ValidationUtils.ValidateResourceCollectionInfoUrl(attribute);
            info.Url = base.ProcessUriFromPayload(attribute, base.XmlReader.XmlBaseUri);
            bool enableAtomMetadataReading = base.MessageReaderSettings.EnableAtomMetadataReading;
            AtomResourceCollectionMetadata collectionMetadata = null;
            if (enableAtomMetadataReading)
            {
                collectionMetadata = new AtomResourceCollectionMetadata();
            }
            if (!base.XmlReader.IsEmptyElement)
            {
                base.XmlReader.ReadStartElement();
                do
                {
                    switch (base.XmlReader.NodeType)
                    {
                        case XmlNodeType.Element:
                            if (base.XmlReader.NamespaceEquals(this.AtomPublishingNamespace))
                            {
                                if (base.XmlReader.LocalNameEquals(this.AtomPublishingCategoriesElementName))
                                {
                                    if (enableAtomMetadataReading)
                                    {
                                        this.ServiceDocumentMetadataDeserializer.ReadCategoriesElementInCollection(collectionMetadata);
                                    }
                                    else
                                    {
                                        base.XmlReader.Skip();
                                    }
                                }
                                else
                                {
                                    if (!base.XmlReader.LocalNameEquals(this.AtomPublishingAcceptElementName))
                                    {
                                        throw new ODataException(Strings.ODataAtomServiceDocumentDeserializer_UnexpectedElementInResourceCollection(base.XmlReader.LocalName));
                                    }
                                    if (enableAtomMetadataReading)
                                    {
                                        this.ServiceDocumentMetadataDeserializer.ReadAcceptElementInCollection(collectionMetadata);
                                    }
                                    else
                                    {
                                        base.XmlReader.Skip();
                                    }
                                }
                            }
                            else if (base.XmlReader.NamespaceEquals(this.AtomNamespace))
                            {
                                if (enableAtomMetadataReading && base.XmlReader.LocalNameEquals(this.AtomTitleElementName))
                                {
                                    this.ServiceDocumentMetadataDeserializer.ReadTitleElementInCollection(collectionMetadata);
                                }
                                else
                                {
                                    base.XmlReader.Skip();
                                }
                            }
                            else
                            {
                                base.XmlReader.Skip();
                            }
                            break;

                        case XmlNodeType.EndElement:
                            break;

                        default:
                            base.XmlReader.Skip();
                            break;
                    }
                }
                while (base.XmlReader.NodeType != XmlNodeType.EndElement);
            }
            base.XmlReader.Read();
            if (enableAtomMetadataReading)
            {
                info.SetAnnotation<AtomResourceCollectionMetadata>(collectionMetadata);
            }
            return info;
        }
        internal void ReadCategoriesElementInCollection(AtomResourceCollectionMetadata collectionMetadata)
        {
            AtomCategoriesMetadata metadata = new AtomCategoriesMetadata();
            List<AtomCategoryMetadata> sourceList = new List<AtomCategoryMetadata>();
            while (base.XmlReader.MoveToNextAttribute())
            {
                string uriFromPayload = base.XmlReader.Value;
                if (base.XmlReader.NamespaceEquals(this.EmptyNamespace))
                {
                    if (base.XmlReader.LocalNameEquals(this.AtomHRefAttributeName))
                    {
                        metadata.Href = base.ProcessUriFromPayload(uriFromPayload, base.XmlReader.XmlBaseUri);
                    }
                    else
                    {
                        if (base.XmlReader.LocalNameEquals(this.AtomPublishingFixedAttributeName))
                        {
                            if (string.CompareOrdinal(uriFromPayload, "yes") != 0)
                            {
                                if (string.CompareOrdinal(uriFromPayload, "no") != 0)
                                {
                                    throw new ODataException(Strings.ODataAtomServiceDocumentMetadataDeserializer_InvalidFixedAttributeValue(uriFromPayload));
                                }
                                metadata.Fixed = false;
                            }
                            else
                            {
                                metadata.Fixed = true;
                            }
                            continue;
                        }
                        if (base.XmlReader.LocalNameEquals(this.AtomCategorySchemeAttributeName))
                        {
                            metadata.Scheme = uriFromPayload;
                        }
                    }
                }
            }
            base.XmlReader.MoveToElement();
            if (!base.XmlReader.IsEmptyElement)
            {
                base.XmlReader.ReadStartElement();
                do
                {
                    switch (base.XmlReader.NodeType)
                    {
                        case XmlNodeType.Element:
                            if (base.XmlReader.NamespaceEquals(this.AtomNamespace) && base.XmlReader.LocalNameEquals(this.AtomCategoryElementName))
                            {
                                sourceList.Add(this.ReadCategoryElementInCollection());
                            }
                            break;

                        case XmlNodeType.EndElement:
                            break;

                        default:
                            base.XmlReader.Skip();
                            break;
                    }
                }
                while (base.XmlReader.NodeType != XmlNodeType.EndElement);
            }
            base.XmlReader.Read();
            metadata.Categories = new ReadOnlyEnumerable<AtomCategoryMetadata>(sourceList);
            collectionMetadata.Categories = metadata;
        }
Beispiel #21
0
        /// <summary>
        /// Reads an app:categories element as well as each atom:category element contained within it, and adds the new information to <paramref name="collectionMetadata"/>.
        /// </summary>
        /// <param name="collectionMetadata">The non-null collection metadata object to augment.</param>
        /// <remarks>
        /// Pre-Condition:  XmlNodeType.Element - The start of the app:categories element.
        /// Post-Condition: Any                 - The next node after the app:categories element.
        /// </remarks>
        internal void ReadCategoriesElementInCollection(AtomResourceCollectionMetadata collectionMetadata)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(collectionMetadata != null, "collectionMetadata != null");
            this.AssertXmlCondition(XmlNodeType.Element);
            Debug.Assert(this.XmlReader.LocalName == AtomConstants.AtomPublishingCategoriesElementName, "Expected element named 'categories'.");
            Debug.Assert(this.XmlReader.NamespaceURI == AtomConstants.AtomPublishingNamespace, "Element 'categories' should be in the atom publishing namespace.");

            AtomCategoriesMetadata      categoriesMetadata = new AtomCategoriesMetadata();
            List <AtomCategoryMetadata> categoryList       = new List <AtomCategoryMetadata>();

            while (this.XmlReader.MoveToNextAttribute())
            {
                string attributeValue = this.XmlReader.Value;

                if (this.XmlReader.NamespaceEquals(this.EmptyNamespace))
                {
                    if (this.XmlReader.LocalNameEquals(this.AtomHRefAttributeName))
                    {
                        categoriesMetadata.Href = this.ProcessUriFromPayload(attributeValue, this.XmlReader.XmlBaseUri);
                    }
                    else if (this.XmlReader.LocalNameEquals(this.AtomPublishingFixedAttributeName))
                    {
                        if (String.CompareOrdinal(attributeValue, AtomConstants.AtomPublishingFixedYesValue) == 0)
                        {
                            categoriesMetadata.Fixed = true;
                        }
                        else if (String.CompareOrdinal(attributeValue, AtomConstants.AtomPublishingFixedNoValue) == 0)
                        {
                            categoriesMetadata.Fixed = false;
                        }
                        else
                        {
                            throw new ODataException(Strings.ODataAtomServiceDocumentMetadataDeserializer_InvalidFixedAttributeValue(attributeValue));
                        }
                    }
                    else if (this.XmlReader.LocalNameEquals(this.AtomCategorySchemeAttributeName))
                    {
                        categoriesMetadata.Scheme = attributeValue;
                    }
                }
            }

            this.XmlReader.MoveToElement();

            if (!this.XmlReader.IsEmptyElement)
            {
                // read over the categories element
                this.XmlReader.ReadStartElement();

                do
                {
                    switch (this.XmlReader.NodeType)
                    {
                    case XmlNodeType.Element:
                        if (this.XmlReader.NamespaceEquals(this.AtomNamespace) && this.XmlReader.LocalNameEquals(this.AtomCategoryElementName))
                        {
                            categoryList.Add(this.ReadCategoryElementInCollection());
                        }

                        break;

                    case XmlNodeType.EndElement:
                        // end of 'categories' element.
                        break;

                    default:
                        // ignore all other nodes.
                        this.XmlReader.Skip();
                        break;
                    }
                }while (this.XmlReader.NodeType != XmlNodeType.EndElement);
            } // if (!this.XmlReader.IsEmptyElement)

            // read over the end tag of the categories element or the start tag if the categories element is empty.
            this.XmlReader.Read();

            categoriesMetadata.Categories = new ReadOnlyEnumerable <AtomCategoryMetadata>(categoryList);
            collectionMetadata.Categories = categoriesMetadata;
        }
        /// <summary>
        /// Reads a resource collection element of a workspace of the service document.
        /// </summary>
        /// <returns>An <see cref="ODataResourceCollectionInfo"/> representing the resource collection in a workspace of a service document.</returns>
        /// <remarks>
        /// Pre-Condition:  XmlNodeType.Element - the collection element inside the workspace.
        /// Post-Condition: Any    - The next node after the collection element.
        /// </remarks>
        private ODataResourceCollectionInfo ReadCollectionElement()
        {
            Debug.Assert(this.XmlReader != null, "this.XmlReader != null");
            this.AssertXmlCondition(XmlNodeType.Element);
            Debug.Assert(this.XmlReader.LocalNameEquals(this.AtomPublishingCollectionElementName), "Expected element named 'collection'.");
            Debug.Assert(this.XmlReader.NamespaceEquals(this.AtomPublishingNamespace), "Element 'collection' should be in the atom publishing namespace.");

            ODataResourceCollectionInfo collectionInfo = new ODataResourceCollectionInfo();

            string href = this.XmlReader.GetAttribute(this.AtomHRefAttributeName, this.EmptyNamespace);

            ValidationUtils.ValidateResourceCollectionInfoUrl(href);

            collectionInfo.Url = ProcessUriFromPayload(href, this.XmlReader.XmlBaseUri);
            bool enableAtomMetadataReading = this.MessageReaderSettings.EnableAtomMetadataReading;

            AtomResourceCollectionMetadata collectionMetadata = null;

            if (enableAtomMetadataReading)
            {
                collectionMetadata = new AtomResourceCollectionMetadata();
            }

            if (!this.XmlReader.IsEmptyElement)
            {
                // read over the 'collection' element.
                this.XmlReader.ReadStartElement();

                do
                {
                    switch (this.XmlReader.NodeType)
                    {
                    case XmlNodeType.Element:
                        if (this.XmlReader.NamespaceEquals(this.AtomPublishingNamespace))
                        {
                            if (this.XmlReader.LocalNameEquals(this.AtomPublishingCategoriesElementName))
                            {
                                if (enableAtomMetadataReading)
                                {
                                    this.ServiceDocumentMetadataDeserializer.ReadCategoriesElementInCollection(collectionMetadata);
                                }
                                else
                                {
                                    this.XmlReader.Skip();
                                }
                            }
                            else if (this.XmlReader.LocalNameEquals(this.AtomPublishingAcceptElementName))
                            {
                                if (enableAtomMetadataReading)
                                {
                                    this.ServiceDocumentMetadataDeserializer.ReadAcceptElementInCollection(collectionMetadata);
                                }
                                else
                                {
                                    this.XmlReader.Skip();
                                }
                            }
                            else
                            {
                                // Throw error if we find anything other then a 'app:categories' or an 'app:accept' element in the ATOM publishing namespace.
                                throw new ODataException(Strings.ODataAtomServiceDocumentDeserializer_UnexpectedElementInResourceCollection(this.XmlReader.LocalName));
                            }
                        }
                        else if (this.XmlReader.NamespaceEquals(this.AtomNamespace))
                        {
                            if (enableAtomMetadataReading && this.XmlReader.LocalNameEquals(this.AtomTitleElementName))
                            {
                                this.ServiceDocumentMetadataDeserializer.ReadTitleElementInCollection(collectionMetadata);
                            }
                            else
                            {
                                // Skip all other elements in the atom namespace
                                this.XmlReader.Skip();
                            }
                        }
                        else
                        {
                            // For now, skip all other elements.
                            this.XmlReader.Skip();
                        }

                        break;

                    case XmlNodeType.EndElement:
                        // end of 'collection' element.
                        break;

                    default:
                        // ignore all other nodes.
                        this.XmlReader.Skip();
                        break;
                    }
                }while (this.XmlReader.NodeType != XmlNodeType.EndElement);
            } // if (!this.XmlReader.IsEmptyElement)

            this.AssertXmlCondition(true, XmlNodeType.EndElement);

            // read over the end tag of the collection element or the start tag if the collection element is empty.
            this.XmlReader.Read();

            if (enableAtomMetadataReading)
            {
                collectionInfo.SetAnnotation(collectionMetadata);
            }

            return(collectionInfo);
        }
        /// <summary>
        /// Extension method to get the <see cref="AtomResourceCollectionMetadata"/> for an annotatable (resource) collection.
        /// </summary>
        /// <param name="collection">The (resource) collection to get the annotation from.</param>
        /// <returns>An <see cref="AtomResourceCollectionMetadata" /> instance or null if no annotation of that type exists.</returns>
        public static AtomResourceCollectionMetadata Atom(this ODataResourceCollectionInfo collection)
        {
            ExceptionUtils.CheckArgumentNotNull(collection, "collection");

            AtomResourceCollectionMetadata collectionMetadata = collection.GetAnnotation<AtomResourceCollectionMetadata>();
            if (collectionMetadata == null)
            {
                collectionMetadata = new AtomResourceCollectionMetadata();
                collection.SetAnnotation(collectionMetadata);
            }

            return collectionMetadata;
        }