public static MaterializerNavigationLink CreateLink(ODataNavigationLink link, MaterializerEntry entry) { MaterializerNavigationLink annotation = new MaterializerNavigationLink(link, entry); link.SetAnnotation <MaterializerNavigationLink>(annotation); return(annotation); }
public static MaterializerNavigationLink CreateLink(ODataNavigationLink link, ODataFeed feed) { MaterializerNavigationLink annotation = new MaterializerNavigationLink(link, feed); link.SetAnnotation <MaterializerNavigationLink>(annotation); return(annotation); }
/// <summary> /// Creates the materializer link with a feed. /// </summary> /// <param name="link">The link.</param> /// <param name="feed">The feed.</param> /// <returns>The materializer link.</returns> public static MaterializerNavigationLink CreateLink(ODataNavigationLink link, ODataFeed feed) { Debug.Assert(link.GetAnnotation <MaterializerNavigationLink>() == null, "there should be no MaterializerNavigationLink annotation on the feed link yet"); MaterializerNavigationLink materializedNavigationLink = new MaterializerNavigationLink(link, feed); link.SetAnnotation <MaterializerNavigationLink>(materializedNavigationLink); return(materializedNavigationLink); }
private static void AddLinkMetadata(DeferredLink payloadElement, ODataNavigationLink link) { AtomLinkMetadata metadata = CreateLinkMetadata(payloadElement.Annotations.OfType <XmlTreeAnnotation>()); if (metadata != null) { link.SetAnnotation <AtomLinkMetadata>(metadata); } }
public static AtomLinkMetadata Atom(this ODataNavigationLink navigationLink) { ExceptionUtils.CheckArgumentNotNull <ODataNavigationLink>(navigationLink, "navigationLink"); AtomLinkMetadata annotation = navigationLink.GetAnnotation <AtomLinkMetadata>(); if (annotation == null) { annotation = new AtomLinkMetadata(); navigationLink.SetAnnotation <AtomLinkMetadata>(annotation); } return(annotation); }
public static AtomLinkMetadata Atom(this ODataNavigationLink navigationLink) { ExceptionUtils.CheckArgumentNotNull(navigationLink, "navigationLink"); AtomLinkMetadata linkMetadata = navigationLink.GetAnnotation <AtomLinkMetadata>(); if (linkMetadata == null) { linkMetadata = new AtomLinkMetadata(); navigationLink.SetAnnotation(linkMetadata); } return(linkMetadata); }
/// <summary> /// Reads a navigation link in entry element. /// </summary> /// <param name="entryState">The reader entry state for the entry being read.</param> /// <param name="linkRelation">The value of the rel attribute of the link to read, unescaped parsed URI.</param> /// <param name="linkHRef">The value of the href attribute of the link to read.</param> /// <returns>A descriptor of a navigation link if a navigation link was found; null otherwise.</returns> /// <remarks> /// Pre-Condition: XmlNodeType.Element atom:link - the start tag of the atom:link element to read. /// Post-Condition: XmlNodeType.Element atom:link - the start tag of the atom:link element - the reader doesn't move /// </remarks> private ODataAtomReaderNavigationLinkDescriptor TryReadNavigationLinkInEntry( IODataAtomReaderEntryState entryState, string linkRelation, string linkHRef) { Debug.Assert(linkRelation != null, "linkRelation != null"); this.XmlReader.AssertNotBuffering(); this.AssertXmlCondition(XmlNodeType.Element); Debug.Assert( this.XmlReader.NamespaceURI == AtomConstants.AtomNamespace && this.XmlReader.LocalName == AtomConstants.AtomLinkElementName, "The XML reader must be on the atom:link element for this method to work."); // We will ignore navigation links with empty property names string navigationLinkName = AtomUtils.GetNameFromAtomLinkRelationAttribute(linkRelation, AtomConstants.ODataNavigationPropertiesRelatedLinkRelationPrefix); if (string.IsNullOrEmpty(navigationLinkName)) { return null; } // Lookup the property in metadata // Note that we already verified that the navigation link name is not empty. IEdmNavigationProperty navigationProperty = ReaderValidationUtils.ValidateNavigationPropertyDefined(navigationLinkName, entryState.EntityType, this.MessageReaderSettings); // Navigation link ODataNavigationLink navigationLink = new ODataNavigationLink { Name = navigationLinkName }; // Get the type of the link string navigationLinkType = this.XmlReader.GetAttribute(this.AtomTypeAttributeName, this.EmptyNamespace); // [Astoria-ODataLib-Integration] Handling of type attribute value on atom:link element. // The behavior of ODataLib is: // Parse the type as content type // Compare media type names ignoring case (as per spec), compare parameter type names ignoring case // If it's application/atom+xml without type parameter or invalid type parameter - use it as a navigation link without specifying collection/singleton (pending metadata validation) // If it's application/atom+xml with type='feed' - use it as a navigation link assuming it's a collection (pending metadata validation) // If it's application/atom+xml with type='entry' - use it as a navigation link assuming it's a singleton (pending metadata validation) // In any other case - skip this link and treat it as if it's not a navigation link. // Note that parsing the type means we may fail if it's not a valid content type. // Missing and invalid type attributes are allowed. We will infer the cardinality either from the model or when expanding the link. if (!string.IsNullOrEmpty(navigationLinkType)) { // Fast path for most common link types bool hasEntryType, hasFeedType; bool isExactMatch = AtomUtils.IsExactNavigationLinkTypeMatch(navigationLinkType, out hasEntryType, out hasFeedType); if (!isExactMatch) { // If the fast path did not work, we have to fully parse the media type. string mediaTypeName, mediaTypeCharset; IList<KeyValuePair<string, string>> contentTypeParameters = HttpUtils.ReadMimeType(navigationLinkType, out mediaTypeName, out mediaTypeCharset); if (!HttpUtils.CompareMediaTypeNames(mediaTypeName, MimeConstants.MimeApplicationAtomXml)) { return null; } string typeParameterValue = null; if (contentTypeParameters != null) { for (int contentTypeParameterIndex = 0; contentTypeParameterIndex < contentTypeParameters.Count; contentTypeParameterIndex++) { KeyValuePair<string, string> contentTypeParameter = contentTypeParameters[contentTypeParameterIndex]; if (HttpUtils.CompareMediaTypeParameterNames(MimeConstants.MimeTypeParameterName, contentTypeParameter.Key)) { typeParameterValue = contentTypeParameter.Value; break; } } } if (typeParameterValue != null) { if (string.Compare(typeParameterValue, MimeConstants.MimeTypeParameterValueEntry, StringComparison.OrdinalIgnoreCase) == 0) { hasEntryType = true; } else if (string.Compare(typeParameterValue, MimeConstants.MimeTypeParameterValueFeed, StringComparison.OrdinalIgnoreCase) == 0) { hasFeedType = true; } } } if (hasEntryType) { navigationLink.IsCollection = false; } else if (hasFeedType) { navigationLink.IsCollection = true; } } // We allow missing HREF on a link and simply report null. if (linkHRef != null) { navigationLink.Url = this.ProcessUriFromPayload(linkHRef, this.XmlReader.XmlBaseUri); } this.XmlReader.MoveToElement(); // Read and store ATOM link metadata (captures extra info like lang, title) if ATOM metadata reading is turned on. AtomLinkMetadata atomLinkMetadata = this.EntryMetadataDeserializer.ReadAtomLinkElementInEntryContent(linkRelation, linkHRef); if (atomLinkMetadata != null) { navigationLink.SetAnnotation(atomLinkMetadata); } return new ODataAtomReaderNavigationLinkDescriptor(navigationLink, navigationProperty); }
internal static ODataItem ReadEntryOrFeed(ODataReader odataReader, ODataDeserializerContext readContext) { ODataItem topLevelItem = null; Stack <ODataItem> itemsStack = new Stack <ODataItem>(); while (odataReader.Read()) { switch (odataReader.State) { case ODataReaderState.EntryStart: ODataEntry entry = (ODataEntry)odataReader.Item; ODataEntryAnnotation entryAnnotation = null; if (entry != null) { entryAnnotation = new ODataEntryAnnotation(); entry.SetAnnotation(entryAnnotation); } if (itemsStack.Count == 0) { Contract.Assert(entry != null, "The top-level entry can never be null."); topLevelItem = entry; } else { ODataItem parentItem = itemsStack.Peek(); ODataFeed parentFeed = parentItem as ODataFeed; if (parentFeed != null) { ODataFeedAnnotation parentFeedAnnotation = parentFeed.GetAnnotation <ODataFeedAnnotation>(); Contract.Assert(parentFeedAnnotation != null, "Every feed we added to the stack should have the feed annotation on it."); parentFeedAnnotation.Add(entry); } else { ODataNavigationLink parentNavigationLink = (ODataNavigationLink)parentItem; ODataNavigationLinkAnnotation parentNavigationLinkAnnotation = parentNavigationLink.GetAnnotation <ODataNavigationLinkAnnotation>(); Contract.Assert(parentNavigationLinkAnnotation != null, "Every navigation link we added to the stack should have the navigation link annotation on it."); Contract.Assert(parentNavigationLink.IsCollection == false, "Only singleton navigation properties can contain entry as their child."); Contract.Assert(parentNavigationLinkAnnotation.Count == 0, "Each navigation property can contain only one entry as its direct child."); parentNavigationLinkAnnotation.Add(entry); } } itemsStack.Push(entry); RecurseEnter(readContext); break; case ODataReaderState.EntryEnd: Contract.Assert(itemsStack.Count > 0 && itemsStack.Peek() == odataReader.Item, "The entry which is ending should be on the top of the items stack."); itemsStack.Pop(); RecurseLeave(readContext); break; case ODataReaderState.NavigationLinkStart: ODataNavigationLink navigationLink = (ODataNavigationLink)odataReader.Item; Contract.Assert(navigationLink != null, "Navigation link should never be null."); navigationLink.SetAnnotation(new ODataNavigationLinkAnnotation()); Contract.Assert(itemsStack.Count > 0, "Navigation link can't appear as top-level item."); { ODataEntry parentEntry = (ODataEntry)itemsStack.Peek(); ODataEntryAnnotation parentEntryAnnotation = parentEntry.GetAnnotation <ODataEntryAnnotation>(); Contract.Assert(parentEntryAnnotation != null, "Every entry we added to the stack should have the entry annotation on it."); parentEntryAnnotation.Add(navigationLink); } itemsStack.Push(navigationLink); RecurseEnter(readContext); break; case ODataReaderState.NavigationLinkEnd: Contract.Assert(itemsStack.Count > 0 && itemsStack.Peek() == odataReader.Item, "The navigation link which is ending should be on the top of the items stack."); itemsStack.Pop(); RecurseLeave(readContext); break; case ODataReaderState.FeedStart: ODataFeed feed = (ODataFeed)odataReader.Item; Contract.Assert(feed != null, "Feed should never be null."); feed.SetAnnotation(new ODataFeedAnnotation()); if (itemsStack.Count > 0) { ODataNavigationLink parentNavigationLink = (ODataNavigationLink)itemsStack.Peek(); Contract.Assert(parentNavigationLink != null, "this has to be an inner feed. inner feeds always have a navigation link."); ODataNavigationLinkAnnotation parentNavigationLinkAnnotation = parentNavigationLink.GetAnnotation <ODataNavigationLinkAnnotation>(); Contract.Assert(parentNavigationLinkAnnotation != null, "Every navigation link we added to the stack should have the navigation link annotation on it."); Contract.Assert(parentNavigationLink.IsCollection == true, "Only collection navigation properties can contain feed as their child."); parentNavigationLinkAnnotation.Add(feed); } else { topLevelItem = feed; } itemsStack.Push(feed); RecurseEnter(readContext); break; case ODataReaderState.FeedEnd: Contract.Assert(itemsStack.Count > 0 && itemsStack.Peek() == odataReader.Item, "The feed which is ending should be on the top of the items stack."); itemsStack.Pop(); RecurseLeave(readContext); break; case ODataReaderState.EntityReferenceLink: ODataEntityReferenceLink entityReferenceLink = (ODataEntityReferenceLink)odataReader.Item; Contract.Assert(entityReferenceLink != null, "Entity reference link should never be null."); Contract.Assert(itemsStack.Count > 0, "Entity reference link should never be reported as top-level item."); { ODataNavigationLink parentNavigationLink = (ODataNavigationLink)itemsStack.Peek(); ODataNavigationLinkAnnotation parentNavigationLinkAnnotation = parentNavigationLink.GetAnnotation <ODataNavigationLinkAnnotation>(); Contract.Assert(parentNavigationLinkAnnotation != null, "Every navigation link we added to the stack should have the navigation link annotation on it."); parentNavigationLinkAnnotation.Add(entityReferenceLink); } break; default: Contract.Assert(false, "We should never get here, it means the ODataReader reported a wrong state."); break; } } Contract.Assert(odataReader.State == ODataReaderState.Completed, "We should have consumed all of the input by now."); Contract.Assert(topLevelItem != null, "A top level entry or feed should have been read by now."); return(topLevelItem); }
private void Read(Lazy <ODataReader> lazyReader) { foreach (var state in this.expectedStates) { lazyReader.Value.Read(); ExceptionUtilities.Assert(lazyReader.Value.State == state, "Expected %1, Found %2", state, lazyReader.Value.State); switch (state) { case ODataReaderState.FeedStart: if (readItems.Count > 0) { ODataNavigationLink navLink = (ODataNavigationLink)readItems.Peek(); var annotation = navLink.GetAnnotation <ODataNavigationLinkExpandedItemObjectModelAnnotation>(); if (annotation == null) { annotation = new ODataNavigationLinkExpandedItemObjectModelAnnotation(); navLink.SetAnnotation(annotation); } annotation.ExpandedItem = lazyReader.Value.Item; } readItems.Push(lazyReader.Value.Item); break; case ODataReaderState.EntryStart: var currentEntry = (ODataEntry)lazyReader.Value.Item; if (readItems.Count > 0) { ODataFeed feed = readItems.Peek() as ODataFeed; if (feed != null) { var annotation = feed.GetAnnotation <ODataFeedEntriesObjectModelAnnotation>(); if (annotation == null) { annotation = new ODataFeedEntriesObjectModelAnnotation(); feed.SetAnnotation(annotation); } annotation.Add((ODataEntry)lazyReader.Value.Item); } else { ODataNavigationLink navLink = (ODataNavigationLink)readItems.Peek(); var annotation = navLink.GetAnnotation <ODataNavigationLinkExpandedItemObjectModelAnnotation>(); if (annotation == null) { annotation = new ODataNavigationLinkExpandedItemObjectModelAnnotation(); navLink.SetAnnotation(annotation); } annotation.ExpandedItem = currentEntry; } } readItems.Push(currentEntry); break; case ODataReaderState.NavigationLinkStart: ODataEntry entry = (ODataEntry)readItems.Peek(); var navLinksAnnotation = entry.GetAnnotation <ODataEntryNavigationLinksObjectModelAnnotation>(); if (navLinksAnnotation == null) { navLinksAnnotation = new ODataEntryNavigationLinksObjectModelAnnotation(); entry.SetAnnotation(navLinksAnnotation); } navLinksAnnotation.Add((ODataNavigationLink)lazyReader.Value.Item, entry.Properties.Count() + navLinksAnnotation.Count); readItems.Push(lazyReader.Value.Item); break; case ODataReaderState.EntryEnd: case ODataReaderState.FeedEnd: case ODataReaderState.NavigationLinkEnd: if (readItems.Count() > 1) { readItems.Pop(); } break; } } this.expectedStates.Clear(); }
/// <summary> /// Reads an entry from the <paramref name="odataReader"/> and all it's children including expanded entries and feeds. /// </summary> /// <param name="odataReader">The ODataReader to read from.</param> /// <param name="topLevelSegmentInfo">The segment info for the top-level entry to read.</param> /// <returns>The <see cref="ODataEntry"/> with annotations which store the navigation links and their expanded values.</returns> private ODataEntry ReadEntry(ODataReader odataReader, SegmentInfo topLevelSegmentInfo) { Debug.Assert(odataReader != null, "odataReader != null"); Debug.Assert(odataReader.State == ODataReaderState.Start, "The ODataReader must not have been used yet."); Debug.Assert(topLevelSegmentInfo != null, "topLevelSegmentInfo != null"); ODataEntry topLevelEntry = null; Stack <ODataItem> itemsStack = new Stack <ODataItem>(); while (odataReader.Read()) { // Don't let the item stack grow larger than we can process later. Also lets us to fail and report the recursion depth limit error before ODL does. if (itemsStack.Count >= RecursionLimit) { throw DataServiceException.CreateDeepRecursion(RecursionLimit); } switch (odataReader.State) { case ODataReaderState.EntryStart: ODataEntry entry = (ODataEntry)odataReader.Item; ODataEntryAnnotation entryAnnotation = null; if (entry != null) { entryAnnotation = new ODataEntryAnnotation(); entry.SetAnnotation(entryAnnotation); } if (itemsStack.Count == 0) { Debug.Assert(entry != null, "The top-level entry can never be null."); topLevelEntry = entry; // For top-level entry, create the entry resource here. // This is needed since the creation of the resource may fail (especially if this is an update in which case // we evaluate the URL query, which may return no results and thus we would fail with 404) // and we need to report that failure rather then potential other failures caused by the properties in the entry in question. this.CreateEntityResource(topLevelSegmentInfo, entry, entryAnnotation, /*topLevel*/ true); } else { ODataItem parentItem = itemsStack.Peek(); ODataFeed parentFeed = parentItem as ODataFeed; if (parentFeed != null) { ODataFeedAnnotation parentFeedAnnotation = parentFeed.GetAnnotation <ODataFeedAnnotation>(); Debug.Assert(parentFeedAnnotation != null, "Every feed we added to the stack should have the feed annotation on it."); parentFeedAnnotation.Add(entry); } else { ODataNavigationLink parentNavigationLink = (ODataNavigationLink)parentItem; ODataNavigationLinkAnnotation parentNavigationLinkAnnotation = parentNavigationLink.GetAnnotation <ODataNavigationLinkAnnotation>(); Debug.Assert(parentNavigationLinkAnnotation != null, "Every navigation link we added to the stack should have the navigation link annotation on it."); Debug.Assert(parentNavigationLink.IsCollection == false, "Only singleton navigation properties can contain entry as their child."); Debug.Assert(parentNavigationLinkAnnotation.Count == 0, "Each navigation property can contain only one entry as its direct child."); parentNavigationLinkAnnotation.Add(entry); } } itemsStack.Push(entry); break; case ODataReaderState.EntryEnd: Debug.Assert(itemsStack.Count > 0 && itemsStack.Peek() == odataReader.Item, "The entry which is ending should be on the top of the items stack."); itemsStack.Pop(); break; case ODataReaderState.NavigationLinkStart: ODataNavigationLink navigationLink = (ODataNavigationLink)odataReader.Item; Debug.Assert(navigationLink != null, "Navigation link should never be null."); navigationLink.SetAnnotation(new ODataNavigationLinkAnnotation()); Debug.Assert(itemsStack.Count > 0, "Navigation link can't appear as top-level item."); { ODataEntry parentEntry = (ODataEntry)itemsStack.Peek(); ODataEntryAnnotation parentEntryAnnotation = parentEntry.GetAnnotation <ODataEntryAnnotation>(); Debug.Assert(parentEntryAnnotation != null, "Every entry we added to the stack should have the navigation link annotation on it."); parentEntryAnnotation.Add(navigationLink); } itemsStack.Push(navigationLink); break; case ODataReaderState.NavigationLinkEnd: Debug.Assert(itemsStack.Count > 0 && itemsStack.Peek() == odataReader.Item, "The navigation link which is ending should be on the top of the items stack."); itemsStack.Pop(); break; case ODataReaderState.FeedStart: ODataFeed feed = (ODataFeed)odataReader.Item; Debug.Assert(feed != null, "Feed should never be null."); feed.SetAnnotation(new ODataFeedAnnotation()); Debug.Assert(itemsStack.Count > 0, "Since we always start reading entry, we should never get a feed as the top-level item."); { ODataNavigationLink parentNavigationLink = (ODataNavigationLink)itemsStack.Peek(); ODataNavigationLinkAnnotation parentNavigationLinkAnnotation = parentNavigationLink.GetAnnotation <ODataNavigationLinkAnnotation>(); Debug.Assert(parentNavigationLinkAnnotation != null, "Every navigation link we added to the stack should have the navigation link annotation on it."); Debug.Assert(parentNavigationLink.IsCollection == true, "Only collection navigation properties can contain feed as their child."); parentNavigationLinkAnnotation.Add(feed); } itemsStack.Push(feed); break; case ODataReaderState.FeedEnd: Debug.Assert(itemsStack.Count > 0 && itemsStack.Peek() == odataReader.Item, "The feed which is ending should be on the top of the items stack."); itemsStack.Pop(); break; case ODataReaderState.EntityReferenceLink: ODataEntityReferenceLink entityReferenceLink = (ODataEntityReferenceLink)odataReader.Item; Debug.Assert(entityReferenceLink != null, "Entity reference link should never be null."); Debug.Assert(itemsStack.Count > 0, "Entity reference link should never be reported as top-level item."); { ODataNavigationLink parentNavigationLink = (ODataNavigationLink)itemsStack.Peek(); ODataNavigationLinkAnnotation parentNavigationLinkAnnotation = parentNavigationLink.GetAnnotation <ODataNavigationLinkAnnotation>(); Debug.Assert(parentNavigationLinkAnnotation != null, "Every navigation link we added to the stack should have the navigation link annotation on it."); parentNavigationLinkAnnotation.Add(entityReferenceLink); } break; default: Debug.Assert(false, "We should never get here, it means the ODataReader reported a wrong state."); break; } } Debug.Assert(odataReader.State == ODataReaderState.Completed, "We should have consumed all of the input by now."); Debug.Assert(topLevelEntry != null, "A top level entry should have been read by now."); return(topLevelEntry); }
private ODataEntry ReadEntry(ODataReader odataReader, System.Data.Services.SegmentInfo topLevelSegmentInfo) { ODataEntry entry = null; Stack <ODataItem> stack = new Stack <ODataItem>(); while (odataReader.Read()) { if (stack.Count >= 100) { throw DataServiceException.CreateDeepRecursion(100); } switch (odataReader.State) { case ODataReaderState.FeedStart: { ODataFeed feed2 = (ODataFeed)odataReader.Item; feed2.SetAnnotation <ODataFeedAnnotation>(new ODataFeedAnnotation()); ODataNavigationLink link3 = (ODataNavigationLink)stack.Peek(); link3.GetAnnotation <ODataNavigationLinkAnnotation>().Add(feed2); stack.Push(feed2); break; } case ODataReaderState.FeedEnd: stack.Pop(); break; case ODataReaderState.EntryStart: { ODataEntry entry2 = (ODataEntry)odataReader.Item; ODataEntryAnnotation annotation = null; if (entry2 != null) { annotation = new ODataEntryAnnotation(); entry2.SetAnnotation <ODataEntryAnnotation>(annotation); } if (stack.Count == 0) { entry = entry2; this.CreateEntityResource(topLevelSegmentInfo, entry2, annotation, true); } else { ODataItem item = stack.Peek(); ODataFeed feed = item as ODataFeed; if (feed != null) { feed.GetAnnotation <ODataFeedAnnotation>().Add(entry2); } else { ODataNavigationLink link = (ODataNavigationLink)item; link.GetAnnotation <ODataNavigationLinkAnnotation>().Add(entry2); } } stack.Push(entry2); break; } case ODataReaderState.EntryEnd: stack.Pop(); break; case ODataReaderState.NavigationLinkStart: { ODataNavigationLink link2 = (ODataNavigationLink)odataReader.Item; link2.SetAnnotation <ODataNavigationLinkAnnotation>(new ODataNavigationLinkAnnotation()); ODataEntry entry3 = (ODataEntry)stack.Peek(); entry3.GetAnnotation <ODataEntryAnnotation>().Add(link2); stack.Push(link2); break; } case ODataReaderState.NavigationLinkEnd: stack.Pop(); break; case ODataReaderState.EntityReferenceLink: { ODataEntityReferenceLink link4 = (ODataEntityReferenceLink)odataReader.Item; ODataNavigationLink link5 = (ODataNavigationLink)stack.Peek(); link5.GetAnnotation <ODataNavigationLinkAnnotation>().Add(link4); break; } } } return(entry); }