Exemplo n.º 1
0
        /// <summary>Determines an extension method to get the <see cref="T:System.Data.OData.Atom.AtomWorkspaceMetadata" /> for an annotatable serviceDocument.</summary>
        /// <returns>An <see cref="T:Microsoft.OData.Core.Atom.AtomWorkspaceMetadata" /> instance or null if no annotation of that type exists.</returns>
        /// <param name="serviceDocument">The serviceDocument to get the annotation from.</param>
        public static AtomWorkspaceMetadata Atom(this ODataServiceDocument serviceDocument)
        {
            ExceptionUtils.CheckArgumentNotNull(serviceDocument, "serviceDocument");

            AtomWorkspaceMetadata workspaceMetadata = serviceDocument.GetAnnotation <AtomWorkspaceMetadata>();

            if (workspaceMetadata == null)
            {
                workspaceMetadata = new AtomWorkspaceMetadata();
                serviceDocument.SetAnnotation(workspaceMetadata);
            }

            return(workspaceMetadata);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reads a workspace of a service document.
        /// </summary>
        /// <returns>An <see cref="ODataServiceDocument"/> representing the workspace of a service document.</returns>
        /// <remarks>
        /// Pre-Condition:  Any    - the next node after the service element.
        /// Post-Condition: Any    - The next node after the workspace element.
        /// </remarks>
        private ODataServiceDocument ReadWorkspace()
        {
            Debug.Assert(this.XmlReader != null, "this.XmlReader != null");

            bool enableAtomMetadataReading = this.AtomInputContext.MessageReaderSettings.EnableAtomMetadataReading;

            // skip anything which is not in the ATOM publishing namespace.
            this.SkipToElementInAtomPublishingNamespace();

            this.AssertXmlCondition(XmlNodeType.Element, XmlNodeType.EndElement);

            // if we already found an EndElement, it means that there is no serviceDocument.
            if (this.XmlReader.NodeType == XmlNodeType.EndElement)
            {
                return(null);
            }

            this.AssertXmlCondition(XmlNodeType.Element);
            Debug.Assert(this.XmlReader.NamespaceEquals(this.AtomPublishingNamespace), "The current element should have been in the Atom publishing namespace.");

            if (!this.XmlReader.LocalNameEquals(this.AtomPublishingWorkspaceElementName))
            {
                throw new ODataException(Strings.ODataAtomServiceDocumentDeserializer_UnexpectedElementInServiceDocument(this.XmlReader.LocalName));
            }

            List <ODataEntitySetInfo>      collections         = new List <ODataEntitySetInfo>();
            List <ODataFunctionImportInfo> functionImportInfos = new List <ODataFunctionImportInfo>();
            List <ODataSingletonInfo>      singletons          = new List <ODataSingletonInfo>();
            AtomWorkspaceMetadata          workspaceMetadata   = null;

            if (enableAtomMetadataReading)
            {
                workspaceMetadata = new AtomWorkspaceMetadata();
            }

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

                do
                {
                    this.XmlReader.SkipInsignificantNodes();

                    switch (this.XmlReader.NodeType)
                    {
                    case XmlNodeType.Element:
                        if (this.XmlReader.NamespaceEquals(this.AtomPublishingNamespace))
                        {
                            if (this.XmlReader.LocalNameEquals(this.AtomPublishingCollectionElementName))
                            {
                                ODataEntitySetInfo collection = this.ReadEntitySet();
                                Debug.Assert(collection != null, "collection != null");
                                collections.Add(collection);
                            }
                            else
                            {
                                // Throw error if we find anything other then a 'collection' element in the Atom publishing namespace.
                                throw new ODataException(Strings.ODataAtomServiceDocumentDeserializer_UnexpectedElementInWorkspace(this.XmlReader.LocalName));
                            }
                        }
                        else if (this.XmlReader.NamespaceEquals(this.ODataMetadataNamespace))
                        {
                            if (this.XmlReader.LocalNameEquals(this.ODataFunctionImportElementName))
                            {
                                ODataFunctionImportInfo functionImportInfo = this.ReadFunctionImportInfo();
                                Debug.Assert(functionImportInfo != null, "functionImportInfo != null");
                                functionImportInfos.Add(functionImportInfo);
                            }
                            else if (this.XmlReader.LocalNameEquals(this.ODataSingletonElementName))
                            {
                                ODataSingletonInfo singletonInfo = this.ReadSingletonInfo();
                                Debug.Assert(singletonInfo != null, "singletonInfo != null");
                                singletons.Add(singletonInfo);
                            }
                            else
                            {
                                // Throw error if we find anything other then a 'function-import' or 'singleton' element in the odata metadata namespace.
                                throw new ODataException(Strings.ODataAtomServiceDocumentDeserializer_UnexpectedODataElementInWorkspace(this.XmlReader.LocalName));
                            }
                        }
                        else if (enableAtomMetadataReading && this.XmlReader.NamespaceEquals(this.AtomNamespace))
                        {
                            if (this.XmlReader.LocalNameEquals(this.AtomTitleElementName))
                            {
                                this.ServiceDocumentMetadataDeserializer.ReadTitleElementInWorkspace(workspaceMetadata);
                            }
                            else
                            {
                                this.XmlReader.Skip();
                            }
                        }
                        else
                        {
                            // skip all other elements
                            this.XmlReader.Skip();
                        }

                        break;

                    case XmlNodeType.EndElement:
                        // end of 'serviceDocument' 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 serviceDocument element or the start tag if the serviceDocument element is empty.
            this.XmlReader.Read();

            ODataServiceDocument serviceDocument = new ODataServiceDocument
            {
                EntitySets      = new ReadOnlyEnumerable <ODataEntitySetInfo>(collections),
                FunctionImports = new ReadOnlyCollection <ODataFunctionImportInfo>(functionImportInfos),
                Singletons      = new ReadOnlyCollection <ODataSingletonInfo>(singletons)
            };

            if (enableAtomMetadataReading)
            {
                serviceDocument.SetAnnotation <AtomWorkspaceMetadata>(workspaceMetadata);
            }

            return(serviceDocument);
        }
        private static ODataServiceDocument CreateWorkspace(bool createMetadataFirst, string workspaceName = null, IEnumerable <CollectionInfo> incomingCollections = null, IEnumerable <SingletonInfo> incomingSingletons = null)
        {
            ODataServiceDocument serviceDocument = ObjectModelUtils.CreateDefaultWorkspace();

            if (createMetadataFirst)
            {
                serviceDocument.SetAnnotation(new AtomWorkspaceMetadata());
            }

            if (workspaceName != null)
            {
                AtomWorkspaceMetadata metadata = serviceDocument.Atom();
                metadata.Title = new AtomTextConstruct {
                    Text = workspaceName
                };
            }

            if (incomingCollections != null)
            {
                var collections = new List <ODataEntitySetInfo>();
                foreach (var collectionInfo in incomingCollections)
                {
                    var collection = new ODataEntitySetInfo()
                    {
                        Url = new Uri(collectionInfo.Url, UriKind.RelativeOrAbsolute), Name = collectionInfo.Name, Title = collectionInfo.TitleAnnotation
                    };
                    if (createMetadataFirst)
                    {
                        collection.SetAnnotation(new AtomResourceCollectionMetadata());
                    }

                    if (collectionInfo.TitleAnnotation != null)
                    {
                        AtomResourceCollectionMetadata metadata = collection.Atom();
                        metadata.Title = new AtomTextConstruct {
                            Text = collectionInfo.TitleAnnotation
                        };
                    }

                    collections.Add(collection);
                }

                serviceDocument.EntitySets = collections;
            }

            if (incomingSingletons != null)
            {
                var singletons = new List <ODataSingletonInfo>();
                foreach (var singletonInfo in incomingSingletons)
                {
                    var singleton = new ODataSingletonInfo()
                    {
                        Url = new Uri(singletonInfo.Url, UriKind.RelativeOrAbsolute), Name = singletonInfo.Name, Title = singletonInfo.TitleAnnotation
                    };
                    if (createMetadataFirst)
                    {
                        singleton.SetAnnotation(new AtomResourceCollectionMetadata());
                    }

                    singletons.Add(singleton);
                }

                serviceDocument.Singletons = singletons;
            }

            return(serviceDocument);
        }