using System;
using System.Configuration;
using System.Collections.Specialized;
using System.Xml;
namespace SuperSocket.Common
{
///
/// ConfigurationElementBase
///
[Serializable]
public class ConfigurationElementBase : ConfigurationElement
{
private bool m_NameRequired;
///
/// Initializes a new instance of the class.
///
public ConfigurationElementBase()
: this(true)
{
}
///
/// Initializes a new instance of the class.
///
/// if set to true [name required].
public ConfigurationElementBase(bool nameRequired)
{
m_NameRequired = nameRequired;
Options = new NameValueCollection();
}
///
/// Gets the name.
///
[ConfigurationProperty("name")]
public string Name
{
get { return this["name"] as string; }
}
///
/// Reads XML from the configuration file.
///
/// The that reads from the configuration file.
/// true to serialize only the collection key properties; otherwise, false.
/// The element to read is locked.- or -An attribute of the current node is not recognized.- or -The lock status of the current node cannot be determined.
protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
{
base.DeserializeElement(reader, serializeCollectionKey);
if (m_NameRequired && string.IsNullOrEmpty(Name))
{
throw new ConfigurationErrorsException("Required attribute 'name' not found.");
}
}
///
/// Gets the options.
///
public NameValueCollection Options { get; set; }
///
/// Gets a value indicating whether an unknown attribute is encountered during deserialization.
///
/// The name of the unrecognized attribute.
/// The value of the unrecognized attribute.
///
/// true when an unknown attribute is encountered while deserializing; otherwise, false.
///
protected override bool OnDeserializeUnrecognizedAttribute(string name, string value)
{
Options.Add(name, value);
return true;
}
///
/// Modifies the object to remove all values that should not be saved.
///
/// A at the current level containing a merged view of the properties.
/// The parent , or null if this is the top level.
/// A that determines which property values to include.
protected override void Unmerge(ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
{
base.Unmerge(sourceElement, parentElement, saveMode);
var element = sourceElement as ConfigurationElementBase;
if (element == null)
return;
if (element.Options != this.Options)
this.Options = element.Options;
if (element.OptionElements != this.OptionElements)
this.OptionElements = element.OptionElements;
}
///
/// Writes the contents of this configuration element to the configuration file when implemented in a derived class.
///
/// The that writes to the configuration file.
/// true to serialize only the collection key properties; otherwise, false.
///
/// true if any data was actually serialized; otherwise, false.
///
protected override bool SerializeElement(XmlWriter writer, bool serializeCollectionKey)
{
if (!base.SerializeElement(writer, serializeCollectionKey))
return false;
if (writer == null)
return true;
var options = Options;
if (options != null && options.Count > 0)
{
for (var i = 0; i < options.Count; i++)
{
writer.WriteAttributeString(options.GetKey(i), options.Get(i));
}
}
var optionElements = OptionElements;
if (optionElements != null && optionElements.Count > 0)
{
for (var i = 0; i < optionElements.Count; i++)
{
writer.WriteRaw(optionElements.Get(i));
}
}
return true;
}
///
/// Gets the option elements.
///
public NameValueCollection OptionElements { get; set; }
///
/// Gets a value indicating whether an unknown element is encountered during deserialization.
///
/// The name of the unknown subelement.
/// The being used for deserialization.
///
/// true when an unknown element is encountered while deserializing; otherwise, false.
///
/// The element identified by is locked.- or -One or more of the element's attributes is locked.- or - is unrecognized, or the element has an unrecognized attribute.- or -The element has a Boolean attribute with an invalid value.- or -An attempt was made to deserialize a property more than once.- or -An attempt was made to deserialize a property that is not a valid member of the element.- or -The element cannot contain a CDATA or text element.
protected override bool OnDeserializeUnrecognizedElement(string elementName, System.Xml.XmlReader reader)
{
if (OptionElements == null)
OptionElements = new NameValueCollection();
OptionElements.Add(elementName, reader.ReadOuterXml());
return true;
}
}
}