public override void WriteStart(DataServiceODataWriterEntryArgs args) { CustomInstanceAnnotationsDescriptor current = new CustomInstanceAnnotationsDescriptor { TypeOfAnnotatedItem = typeof(ODataResource), Parent = writtenItemsStack.Count == 0 ? null : writtenItemsStack.Peek(), AnnotationsOnStart = new Collection <ODataInstanceAnnotation>(CustomInstanceAnnotationsGenerator.GetAnnotations("AnnotationOnEntry.AddedBeforeWriteStart.").ToList()), AnnotationsOnEnd = new Collection <ODataInstanceAnnotation>(CustomInstanceAnnotationsGenerator.GetAnnotations("AnnotationOnEntry.AddedAfterWriteStart.").Concat(CustomInstanceAnnotationsGenerator.GetAnnotationsWithTermInMetadata()).ToList()), }; AnnotatedItemsBaseline.Add(current); writtenItemsStack.Push(current); foreach (var annotation in current.AnnotationsOnStart) { args.Entry.InstanceAnnotations.Add(annotation); } base.WriteStart(args); foreach (var annotation in current.AnnotationsOnEnd) { args.Entry.InstanceAnnotations.Add(annotation); } }
public override void WriteStart(DataServiceODataWriterNestedResourceInfoArgs args) { var current = new CustomInstanceAnnotationsDescriptor { TypeOfAnnotatedItem = typeof(ODataNestedResourceInfo) }; writtenItemsStack.Push(current); base.WriteStart(args); }
public static List <CustomInstanceAnnotationsDescriptor> ReadFeedOrEntry(bool isFeed, Uri uri, string contentType, ODataMessageReaderSettings readerSettings, IEdmModel model) { var annotationsStack = new Stack <CustomInstanceAnnotationsDescriptor>(); var allAnnotations = new List <CustomInstanceAnnotationsDescriptor>(); var responseMessage = GetResponseMessge(uri, contentType); using (var messageReader = new ODataMessageReader(responseMessage, readerSettings, model)) { var reader = isFeed ? messageReader.CreateODataFeedReader() : messageReader.CreateODataEntryReader(); while (reader.Read()) { switch (reader.State) { case ODataReaderState.FeedStart: case ODataReaderState.EntryStart: { var instanceAnnotations = GetItemInstanceAnnotations(reader); var parent = annotationsStack.Count == 0 ? null : annotationsStack.Peek(); var current = new CustomInstanceAnnotationsDescriptor { TypeOfAnnotatedItem = reader.Item.GetType(), Parent = parent, AnnotationsOnStart = instanceAnnotations }; annotationsStack.Push(current); allAnnotations.Add(current); break; } case ODataReaderState.FeedEnd: case ODataReaderState.EntryEnd: { var instanceAnnotations = GetItemInstanceAnnotations(reader); var current = annotationsStack.Pop(); current.AnnotationsOnEnd = instanceAnnotations; break; } } } } return(allAnnotations); }
public override void WriteStart(DataServiceODataWriterFeedArgs args) { CustomInstanceAnnotationsDescriptor current; if (writtenItemsStack.Count == 0) { current = new CustomInstanceAnnotationsDescriptor { TypeOfAnnotatedItem = typeof(ODataFeed), Parent = null, AnnotationsOnStart = new Collection<ODataInstanceAnnotation>(CustomInstanceAnnotationsGenerator.GetAnnotations("AnnotationOnFeed.AddedBeforeWriteStart.").Concat(CustomInstanceAnnotationsGenerator.GetAnnotationsWithTermInMetadata()).ToList()), AnnotationsOnEnd = new Collection<ODataInstanceAnnotation>(CustomInstanceAnnotationsGenerator.GetAnnotations("AnnotationOnFeed.AddedAfterWriteStart.").ToList()), }; } else { current = new CustomInstanceAnnotationsDescriptor { TypeOfAnnotatedItem = typeof(ODataFeed), Parent = writtenItemsStack.Peek(), AnnotationsOnEnd = new Collection<ODataInstanceAnnotation>(), AnnotationsOnStart = new Collection<ODataInstanceAnnotation>() }; } AnnotatedItemsBaseline.Add(current); writtenItemsStack.Push(current); foreach (var annotation in current.AnnotationsOnStart) { args.Feed.InstanceAnnotations.Add(annotation); } base.WriteStart(args); foreach (var annotation in current.AnnotationsOnEnd) { args.Feed.InstanceAnnotations.Add(annotation); } }
public static List<CustomInstanceAnnotationsDescriptor> ReadFeedOrEntry(bool isFeed, Uri uri, string contentType, ODataMessageReaderSettings readerSettings, IEdmModel model) { var annotationsStack = new Stack<CustomInstanceAnnotationsDescriptor>(); var allAnnotations = new List<CustomInstanceAnnotationsDescriptor>(); var responseMessage = GetResponseMessge(uri, contentType); using (var messageReader = new ODataMessageReader(responseMessage, readerSettings, model)) { var reader = isFeed ? messageReader.CreateODataFeedReader() : messageReader.CreateODataEntryReader(); while (reader.Read()) { switch (reader.State) { case ODataReaderState.FeedStart: case ODataReaderState.EntryStart: { var instanceAnnotations = GetItemInstanceAnnotations(reader); var parent = annotationsStack.Count == 0 ? null : annotationsStack.Peek(); var current = new CustomInstanceAnnotationsDescriptor { TypeOfAnnotatedItem = reader.Item.GetType(), Parent = parent, AnnotationsOnStart = instanceAnnotations }; annotationsStack.Push(current); allAnnotations.Add(current); break; } case ODataReaderState.FeedEnd: case ODataReaderState.EntryEnd: { var instanceAnnotations = GetItemInstanceAnnotations(reader); var current = annotationsStack.Pop(); current.AnnotationsOnEnd = instanceAnnotations; break; } } } } return allAnnotations; }
private static bool IsTopLevelEntryOrEntryOfTopLevelFeed(this CustomInstanceAnnotationsDescriptor annotatedItem) { // Not an entry if (annotatedItem.TypeOfAnnotatedItem != typeof(ODataResource)) { return(false); } // A top level entry if (annotatedItem.Parent == null) { return(true); } // An entry of a top level feed if (annotatedItem.Parent.Parent == null) { Assert.IsTrue(annotatedItem.Parent.TypeOfAnnotatedItem == typeof(ODataResourceSet), "Found an entry whose parent is not a feed! Revisit assumptions of top level feed."); return(true); } // All other entries return(false); }
private static IEnumerable <ODataInstanceAnnotation> GetExpectedAnnotationsOnStart(this CustomInstanceAnnotationsDescriptor annotatedItem, string contentType, bool hasExpandedNavigationProperties) { if (annotatedItem.IsFeedWithStreaming(contentType)) { return(annotatedItem.AnnotationsOnStart); } if (annotatedItem.TypeOfAnnotatedItem == typeof(ODataResource) && hasExpandedNavigationProperties) { return(annotatedItem.AnnotationsOnStart); } return(annotatedItem.AnnotationsOnStart.Concat(annotatedItem.AnnotationsOnEnd)); }
private static bool IsFeedWithStreaming(this CustomInstanceAnnotationsDescriptor item, string contentType) { return(item.TypeOfAnnotatedItem == typeof(ODataResourceSet) && contentType.Contains(MimeTypes.StreamingParameterTrue)); }
public override void WriteStart(DataServiceODataWriterNavigationLinkArgs args) { var current = new CustomInstanceAnnotationsDescriptor { TypeOfAnnotatedItem = typeof(ODataNavigationLink) }; writtenItemsStack.Push(current); base.WriteStart(args); }
public static List <CustomInstanceAnnotationsDescriptor> ReadFeedOrEntry(bool isFeed, Uri uri, string contentType, ODataMessageReaderSettings readerSettings, IEdmModel model) { var annotationsStack = new Stack <CustomInstanceAnnotationsDescriptor>(); var allAnnotations = new List <CustomInstanceAnnotationsDescriptor>(); var responseMessage = GetResponseMessge(uri, contentType); using (var messageReader = new ODataMessageReader(responseMessage, readerSettings, model)) { var reader = isFeed ? messageReader.CreateODataResourceSetReader() : messageReader.CreateODataResourceReader(); Stack <bool> isNavigations = new Stack <bool>(); while (reader.Read()) { switch (reader.State) { case ODataReaderState.ResourceStart: { var resource = reader.Item as ODataResource; var parent = annotationsStack.Count == 0 ? null : annotationsStack.Peek(); if (parent != null) { parent.hasNestedResourceInfo = true; } if (resource != null && (resource.TypeName.EndsWith("Customer") || resource.TypeName.EndsWith("Login") || resource.TypeName.EndsWith("Message") || resource.TypeName.EndsWith("Order"))) { var instanceAnnotations = resource.InstanceAnnotations.Clone(); var current = new CustomInstanceAnnotationsDescriptor { TypeOfAnnotatedItem = reader.Item.GetType(), Parent = parent, AnnotationsOnStart = instanceAnnotations }; annotationsStack.Push(current); allAnnotations.Add(current); } break; } case ODataReaderState.ResourceSetStart: { var feed = reader.Item as ODataResourceSet; var instanceAnnotations = feed.InstanceAnnotations.Clone(); var parent = annotationsStack.Count == 0 ? null : annotationsStack.Peek(); if (parent != null) { parent.hasNestedResourceInfo = true; } var current = new CustomInstanceAnnotationsDescriptor { TypeOfAnnotatedItem = reader.Item.GetType(), Parent = parent, AnnotationsOnStart = instanceAnnotations }; annotationsStack.Push(current); if (isNavigations.Count == 0 || isNavigations.Peek()) { allAnnotations.Add(current); } break; } case ODataReaderState.NestedResourceInfoStart: { var nestedResource = reader.Item as ODataNestedResourceInfo; if (nestedResource.Name.Equals("Customer") || nestedResource.Name.Equals("Login") || nestedResource.Name.Equals("Orders") || nestedResource.Name.Equals("SentMessages") || nestedResource.Name.Equals("ReceivedMessages")) { isNavigations.Push(true); } else { isNavigations.Push(false); } break; } case ODataReaderState.ResourceEnd: { var resource = reader.Item as ODataResource; if (resource != null && (resource.TypeName.EndsWith("Customer") || resource.TypeName.EndsWith("Login") || resource.TypeName.EndsWith("Message") || resource.TypeName.EndsWith("Order"))) { var instanceAnnotations = resource.InstanceAnnotations.Clone(); var current = annotationsStack.Pop(); current.AnnotationsOnEnd = instanceAnnotations; } break; } case ODataReaderState.ResourceSetEnd: { var feed = reader.Item as ODataResourceSet; var instanceAnnotations = feed.InstanceAnnotations.Clone(); var current = annotationsStack.Pop(); if (isNavigations.Count == 0 || isNavigations.Peek()) { current.AnnotationsOnEnd = instanceAnnotations; } break; } case ODataReaderState.NestedResourceInfoEnd: { isNavigations.Pop(); break; } } } } return(allAnnotations); }