//------------------------------------------------------------------------------
// <autogenerated>
//     This code was generated by a tool.
//     Runtime Version: 1.1.4322.573
//
//     Changes to this file may cause incorrect behavior and will be lost if 
//     the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------

namespace ConfigBuilderExample
{
   using System;
   using System.Configuration;
   using System.Xml;
   
   
   /// <summary>
   /// Handles the 'DatabaseSettings' custom configuration section.
   /// </summary>
   public sealed class DatabaseSettingsSectionHandler : object, IConfigurationSectionHandler
   {
      
      /// <summary>
      /// The name of the configuration section handled by this class.
      /// </summary>
      public const string SectionName = "DatabaseSettings";
      
      /// <summary>
      /// Gets the object containing configuration values for the 'DatabaseSettings' configuration section.
      /// </summary>
      public static DatabaseSettingsConfig Config
      {
         get
         {
            object oConfig = ConfigurationSettings.GetConfig(DatabaseSettingsSectionHandler.SectionName);
            if ((oConfig == null))
            {
               throw new System.Configuration.ConfigurationException("The application configuration file must have the \'DatabaseSettings\' configuration" +
" section defined.");
            }
            return ((DatabaseSettingsConfig)(oConfig));
         }
      }
      
      /// <summary>
      /// See <see cref="System.Configuration.IConfigurationSectionHandler.Create"/>.
      /// </summary>
      object IConfigurationSectionHandler.Create(object parentConfig, object webContext, XmlNode sectionNode)
      {
         XmlElement sectionElement = ((XmlElement)(sectionNode));
         return DatabaseSettingsConfig.Create(sectionElement);
      }
      
      /// <summary>
      /// Returns a typed value that represents the value of the specified attribute, or the default value if no attribute is present.
      /// </summary>
      internal static object GetValue(XmlElement element, string attributeName, Type attributeType, string defaultVal)
      {
         try
         {
            string attrVal = element.GetAttribute(attributeName);
            if ((attrVal.Length == 0))
            {
               if ((defaultVal == null))
               {
                  throw new System.Configuration.ConfigurationException(String.Format("Missing value for required attribute \'{0}\' of element \'{1}\'.", attributeName, element.Name));
               }
               attrVal = defaultVal;
            }
            if (attributeType.IsEnum)
            {
               return Enum.Parse(attributeType, attrVal);
            }
            return Convert.ChangeType(attrVal, attributeType, System.Threading.Thread.CurrentThread.CurrentCulture);
         }
         catch (System.Configuration.ConfigurationException )
         {
            throw;
         }
         catch (System.Exception ex)
         {
            throw new System.Configuration.ConfigurationException(String.Format("Failed to extract value for attribute \'{0}\' from element \'{1}\'.", attributeName, element.Name), ex);
         }
      }
   }
   
   /// <summary>
   /// Represents data from a 'DatabaseSettings' element in the 'DatabaseSettings' configuration section.
   /// </summary>
   public class DatabaseSettingsConfig
   {
      
      private String _server;
      
      private String _username;
      
      private int  _timeout;
      
      /// <summary>
      /// Initializes a new DatabaseSettingsConfig instance.
      /// </summary>
      public DatabaseSettingsConfig(String server, String username, int  timeout)
      {
         this._server = server;
         this._username = username;
         this._timeout = timeout;
      }
      
      /// <summary/>
      public String Server
      {
         get
         {
            return this._server;
         }
      }
      
      /// <summary/>
      public String Username
      {
         get
         {
            return this._username;
         }
      }
      
      /// <summary/>
      public int  Timeout
      {
         get
         {
            return this._timeout;
         }
      }
      
      /// <summary>
      /// Creates a new DatabaseSettingsConfig instance based on the specified element from the application configuration file.
      /// </summary>
      internal static DatabaseSettingsConfig Create(XmlElement configElement)
      {
         String server = ((String)(DatabaseSettingsSectionHandler.GetValue(configElement, "server", typeof(String), null)));
         String username = ((String)(DatabaseSettingsSectionHandler.GetValue(configElement, "username", typeof(String), null)));
         int  timeout = ((int )(DatabaseSettingsSectionHandler.GetValue(configElement, "timeout", typeof(int ), " 60")));
         return new DatabaseSettingsConfig(server, username, timeout);
      }
   }
}
