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;
        }
        /// <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;
        }
        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();
                }
            }
        }
Ejemplo n.º 5
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;
        }
        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;
        }