// -----------------------------------------------------------------------
//
// Copyright (c) James South.
// Dual licensed under the MIT or GPL Version 2 licenses.
//
// -----------------------------------------------------------------------
namespace ImageProcessor.Web.Config
{
#region Using
using System.Configuration;
using System.Linq;
#endregion
///
/// Represents an image processing section within a configuration file.
/// Nested syntax adapted from
///
public class ImageProcessingSection : ConfigurationSection
{
#region Properties
///
/// Gets the .
///
///
/// The .
///
[ConfigurationProperty("plugins", IsRequired = true)]
public PluginElementCollection Plugins
{
get
{
return this["plugins"] as PluginElementCollection;
}
}
#endregion
#region Methods
///
/// Retrieves the processing configuration section from the current application configuration.
///
/// The processing configuration section from the current application configuration.
public static ImageProcessingSection GetConfiguration()
{
ImageProcessingSection imageProcessingSection =
ConfigurationManager.GetSection("imageProcessor/processing") as ImageProcessingSection;
if (imageProcessingSection != null)
{
return imageProcessingSection;
}
return new ImageProcessingSection();
}
#endregion
///
/// Represents a PluginElement configuration element within the configuration.
///
public class PluginElement : ConfigurationElement
{
///
/// Gets or sets the name of the plugin file.
///
/// The name of the plugin.
[ConfigurationProperty("name", DefaultValue = "", IsRequired = true)]
public string Name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
///
/// Gets the .
///
///
/// The .
///
[ConfigurationProperty("settings", IsRequired = true)]
public SettingElementCollection Settings
{
get
{
return this["settings"] as SettingElementCollection;
}
}
}
///
/// Represents a PluginElementCollection collection configuration element within the configuration.
///
public class PluginElementCollection : ConfigurationElementCollection
{
///
/// Gets the type of the .
///
///
/// The of this collection.
///
public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.BasicMap; }
}
///
/// Gets the name used to identify this collection of elements in the configuration file when overridden in a derived class.
///
///
/// The name of the collection; otherwise, an empty string. The default is an empty string.
///
protected override string ElementName
{
get { return "plugin"; }
}
///
/// Gets or sets the
/// at the specified index within the collection.
///
/// The index at which to get the specified object.
///
/// The the
/// at the specified index within the collection.
///
public PluginElement this[int index]
{
get
{
return (PluginElement)BaseGet(index);
}
set
{
if (this.BaseGet(index) != null)
{
this.BaseRemoveAt(index);
}
this.BaseAdd(index, value);
}
}
///
/// Creates a new PluginConfig configuration element.
///
///
/// A new PluginConfig configuration element.
///
protected override ConfigurationElement CreateNewElement()
{
return new PluginElement();
}
///
/// Gets the element key for a specified PluginElement configuration element.
///
///
/// The ConfigurationElement
/// to return the key for.
///
/// The element key for a specified PluginElement configuration element.
protected override object GetElementKey(ConfigurationElement element)
{
return ((PluginElement)element).Name;
}
}
///
/// Represents a SettingElement configuration element within the configuration.
///
public class SettingElement : ConfigurationElement
{
///
/// Gets or sets the key of the plugin setting.
///
/// The key of the plugin setting.
[ConfigurationProperty("key", IsRequired = true, IsKey = true)]
public string Key
{
get
{
return this["key"] as string;
}
set
{
this["key"] = value;
}
}
///
/// Gets or sets the value of the plugin setting.
///
/// The value of the plugin setting.
[ConfigurationProperty("value", IsRequired = true)]
public string Value
{
get
{
return (string)this["value"];
}
set
{
this["value"] = value;
}
}
}
///
/// Represents a SettingElementCollection collection configuration element within the configuration.
///
public class SettingElementCollection : ConfigurationElementCollection
{
///
/// Gets the type of the .
///
///
/// The of this collection.
///
public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.BasicMap; }
}
///
/// Gets the name used to identify this collection of elements in the configuration file when overridden in a derived class.
///
///
/// The name of the collection; otherwise, an empty string. The default is an empty string.
///
protected override string ElementName
{
get { return "setting"; }
}
///
/// Gets or sets the
/// at the specified index within the collection.
///
/// The index at which to get the specified object.
///
/// The the
/// at the specified index within the collection.
///
public SettingElement this[int index]
{
get
{
return (SettingElement)BaseGet(index);
}
set
{
if (this.BaseGet(index) != null)
{
this.BaseRemoveAt(index);
}
this.BaseAdd(index, value);
}
}
///
/// Returns the setting element with the specified key.
///
/// the key representing the element
/// the setting element
public new SettingElement this[string key]
{
get { return (SettingElement)BaseGet(key); }
}
///
/// Returns a value indicating whether the settings collection contains the
/// given object.
///
/// The key to identify the setting.
/// True if the collection contains the key; otherwise false.
public bool ContainsKey(string key)
{
object[] keys = BaseGetAllKeys();
return keys.Any(obj => (string)obj == key);
}
///
/// Gets the element key for a specified PluginElement configuration element.
///
///
/// The ConfigurationElement
/// to return the key for.
///
/// The element key for a specified PluginElement configuration element.
protected override object GetElementKey(ConfigurationElement element)
{
return ((SettingElement)element).Key;
}
///
/// Creates a new SettingElement configuration element.
///
///
/// A new SettingElement configuration element.
///
protected override ConfigurationElement CreateNewElement()
{
return new SettingElement();
}
}
}
}