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 the cannot 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
- CacheManager.cs
- MailWebEventProvider.cs
- MachineKeyConverter.cs
- WebControlToolBoxItem.cs
- StateValidator.cs
- GroupStyle.cs
- SignHashRequest.cs
- OdbcUtils.cs
- DesignerListAdapter.cs
- ProfilePropertySettingsCollection.cs
- TemplateKey.cs
- RangeValidator.cs
- FilteredXmlReader.cs
- DataGrid.cs
- WebExceptionStatus.cs
- PermissionToken.cs
- SynchronizingStream.cs
- HyperLink.cs
- SessionStateSection.cs
- GroupDescription.cs
- UniqueEventHelper.cs
- CatalogZone.cs
- XmlNodeWriter.cs
- XmlSerializerFactory.cs
- GridViewCancelEditEventArgs.cs
- TextWriterTraceListener.cs
- PeerNameRegistration.cs
- FtpRequestCacheValidator.cs
- EntityDataSourceViewSchema.cs
- OdbcDataReader.cs
- CoreSwitches.cs
- NetPeerTcpBindingCollectionElement.cs
- ContentPresenter.cs
- FormCollection.cs
- WebPartRestoreVerb.cs
- DeferredTextReference.cs
- ReaderWriterLockWrapper.cs
- TableChangeProcessor.cs
- CorePropertiesFilter.cs
- ErrorFormatterPage.cs
- ObjectQueryProvider.cs
- CompilerCollection.cs
- XmlSchemaSimpleContentExtension.cs
- CompilerError.cs
- KeyEvent.cs
- FullTextLine.cs
- InvokeWebService.cs
- Int32Animation.cs
- JoinCqlBlock.cs
- MonthCalendar.cs
- IODescriptionAttribute.cs
- TypeElement.cs
- PreviewPrintController.cs
- VisualBrush.cs
- TypedTableBase.cs
- BoundPropertyEntry.cs
- TextEditorCopyPaste.cs
- Transform.cs
- Semaphore.cs
- PropertyGridView.cs
- DataGridViewColumnEventArgs.cs
- SystemIPAddressInformation.cs
- CodeAttributeDeclaration.cs
- ColorTranslator.cs
- GroupBox.cs
- PngBitmapDecoder.cs
- WindowsUpDown.cs
- ApplicationDirectory.cs
- OrderByQueryOptionExpression.cs
- Condition.cs
- MembershipValidatePasswordEventArgs.cs
- ClientRolePrincipal.cs
- SmiTypedGetterSetter.cs
- LocationSectionRecord.cs
- FaultConverter.cs
- InstanceHandleReference.cs
- ItemsChangedEventArgs.cs
- UnauthorizedWebPart.cs
- LogicalTreeHelper.cs
- DataGridColumnCollection.cs
- SignatureConfirmationElement.cs
- RuntimeIdentifierPropertyAttribute.cs
- Label.cs
- BindingCollection.cs
- ContentHostHelper.cs
- XmlSchemaCompilationSettings.cs
- DocumentCollection.cs
- CodeSpit.cs
- RegexCode.cs
- ValidationErrorCollection.cs
- LoginUtil.cs
- localization.cs
- LongMinMaxAggregationOperator.cs
- SecurityException.cs
- BigInt.cs
- Authorization.cs
- FixedSOMPageConstructor.cs
- SqlException.cs
- MsmqProcessProtocolHandler.cs
- InteropAutomationProvider.cs