#pragma warning restore 649 #endregion Private fields. internal MaterializeAtom(DataServiceContext context, XmlReader reader, QueryComponents queryComponents, ProjectionPlan plan, MergeOption mergeOption) { Debug.Assert(queryComponents != null, "queryComponents != null"); this.context = context; this.elementType = queryComponents.LastSegmentType; this.MergeOptionValue = mergeOption; this.ignoreMissingProperties = context.IgnoreMissingProperties; this.reader = (reader == null) ? null : new Microsoft.Samples.Data.Services.Client.Xml.XmlAtomErrorReader(reader); this.countValue = CountStateInitial; this.expectingSingleValue = ClientConvert.IsKnownNullableType(this.elementType); Debug.Assert(reader != null, "Materializer reader is null! Did you mean to use Materializer.ResultsWrapper/EmptyResults?"); reader.Settings.NameTable.Add(context.DataNamespace); string typeScheme = this.context.TypeScheme.OriginalString; this.parser = new AtomParser(this.reader, AtomParser.XElementBuilderCallback, typeScheme, context.DataNamespace); AtomMaterializerLog log = new AtomMaterializerLog(this.context, mergeOption); Type implementationType; Type materializerType = GetTypeForMaterializer(this.expectingSingleValue, this.elementType, out implementationType); this.materializer = new AtomMaterializer(this.parser, context, materializerType, this.ignoreMissingProperties, mergeOption, log, this.MaterializedObjectCallback, queryComponents, plan); }
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); } }