Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / ndp / fx / src / DataWeb / Server / System / Data / Services / Serializers / DictionaryContent.cs / 1 / DictionaryContent.cs
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Provides a syndication content that holds name/value pairs.
//
//
// @owner [....]
//---------------------------------------------------------------------
namespace System.Data.Services.Serializers
{
#region Namespaces.
using System.Collections.Generic;
using System.Data.Services.Providers;
using System.Diagnostics;
using System.ServiceModel.Syndication;
using System.Xml;
#endregion Namespaces.
///
/// Use this class to hold the name/value pairs of content that will be written
/// to the content element of a syndication item.
///
internal class DictionaryContent : SyndicationContent
{
#region Private fields.
/// Content for a property value: one of a string, dictionary or null.
private List valueContents;
/// Names for property values.
private List valueNames;
/// Declared type names for property values.
private List valueTypes;
#endregion Private fields.
#region Constructors.
/// Initializes a new DictionaryContent instance.
public DictionaryContent()
{
this.valueContents = new List();
this.valueTypes = new List();
this.valueNames = new List();
}
/// Initializes a new DictionaryContent instance.
/// Initial capacity for entries.
public DictionaryContent(int capacity)
{
this.valueContents = new List(capacity);
this.valueTypes = new List(capacity);
this.valueNames = new List(capacity);
}
///
/// Initializes a new DictionaryContent instance by copying values from
/// the specified one.
///
/// Dictionary to copy content from.
/// This produces a shallow copy only.
private DictionaryContent(DictionaryContent other)
{
Debug.Assert(other != null, "other != null");
this.valueContents = other.valueContents;
this.valueTypes = other.valueTypes;
this.valueNames = other.valueNames;
}
#endregion Constructors.
#region Properties.
/// The MIME type of this content.
public override string Type
{
get { return XmlConstants.MimeApplicationXml; }
}
#endregion Properties.
#region Methods.
/// Creates a shallow copy of this content.
/// A shallow copy of this content.
public override SyndicationContent Clone()
{
return new DictionaryContent(this);
}
/// Adds the specified property.
/// Property name.
/// Expected type name for value.
/// Property value in text form.
internal void Add(string name, string expectedTypeName, object value)
{
Debug.Assert(value != null, "value != null -- otherwise AddNull should have been called.");
Debug.Assert(
value is DictionaryContent || value is string,
"value is DictionaryContent || value is string -- only sub-dictionaries and formatted strings are allowed.");
Debug.Assert(!this.valueNames.Contains(name), "!this.valueNames.Contains(name) -- otherwise repeated property set.");
this.valueNames.Add(name);
this.valueTypes.Add(expectedTypeName);
this.valueContents.Add(value);
Debug.Assert(this.valueNames.Count == this.valueTypes.Count, "this.valueNames.Count == this.valueTypes.Count");
Debug.Assert(this.valueNames.Count == this.valueContents.Count, "this.valueNames.Count == this.valueContents.Count");
}
/// Adds a property with a null value.
/// Type for the property.
/// Property name.
internal void AddNull(string expectedTypeName, string name)
{
Debug.Assert(!this.valueNames.Contains(name), "!this.valueNames.Contains(name) -- otherwise repeated property set.");
this.valueNames.Add(name);
this.valueTypes.Add(expectedTypeName);
this.valueContents.Add(null);
Debug.Assert(this.valueNames.Count == this.valueTypes.Count, "this.valueNames.Count == this.valueTypes.Count");
Debug.Assert(this.valueNames.Count == this.valueContents.Count, "this.valueNames.Count == this.valueContents.Count");
}
/// Writes the contents of this SyndicationContent object to the specified XmlWriter.
/// The XmlWriter to write to.
protected override void WriteContentsTo(XmlWriter writer)
{
if (0 < this.valueNames.Count)
{
writer.WriteStartElement(XmlConstants.AtomPropertiesElementName, XmlConstants.DataWebMetadataNamespace);
this.WritePropertyContentsTo(writer);
writer.WriteEndElement();
}
}
/// Writes the contents of this SyndicationContent object to the specified XmlWriter.
/// The XmlWriter to write to.
private void WritePropertyContentsTo(XmlWriter writer)
{
for (int i = 0; i < this.valueNames.Count; i++)
{
string propertyName = this.valueNames[i];
string propertyTypeName = this.valueTypes[i];
object propertyValue = this.valueContents[i];
if (propertyValue == null)
{
PlainXmlSerializer.WriteNullValue(writer, propertyName, propertyTypeName);
}
else if (propertyValue is DictionaryContent)
{
PlainXmlSerializer.WriteStartElementWithType(writer, propertyName, propertyTypeName);
((DictionaryContent)propertyValue).WritePropertyContentsTo(writer);
writer.WriteEndElement();
}
else
{
string propertyText = (string)propertyValue;
PlainXmlSerializer.WriteTextValue(writer, propertyName, propertyTypeName, propertyText);
}
}
}
#endregion Methods.
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Provides a syndication content that holds name/value pairs.
//
//
// @owner [....]
//---------------------------------------------------------------------
namespace System.Data.Services.Serializers
{
#region Namespaces.
using System.Collections.Generic;
using System.Data.Services.Providers;
using System.Diagnostics;
using System.ServiceModel.Syndication;
using System.Xml;
#endregion Namespaces.
///
/// Use this class to hold the name/value pairs of content that will be written
/// to the content element of a syndication item.
///
internal class DictionaryContent : SyndicationContent
{
#region Private fields.
/// Content for a property value: one of a string, dictionary or null.
private List valueContents;
/// Names for property values.
private List valueNames;
/// Declared type names for property values.
private List valueTypes;
#endregion Private fields.
#region Constructors.
/// Initializes a new DictionaryContent instance.
public DictionaryContent()
{
this.valueContents = new List();
this.valueTypes = new List();
this.valueNames = new List();
}
/// Initializes a new DictionaryContent instance.
/// Initial capacity for entries.
public DictionaryContent(int capacity)
{
this.valueContents = new List(capacity);
this.valueTypes = new List(capacity);
this.valueNames = new List(capacity);
}
///
/// Initializes a new DictionaryContent instance by copying values from
/// the specified one.
///
/// Dictionary to copy content from.
/// This produces a shallow copy only.
private DictionaryContent(DictionaryContent other)
{
Debug.Assert(other != null, "other != null");
this.valueContents = other.valueContents;
this.valueTypes = other.valueTypes;
this.valueNames = other.valueNames;
}
#endregion Constructors.
#region Properties.
/// The MIME type of this content.
public override string Type
{
get { return XmlConstants.MimeApplicationXml; }
}
#endregion Properties.
#region Methods.
/// Creates a shallow copy of this content.
/// A shallow copy of this content.
public override SyndicationContent Clone()
{
return new DictionaryContent(this);
}
/// Adds the specified property.
/// Property name.
/// Expected type name for value.
/// Property value in text form.
internal void Add(string name, string expectedTypeName, object value)
{
Debug.Assert(value != null, "value != null -- otherwise AddNull should have been called.");
Debug.Assert(
value is DictionaryContent || value is string,
"value is DictionaryContent || value is string -- only sub-dictionaries and formatted strings are allowed.");
Debug.Assert(!this.valueNames.Contains(name), "!this.valueNames.Contains(name) -- otherwise repeated property set.");
this.valueNames.Add(name);
this.valueTypes.Add(expectedTypeName);
this.valueContents.Add(value);
Debug.Assert(this.valueNames.Count == this.valueTypes.Count, "this.valueNames.Count == this.valueTypes.Count");
Debug.Assert(this.valueNames.Count == this.valueContents.Count, "this.valueNames.Count == this.valueContents.Count");
}
/// Adds a property with a null value.
/// Type for the property.
/// Property name.
internal void AddNull(string expectedTypeName, string name)
{
Debug.Assert(!this.valueNames.Contains(name), "!this.valueNames.Contains(name) -- otherwise repeated property set.");
this.valueNames.Add(name);
this.valueTypes.Add(expectedTypeName);
this.valueContents.Add(null);
Debug.Assert(this.valueNames.Count == this.valueTypes.Count, "this.valueNames.Count == this.valueTypes.Count");
Debug.Assert(this.valueNames.Count == this.valueContents.Count, "this.valueNames.Count == this.valueContents.Count");
}
/// Writes the contents of this SyndicationContent object to the specified XmlWriter.
/// The XmlWriter to write to.
protected override void WriteContentsTo(XmlWriter writer)
{
if (0 < this.valueNames.Count)
{
writer.WriteStartElement(XmlConstants.AtomPropertiesElementName, XmlConstants.DataWebMetadataNamespace);
this.WritePropertyContentsTo(writer);
writer.WriteEndElement();
}
}
/// Writes the contents of this SyndicationContent object to the specified XmlWriter.
/// The XmlWriter to write to.
private void WritePropertyContentsTo(XmlWriter writer)
{
for (int i = 0; i < this.valueNames.Count; i++)
{
string propertyName = this.valueNames[i];
string propertyTypeName = this.valueTypes[i];
object propertyValue = this.valueContents[i];
if (propertyValue == null)
{
PlainXmlSerializer.WriteNullValue(writer, propertyName, propertyTypeName);
}
else if (propertyValue is DictionaryContent)
{
PlainXmlSerializer.WriteStartElementWithType(writer, propertyName, propertyTypeName);
((DictionaryContent)propertyValue).WritePropertyContentsTo(writer);
writer.WriteEndElement();
}
else
{
string propertyText = (string)propertyValue;
PlainXmlSerializer.WriteTextValue(writer, propertyName, propertyTypeName, propertyText);
}
}
}
#endregion Methods.
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.