// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // // // Represents an image processing section within a configuration file. // Nested syntax adapted from // // -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Web.Configuration { #region Using using System.Configuration; using System.IO; using System.Linq; using System.Xml; using ImageProcessor.Web.Helpers; #endregion /// /// Represents an image processing section within a configuration file. /// Nested syntax adapted from /// public sealed class ImageProcessingSection : ConfigurationSection { #region Properties /// /// Gets or sets a value indicating whether to preserve exif meta data. /// [ConfigurationProperty("preserveExifMetaData", IsRequired = false, DefaultValue = false)] public bool PreserveExifMetaData { get { return (bool)this["preserveExifMetaData"]; } set { this["preserveExifMetaData"] = value; } } /// /// Gets the . /// /// /// The . /// [ConfigurationProperty("presets", IsRequired = true)] public PresetElementCollection Presets { get { return this["presets"] as PresetElementCollection; } } /// /// 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; } string section = ResourceHelpers.ResourceAsString("ImageProcessor.Web.Configuration.Resources.processing.config"); XmlReader reader = new XmlTextReader(new StringReader(section)); imageProcessingSection = new ImageProcessingSection(); imageProcessingSection.DeserializeSection(reader); return imageProcessingSection; } #endregion /// /// Represents a PresetElement configuration element within the configuration. /// public class PresetElement : ConfigurationElement { /// /// Gets or sets the name of the preset. /// /// The name of the plugin. [ConfigurationProperty("name", DefaultValue = "", IsRequired = true)] public string Name { get { return (string)this["name"]; } set { this["name"] = value; } } /// /// Gets or sets the value of the preset. /// /// The full Type definition of the plugin [ConfigurationProperty("value", DefaultValue = "", IsRequired = true)] public string Value { get { return (string)this["value"]; } set { this["value"] = value; } } } /// /// Represents a PresetElementCollection collection configuration element within the configuration. /// public class PresetElementCollection : 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 "preset"; } } /// /// 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 PresetElement this[int index] { get { return (PresetElement)BaseGet(index); } set { if (this.BaseGet(index) != null) { this.BaseRemoveAt(index); } this.BaseAdd(index, value); } } /// /// Creates a new Preset configuration element. /// /// /// A new PluginConfig configuration element. /// protected override ConfigurationElement CreateNewElement() { return new PresetElement(); } /// /// 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 ((PresetElement)element).Name; } } /// /// 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 or sets the type of the plugin file. /// /// The full Type definition of the plugin [ConfigurationProperty("type", DefaultValue = "", IsRequired = true)] public string Type { get { return (string)this["type"]; } set { this["type"] = value; } } /// /// Gets the . /// /// /// The . /// [ConfigurationProperty("settings", IsRequired = false)] public SettingElementCollection Settings { get { return this["settings"] as SettingElementCollection; } } } /// /// Represents a PluginElementCollection collection configuration element within the configuration. /// public class PluginElementCollection : ConfigurationElementCollection { /// /// Gets or sets a value indicating whether to auto load all plugins. /// Defaults to True. /// /// If True plugins are auto discovered and loaded from all assemblies otherwise they must be defined in the configuration file [ConfigurationProperty("autoLoadPlugins", DefaultValue = true, IsRequired = false)] public bool AutoLoadPlugins { get { return (bool)this["autoLoadPlugins"]; } set { this["autoLoadPlugins"] = value; } } /// /// 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 Plugin configuration element. /// /// /// A new Plugin 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(); } } } }