Example #1
0
        /// <summary>
        /// Parse the given configuration file and stores the data to the config
        /// </summary>
        /// <param name="configFilepath">the path to the config file to read</param>
        public static ConfigurationWrapper readConfiguration(string configFilepath)
        {
            FileStream    configFileStream  = null;
            StreamReader  configFileReader  = null;
            Configuration someConfiguration = new Configuration();

            try {
                ParserMode    mode = ParserMode.Normal;
                string        currentLine;
                int           lineCounter         = 0;
                string        currentPropertyName = null;
                List <string> multipleValues      = new List <string>();

                configFileStream = File.Open(configFilepath, FileMode.OpenOrCreate, FileAccess.Read);
                configFileReader = new StreamReader(configFileStream);

                while ((currentLine = configFileReader.ReadLine()) != null)
                {
                    lineCounter++;

                    currentLine = cleanupLine(currentLine);

                    if (String.IsNullOrEmpty(currentLine) || isComment(currentLine))
                    {
                        continue;
                    }

                    switch (mode)
                    {
                    case ParserMode.MultiValueProperty:
                        if (currentLine.Equals(Configuration.END_MULTI_VALUE_FIELD))
                        {
                            someConfiguration[currentPropertyName] = multipleValues;
                            mode = ParserMode.Normal;
                        }
                        else
                        {
                            multipleValues.Add(currentLine);
                        }
                        continue;

                    case ParserMode.Normal:
                        int keyValueSeparatorIndex =
                            currentLine.IndexOf(Configuration.KEY_VALUE_SEPARATOR, StringComparison.Ordinal);
                        if (keyValueSeparatorIndex <= 0)
                        {
                            logCorruptLine(lineCounter);
                            continue;
                        }
                        currentPropertyName = currentLine.Substring(0, keyValueSeparatorIndex).Trim();
                        string valuePart = currentLine.Substring(keyValueSeparatorIndex + 1).Trim();

                        if (String.IsNullOrEmpty(valuePart))
                        {
                            log.Debug("Property " + currentPropertyName + " is not set");
                            continue;
                        }
                        if (valuePart.Equals(Configuration.BEGIN_MULTI_VALUE_PROPERTY))
                        {
                            mode = ParserMode.MultiValueProperty;
                            multipleValues.Clear();
                        }
                        else
                        {
                            someConfiguration[currentPropertyName] = valuePart;
                        }
                        break;

                    default:
                        continue;
                    }
                }
            } catch (Exception ex) {
                AppConfigurationWrapper appConfiguration = AppDefaults.getDefaultConfiguration();
                writeConfiguration(appConfiguration, configFilepath);
                log.Error("Couldn't process config file " + configFilepath + ":" + ex.Message);
                log.Warn("Generating configuration file using default values.");
            } finally {
                configFileStream?.Close();
                configFileReader?.Close();
            }
            return(new ConfigurationWrapper(someConfiguration));
        }
 private void resetToDefaults()
 {
     readConfiguration(AppDefaults.getDefaultConfiguration());
 }