示例#1
0
        public static Boolean TryParse(String path, out Configuration aConfiguration)
        {
            Errors = new List <String>();
            ActualIntersectingKeys    = new Boolean[26];
            ActualNonIntersectingKeys = new Boolean[26];
            ActualKeys     = new List <string>();
            aConfiguration = new Configuration(path);

            if (aConfiguration.ConfigurationFileName.IndexOfAny(Path.GetInvalidFileNameChars()) > -1)
            {
                Errors.Add(String.Format(ConfigurationErrors.FilenameError, path));
            }
            else
            {
                StreamReader fileIn = new StreamReader(path);

                // Validate file.
                while (!fileIn.EndOfStream)
                {
                    // Read a line.
                    String line = fileIn.ReadLine();

                    // Parse a configuration item.
                    ConfigurationFileItem aConfigurationItem;
                    if (ConfigurationFileItem.TryParse(line, out aConfigurationItem))
                    {
                        if (aConfigurationItem.KeyValue != null && ActualKeys.Contains(aConfigurationItem.KeyValue.Key))
                        {
                            Errors.Add(String.Format(ConfigurationErrors.DuplicateKeyError, aConfigurationItem.KeyValue.OriginalKeyValue));
                        }
                        else
                        {
                            // Record that this key has been found.
                            if (aConfigurationItem.KeyValue != null)
                            {
                                ActualKeys.Add(aConfigurationItem.KeyValue.Key);
                            }

                            // Process the key-value.
                            if (aConfigurationItem.IsLogFile)
                            {
                                // Get the value representing an invalid score.
                                aConfiguration.LogFileName = aConfigurationItem.KeyValue.Value.Trim();
                                if (Validator.IsDelimited(aConfiguration.LogFileName, Crozzle.StringDelimiters))
                                {
                                    aConfiguration.LogFileName = aConfiguration.LogFileName.Trim(Crozzle.StringDelimiters);
                                    if (!Validator.IsFilename(aConfiguration.LogFileName))
                                    {
                                        Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                    }
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsMinimumNumberOfUniqueWords)
                            {
                                // Get the value of the minimum number of unique words allowed in the wordlist.
                                int minimum;
                                if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out minimum))
                                {
                                    aConfiguration.MinimumNumberOfUniqueWords = minimum;
                                    if (!Validator.TryRange(minimum, 1, Int32.MaxValue))
                                    {
                                        Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                    }
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsMaximumNumberOfUniqueWords)
                            {
                                // Get the value of the maximum number of unique words allowed in the wordlist.
                                int maximum;
                                if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out maximum))
                                {
                                    aConfiguration.MaximumNumberOfUniqueWords = maximum;
                                    if (!Validator.TryRange(maximum, 1, Int32.MaxValue))
                                    {
                                        Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                    }
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsInvalidCrozzleScore)
                            {
                                // Get the value representing an invalid score.
                                aConfiguration.InvalidCrozzleScore = aConfigurationItem.KeyValue.Value.Trim();
                                if (Validator.IsDelimited(aConfiguration.InvalidCrozzleScore, Crozzle.StringDelimiters))
                                {
                                    aConfiguration.InvalidCrozzleScore = aConfiguration.InvalidCrozzleScore.Trim(Crozzle.StringDelimiters);
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsUppercase)
                            {
                                // Get the Boolean value that determines whether to display the crozzle letters in uppercase or lowercase.
                                Boolean uppercase = true;
                                if (!Validator.IsMatch(aConfigurationItem.KeyValue.Value, allowedBooleans))
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                                aConfiguration.Uppercase = uppercase;
                            }
                            else if (aConfigurationItem.IsStyle)
                            {
                                // Get the value of the HTML style to display the crozzle in an HTML table.
                                aConfiguration.Style = aConfigurationItem.KeyValue.Value.Trim();
                                if (Validator.IsDelimited(aConfiguration.Style, Crozzle.StringDelimiters))
                                {
                                    aConfiguration.Style = aConfiguration.Style.Trim(Crozzle.StringDelimiters);
                                    if (!Validator.IsStyleTag(aConfiguration.Style))
                                    {
                                        Errors.Add(String.Format(ConfigurationErrors.StyleError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                    }
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsBGcolourEmptyTD)
                            {
                                // Get the value of the background colour for an empty TD (HTML table data).
                                aConfiguration.BGcolourEmptyTD = aConfigurationItem.KeyValue.Value.Trim();
                                if (!Validator.IsHexColourCode(aConfiguration.BGcolourEmptyTD))
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ColourError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsBGcolourNonEmptyTD)
                            {
                                // Get the value of the background colour for a non empty TD (HTML table data).
                                aConfiguration.BGcolourNonEmptyTD = aConfigurationItem.KeyValue.Value.Trim();
                                if (!Validator.IsHexColourCode(aConfiguration.BGcolourNonEmptyTD))
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ColourError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsMinimumNumberOfRows)
                            {
                                // Get the value of the minimum number of rows per crozzle.
                                int minimum;
                                if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out minimum))
                                {
                                    aConfiguration.MinimumNumberOfRows = minimum;
                                    if (!Validator.TryRange(minimum, 1, Int32.MaxValue))
                                    {
                                        Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                    }
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsMaximumNumberOfRows)
                            {
                                // Get the value of the maximum number of rows per crozzle.
                                int maximum;
                                if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out maximum))
                                {
                                    aConfiguration.MaximumNumberOfRows = maximum;
                                    if (!Validator.TryRange(maximum, 1, Int32.MaxValue))
                                    {
                                        Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                    }
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsMinimumNumberOfColumns)
                            {
                                // Get the value of the minimum number of columns per crozzle.
                                int minimum;
                                if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out minimum))
                                {
                                    aConfiguration.MinimumNumberOfColumns = minimum;
                                    if (!Validator.TryRange(minimum, 1, Int32.MaxValue))
                                    {
                                        Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                    }
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsMaximumNumberOfColumns)
                            {
                                // Get the value of the maximum number of columns per crozzle.
                                int maximum;
                                if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out maximum))
                                {
                                    aConfiguration.MaximumNumberOfColumns = maximum;
                                    if (!Validator.TryRange(maximum, 1, Int32.MaxValue))
                                    {
                                        Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                    }
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsMinimumHorizontalWords)
                            {
                                // Get the value of the minimum number of horizontal words in a crozzle.
                                int minimum;
                                if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out minimum))
                                {
                                    aConfiguration.MinimumHorizontalWords = minimum;
                                    if (!Validator.TryRange(minimum, 1, Int32.MaxValue))
                                    {
                                        Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                    }
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsMaximumHorizontalWords)
                            {
                                // Get the value of the maximum number of horizontal words in a crozzle.
                                int maximum;
                                if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out maximum))
                                {
                                    aConfiguration.MaximumHorizontalWords = maximum;
                                    if (!Validator.TryRange(maximum, 1, Int32.MaxValue))
                                    {
                                        Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                    }
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsMinimumVerticalWords)
                            {
                                // Get the value of the minimum number of vertical words in a crozzle.
                                int minimum;
                                if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out minimum))
                                {
                                    aConfiguration.MinimumVerticalWords = minimum;
                                    if (!Validator.TryRange(minimum, 1, Int32.MaxValue))
                                    {
                                        Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                    }
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsMaximumVerticalWords)
                            {
                                // Get the value of the maximum number of vertical words in a crozzle.
                                int maximum;
                                if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out maximum))
                                {
                                    aConfiguration.MaximumVerticalWords = maximum;
                                    if (!Validator.TryRange(maximum, 1, Int32.MaxValue))
                                    {
                                        Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                    }
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsMinimumIntersectionsInHorizontalWords)
                            {
                                // Get the value of the minimum number of the intersections in a horizontal word.
                                int minimum;
                                if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out minimum))
                                {
                                    aConfiguration.MinimumIntersectionsInHorizontalWords = minimum;
                                    if (!Validator.TryRange(minimum, 1, Int32.MaxValue))
                                    {
                                        Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                    }
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsMaximumIntersectionsInHorizontalWords)
                            {
                                // Get the value of the maximum number of the intersections in a horizontal word.
                                int maximum;
                                if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out maximum))
                                {
                                    aConfiguration.MaximumIntersectionsInHorizontalWords = maximum;
                                    if (!Validator.TryRange(maximum, 1, Int32.MaxValue))
                                    {
                                        Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                    }
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsMinimumIntersectionsInVerticalWords)
                            {
                                // Get the value of the minimum number of the intersections in a vertical word.
                                int minimum;
                                if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out minimum))
                                {
                                    aConfiguration.MinimumIntersectionsInVerticalWords = minimum;
                                    if (!Validator.TryRange(minimum, 1, Int32.MaxValue))
                                    {
                                        Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                    }
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsMaximumIntersectionsInVerticalWords)
                            {
                                // Get the value of the maximum number of the intersections in a vertical word.
                                int maximum;
                                if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out maximum))
                                {
                                    aConfiguration.MaximumIntersectionsInVerticalWords = maximum;
                                    if (!Validator.TryRange(maximum, 1, Int32.MaxValue))
                                    {
                                        Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                    }
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsMinimumNumberOfTheSameWord)
                            {
                                // Get the value of the minimum number of the same word per crozzle limit.
                                int minimum;
                                if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out minimum))
                                {
                                    aConfiguration.MinimumNumberOfTheSameWord = minimum;
                                    if (!Validator.TryRange(minimum, 1, Int32.MaxValue))
                                    {
                                        Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                    }
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsMaximumNumberOfTheSameWord)
                            {
                                // Get the value of the maximum number of the same word per crozzle limit.
                                int maximum;
                                if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out maximum))
                                {
                                    aConfiguration.MaximumNumberOfTheSameWord = maximum;
                                    if (!Validator.TryRange(maximum, 1, Int32.MaxValue))
                                    {
                                        Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                    }
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsMinimumNumberOfGroups)
                            {
                                // Get the value of the minimum number of groups per crozzle limit.
                                int minimum;
                                if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out minimum))
                                {
                                    aConfiguration.MinimumNumberOfGroups = minimum;
                                    if (!Validator.TryRange(minimum, 1, Int32.MaxValue))
                                    {
                                        Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                    }
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsMaximumNumberOfGroups)
                            {
                                // Get the value of the maximum number of groups per crozzle limit.
                                int maximum;
                                if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out maximum))
                                {
                                    aConfiguration.MaximumNumberOfGroups = maximum;
                                    if (!Validator.TryRange(maximum, 1, Int32.MaxValue))
                                    {
                                        Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                    }
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsPointsPerWord)
                            {
                                // Get the value of points per words.
                                int pointsPerWord;
                                if (Validator.IsInt32(aConfigurationItem.KeyValue.Value.Trim(), out pointsPerWord))
                                {
                                    aConfiguration.PointsPerWord = pointsPerWord;
                                    if (!Validator.TryRange(pointsPerWord, 0, Int32.MaxValue))
                                    {
                                        Errors.Add(String.Format(ConfigurationErrors.IntegerError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                    }
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, aConfigurationItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                }
                            }
                            else if (aConfigurationItem.IsIntersecting)
                            {
                                // Get the values of each INTERSECTING point.
                                String originalValues = aConfigurationItem.KeyValue.Value.Trim();
                                if (Validator.IsDelimited(originalValues, Crozzle.StringDelimiters))
                                {
                                    originalValues = originalValues.Trim(Crozzle.StringDelimiters).Trim();
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, originalValues, Validator.Errors[0]));
                                }

                                String[] intersectingPoints = originalValues.Split(PointSeparators);
                                foreach (String intersectingPoint in intersectingPoints)
                                {
                                    KeyValue aKeyValue;
                                    if (KeyValue.TryParse(intersectingPoint, @"[A-Z]", out aKeyValue))
                                    {
                                        int points;
                                        if (Validator.IsInt32(aKeyValue.Value, out points))
                                        {
                                            int index = (int)aKeyValue.Key[0] - (int)'A';
                                            aConfiguration.IntersectingPointsPerLetter[index] = points;
                                            ActualIntersectingKeys[index] = true;
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aKeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                    else
                                    {
                                        Errors.AddRange(KeyValue.Errors);
                                    }
                                }
                            }
                            else if (aConfigurationItem.IsNonIntersecting)
                            {
                                // Get the value of each NONINTERSECTING point.
                                String originalValues = aConfigurationItem.KeyValue.Value.Trim();
                                if (Validator.IsDelimited(originalValues, Crozzle.StringDelimiters))
                                {
                                    originalValues = originalValues.Trim(Crozzle.StringDelimiters).Trim();
                                }
                                else
                                {
                                    Errors.Add(String.Format(ConfigurationErrors.ValueError, originalValues, Validator.Errors[0]));
                                }

                                String[] nonIntersectingPoints = originalValues.Split(PointSeparators);
                                foreach (String nonIntersectingPoint in nonIntersectingPoints)
                                {
                                    KeyValue aKeyValue;
                                    if (KeyValue.TryParse(nonIntersectingPoint, @"[A-Z]", out aKeyValue))
                                    {
                                        int points;
                                        if (Validator.IsInt32(aKeyValue.Value, out points))
                                        {
                                            int index = (int)aKeyValue.Key[0] - (int)'A';
                                            aConfiguration.NonIntersectingPointsPerLetter[index] = points;
                                            ActualNonIntersectingKeys[index] = true;
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aKeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                    else
                                    {
                                        Errors.AddRange(KeyValue.Errors);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        Errors.AddRange(ConfigurationFileItem.Errors);
                    }
                }

                // Close files.
                fileIn.Close();

                // Check which keys are missing from the configuration file.
                foreach (string expectedKey in ExpectedKeys)
                {
                    if (!ActualKeys.Contains(expectedKey))
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MissingKeyError, expectedKey));
                    }
                }
                for (char ch = 'A'; ch <= 'Z'; ch++)
                {
                    if (!ActualIntersectingKeys[(int)ch - (int)'A'])
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MissingIntersectionKeyError, ch.ToString()));
                    }
                }
                for (char ch = 'A'; ch <= 'Z'; ch++)
                {
                    if (!ActualNonIntersectingKeys[(int)ch - (int)'A'])
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MissingNonIntersectionKeyError, ch.ToString()));
                    }
                }

                // Check that minimum values are <= to their maximmum counterpart values.
                if (ActualKeys.Contains("MINIMUM_NUMBER_OF_UNIQUE_WORDS") && ActualKeys.Contains("MAXIMUM_NUMBER_OF_UNIQUE_WORDS"))
                {
                    if (aConfiguration.MinimumNumberOfUniqueWords > aConfiguration.MaximumNumberOfUniqueWords)
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "MINIMUM_NUMBER_OF_UNIQUE_WORDS",
                                                 aConfiguration.MinimumNumberOfUniqueWords, aConfiguration.MaximumNumberOfUniqueWords));
                    }
                }
                if (ActualKeys.Contains("MINIMUM_NUMBER_OF_ROWS") && ActualKeys.Contains("MAXIMUM_NUMBER_OF_ROWS"))
                {
                    if (aConfiguration.MinimumNumberOfRows > aConfiguration.MaximumNumberOfRows)
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "MINIMUM_NUMBER_OF_ROWS",
                                                 aConfiguration.MinimumNumberOfRows, aConfiguration.MaximumNumberOfRows));
                    }
                }
                if (ActualKeys.Contains("MINIMUM_NUMBER_OF_COLUMNS") && ActualKeys.Contains("MAXIMUM_NUMBER_OF_COLUMNS"))
                {
                    if (aConfiguration.MinimumNumberOfColumns > aConfiguration.MaximumNumberOfColumns)
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "MINIMUM_NUMBER_OF_COLUMNS",
                                                 aConfiguration.MinimumNumberOfColumns, aConfiguration.MaximumNumberOfColumns));
                    }
                }
                if (ActualKeys.Contains("MINIMUM_HORIZONTAL_WORDS") && ActualKeys.Contains("MAXIMUM_HORIZONTAL_WORDS"))
                {
                    if (aConfiguration.MinimumHorizontalWords > aConfiguration.MaximumHorizontalWords)
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "MINIMUM_HORIZONTAL_WORDS",
                                                 aConfiguration.MinimumHorizontalWords, aConfiguration.MaximumHorizontalWords));
                    }
                }
                if (ActualKeys.Contains("MINIMUM_VERTICAL_WORDS") && ActualKeys.Contains("MAXIMUM_VERTICAL_WORDS"))
                {
                    if (aConfiguration.MinimumVerticalWords > aConfiguration.MaximumVerticalWords)
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "MINIMUM_VERTICAL_WORDS",
                                                 aConfiguration.MinimumVerticalWords, aConfiguration.MaximumVerticalWords));
                    }
                }
                if (ActualKeys.Contains("MINIMUM_INTERSECTIONS_IN_HORIZONTAL_WORDS") && ActualKeys.Contains("MAXIMUM_INTERSECTIONS_IN_HORIZONTAL_WORDS"))
                {
                    if (aConfiguration.MinimumIntersectionsInHorizontalWords > aConfiguration.MaximumIntersectionsInHorizontalWords)
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "MINIMUM_INTERSECTIONS_IN_HORIZONTAL_WORDS",
                                                 aConfiguration.MinimumIntersectionsInHorizontalWords, aConfiguration.MaximumIntersectionsInHorizontalWords));
                    }
                }
                if (ActualKeys.Contains("MAXIMUM_INTERSECTIONS_IN_VERTICAL_WORDS") && ActualKeys.Contains("MAXIMUM_INTERSECTIONS_IN_VERTICAL_WORDS"))
                {
                    if (aConfiguration.MinimumIntersectionsInVerticalWords > aConfiguration.MaximumIntersectionsInVerticalWords)
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "MAXIMUM_INTERSECTIONS_IN_VERTICAL_WORDS",
                                                 aConfiguration.MinimumIntersectionsInVerticalWords, aConfiguration.MaximumIntersectionsInVerticalWords));
                    }
                }
                if (ActualKeys.Contains("MINIMUM_NUMBER_OF_THE_SAME_WORD") && ActualKeys.Contains("MAXIMUM_NUMBER_OF_THE_SAME_WORD"))
                {
                    if (aConfiguration.MinimumNumberOfTheSameWord > aConfiguration.MaximumNumberOfTheSameWord)
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "MINIMUM_NUMBER_OF_THE_SAME_WORD",
                                                 aConfiguration.MinimumNumberOfTheSameWord, aConfiguration.MaximumNumberOfTheSameWord));
                    }
                }
                if (ActualKeys.Contains("MINIMUM_NUMBER_OF_GROUPS") && ActualKeys.Contains("MAXIMUM_NUMBER_OF_GROUPS"))
                {
                    if (aConfiguration.MinimumNumberOfGroups > aConfiguration.MaximumNumberOfGroups)
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "MINIMUM_NUMBER_OF_GROUPS",
                                                 aConfiguration.MinimumNumberOfGroups, aConfiguration.MaximumNumberOfGroups));
                    }
                }
            }

            // Store validity.
            aConfiguration.Valid = Errors.Count == 0;
            return(aConfiguration.Valid);
        }
示例#2
0
        public static Boolean TryParse(String path, out Configuration aConfiguration)
        {
            Errors = new List <String>();
            ActualIntersectingKeys    = new Boolean[26];
            ActualNonIntersectingKeys = new Boolean[26];
            ActualKeys     = new List <string>();
            aConfiguration = new Configuration(path);

            if (aConfiguration.ConfigurationFileName.IndexOfAny(Path.GetInvalidFileNameChars()) > -1)
            {
                Errors.Add(String.Format(ConfigurationErrors.FilenameError, path));
            }
            else
            {
                StreamReader fileIn = new StreamReader(path);

                // Validate file.
                while (!fileIn.EndOfStream)
                {
                    List <String> fragment = new List <String>();
                    do
                    {
                        /// Add multiple lines as part of a text fragment until the last element is empty or null.
                        fragment.Add(fileIn.ReadLine());
                    } while (!String.IsNullOrEmpty(fragment.Last()));

                    // Parse a configuration fragment.
                    FileFragment <ConfigurationFileItem> aConfigurationFileFragment;
                    if (ConfigurationFileItem.TryParse(fragment, out aConfigurationFileFragment))
                    {
                        List <ConfigurationFileItem> aItemsWithErrors;
                        if ((aItemsWithErrors = aConfigurationFileFragment.Items.Where(item => item.KeyValue != null && ActualKeys.Contains(item.KeyValue.Key)).ToList()).Any())
                        {
                            foreach (ConfigurationFileItem error in aItemsWithErrors)
                            {
                                Errors.Add(String.Format(ConfigurationErrors.DuplicateKeyError, error.KeyValue.OriginalKeyValue));
                            }
                        }
                        else
                        {
                            /// TODO: Process key record and key-value validation.
                            String aFragmentKey = aConfigurationFileFragment.Name;

                            foreach (ConfigurationFileItem aItem in aConfigurationFileFragment.Items)
                            {
                                /// Record that this key has been found.
                                if (aItem.KeyValue.Key != null)
                                {
                                    /// Identified fragments contain keys that have the same string. To minimise duplicate key error, lead the key with the fragment's name.
                                    if (new string[] { ConfigurationKeys.UNIQUESEQ_OPENBRACKET, ConfigurationKeys.DUPE_OPENBRACKET,
                                                       ConfigurationKeys.VALIDGROUP_OPENBRACKET, ConfigurationKeys.DIRECTIONALSEQ_OPENBRACKET,
                                                       ConfigurationKeys.DIRECTIONALINTERSECT_OPENBRACKET }.Contains(aFragmentKey))
                                    {
                                        ActualKeys.Add(aFragmentKey + "-" + aItem.KeyValue.Key);
                                    }
                                    else if (!(new string[] { ConfigurationKeys.INTERSECT_OPENBRACKET, ConfigurationKeys.NONINTERSECT_OPENBRACKET }.Contains(aFragmentKey)))
                                    {
                                        ActualKeys.Add(aItem.KeyValue.Key);
                                    }
                                }

                                /// Process the key-value, given the current fragment key.
                                if (aFragmentKey.Equals(ConfigurationKeys.LOGFILE_OPENBRACKET))
                                {
                                    if (aItem.Name == ConfigurationKeys.LOGFILE_FILENAME)
                                    {
                                        aConfiguration.LogFileName = aItem.KeyValue.Value.Trim();
                                        if (Validator.IsDelimited(aConfiguration.LogFileName, Crozzle.StringDelimiters))
                                        {
                                            String value = aConfiguration.LogFileName.Trim(Crozzle.StringDelimiters);
                                            aConfiguration.LogFileName = (!String.IsNullOrEmpty(value)) ? value : DefaultLogFileName;
                                            if (!Validator.IsFilename(aConfiguration.LogFileName))
                                            {
                                                Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                            }
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                }
                                else if (aFragmentKey.Equals(ConfigurationKeys.UNIQUESEQ_OPENBRACKET))
                                {
                                    if (aItem.Name == ConfigurationKeys.UNIQUESEQ_MINIMUM)
                                    {
                                        int minimum;
                                        if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out minimum))
                                        {
                                            aConfiguration.MinimumNumberOfUniqueWords = minimum;
                                            if (!Validator.TryRange(minimum, 1, Int32.MaxValue))
                                            {
                                                Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                            }
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                    else if (aItem.Name == ConfigurationKeys.UNIQUESEQ_MAXIMUM)
                                    {
                                        int maximum;
                                        if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out maximum))
                                        {
                                            aConfiguration.MaximumNumberOfUniqueWords = maximum;
                                            if (!Validator.TryRange(maximum, 1, Int32.MaxValue))
                                            {
                                                Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                            }
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                }
                                else if (aFragmentKey.Equals(ConfigurationKeys.OUTPUT_OPENBRACKET))
                                {
                                    if (aItem.Name == ConfigurationKeys.OUTPUT_INVALID)
                                    {
                                        aConfiguration.InvalidCrozzleScore = aItem.KeyValue.Value.Trim();
                                        if (Validator.IsDelimited(aConfiguration.InvalidCrozzleScore, Crozzle.StringDelimiters))
                                        {
                                            aConfiguration.InvalidCrozzleScore = aConfiguration.InvalidCrozzleScore.Trim(Crozzle.StringDelimiters);
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                    else if (aItem.Name == ConfigurationKeys.OUTPUT_UPPERCASE)
                                    {
                                        Boolean uppercase = true;
                                        if (!Validator.IsMatch(aItem.KeyValue.Value, allowedBooleans))
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                        aConfiguration.Uppercase = uppercase;
                                    }
                                    else if (aItem.Name == ConfigurationKeys.OUTPUT_STYLE)
                                    {
                                        aConfiguration.Style = aItem.KeyValue.Value.Trim();
                                        if (Validator.IsDelimited(aConfiguration.Style, Crozzle.StringDelimiters))
                                        {
                                            aConfiguration.Style = aConfiguration.Style.Trim(Crozzle.StringDelimiters);
                                            if (!Validator.IsStyleTag(aConfiguration.Style))
                                            {
                                                Errors.Add(String.Format(ConfigurationErrors.StyleError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                            }
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                    else if (aItem.Name == ConfigurationKeys.OUTPUT_BGCOLOUR_EMPTY)
                                    {
                                        aConfiguration.BGcolourEmptyTD = aItem.KeyValue.Value.Trim();
                                        if (!Validator.IsHexColourCode(aConfiguration.BGcolourEmptyTD))
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ColourError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                    else if (aItem.Name == ConfigurationKeys.OUTPUT_BGCOLOUR_NONEMPTY)
                                    {
                                        aConfiguration.BGcolourNonEmptyTD = aItem.KeyValue.Value.Trim();
                                        if (!Validator.IsHexColourCode(aConfiguration.BGcolourNonEmptyTD))
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ColourError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                }
                                else if (aFragmentKey.Equals(ConfigurationKeys.SIZE_OPENBRACKET))
                                {
                                    if (aItem.Name == ConfigurationKeys.SIZE_MINIMUMROWS)
                                    {
                                        int minimum;
                                        if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out minimum))
                                        {
                                            aConfiguration.MinimumNumberOfRows = minimum;
                                            if (!Validator.TryRange(minimum, 1, Int32.MaxValue))
                                            {
                                                Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                            }
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                    else if (aItem.Name == ConfigurationKeys.SIZE_MAXIMUMROWS)
                                    {
                                        int maximum;
                                        if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out maximum))
                                        {
                                            aConfiguration.MaximumNumberOfRows = maximum;
                                            if (!Validator.TryRange(maximum, 1, Int32.MaxValue))
                                            {
                                                Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                            }
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                    else if (aItem.Name == ConfigurationKeys.SIZE_MINIMUMCOLUMNS)
                                    {
                                        int minimum;
                                        if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out minimum))
                                        {
                                            aConfiguration.MinimumNumberOfColumns = minimum;
                                            if (!Validator.TryRange(minimum, 1, Int32.MaxValue))
                                            {
                                                Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                            }
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                    else if (aItem.Name == ConfigurationKeys.SIZE_MAXIMUMCOLUMNS)
                                    {
                                        int maximum;
                                        if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out maximum))
                                        {
                                            aConfiguration.MaximumNumberOfColumns = maximum;
                                            if (!Validator.TryRange(maximum, 1, Int32.MaxValue))
                                            {
                                                Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                            }
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                }
                                else if (aFragmentKey.Equals(ConfigurationKeys.DIRECTIONALSEQ_OPENBRACKET))
                                {
                                    if (aItem.Name == ConfigurationKeys.DIRECTIONALSEQ_MINIMUMHORZ)
                                    {
                                        int minimum;
                                        if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out minimum))
                                        {
                                            aConfiguration.MinimumHorizontalWords = minimum;
                                            if (!Validator.TryRange(minimum, 1, Int32.MaxValue))
                                            {
                                                Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                            }
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                    else if (aItem.Name == ConfigurationKeys.DIRECTIONALSEQ_MAXIMUMHORZ)
                                    {
                                        int maximum;
                                        if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out maximum))
                                        {
                                            aConfiguration.MaximumHorizontalWords = maximum;
                                            if (!Validator.TryRange(maximum, 1, Int32.MaxValue))
                                            {
                                                Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                            }
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                    else if (aItem.Name == ConfigurationKeys.DIRECTIONALSEQ_MINIMUMVERT)
                                    {
                                        int minimum;
                                        if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out minimum))
                                        {
                                            aConfiguration.MinimumVerticalWords = minimum;
                                            if (!Validator.TryRange(minimum, 1, Int32.MaxValue))
                                            {
                                                Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                            }
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                    else if (aItem.Name == ConfigurationKeys.DIRECTIONALSEQ_MAXIMUMVERT)
                                    {
                                        int maximum;
                                        if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out maximum))
                                        {
                                            aConfiguration.MaximumVerticalWords = maximum;
                                            if (!Validator.TryRange(maximum, 1, Int32.MaxValue))
                                            {
                                                Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                            }
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                }
                                else if (aFragmentKey.Equals(ConfigurationKeys.DIRECTIONALINTERSECT_OPENBRACKET))
                                {
                                    if (aItem.Name == ConfigurationKeys.DIRECTIONALINTERSECT_MINIMUMHORZ)
                                    {
                                        int minimum;
                                        if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out minimum))
                                        {
                                            aConfiguration.MinimumIntersectionsInHorizontalWords = minimum;
                                            if (!Validator.TryRange(minimum, 1, Int32.MaxValue))
                                            {
                                                Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                            }
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                    else if (aItem.Name == ConfigurationKeys.DIRECTIONALINTERSECT_MAXIMUMHORZ)
                                    {
                                        int maximum;
                                        if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out maximum))
                                        {
                                            aConfiguration.MaximumIntersectionsInHorizontalWords = maximum;
                                            if (!Validator.TryRange(maximum, 1, Int32.MaxValue))
                                            {
                                                Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                            }
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                    else if (aItem.Name == ConfigurationKeys.DIRECTIONALINTERSECT_MINIMUMVERT)
                                    {
                                        int minimum;
                                        if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out minimum))
                                        {
                                            aConfiguration.MinimumIntersectionsInVerticalWords = minimum;
                                            if (!Validator.TryRange(minimum, 1, Int32.MaxValue))
                                            {
                                                Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                            }
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                    else if (aItem.Name == ConfigurationKeys.DIRECTIONALINTERSECT_MAXIMUMVERT)
                                    {
                                        int maximum;
                                        if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out maximum))
                                        {
                                            aConfiguration.MaximumIntersectionsInVerticalWords = maximum;
                                            if (!Validator.TryRange(maximum, 1, Int32.MaxValue))
                                            {
                                                Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                            }
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                }
                                else if (aFragmentKey.Equals(ConfigurationKeys.DUPE_OPENBRACKET))
                                {
                                    if (aItem.Name == ConfigurationKeys.DUPE_MINIMUMVALID)
                                    {
                                        int minimum;
                                        if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out minimum))
                                        {
                                            aConfiguration.MinimumNumberOfTheSameWord = minimum;
                                            if (!Validator.TryRange(minimum, 0, Int32.MaxValue))
                                            {
                                                Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                            }
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                    else if (aItem.Name == ConfigurationKeys.DUPE_MAXIMUMVALID)
                                    {
                                        int maximum;
                                        if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out maximum))
                                        {
                                            aConfiguration.MaximumNumberOfTheSameWord = maximum;
                                            if (!Validator.TryRange(maximum, 0, Int32.MaxValue))
                                            {
                                                Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                            }
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                }
                                else if (aFragmentKey.Equals(ConfigurationKeys.VALIDGROUP_OPENBRACKET))
                                {
                                    if (aItem.Name == ConfigurationKeys.VALIDGROUP_MINIMUM)
                                    {
                                        int minimum;
                                        if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out minimum))
                                        {
                                            aConfiguration.MinimumNumberOfGroups = minimum;
                                            if (!Validator.TryRange(minimum, 1, Int32.MaxValue))
                                            {
                                                Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                            }
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                    else if (aItem.Name == ConfigurationKeys.VALIDGROUP_MAXIMUM)
                                    {
                                        int maximum;
                                        if (Validator.IsInt32(aItem.KeyValue.Value.Trim(), out maximum))
                                        {
                                            aConfiguration.MaximumNumberOfGroups = maximum;
                                            if (!Validator.TryRange(maximum, 1, Int32.MaxValue))
                                            {
                                                Errors.Add(String.Format(ConfigurationErrors.IntegerError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                            }
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                }
                                else if (aFragmentKey.Equals(ConfigurationKeys.INTERSECT_OPENBRACKET))
                                {
                                    if (aItem.Name.Length == 1) /// ConfigurationFileItem.cs defines the key as the letter used in the CFG
                                    {
                                        String originalValues = aItem.KeyValue.Value.Trim();

                                        int points;
                                        if (Validator.IsInt32(aItem.KeyValue.Value, out points))
                                        {
                                            int index = (int)aItem.KeyValue.Key[0] - (int)'A';
                                            aConfiguration.IntersectingPointsPerLetter[index] = points;
                                            ActualIntersectingKeys[index] = true;
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                    else
                                    {
                                        Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                    }
                                }
                                else if (aFragmentKey.Equals(ConfigurationKeys.NONINTERSECT_OPENBRACKET))
                                {
                                    if (aItem.Name.Length == 1) /// ConfigurationFileItem.cs defines the key as the letter used in the CFG
                                    {
                                        String originalValues = aItem.KeyValue.Value.Trim();

                                        int points;
                                        if (Validator.IsInt32(aItem.KeyValue.Value, out points))
                                        {
                                            int index = (int)aItem.KeyValue.Key[0] - (int)'A';
                                            aConfiguration.NonIntersectingPointsPerLetter[index] = points;
                                            ActualNonIntersectingKeys[index] = true;
                                        }
                                        else
                                        {
                                            Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                        }
                                    }
                                    else
                                    {
                                        Errors.Add(String.Format(ConfigurationErrors.ValueError, aItem.KeyValue.OriginalKeyValue, Validator.Errors[0]));
                                    }
                                }
                                else
                                {
                                    Errors.AddRange(ConfigurationFileItem.Errors);
                                }
                            }
                        }
                    }
                }

                // Close files.
                fileIn.Close();

                // Check which keys are missing from the configuration file.
                foreach (string expectedKey in ExpectedKeys)
                {
                    if (!ActualKeys.Contains(expectedKey))
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MissingKeyError, expectedKey));
                    }
                }
                for (char ch = 'A'; ch <= 'Z'; ch++)
                {
                    if (!ActualIntersectingKeys[(int)ch - (int)'A'])
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MissingIntersectionKeyError, ch.ToString()));
                    }
                }
                for (char ch = 'A'; ch <= 'Z'; ch++)
                {
                    if (!ActualNonIntersectingKeys[(int)ch - (int)'A'])
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MissingNonIntersectionKeyError, ch.ToString()));
                    }
                }

                // Check that minimum values are <= to their maximmum counterpart values.
                if (ActualKeys.Contains("SEQUENCES-IN-FILE-MINIMUM") && ActualKeys.Contains("SEQUENCES-IN-FILE-MAXIMUM"))
                {
                    if (aConfiguration.MinimumNumberOfUniqueWords > aConfiguration.MaximumNumberOfUniqueWords)
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "SEQUENCES-IN-FILE-MINIMUM",
                                                 aConfiguration.MinimumNumberOfUniqueWords, aConfiguration.MaximumNumberOfUniqueWords));
                    }
                }

                if (ActualKeys.Contains("MINIMUM-ROWS") && ActualKeys.Contains("MAXIMUM-ROWS"))
                {
                    if (aConfiguration.MinimumNumberOfRows > aConfiguration.MaximumNumberOfRows)
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "MINIMUM-ROWS",
                                                 aConfiguration.MinimumNumberOfRows, aConfiguration.MaximumNumberOfRows));
                    }
                }

                if (ActualKeys.Contains("MINIMUM-COLUMNS") && ActualKeys.Contains("MAXIMUM-COLUMNS"))
                {
                    if (aConfiguration.MinimumNumberOfColumns > aConfiguration.MaximumNumberOfColumns)
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "MINIMUM-COLUMNS",
                                                 aConfiguration.MinimumNumberOfColumns, aConfiguration.MaximumNumberOfColumns));
                    }
                }

                if (ActualKeys.Contains("SEQUENCES-IN-CROZZLE-MINIMUM-HORIZONTAL") && ActualKeys.Contains("SEQUENCES-IN-CROZZLE-MAXIMUM-HORIZONTAL"))
                {
                    if (aConfiguration.MinimumHorizontalWords > aConfiguration.MaximumHorizontalWords)
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "SEQUENCES-IN-CROZZLE-MINIMUM-HORIZONTAL",
                                                 aConfiguration.MinimumHorizontalWords, aConfiguration.MaximumHorizontalWords));
                    }
                }

                if (ActualKeys.Contains("SEQUENCES-IN-CROZZLE-MINIMUM-VERTICAL") && ActualKeys.Contains("SEQUENCES-IN-CROZZLE-MAXIMUM-VERTICAL"))
                {
                    if (aConfiguration.MinimumVerticalWords > aConfiguration.MaximumVerticalWords)
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "SEQUENCES-IN-CROZZLE-MINIMUM-VERTICAL",
                                                 aConfiguration.MinimumVerticalWords, aConfiguration.MaximumVerticalWords));
                    }
                }

                if (ActualKeys.Contains("INTERSECTIONS-IN-SEQUENCES-MINIMUM-HORIZONTAL") && ActualKeys.Contains("INTERSECTIONS-IN-SEQUENCES-MINIMUM-HORIZONTAL"))
                {
                    if (aConfiguration.MinimumIntersectionsInHorizontalWords > aConfiguration.MaximumIntersectionsInHorizontalWords)
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "INTERSECTIONS-IN-SEQUENCES-MINIMUM-VERTICAL",
                                                 aConfiguration.MinimumIntersectionsInHorizontalWords, aConfiguration.MaximumIntersectionsInHorizontalWords));
                    }
                }

                if (ActualKeys.Contains("INTERSECTIONS-IN-SEQUENCES-MINIMUM-VERTICAL") && ActualKeys.Contains("INTERSECTIONS-IN-SEQUENCES-MINIMUM-VERTICAL"))
                {
                    if (aConfiguration.MinimumIntersectionsInVerticalWords > aConfiguration.MaximumIntersectionsInVerticalWords)
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "INTERSECTIONS-IN-SEQUENCES-MINIMUM-VERTICAL",
                                                 aConfiguration.MinimumIntersectionsInVerticalWords, aConfiguration.MaximumIntersectionsInVerticalWords));
                    }
                }

                if (ActualKeys.Contains("DUPLICATE-SEQUENCES-MINIMUM") && ActualKeys.Contains("DUPLICATE-SEQUENCES-MAXIMUM"))
                {
                    if (aConfiguration.MaximumNumberOfTheSameWord < aConfiguration.MinimumNumberOfTheSameWord)
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MaxDupeLowerThanMinError, "DUPLICATE-SEQUENCES-MAXIMUM",
                                                 aConfiguration.MaximumNumberOfTheSameWord, aConfiguration.MaximumNumberOfTheSameWord));
                    }
                }

                if (ActualKeys.Contains("VALID-GROUPS-MINIMUM") && ActualKeys.Contains("VALID-GROUPS-MAXIMUM"))
                {
                    if (aConfiguration.MinimumNumberOfGroups > aConfiguration.MaximumNumberOfGroups)
                    {
                        Errors.Add(String.Format(ConfigurationErrors.MinGreaterThanMaxError, "VALID-GROUPS-MINIMUM",
                                                 aConfiguration.MinimumNumberOfGroups, aConfiguration.MaximumNumberOfGroups));
                    }
                }
            }

            // Store validity.
            aConfiguration.Valid = Errors.Count == 0;
            return(aConfiguration.Valid);
        }