/// <summary> /// Initializes this config file handler /// </summary> /// <param name="configHanlderDefNode">The BASE.Config file node that defines this handler</param> public void Init(XmlNode configHanlderDefNode) { //Setup section handlers _sectionHandlers = new Dictionary <string, ISectionHandler>(); _extension = configHanlderDefNode.Attributes["extension"].Value; _relativePath = configHanlderDefNode.Attributes["relativePath"].Value; //Loop thru section handlers and load. foreach (XmlNode ch in configHanlderDefNode.ChildNodes) { if (ch.Name != "sectionHandler") { continue; } string section = ch.Attributes["section"].Value; string type = ch.Attributes["type"].Value; //CReate the SectionHandlers defined object handler = TypeHelper.CreateTypeFromConfigString(type); if (handler is ISectionHandler) { //Initia;lize them and add them to the list of section handlers ISectionHandler ihand = (ISectionHandler)handler; ihand.Init(ch); _sectionHandlers.Add(section, ihand); } } }
/// <summary> /// Handles the file. /// </summary> /// <param name="fileToHandle"></param> public void HandleFile(string fileToHandle) { //Check to make sure the files are correct and exist if (!fileToHandle.EndsWith(".def")) { throw new XmlDefinitionParsingException("Invalid file format", fileToHandle); } if (!File.Exists(fileToHandle)) { throw new FileNotFoundException("def file not found", fileToHandle); } //Load the xml document XmlDocument doc = new XmlDocument(); doc.Load(fileToHandle); //Go through each section and call the registered handler foreach (XmlNode node in doc.DocumentElement.ChildNodes) { string sectionName = node.Name; //Make sure this section has a handler installed, if not, log and ignore if (!_sectionHandlers.ContainsKey(sectionName)) { Logging.Logger.Log("Unknown Node[" + sectionName + "] in file: " + fileToHandle, BASE.Logging.LogPriority.Debug); continue; } //Section has a registered handler, so Handle it ISectionHandler sectionHandler = _sectionHandlers[sectionName]; sectionHandler.HandleSection(node, fileToHandle); } }
public override ConfigurationSection ProcessConfigurationSection(ConfigurationSection configSection) { // Expand mode only works on the raw string input if (Mode == KeyValueMode.Expand) { return(configSection); } // See if we know how to process this section ISectionHandler handler = SectionHandlersSection.GetSectionHandler(configSection); if (handler == null) { return(configSection); } if (configSection.SectionInformation?.SectionName == "appSettings") { _appSettings = configSection as AppSettingsSection; } // Strict Mode. Only replace existing key/values. if (Mode == KeyValueMode.Strict) { foreach (var configItem in handler) { // Presumably, UpdateKey will preserve casing appropriately, so newKey is cased as expected. string newKey = UpdateKey(configItem.Key); string newValue = GetValueInternal(configItem.Key); if (newValue != null) { handler.InsertOrUpdate(newKey, newValue, configItem.Key, configItem.Value); } } } // Greedy Mode. Insert all key/values. else if (Mode == KeyValueMode.Greedy) { EnsureGreedyInitialized(); foreach (KeyValuePair <string, string> kvp in _cachedValues) { if (kvp.Value != null) { // Here, kvp.Key is not from the config file, so it might not be correctly cased. Get the correct casing for UpdateKey. string oldKey = TrimPrefix(kvp.Key); string newKey = UpdateKey(handler.TryGetOriginalCase(oldKey)); handler.InsertOrUpdate(newKey, kvp.Value, oldKey); } } } return(configSection); }
public override ConfigurationSection ProcessConfigurationSection(ConfigurationSection configSection) { // Expand mode only works on the raw string input if (Mode == KeyValueMode.Expand) { return(configSection); } // See if we know how to process this section ISectionHandler handler = SectionHandlersSection.GetSectionHandler(configSection); if (handler == null) { return(configSection); } _appSettings = configSection as AppSettingsSection; // Strict Mode. Only replace existing key/values. if (Mode == KeyValueMode.Strict) { foreach (var configItem in handler) { string newValue = GetValueInternal(configItem.Key); string newKey = UpdateKey(configItem.Key); if (newValue != null) { handler.InsertOrUpdate(newKey, newValue, configItem.Key, configItem.Value); } } } // Greedy Mode. Insert all key/values. else if (Mode == KeyValueMode.Greedy) { EnsureGreedyInitialized(); foreach (KeyValuePair <string, string> kvp in _cachedValues) { if (kvp.Value != null) { string oldKey = TrimPrefix(kvp.Key); string newKey = UpdateKey(oldKey); handler.InsertOrUpdate(newKey, kvp.Value, oldKey); } } } return(configSection); }
static internal ISectionHandler GetSectionHandler <T>(T configSection) where T : ConfigurationSection { if (configSection == null) { return(null); } SectionHandlersSection handlerSection = ConfigurationManager.GetSection(handlerSectionName) as SectionHandlersSection; if (handlerSection == null) { handlerSection = new SectionHandlersSection(); handlerSection.InitializeDefault(); } // Look at each handler to see if it works on this section. Reverse order so last match wins. // .IsSubclassOf() requires an exact type match. So SectionHandler<BaseConfigSectionType> won't work. Type sectionHandlerGenericTemplate = typeof(SectionHandler <>); Type sectionHandlerDesiredType = sectionHandlerGenericTemplate.MakeGenericType(configSection.GetType()); for (int i = handlerSection.Handlers.Count; i-- > 0;) { Type handlerType = Type.GetType(handlerSection.Handlers[i].Type); if (handlerType != null && handlerType.IsSubclassOf(sectionHandlerDesiredType)) { ISectionHandler handler = Activator.CreateInstance(handlerType) as ISectionHandler; if (handler != null) { ProviderSettings settings = handlerSection.Handlers[i]; NameValueCollection clonedParams = new NameValueCollection(settings.Parameters.Count); foreach (string key in settings.Parameters) { clonedParams[key] = settings.Parameters[key]; } MethodInfo init = sectionHandlerDesiredType.GetMethod("Initialize", BindingFlags.NonPublic | BindingFlags.Instance); init.Invoke(handler, new object[] { settings.Name, configSection, clonedParams }); return(handler); } } } throw new Exception($"Error in Configuration: Cannot find ISectionHandler for '{configSection.SectionInformation.Name}' section."); }
static internal ISectionHandler GetSectionHandler <T>(T configSection) where T : ConfigurationSection { if (configSection == null) { return(null); } SectionHandlersSection handlerSection = ConfigurationManager.GetSection(handlerSectionName) as SectionHandlersSection; if (handlerSection == null) { handlerSection = new SectionHandlersSection(); handlerSection.InitializeDefault(); } // Look at each handler to see if it works on this section. Reverse order so last match wins. // .IsSubclassOf() requires an exact type match. So SectionHandler<BaseConfigSectionType> won't work. Type sectionHandlerGenericTemplate = typeof(SectionHandler <>); Type sectionHandlerDesiredType = sectionHandlerGenericTemplate.MakeGenericType(configSection.GetType()); for (int i = handlerSection.Handlers.Count; i-- > 0;) { Type handlerType = Type.GetType(handlerSection.Handlers[i].Type); if (handlerType != null && handlerType.IsSubclassOf(sectionHandlerDesiredType)) { ISectionHandler handler = Activator.CreateInstance(handlerType) as ISectionHandler; if (handler != null) { sectionHandlerDesiredType.GetProperty("ConfigSection").SetValue(handler, configSection); } return(handler); } } return(null); }