internal EpmTargetPathSegment(string segmentName, string segmentNamespaceUri, string segmentNamespacePrefix, EpmTargetPathSegment parentSegment) : this() { this.segmentName = segmentName; this.segmentNamespaceUri = segmentNamespaceUri; this.segmentNamespacePrefix = segmentNamespacePrefix; this.parentSegment = parentSegment; }
internal EpmCustomContentWriterNodeData(EpmTargetPathSegment segment, object element) { this.XmlContentStream = new MemoryStream(); XmlWriterSettings customContentWriterSettings = new XmlWriterSettings(); customContentWriterSettings.OmitXmlDeclaration = true; customContentWriterSettings.ConformanceLevel = ConformanceLevel.Fragment; this.XmlContentWriter = XmlWriter.Create(this.XmlContentStream, customContentWriterSettings); this.PopulateData(segment, element); }
protected override void Serialize(EpmTargetPathSegment targetSegment, EpmSerializationKind kind) { if (targetSegment.IsAttribute) { this.WriteAttribute(targetSegment); } else { this.WriteElement(targetSegment); } }
protected override void Serialize(EpmTargetPathSegment targetSegment, EpmSerializationKind kind) { if (targetSegment.HasContent) { EntityPropertyMappingInfo epmInfo = targetSegment.EpmInfo; object propertyValue; try { propertyValue = epmInfo.ReadPropertyValue(this.Element); } catch (System.Reflection.TargetInvocationException) { throw; } String contentType; Action<String> contentWriter; switch (epmInfo.Attribute.TargetTextContentKind) { case SyndicationTextContentKind.Html: contentType = "html"; contentWriter = this.Target.WriteString; break; case SyndicationTextContentKind.Xhtml: contentType = "xhtml"; contentWriter = this.Target.WriteRaw; break; default: contentType = "text"; contentWriter = this.Target.WriteString; break; } Action<String, bool, bool> textSyndicationWriter = (c, nonTextPossible, atomDateConstruct) => { this.Target.WriteStartElement(c, XmlConstants.AtomNamespace); if (nonTextPossible) { this.Target.WriteAttributeString(XmlConstants.AtomTypeAttributeName, String.Empty, contentType); } String textPropertyValue = propertyValue != null ? ClientConvert.ToString(propertyValue, atomDateConstruct) : atomDateConstruct ? ClientConvert.ToString(DateTime.MinValue, atomDateConstruct) : String.Empty; contentWriter(textPropertyValue); this.Target.WriteEndElement(); }; switch (epmInfo.Attribute.TargetSyndicationItem) { case SyndicationItemProperty.AuthorEmail: case SyndicationItemProperty.ContributorEmail: textSyndicationWriter(XmlConstants.AtomEmailElementName, false, false); break; case SyndicationItemProperty.AuthorName: case SyndicationItemProperty.ContributorName: textSyndicationWriter(XmlConstants.AtomNameElementName, false, false); this.authorNamePresent = true; break; case SyndicationItemProperty.AuthorUri: case SyndicationItemProperty.ContributorUri: textSyndicationWriter(XmlConstants.AtomUriElementName, false, false); break; case SyndicationItemProperty.Updated: textSyndicationWriter(XmlConstants.AtomUpdatedElementName, false, true); this.updatedPresent = true; break; case SyndicationItemProperty.Published: textSyndicationWriter(XmlConstants.AtomPublishedElementName, false, true); break; case SyndicationItemProperty.Rights: textSyndicationWriter(XmlConstants.AtomRightsElementName, true, false); break; case SyndicationItemProperty.Summary: textSyndicationWriter(XmlConstants.AtomSummaryElementName, true, false); break; case SyndicationItemProperty.Title: textSyndicationWriter(XmlConstants.AtomTitleElementName, true, false); break; default: Debug.Assert(false, "Unhandled SyndicationItemProperty enum value - should never get here."); break; } } else { if (targetSegment.SegmentName == XmlConstants.AtomAuthorElementName) { this.CreateAuthor(false); base.Serialize(targetSegment, kind); this.FinishAuthor(); } else if (targetSegment.SegmentName == XmlConstants.AtomContributorElementName) { this.Target.WriteStartElement(XmlConstants.AtomContributorElementName, XmlConstants.AtomNamespace); base.Serialize(targetSegment, kind); this.Target.WriteEndElement(); } else { Debug.Assert(false, "Only authors and contributors have nested elements"); } } }
private void InitializeSubSegmentVisitorContent(EpmTargetPathSegment subSegment) { foreach (EpmTargetPathSegment segment in subSegment.SubSegments) { this.visitorContent.Add(segment, new EpmCustomContentWriterNodeData(this.visitorContent[subSegment], segment, this.Element)); this.InitializeSubSegmentVisitorContent(segment); } }
private void WriteElement(EpmTargetPathSegment targetSegment) { EpmCustomContentWriterNodeData currentContent = this.visitorContent[targetSegment]; currentContent.XmlContentWriter.WriteStartElement( targetSegment.SegmentNamespacePrefix, targetSegment.SegmentName, targetSegment.SegmentNamespaceUri); base.Serialize(targetSegment, EpmSerializationKind.Attributes); if (targetSegment.HasContent) { Debug.Assert(currentContent.Data != null, "Must always have non-null data content value"); currentContent.XmlContentWriter.WriteString(currentContent.Data); } base.Serialize(targetSegment, EpmSerializationKind.Elements); currentContent.XmlContentWriter.WriteEndElement(); }
private void WriteAttribute(EpmTargetPathSegment targetSegment) { Debug.Assert(targetSegment.HasContent, "Must have content for attributes"); EpmCustomContentWriterNodeData currentContent = this.visitorContent[targetSegment]; currentContent.XmlContentWriter.WriteAttributeString( targetSegment.SegmentNamespacePrefix, targetSegment.SegmentName.Substring(1), targetSegment.SegmentNamespaceUri, currentContent.Data); }
protected virtual void Serialize(EpmTargetPathSegment targetSegment, EpmSerializationKind kind) { IEnumerable<EpmTargetPathSegment> segmentsToSerialize; switch (kind) { case EpmSerializationKind.Attributes: segmentsToSerialize = targetSegment.SubSegments.Where(s => s.IsAttribute == true); break; case EpmSerializationKind.Elements: segmentsToSerialize = targetSegment.SubSegments.Where(s => s.IsAttribute == false); break; default: Debug.Assert(kind == EpmSerializationKind.All, "Must serialize everything"); segmentsToSerialize = targetSegment.SubSegments; break; } foreach (EpmTargetPathSegment segment in segmentsToSerialize) { this.Serialize(segment, kind); } }
internal EpmCustomContentWriterNodeData(EpmCustomContentWriterNodeData parentData, EpmTargetPathSegment segment, object element) { this.XmlContentStream = parentData.XmlContentStream; this.XmlContentWriter = parentData.XmlContentWriter; this.PopulateData(segment, element); }
private void PopulateData(EpmTargetPathSegment segment, object element) { if (segment.EpmInfo != null) { Object propertyValue; try { propertyValue = segment.EpmInfo.ReadPropertyValue(element); } catch (System.Reflection.TargetInvocationException) { throw; } this.Data = propertyValue == null ? String.Empty : ClientConvert.ToString(propertyValue, false); } }
internal void Add(EntityPropertyMappingInfo epmInfo) { string targetName = epmInfo.Attribute.TargetPath; bool isSyndication = epmInfo.Attribute.TargetSyndicationItem != SyndicationItemProperty.CustomProperty; string namespaceUri = epmInfo.Attribute.TargetNamespaceUri; string namespacePrefix = epmInfo.Attribute.TargetNamespacePrefix; EpmTargetPathSegment currentSegment = isSyndication ? this.SyndicationRoot : this.NonSyndicationRoot; IList<EpmTargetPathSegment> activeSubSegments = currentSegment.SubSegments; Debug.Assert(!string.IsNullOrEmpty(targetName), "Must have been validated during EntityPropertyMappingAttribute construction"); string[] targetSegments = targetName.Split('/'); for (int i = 0; i < targetSegments.Length; i++) { string targetSegment = targetSegments[i]; if (targetSegment.Length == 0) { throw new InvalidOperationException(Strings.EpmTargetTree_InvalidTargetPath(targetName)); } if (targetSegment[0] == '@' && i != targetSegments.Length - 1) { throw new InvalidOperationException(Strings.EpmTargetTree_AttributeInMiddle(targetSegment)); } EpmTargetPathSegment foundSegment = activeSubSegments.SingleOrDefault( segment => segment.SegmentName == targetSegment && (isSyndication || segment.SegmentNamespaceUri == namespaceUri)); if (foundSegment != null) { currentSegment = foundSegment; } else { currentSegment = new EpmTargetPathSegment(targetSegment, namespaceUri, namespacePrefix, currentSegment); if (targetSegment[0] == '@') { activeSubSegments.Insert(0, currentSegment); } else { activeSubSegments.Add(currentSegment); } } activeSubSegments = currentSegment.SubSegments; } if (currentSegment.HasContent) { throw new ArgumentException(Strings.EpmTargetTree_DuplicateEpmAttrsWithSameTargetName(EpmTargetTree.GetPropertyNameFromEpmInfo(currentSegment.EpmInfo), currentSegment.EpmInfo.DefiningType.Name, currentSegment.EpmInfo.Attribute.SourcePath, epmInfo.Attribute.SourcePath)); } if (!epmInfo.Attribute.KeepInContent) { this.countOfNonContentProperties++; } currentSegment.EpmInfo = epmInfo; if (EpmTargetTree.HasMixedContent(this.NonSyndicationRoot, false)) { throw new InvalidOperationException(Strings.EpmTargetTree_InvalidTargetPath(targetName)); } }
private static bool HasMixedContent(EpmTargetPathSegment currentSegment, bool ancestorHasContent) { foreach (EpmTargetPathSegment childSegment in currentSegment.SubSegments.Where(s => !s.IsAttribute)) { if (childSegment.HasContent && ancestorHasContent) { return true; } if (HasMixedContent(childSegment, childSegment.HasContent || ancestorHasContent)) { return true; } } return false; }