context: ///////////////////////////////////////////////////////////////////////////////////////
  // Copyright (C) 2006-2019 Esper Team. All rights reserved.                           /
  // http://esper.codehaus.org                                                          /
  // ---------------------------------------------------------------------------------- /
  // The software in this package is published under the terms of the GPL license       /
  // a copy of which has been included with this distribution in the license.txt file.  /
  ///////////////////////////////////////////////////////////////////////////////////////
  
  using System;
  using System.Collections.Generic;
  using System.Data;
  using System.Linq;
  using System.Xml;
  using System.Xml.XPath;
  
  using com.espertech.esper.common.client.util;
  using com.espertech.esper.common.@internal.util;
  using com.espertech.esper.compat;
  using com.espertech.esper.compat.collections;
  
  using static com.espertech.esper.common.@internal.util.DOMExtensions;
  
  namespace com.espertech.esper.common.client.configuration.common
  {
      /// <summary>
      /// Parser for the common section of configuration.
      /// </summary>
      public class ConfigurationCommonParser
      {
          /// <summary>
          /// Configure the common section from a provided element
          /// </summary>
          /// <param name="common">common section</param>
          /// <param name="commonElement">element</param>
          public static void DoConfigure(
              ConfigurationCommon common,
              XmlElement commonElement)
          {
              var eventTypeNodeEnumerator = DOMElementEnumerator.Create(commonElement.ChildNodes);
              while (eventTypeNodeEnumerator.MoveNext()) {
                  var element = eventTypeNodeEnumerator.Current;
                  var nodeName = element.Name;
                  switch (nodeName) {
                      case "event-type":
                          HandleEventTypes(common, element);
                          break;
  
                      case "auto-import":
                          HandleAutoImports(common, element);
                          break;
  
                      case "auto-import-annotations":
                          HandleAutoImportAnnotations(common, element);
                          break;
  
                      case "method-reference":
                          HandleMethodReference(common, element);
                          break;
  
                      case "database-reference":
                          HandleDatabaseRefs(common, element);
                          break;
  
                      case "variable":
                          HandleVariable(common, element);
                          break;
  
                      case "variant-stream":
                          HandleVariantStream(common, element);
                          break;
  
                      case "event-meta":
                          HandleEventMeta(common, element);
                          break;
  
                      case "logging":
                          HandleLogging(common, element);
                          break;
  
                      case "scripting":
                          HandleScripting(common, element);
                          break;
  
                      case "time-source":
                          HandleTimeSource(common, element);
                          break;
  
                      case "execution":
                          HandleExecution(common, element);
                          break;
  
                      case "event-type-auto-name":
                          HandleEventTypeAutoNames(common, element);
                          break;
                  }
              }
          }
  
          private static void HandleEventTypeAutoNames(
              ConfigurationCommon configuration,
              XmlElement element)
          {
              var name = GetRequiredAttribute(element, "package-name");
              configuration.AddEventTypeAutoName(name);
          }
  
          private static void HandleExecution(
              ConfigurationCommon common,
              XmlElement parentElement)
          {
              var threadingProfileStr = GetOptionalAttribute(parentElement, "threading-profile");
              if (threadingProfileStr != null) {
                  var profile = EnumHelper.Parse<ThreadingProfile>(threadingProfileStr);
                  common.Execution.ThreadingProfile = profile;
              }
          }
  
          private static void HandleTimeSource(
              ConfigurationCommon common,
              XmlElement element)
          {
              var nodeEnumerator = DOMElementEnumerator.Create(element.ChildNodes);
              while (nodeEnumerator.MoveNext()) {
                  var subElement = nodeEnumerator.Current;
                  switch (subElement.Name) {
                      case "time-unit":
                          var valueText = GetRequiredAttribute(subElement, "value");
                          if (valueText == null) {
                              throw new ConfigurationException("No value attribute supplied for time-unit element");
                          }
  
                          try {
                              var timeUnit = EnumHelper.Parse<TimeUnit>(valueText);
                              common.TimeSource.TimeUnit = timeUnit;
                          }
                          catch (EPException) {
                              throw;
                          }
                          catch (Exception e) {
                              throw new ConfigurationException(
                                  "Value attribute for time-unit element invalid: " + e.Message,
                                  e);
                          }
  
                          break;
                  }
              }
          }
  
          private static void HandleLogging(
              ConfigurationCommon common,
              XmlElement element)
          {
              var nodeEnumerator = DOMElementEnumerator.Create(element.ChildNodes);
              while (nodeEnumerator.MoveNext()) {
                  var subElement = nodeEnumerator.Current;
                  switch (subElement.Name) {
                      case "query-plan": {
                          var valueText = GetRequiredAttribute(subElement, "enabled");
                          var value = bool.Parse(valueText);
                          common.Logging.IsEnableQueryPlan = value;
                          break;
                      }
  
                      case "jdbc": {
                          var valueText = GetRequiredAttribute(subElement, "enabled");
                          var value = bool.Parse(valueText);
                          common.Logging.IsEnableADO = value;
                          break;
                      }
                  }
              }
          }
          
          private static void HandleScripting(
              ConfigurationCommon common,
              XmlElement element)
          {
              var nodeEnumerator = DOMElementEnumerator.Create(element.ChildNodes);
              while (nodeEnumerator.MoveNext()) {
                  var subElement = nodeEnumerator.Current;
                  if (subElement.Name == "engine") {
                      var name = subElement.Attributes.GetNamedItem("type").InnerText;
                      common.Scripting.AddEngine(name);
                  }
              }
          }
  
          private static void HandleVariantStream(
              ConfigurationCommon configuration,
              XmlElement element)
          {
              var variantStream = new ConfigurationCommonVariantStream();
              var varianceName = GetRequiredAttribute(element, "name");
  
              if (element.Attributes.GetNamedItem("type-variance") != null) {
                  var typeVar = element.Attributes.GetNamedItem("type-variance").InnerText;
                  TypeVariance typeVarianceEnum;
                  try {
                      typeVarianceEnum = EnumHelper.Parse<TypeVariance>(typeVar.Trim());
                      variantStream.TypeVariance = typeVarianceEnum;
                  }
                  catch (EPException) {
                      throw;
                  }
                  catch (Exception) {
                      throw new ConfigurationException(
                          "Invalid enumeration value for type-variance attribute '" + typeVar + "'");
                  }
              }
  
              var nodeEnumerator = DOMElementEnumerator.Create(element.ChildNodes);
              while (nodeEnumerator.MoveNext()) {
                  var subElement = nodeEnumerator.Current;
                  if (subElement.Name == "variant-event-type") {
                      var name = subElement.Attributes.GetNamedItem("name").InnerText;
                      variantStream.AddEventTypeName(name);
                  }
              }
  
              configuration.AddVariantStream(varianceName, variantStream);
          }
  
          private static void HandleDatabaseRefs(
              ConfigurationCommon configuration,
              XmlElement element)
          {
              var name = GetRequiredAttribute(element, "name");
              var configDBRef = new ConfigurationCommonDBRef();
              configuration.AddDatabaseReference(name, configDBRef);
  
              var nodeEnumerator = DOMElementEnumerator.Create(element.ChildNodes);
              while (nodeEnumerator.MoveNext()) {
                  var subElement = nodeEnumerator.Current;
                  switch (subElement.Name) {
                      case "datasource-connection": {
                          var lookup = GetRequiredAttribute(subElement, "context-lookup-name");
                          var properties = DOMUtil.GetProperties(subElement, "env-property");
                          configDBRef.SetDataSourceConnection(lookup, properties);
                          break;
                      }
  
                      case "datasourcefactory-connection": {
                          var className = GetRequiredAttribute(subElement, "class-name");
                          var properties = DOMUtil.GetProperties(subElement, "env-property");
                          configDBRef.SetDataSourceFactory(properties, className);
                          break;
                      }
  
                      case "driver": {
                          var driverType = GetRequiredAttribute(subElement, "type");
                          var connectionString = GetRequiredAttribute(subElement, "connection-string");
                          var properties = DOMUtil.GetProperties(subElement, "env-property");
                          configDBRef.SetDatabaseDriver(driverType, connectionString, properties);
                          break;
                      }
  
                      case "connection-lifecycle": {
                          var value = GetRequiredAttribute(subElement, "value");
                          configDBRef.ConnectionLifecycleEnum = EnumHelper.Parse<ConnectionLifecycleEnum>(value);
                          break;
                      }
  
                      case "connection-settings":
                          if (subElement.Attributes.GetNamedItem("auto-commit") != null) {
                              var autoCommit = subElement.Attributes.GetNamedItem("auto-commit").InnerText;
                              configDBRef.ConnectionAutoCommit = bool.Parse(autoCommit);
                          }
  
                          if (subElement.Attributes.GetNamedItem("transaction-isolation") != null) {
                              var transactionIsolation =
                                  subElement.Attributes.GetNamedItem("transaction-isolation").InnerText;
                              configDBRef.ConnectionTransactionIsolation =
                                  EnumHelper.Parse<IsolationLevel>(transactionIsolation);
                          }
  
  
gt:                         if (subElement.Attributes.GetNamedItem("catalog") != null) {