Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataWeb / Server / System / Data / Services / Epm / EpmSyndicationContentSerializer.cs / 1305376 / EpmSyndicationContentSerializer.cs
//---------------------------------------------------------------------- //// Copyright (c) Microsoft Corporation. All rights reserved. // //// Classes used for serializing EntityPropertyMappingAttribute content for // syndication specific mappings // // // @owner [....] //--------------------------------------------------------------------- namespace System.Data.Services.Common { #region Namespaces using System.Data.Services.Providers; using System.Data.Services.Serializers; using System.Globalization; using System.ServiceModel.Syndication; using System.Diagnostics; #endregion ////// Base visitor class for performing serialization of syndication specific content in the feed entry whose mapping /// is provided through EntityPropertyMappingAttributes /// internal sealed class EpmSyndicationContentSerializer : EpmContentSerializerBase, IDisposable { ////// Syndication author object resulting from mapping /// private SyndicationPerson author; ////// Is author mapping provided in the target tree /// private bool authorInfoPresent; ////// Is author name provided in the target tree /// private bool authorNamePresent; ///Collection of null valued properties for this current serialization private EpmContentSerializer.EpmNullValuedPropertyTree nullValuedProperties; ////// Constructor initializes the base class be identifying itself as a syndication serializer /// /// Target tree containing mapping information /// Object to be serialized /// SyndicationItem to which content will be added /// Null valued properties found during serialization internal EpmSyndicationContentSerializer(EpmTargetTree tree, object element, SyndicationItem target, EpmContentSerializer.EpmNullValuedPropertyTree nullValuedProperties) : base(tree, true, element, target) { this.nullValuedProperties = nullValuedProperties; } ////// Used for housecleaning once every node has been visited, creates an author syndication element if none has been /// created by the visitor /// public void Dispose() { this.CreateAuthor(true); if (!this.authorNamePresent) { this.author.Name = String.Empty; this.authorNamePresent = true; } this.Target.Authors.Add(this.author); } ////// Override of the base Visitor method, which actually performs mapping search and serialization /// /// Current segment being checked for mapping /// Which sub segments to serialize /// Data Service provider used for rights verification. protected override void Serialize(EpmTargetPathSegment targetSegment, EpmSerializationKind kind, DataServiceProviderWrapper provider) { if (targetSegment.HasContent) { EntityPropertyMappingInfo epmInfo = targetSegment.EpmInfo; Object propertyValue; try { propertyValue = epmInfo.PropValReader.DynamicInvoke(this.Element, provider); } catch (System.Reflection.TargetInvocationException e) { ErrorHandler.HandleTargetInvocationException(e); throw; } if (propertyValue == null) { this.nullValuedProperties.Add(epmInfo); } String textPropertyValue = propertyValue != null ? PlainXmlSerializer.PrimitiveToString(propertyValue) : String.Empty; TextSyndicationContentKind contentKind = (TextSyndicationContentKind)epmInfo.Attribute.TargetTextContentKind; switch (epmInfo.Attribute.TargetSyndicationItem) { case SyndicationItemProperty.AuthorEmail: this.CreateAuthor(false); this.author.Email = textPropertyValue; break; case SyndicationItemProperty.AuthorName: this.CreateAuthor(false); this.author.Name = textPropertyValue; this.authorNamePresent = true; break; case SyndicationItemProperty.AuthorUri: this.CreateAuthor(false); this.author.Uri = textPropertyValue; break; case SyndicationItemProperty.ContributorEmail: case SyndicationItemProperty.ContributorName: case SyndicationItemProperty.ContributorUri: this.SetContributorProperty(epmInfo.Attribute.TargetSyndicationItem, textPropertyValue); break; case SyndicationItemProperty.Updated: this.Target.LastUpdatedTime = EpmSyndicationContentSerializer.GetDate(propertyValue, Strings.EpmSerializer_UpdatedHasWrongType); break; case SyndicationItemProperty.Published: this.Target.PublishDate = EpmSyndicationContentSerializer.GetDate(propertyValue, Strings.EpmSerializer_PublishedHasWrongType); break; case SyndicationItemProperty.Rights: this.Target.Copyright = new TextSyndicationContent(textPropertyValue, contentKind); break; case SyndicationItemProperty.Summary: this.Target.Summary = new TextSyndicationContent(textPropertyValue, contentKind); break; case SyndicationItemProperty.Title: this.Target.Title = new TextSyndicationContent(textPropertyValue, contentKind); break; default: Debug.Fail("Unhandled SyndicationItemProperty enum value - should never get here."); break; } } else { base.Serialize(targetSegment, kind, provider); } } ////// Given an object returns the corresponding DateTimeOffset value through conversions /// /// Object containing property value /// Message of the thrown exception if thecannot be converted to DateTimeOffset. /// DateTimeOffset after conversion private static DateTimeOffset GetDate(object propertyValue, string exceptionMsg) { if (propertyValue == null) { return DateTimeOffset.Now; } DateTimeOffset date; if (propertyValue is DateTimeOffset) { date = (DateTimeOffset)propertyValue; } else if (propertyValue is DateTime) { // DateTimeOffset takes care of DateTimes of Unspecified kind so we won't end up // with datetime without timezone info mapped to atom:updated or atom:published element. date = new DateTimeOffset((DateTime)propertyValue); } else if (propertyValue is String) { if (!DateTimeOffset.TryParse((String)propertyValue, out date)) { DateTime result; if (!DateTime.TryParse((String)propertyValue, out result)) { throw new DataServiceException(500, null, exceptionMsg, null, null); } date = new DateTimeOffset(result); } } else { try { date = new DateTimeOffset(Convert.ToDateTime(propertyValue, CultureInfo.InvariantCulture)); } catch (Exception e) { throw new DataServiceException(500, null, exceptionMsg, null, e); } } return date; } ////// Creates a new author syndication specific object /// /// Whether to create a null author or a non-null author private void CreateAuthor(bool createNull) { if (!this.authorInfoPresent) { this.author = createNull ? new SyndicationPerson(null, String.Empty, null) : new SyndicationPerson(); this.authorInfoPresent = true; } } ////// Set a property of atom:contributor element. atom:contributor is created if does not exist. /// /// Property to be set. /// Value of the property. private void SetContributorProperty(SyndicationItemProperty propertyToSet, string textPropertyValue) { if (this.Target.Contributors.Count == 0) { this.Target.Contributors.Add(new SyndicationPerson()); } // We can have at most one contributor switch (propertyToSet) { case SyndicationItemProperty.ContributorEmail: this.Target.Contributors[0].Email = textPropertyValue; break; case SyndicationItemProperty.ContributorName: this.Target.Contributors[0].Name = textPropertyValue; break; case SyndicationItemProperty.ContributorUri: this.Target.Contributors[0].Uri = textPropertyValue; break; default: Debug.Fail("propertyToSet is not a Contributor property."); break; } Debug.Assert(this.Target.Contributors.Count == 1, "There should be one and only one contributor."); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SQLBoolean.cs
- ServicePointManager.cs
- MissingMethodException.cs
- OracleConnectionString.cs
- SQLGuidStorage.cs
- CodeSubDirectory.cs
- PrivateFontCollection.cs
- DispatcherProcessingDisabled.cs
- CompoundFileReference.cs
- graph.cs
- BindingMAnagerBase.cs
- QilBinary.cs
- IPEndPointCollection.cs
- XpsFilter.cs
- InvalidCommandTreeException.cs
- sortedlist.cs
- SynthesizerStateChangedEventArgs.cs
- EncodingTable.cs
- HandlerWithFactory.cs
- DataSysAttribute.cs
- ExtractedStateEntry.cs
- XpsPackagingException.cs
- CallbackValidatorAttribute.cs
- SqlUserDefinedAggregateAttribute.cs
- HtmlElementEventArgs.cs
- JapaneseLunisolarCalendar.cs
- GroupedContextMenuStrip.cs
- DataListItemCollection.cs
- GeometryHitTestParameters.cs
- NativeObjectSecurity.cs
- TemplateControlCodeDomTreeGenerator.cs
- SqlDependencyListener.cs
- PackagePartCollection.cs
- InputBinding.cs
- EventLog.cs
- XmlILIndex.cs
- BufferedGraphicsManager.cs
- PolicyStatement.cs
- KernelTypeValidation.cs
- DispatchOperation.cs
- NameValueConfigurationElement.cs
- Item.cs
- WSTrustDec2005.cs
- PrintingPermission.cs
- ButtonChrome.cs
- IncomingWebResponseContext.cs
- WebCategoryAttribute.cs
- ECDsa.cs
- NumberFunctions.cs
- DbConnectionOptions.cs
- ConcurrentStack.cs
- TextClipboardData.cs
- TypeForwardedToAttribute.cs
- Sql8ExpressionRewriter.cs
- MemberDescriptor.cs
- VerticalConnector.xaml.cs
- SmtpDigestAuthenticationModule.cs
- MouseDevice.cs
- HttpCacheVary.cs
- DecoderReplacementFallback.cs
- SingletonInstanceContextProvider.cs
- XamlSerializationHelper.cs
- ManipulationVelocities.cs
- X509CertificateValidator.cs
- ToolStripMenuItem.cs
- TdsRecordBufferSetter.cs
- ApplicationInfo.cs
- Renderer.cs
- EncoderReplacementFallback.cs
- QilTargetType.cs
- DataSourceSelectArguments.cs
- BitConverter.cs
- RtfToXamlLexer.cs
- XmlReaderSettings.cs
- BoolLiteral.cs
- DataGridViewSelectedCellCollection.cs
- PlacementWorkspace.cs
- ManifestResourceInfo.cs
- AncestorChangedEventArgs.cs
- SystemEvents.cs
- RC2CryptoServiceProvider.cs
- ElementUtil.cs
- SqlCrossApplyToCrossJoin.cs
- CustomTypeDescriptor.cs
- CallbackValidatorAttribute.cs
- TextDecoration.cs
- Path.cs
- MetadataCacheItem.cs
- Color.cs
- Scheduling.cs
- StatusBar.cs
- AutomationProperties.cs
- PositiveTimeSpanValidator.cs
- EntityDataSourceWrapperPropertyDescriptor.cs
- WebPartHeaderCloseVerb.cs
- TextLine.cs
- DBBindings.cs
- ImportContext.cs
- versioninfo.cs
- DataGridViewTopRowAccessibleObject.cs