// -------------------------------------------------------------------------------------------------------------------- // // 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 { using System.Configuration; using System.IO; using System.Linq; using System.Xml; using ImageProcessor.Web.Helpers; /// /// 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; } } /// /// Gets or sets a value indicating whether to auto load plugins. /// public bool AutoLoadPlugins { get; set; } #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) { imageProcessingSection.AutoLoadPlugins = false; 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); imageProcessingSection.AutoLoadPlugins = true; 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 /// 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 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 /// 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; } } } }