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
- FontUnitConverter.cs
- WindowsUpDown.cs
- DataGridDefaultColumnWidthTypeConverter.cs
- BoolExpr.cs
- WebPartConnectionsConfigureVerb.cs
- DesignerHost.cs
- NestPullup.cs
- Subset.cs
- ToolStripPanel.cs
- InternalSafeNativeMethods.cs
- CallbackException.cs
- RectAnimationBase.cs
- AuthenticationConfig.cs
- SerializationObjectManager.cs
- PhysicalFontFamily.cs
- ObjectListCommand.cs
- AddInServer.cs
- SoapCodeExporter.cs
- DelimitedListTraceListener.cs
- XmlDocumentFragment.cs
- XsltSettings.cs
- ControlType.cs
- ScaleTransform.cs
- ListenerElementsCollection.cs
- SHA384Managed.cs
- GeometryDrawing.cs
- LinqDataSourceInsertEventArgs.cs
- StatusBarItemAutomationPeer.cs
- CompilerParameters.cs
- ServicePointManagerElement.cs
- DictionaryBase.cs
- ActivityDesignerAccessibleObject.cs
- SQLInt16.cs
- Directory.cs
- TextEndOfLine.cs
- Double.cs
- BlurBitmapEffect.cs
- AutomationAttributeInfo.cs
- DataGridHeadersVisibilityToVisibilityConverter.cs
- PageClientProxyGenerator.cs
- ExtensionSimplifierMarkupObject.cs
- Point4D.cs
- SynchronizationScope.cs
- IgnorePropertiesAttribute.cs
- ForAllOperator.cs
- WebBaseEventKeyComparer.cs
- Parser.cs
- Activation.cs
- ReferenceEqualityComparer.cs
- ManagedWndProcTracker.cs
- DateTimeStorage.cs
- FormCollection.cs
- XXXOnTypeBuilderInstantiation.cs
- RNGCryptoServiceProvider.cs
- AssemblyCollection.cs
- Crc32.cs
- SpeechRecognizer.cs
- MappingException.cs
- IntPtr.cs
- XmlDeclaration.cs
- InterleavedZipPartStream.cs
- QilPatternVisitor.cs
- GradientSpreadMethodValidation.cs
- BitmapEffectGroup.cs
- OraclePermission.cs
- MapPathBasedVirtualPathProvider.cs
- KeyValuePair.cs
- EncoderParameter.cs
- ContainerActivationHelper.cs
- EditableRegion.cs
- DateTimeConverter.cs
- PlanCompilerUtil.cs
- WebRequestModuleElementCollection.cs
- NonSerializedAttribute.cs
- RelationshipSet.cs
- ObjectDataSource.cs
- SQLSingle.cs
- DependencySource.cs
- TableCellCollection.cs
- IntranetCredentialPolicy.cs
- DataGridViewDataConnection.cs
- ListBoxAutomationPeer.cs
- ArrayEditor.cs
- BStrWrapper.cs
- ComboBoxItem.cs
- SrgsOneOf.cs
- TeredoHelper.cs
- CqlLexerHelpers.cs
- StyleCollectionEditor.cs
- precedingsibling.cs
- ParameterCollection.cs
- XmlSchemaDatatype.cs
- ObjectDataSource.cs
- TextRange.cs
- ExcCanonicalXml.cs
- CodeMemberEvent.cs
- GradientStopCollection.cs
- CodePrimitiveExpression.cs
- PageRequestManager.cs
- Cell.cs