internal object Run(AtomMaterializer materializer, AtomEntry entry, Type expectedType) { Debug.Assert(materializer != null, "materializer != null"); Debug.Assert(entry != null, "entry != null"); return this.Plan(materializer, entry, expectedType); }
internal void AddedLink(AtomEntry source, string propertyName, object target) { Debug.Assert(source != null, "source != null"); Debug.Assert(propertyName != null, "propertyName != null"); if (!this.Tracking) { return; } if (ShouldTrackWithContext(source) && ShouldTrackWithContext(target)) { LinkDescriptor item = new LinkDescriptor(source.ResolvedObject, propertyName, target, EntityStates.Added); this.links.Add(item); } }
private void ParseCurrentEntry(out AtomEntry targetEntry) { Debug.Assert(this.reader.NodeType == XmlNodeType.Element, "this.reader.NodeType == XmlNodeType.Element"); var callbackResult = this.entryCallback(this.reader); Debug.Assert(callbackResult.Key != null, "callbackResult.Key != null"); this.readers.Push(this.reader); this.reader = callbackResult.Key; this.reader.Read(); Debug.Assert(this.reader.LocalName == "entry", "this.reader.LocalName == 'entry' - otherwise we're not reading the subtree"); bool hasContent = false; targetEntry = new AtomEntry(); targetEntry.DataValues = new List<AtomContentProperty>(); targetEntry.Tag = callbackResult.Value; targetEntry.ETagText = this.reader.GetAttribute(XmlConstants.AtomETagAttributeName, XmlConstants.DataWebMetadataNamespace); while (this.reader.Read()) { if (ShouldIgnoreNode(this.reader)) { continue; } if (this.reader.NodeType == XmlNodeType.Element) { int depth = this.reader.Depth; string elementName = this.reader.LocalName; string namespaceURI = this.reader.NamespaceURI; if (namespaceURI == XmlConstants.AtomNamespace) { if (elementName == XmlConstants.AtomCategoryElementName && targetEntry.TypeName == null) { string text = this.reader.GetAttributeEx(XmlConstants.AtomCategorySchemeAttributeName, XmlConstants.AtomNamespace); if (text == this.typeScheme) { targetEntry.TypeName = this.reader.GetAttributeEx(XmlConstants.AtomCategoryTermAttributeName, XmlConstants.AtomNamespace); } } else if (elementName == XmlConstants.AtomContentElementName) { hasContent = true; this.ParseCurrentContent(targetEntry); } else if (elementName == XmlConstants.AtomIdElementName && targetEntry.Identity == null) { string idText = ReadElementStringForText(this.reader); idText = Util.ReferenceIdentity(idText); Uri idUri = Util.CreateUri(idText, UriKind.RelativeOrAbsolute); if (!idUri.IsAbsoluteUri) { throw Error.InvalidOperation(Strings.Context_TrackingExpectsAbsoluteUri); } targetEntry.Identity = idText; } else if (elementName == XmlConstants.AtomLinkElementName) { this.ParseCurrentLink(targetEntry); } } else if (namespaceURI == XmlConstants.DataWebMetadataNamespace) { if (elementName == XmlConstants.AtomPropertiesElementName) { if (targetEntry.MediaLinkEntry.HasValue && !targetEntry.MediaLinkEntry.Value) { throw Error.InvalidOperation(Strings.Deserialize_ContentPlusPropertiesNotAllowed); } targetEntry.MediaLinkEntry = true; if (!this.reader.IsEmptyElement) { this.ReadCurrentProperties(targetEntry.DataValues); } } } SkipToEndAtDepth(this.reader, depth); } } if (targetEntry.Identity == null) { throw Error.InvalidOperation(Strings.Deserialize_MissingIdElement); } if (!hasContent) { throw Error.BatchStreamContentExpected(BatchStreamState.GetResponse); } this.reader = this.readers.Pop(); }
private void ParseCurrentLink(AtomEntry targetEntry) { Debug.Assert(targetEntry != null, "targetEntry != null"); Debug.Assert( this.reader.NodeType == XmlNodeType.Element, "this.reader.NodeType == XmlNodeType.Element -- otherwise we shouldn't try to parse a link"); Debug.Assert( this.reader.LocalName == "link", "this.reader.LocalName == 'link' -- otherwise we shouldn't try to parse a link"); string relation = this.reader.GetAttribute(XmlConstants.AtomLinkRelationAttributeName); if (relation == null) { return; } if (relation == XmlConstants.AtomEditRelationAttributeValue && targetEntry.EditLink == null) { string href = this.reader.GetAttribute(XmlConstants.AtomHRefAttributeName); if (string.IsNullOrEmpty(href)) { throw Error.InvalidOperation(Strings.Context_MissingEditLinkInResponseBody); } targetEntry.EditLink = this.ConvertHRefAttributeValueIntoURI(href); } else if (relation == XmlConstants.AtomSelfRelationAttributeValue && targetEntry.QueryLink == null) { string href = this.reader.GetAttribute(XmlConstants.AtomHRefAttributeName); if (string.IsNullOrEmpty(href)) { throw Error.InvalidOperation(Strings.Context_MissingSelfLinkInResponseBody); } targetEntry.QueryLink = this.ConvertHRefAttributeValueIntoURI(href); } else if (relation == XmlConstants.AtomEditMediaRelationAttributeValue && targetEntry.MediaEditUri == null) { string href = this.reader.GetAttribute(XmlConstants.AtomHRefAttributeName); if (string.IsNullOrEmpty(href)) { throw Error.InvalidOperation(Strings.Context_MissingEditMediaLinkInResponseBody); } targetEntry.MediaEditUri = this.ConvertHRefAttributeValueIntoURI(href); targetEntry.StreamETagText = this.reader.GetAttribute(XmlConstants.AtomETagAttributeName, XmlConstants.DataWebMetadataNamespace); } if (!this.reader.IsEmptyElement) { string propertyName = UriUtil.GetNameFromAtomLinkRelationAttribute(relation); if (propertyName == null) { return; } string propertyValueText = this.reader.GetAttribute(XmlConstants.AtomTypeAttributeName); bool isFeed; if (!IsAllowedLinkType(propertyValueText, out isFeed)) { return; } if (!ReadChildElement(this.reader, XmlConstants.AtomInlineElementName, XmlConstants.DataWebMetadataNamespace)) { return; } bool emptyInlineCollection = this.reader.IsEmptyElement; object propertyValue = null; if (!emptyInlineCollection) { AtomFeed nestedFeed = null; AtomEntry nestedEntry = null; List<AtomEntry> feedEntries = null; Debug.Assert(this.reader is Xml.XmlWrappingReader, "reader must be a instance of XmlWrappingReader"); string readerBaseUri = this.reader.BaseURI; XmlReader nestedReader = Xml.XmlWrappingReader.CreateReader(readerBaseUri, this.reader.ReadSubtree()); nestedReader.Read(); Debug.Assert(nestedReader.LocalName == "inline", "nestedReader.LocalName == 'inline'"); AtomParser nested = new AtomParser(nestedReader, this.entryCallback, this.typeScheme, this.currentDataNamespace); while (nested.Read()) { switch (nested.DataKind) { case AtomDataKind.Feed: feedEntries = new List<AtomEntry>(); nestedFeed = nested.CurrentFeed; propertyValue = nestedFeed; break; case AtomDataKind.Entry: nestedEntry = nested.CurrentEntry; if (feedEntries != null) { feedEntries.Add(nestedEntry); } else { propertyValue = nestedEntry; } break; case AtomDataKind.PagingLinks: break; default: throw new InvalidOperationException(Strings.AtomParser_UnexpectedContentUnderExpandedLink); } } if (nestedFeed != null) { Debug.Assert( nestedFeed.Entries == null, "nestedFeed.Entries == null -- otherwise someone initialized this for us"); nestedFeed.Entries = feedEntries; } } AtomContentProperty property = new AtomContentProperty(); property.Name = propertyName; if (emptyInlineCollection || propertyValue == null) { property.IsNull = true; if (isFeed) { property.Feed = new AtomFeed(); property.Feed.Entries = Enumerable.Empty<AtomEntry>(); } else { property.Entry = new AtomEntry(); property.Entry.IsNull = true; } } else { property.Feed = propertyValue as AtomFeed; property.Entry = propertyValue as AtomEntry; } targetEntry.DataValues.Add(property); } }
private void ParseCurrentContent(AtomEntry targetEntry) { Debug.Assert(targetEntry != null, "targetEntry != null"); Debug.Assert(this.reader.NodeType == XmlNodeType.Element, "this.reader.NodeType == XmlNodeType.Element"); string propertyValue = this.reader.GetAttributeEx(XmlConstants.AtomContentSrcAttributeName, XmlConstants.AtomNamespace); if (propertyValue != null) { if (!this.reader.IsEmptyElement) { throw Error.InvalidOperation(Strings.Deserialize_ExpectedEmptyMediaLinkEntryContent); } targetEntry.MediaLinkEntry = true; targetEntry.MediaContentUri = new Uri(propertyValue, UriKind.RelativeOrAbsolute); } else { if (targetEntry.MediaLinkEntry.HasValue && targetEntry.MediaLinkEntry.Value) { throw Error.InvalidOperation(Strings.Deserialize_ContentPlusPropertiesNotAllowed); } targetEntry.MediaLinkEntry = false; propertyValue = this.reader.GetAttributeEx(XmlConstants.AtomTypeAttributeName, XmlConstants.AtomNamespace); if (AtomParser.IsAllowedContentType(propertyValue)) { if (this.reader.IsEmptyElement) { return; } if (ReadChildElement(this.reader, XmlConstants.AtomPropertiesElementName, XmlConstants.DataWebMetadataNamespace)) { this.ReadCurrentProperties(targetEntry.DataValues); } else if (this.reader.NodeType != XmlNodeType.EndElement) { throw Error.InvalidOperation(Strings.Deserialize_NotApplicationXml); } } } }
internal bool Read() { if (this.DataKind == AtomDataKind.Finished) { return false; } while (this.reader.Read()) { if (ShouldIgnoreNode(this.reader)) { continue; } Debug.Assert( this.reader.NodeType == XmlNodeType.Element || this.reader.NodeType == XmlNodeType.EndElement, "this.reader.NodeType == XmlNodeType.Element || this.reader.NodeType == XmlNodeType.EndElement -- otherwise we should have ignored or thrown"); AtomDataKind readerData = ParseStateForReader(this.reader); if (this.reader.NodeType == XmlNodeType.EndElement) { break; } switch (readerData) { case AtomDataKind.Custom: if (this.DataKind == AtomDataKind.None) { this.kind = AtomDataKind.Custom; return true; } else { MaterializeAtom.SkipToEnd(this.reader); continue; } case AtomDataKind.Entry: this.kind = AtomDataKind.Entry; this.ParseCurrentEntry(out this.entry); return true; case AtomDataKind.Feed: if (this.DataKind == AtomDataKind.None) { this.feed = new AtomFeed(); this.kind = AtomDataKind.Feed; return true; } throw new InvalidOperationException(Strings.AtomParser_FeedUnexpected); case AtomDataKind.FeedCount: this.ParseCurrentFeedCount(); break; case AtomDataKind.PagingLinks: if (this.feed == null) { throw new InvalidOperationException(Strings.AtomParser_PagingLinkOutsideOfFeed); } this.kind = AtomDataKind.PagingLinks; this.ParseCurrentFeedPagingLinks(); return true; default: Debug.Assert(false, "Atom Parser is in a wrong state...Did you add a new AtomDataKind?"); break; } } this.kind = AtomDataKind.Finished; this.entry = null; return false; }
private static bool ShouldTrackWithContext(AtomEntry entry) { Debug.Assert(entry.ActualType != null, "Entry with no type added to log"); return entry.ActualType.IsEntityType; }
private void ApplyMediaEntryInformation(AtomEntry entry, EntityDescriptor descriptor) { Debug.Assert(entry != null, "entry != null"); Debug.Assert(descriptor != null, "descriptor != null"); if (entry.MediaEditUri != null || entry.MediaContentUri != null) { if (entry.MediaEditUri != null) { descriptor.EditStreamUri = new Uri(this.context.BaseUriWithSlash, entry.MediaEditUri); } if (entry.MediaContentUri != null) { descriptor.ReadStreamUri = new Uri(this.context.BaseUriWithSlash, entry.MediaContentUri); } descriptor.StreamETag = entry.StreamETagText; } }
internal bool TryResolve(AtomEntry entry, out AtomEntry existingEntry) { Debug.Assert(entry != null, "entry != null"); Debug.Assert(entry.Identity != null, "entry.Identity != null"); if (this.identityStack.TryGetValue(entry.Identity, out existingEntry)) { return true; } if (this.appendOnlyEntries.TryGetValue(entry.Identity, out existingEntry)) { EntityStates state; this.context.TryGetEntity(entry.Identity, entry.ETagText, this.mergeOption, out state); if (state == EntityStates.Unchanged) { return true; } else { this.appendOnlyEntries.Remove(entry.Identity); } } existingEntry = null; return false; }
internal void SetLink(AtomEntry source, string propertyName, object target) { Debug.Assert(source != null, "source != null"); Debug.Assert(propertyName != null, "propertyName != null"); if (!this.Tracking) { return; } if (ShouldTrackWithContext(source) && ShouldTrackWithContext(target)) { Debug.Assert(this.Tracking, "this.Tracking -- otherwise there's an 'if' missing (it happens to be that the assert holds for all current callers"); LinkDescriptor item = new LinkDescriptor(source.ResolvedObject, propertyName, target, EntityStates.Modified); this.links.Add(item); } }
internal void FoundTargetInstance(AtomEntry entry) { Debug.Assert(entry != null, "entry != null"); Debug.Assert(entry.ResolvedObject != null, "entry.ResolvedObject != null -- otherwise this is not a target"); if (ShouldTrackWithContext(entry)) { this.context.AttachIdentity(entry.Identity, entry.QueryLink, entry.EditLink, entry.ResolvedObject, entry.ETagText); this.identityStack.Add(entry.Identity, entry); this.insertRefreshObject = entry.ResolvedObject; } }
internal void FoundExistingInstance(AtomEntry entry) { Debug.Assert(entry != null, "entry != null"); Debug.Assert(ShouldTrackWithContext(entry), "Existing entries should be entity"); if (this.mergeOption == MergeOption.OverwriteChanges) { this.identityStack[entry.Identity] = entry; } else if (this.Tracking && entry.MediaLinkEntry == true) { this.foundEntriesWithMedia[entry.Identity] = entry; } }
internal void CreatedInstance(AtomEntry entry) { Debug.Assert(entry != null, "entry != null"); Debug.Assert(entry.ResolvedObject != null, "entry.ResolvedObject != null -- otherwise, what did we create?"); Debug.Assert(entry.CreatedByMaterializer, "entry.CreatedByMaterializer -- otherwise we shouldn't be calling this"); if (ShouldTrackWithContext(entry)) { this.identityStack.Add(entry.Identity, entry); if (this.mergeOption == MergeOption.AppendOnly) { this.appendOnlyEntries.Add(entry.Identity, entry); } } }