Index: Ringtoets/GrassCoverErosionInwards/src/Ringtoets.GrassCoverErosionInwards.IO/DikeProfiles/DikeProfileDataReader.cs =================================================================== diff -u -r02b9c1938c77fec1599daf755bc1e7e70598621c -r9b2cc43e34e4e4202f435ea6720c8e9637db45a7 --- Ringtoets/GrassCoverErosionInwards/src/Ringtoets.GrassCoverErosionInwards.IO/DikeProfiles/DikeProfileDataReader.cs (.../DikeProfileDataReader.cs) (revision 02b9c1938c77fec1599daf755bc1e7e70598621c) +++ Ringtoets/GrassCoverErosionInwards/src/Ringtoets.GrassCoverErosionInwards.IO/DikeProfiles/DikeProfileDataReader.cs (.../DikeProfileDataReader.cs) (revision 9b2cc43e34e4e4202f435ea6720c8e9637db45a7) @@ -57,21 +57,8 @@ KRUINHOOGTE = 128, DIJK = 256, MEMO = 512, - All = 1023 } - private const string idPattern = @"^ID(\s+(?.+?)?)?\s*$"; - private const string versionPattern = @"^VERSIE(\s+(?.+?)?)?\s*$"; - private const string orientationPattern = @"^RICHTING(\s+(?.+?)?)?\s*$"; - private const string damTypePattern = @"^DAM(\s+(?.+?)?)?\s*$"; - private const string profileTypePattern = @"^DAMWAND(\s+(?.+?)?)?\s*$"; - private const string damHeightPattern = @"^DAMHOOGTE(\s+(?.+?)?)?\s*$"; - private const string crestLevelPattern = @"^KRUINHOOGTE(\s+(?.+?)?)?\s*$"; - private const string dikeGeometryPattern = @"^DIJK(\s+(?.+?)?)?\s*$"; - private const string foreshoreGeometryPattern = @"^VOORLAND(\s+(?.+?)?)?\s*$"; - private const string roughnessPointPattern = @"^(\s*)?(?.+?)?(\s+(?.+?)?(\s+(?.+?)?)?)?\s*$"; - private const string memoPattern = @"^MEMO\s*$"; - private string fileBeingRead; private ParametersFoundInFile readParameters; @@ -82,10 +69,11 @@ /// The read dike profile data. /// When an error occurs like: /// + /// is invalid. /// refers to a file that doesn't exist. /// A piece of text from the file cannot be converted in the expected variable type. /// A converted value is invalid. - /// The file is not complete. + /// The file is incomplete. /// public DikeProfileData ReadDikeProfileData(string filePath) { @@ -173,20 +161,16 @@ /// True if the text matches a VERSIE key-value pair and has been /// validated successfully; false otherwise. /// The value after the VERSIE key does - /// not represent a valid version code or the file version is not supported by this reader. + /// not represent a valid version code or the version is not supported by this reader. private bool TryReadVersion(string text, int lineNumber) { - Match versionMatch = new Regex(versionPattern).Match(text); + Match versionMatch = new Regex(@"^VERSIE(\s+(?.+?)?)?\s*$").Match(text); if (versionMatch.Success) { string readVersionText = versionMatch.Groups["version"].Value; Version fileVersion = ParseVersion(lineNumber, readVersionText); - if (!fileVersion.Equals(new Version(4, 0))) - { - string message = Resources.DikeProfileDataReader_TryReadVersion_Only_version_four_zero_supported; - throw CreateCriticalFileReadException(lineNumber, message); - } + ValidateVersion(fileVersion, lineNumber); readParameters |= ParametersFoundInFile.VERSIE; return true; @@ -195,10 +179,10 @@ } /// - /// Parses the version code from a piece of text representing a VERSIE key-value pair. + /// Parses the version code from a piece of text. /// /// The line number. - /// The VERSIE key-value pair text. + /// The text to parse. /// The read version code. /// When /// does not represent a valid version code. @@ -229,34 +213,40 @@ } /// - /// Attempts to match the given text to a ID key-value pair. If a match is found, + /// Validates the version. + /// + /// The file version. + /// The line number. + /// When + /// is not supported by this reader. + private void ValidateVersion(Version fileVersion, int lineNumber) + { + if (!fileVersion.Equals(new Version(4, 0))) + { + string message = Resources.DikeProfileDataReader_ValidateVersion_Only_version_four_zero_supported; + throw CreateCriticalFileReadException(lineNumber, message); + } + } + + /// + /// Attempts to match the given text to an ID key-value pair. If a match is found, /// the value is parsed and validated. /// /// The text. /// The data to be updated. /// The line number. - /// True if the text matches a ID key-value pair and has been + /// True if the text matches an ID key-value pair and has been /// validated successfully; false otherwise. /// The value after the ID key does - /// not represent a id. + /// not represent an id. private bool TryReadId(string text, DikeProfileData data, int lineNumber) { - Match idMatch = new Regex(idPattern).Match(text); + Match idMatch = new Regex(@"^ID(\s+(?.+?)?)?\s*$").Match(text); if (idMatch.Success) { string readIdText = idMatch.Groups["id"].Value; - if (string.IsNullOrWhiteSpace(readIdText)) - { - string message = string.Format(Resources.DikeProfileDataReader_TryReadId_Id_0_not_valid, - readIdText); - throw CreateCriticalFileReadException(lineNumber, message); - } - if (new Regex(@"\s").IsMatch(readIdText)) - { - string message = string.Format(Resources.DikeProfileDataReader_TryReadId_Id_0_has_unsupported_white_space, - readIdText); - throw CreateCriticalFileReadException(lineNumber, message); - } + ValidateId(readIdText, lineNumber); + data.Id = readIdText; readParameters |= ParametersFoundInFile.ID; return true; @@ -265,6 +255,30 @@ } /// + /// Validates the identifier. + /// + /// The id. + /// The line number. + /// When is not + /// present or has a whitespace. + private void ValidateId(string id, int lineNumber) + { + if (string.IsNullOrWhiteSpace(id)) + { + string message = string.Format(Resources.DikeProfileDataReader_ValidateId_Id_0_not_valid, + id); + throw CreateCriticalFileReadException(lineNumber, message); + } + + if (new Regex(@"\s").IsMatch(id)) + { + string message = string.Format(Resources.DikeProfileDataReader_ValidateId_Id_0_has_unsupported_white_space, + id); + throw CreateCriticalFileReadException(lineNumber, message); + } + } + + /// /// Attempts to match the given text to a RICHTING key-value pair. If a match is /// found, the value is parsed and validated. If valid, the value is stored. /// @@ -277,18 +291,14 @@ /// not represent a valid number or the orientation value is not in the range [0, 360]. private bool TryReadOrientation(string text, DikeProfileData data, int lineNumber) { - Match orientationMatch = new Regex(orientationPattern).Match(text); + Match orientationMatch = new Regex(@"^RICHTING(\s+(?.+?)?)?\s*$").Match(text); if (orientationMatch.Success) { string readOrientationText = orientationMatch.Groups["orientation"].Value; double orientation = ParseOrientation(lineNumber, readOrientationText); - if (orientation < 0.0 || orientation > 360.0) - { - string message = string.Format(Resources.DikeProfileDataReader_TryReadOrientation_Orientation_0_must_be_in_range, - orientation); - throw CreateCriticalFileReadException(lineNumber, message); - } + ValidateOrientation(orientation, lineNumber); + data.Orientation = orientation; readParameters |= ParametersFoundInFile.RICHTING; return true; @@ -297,10 +307,10 @@ } /// - /// Parses the orientation from a piece representing a RICHTING key-value pair. + /// Parses the orientation from a piece of text. /// /// The line number. - /// The RICHTING key-value pair text. + /// The text. /// The value corresponding to the RICHTING key. /// When /// does not represent a valid number. @@ -325,6 +335,23 @@ } /// + /// Validates the orientation. + /// + /// The orientation. + /// The line number. + /// When + /// is outside the [0, 360] range. + private void ValidateOrientation(double orientation, int lineNumber) + { + if (orientation < 0.0 || orientation > 360.0) + { + string message = string.Format(Resources.DikeProfileDataReader_ValidateOrientation_Orientation_0_must_be_in_range, + orientation); + throw CreateCriticalFileReadException(lineNumber, message); + } + } + + /// /// Attempts to match the given text to a DAM key-value pair. If a match is found, /// the value is parsed and validated. If valid, the value is stored. /// @@ -337,7 +364,7 @@ /// not represent a valid value. private bool TryReadDamType(string text, DikeProfileData data, int lineNumber) { - Match damTypeMatch = new Regex(damTypePattern).Match(text); + Match damTypeMatch = new Regex(@"^DAM(\s+(?.+?)?)?\s*$").Match(text); if (damTypeMatch.Success) { string readDamTypeText = damTypeMatch.Groups["damtype"].Value; @@ -351,10 +378,10 @@ } /// - /// Parses the dam-type from a piece of text representing a DAM key-value pair. + /// Parses the dam-type from a piece of text. /// /// The line number. - /// The DAM key-value pair text. + /// The text. /// The read dam-type. /// When /// does not represent a . @@ -401,7 +428,7 @@ /// does not represent a valid value. private bool TryReadProfileType(string text, DikeProfileData data, int lineNumber) { - Match profileTypeMatch = new Regex(profileTypePattern).Match(text); + Match profileTypeMatch = new Regex(@"^DAMWAND(\s+(?.+?)?)?\s*$").Match(text); if (profileTypeMatch.Success) { string readProfileTypeText = profileTypeMatch.Groups["profiletype"].Value; @@ -415,10 +442,10 @@ } /// - /// Parses the profile-type from a piece of text representing a DAMWAND key-value pair. + /// Parses the profile-type from a piece of text. /// /// The line number. - /// The DAMWAND key-value pair text. + /// The text. /// The read profile-type. /// When /// does not represent a . @@ -464,7 +491,7 @@ /// does not represent a valid number. private bool TryReadDamHeight(string text, DikeProfileData data, int lineNumber) { - Match damHeightMatch = new Regex(damHeightPattern).Match(text); + Match damHeightMatch = new Regex(@"^DAMHOOGTE(\s+(?.+?)?)?\s*$").Match(text); if (damHeightMatch.Success) { string readDamHeightText = damHeightMatch.Groups["damheight"].Value; @@ -478,11 +505,10 @@ } /// - /// Parses the height of the dam from a piece of text representing a DAMHOOGTE - /// key-value pair. + /// Parses the height of the dam from a piece of text. /// /// The line number. - /// The DAMHOOGTE key-value pair text. + /// The text. /// The height of the dam. /// When /// does not represent a number. @@ -519,7 +545,7 @@ /// does not represent a valid number. private bool TryReadCrestLevel(string text, DikeProfileData data, int lineNumber) { - Match crestLevelMatch = new Regex(crestLevelPattern).Match(text); + Match crestLevelMatch = new Regex(@"^KRUINHOOGTE(\s+(?.+?)?)?\s*$").Match(text); if (crestLevelMatch.Success) { string readCrestLevelText = crestLevelMatch.Groups["crestlevel"].Value; @@ -533,11 +559,10 @@ } /// - /// Parses the height of the dike from a piece of text representing a KRUINHOOGTE - /// key-value pair. + /// Parses the height of the dike from a piece of text. /// /// The line number. - /// The KRUINHOOGTE key-value pair text. + /// The text. /// The height of the dike. /// When /// does not represent a number. @@ -576,25 +601,21 @@ /// data block is invalid. private bool TryReadDikeRoughnessPoints(string text, DikeProfileData data, TextReader reader, ref int lineNumber) { - Match dikeGeometryMatch = new Regex(dikeGeometryPattern).Match(text); + Match dikeGeometryMatch = new Regex(@"^DIJK(\s+(?.+?)?)?\s*$").Match(text); if (dikeGeometryMatch.Success) { string readDikeGeometryCountText = dikeGeometryMatch.Groups["dikegeometry"].Value; int numberOfElements = ParseNumberOfDikeElements(lineNumber, readDikeGeometryCountText); - if (numberOfElements < 0) - { - string message = string.Format(Resources.DikeProfileDataReader_ReadDikeProfileData_DikeCount_cannot_be_negative, - numberOfElements); - throw CreateCriticalFileReadException(lineNumber, message); - } + ValidateDikePointCount(numberOfElements, lineNumber); + if (numberOfElements == 0) { readParameters |= ParametersFoundInFile.DIJK; return true; } + data.DikeGeometry = new RoughnessPoint[numberOfElements]; - for (int i = 0; i < numberOfElements; i++) { lineNumber++; @@ -645,20 +666,36 @@ } /// - /// Attempts to match the given text to a roughness-point value triplet. If a match - /// is found, the values are parsed and validated. If valid, the resulting + /// Validates the number of dike points to be read from file. + /// + /// The number of elements. + /// The line number. + /// When + /// is negative. + private void ValidateDikePointCount(int numberOfElements, int lineNumber) + { + if (numberOfElements < 0) + { + string message = string.Format(Resources.DikeProfileDataReader_ReadDikeProfileData_DikeCount_cannot_be_negative, + numberOfElements); + throw CreateCriticalFileReadException(lineNumber, message); + } + } + + /// + /// Matches the given text to a roughness-point value triplet. If a match is found, + /// the values are parsed and validated. If valid, the resulting /// based on the values will be returned. /// /// The text. /// The line number. - /// True if the text matches a roughness-point value triplet and - /// has been validated successfully; false otherwise. + /// A instance built using data in . /// Any parameter value in the roughness-point /// value triplet does not represent a valid number or the roughness parameter /// is not valid. private RoughnessPoint ReadRoughnessPoint(string text, int lineNumber) { - Match roughnessSectionDataMatch = new Regex(roughnessPointPattern).Match(text); + Match roughnessSectionDataMatch = new Regex(@"^(\s*)?(?.+?)?(\s+(?.+?)?(\s+(?.+?)?)?)?\s*$").Match(text); string readLocalXText = roughnessSectionDataMatch.Groups["localx"].Value; double localX = ParseRoughnessPointParameter(readLocalXText, @@ -675,12 +712,8 @@ Resources.DikeProfileDataReader_ReadRoughnessPoint_Roughness_DisplayName, lineNumber); - if (roughness < 0.5 || roughness > 1.0) - { - string message = string.Format(Resources.DikeProfileDataReader_ReadRoughnessPoint_Roughness_0_must_be_in_range_LowerLimit_1_, - roughness, 0.5); - throw CreateCriticalFileReadException(lineNumber, message); - } + ValidateRoughness(roughness, lineNumber); + return new RoughnessPoint(new Point2D(localX, localY), roughness); } @@ -714,6 +747,23 @@ } /// + /// Validates the roughness. + /// + /// The roughness. + /// The line number. + /// When + /// is outside the range [0.5, 1]. + private void ValidateRoughness(double roughness, int lineNumber) + { + if (roughness < 0.5 || roughness > 1.0) + { + string message = string.Format(Resources.DikeProfileDataReader_ReadRoughnessPoint_Roughness_0_must_be_in_range_LowerLimit_1_, + roughness, 0.5); + throw CreateCriticalFileReadException(lineNumber, message); + } + } + + /// /// Attempts to match the given text to a VOORLAND key-value pair. If a match is /// found, the data block is being read. If valid, the value is stored. /// @@ -728,18 +778,14 @@ /// data block is invalid. private bool TryReadForeshoreRoughnessPoints(string text, DikeProfileData data, TextReader reader, ref int lineNumber) { - Match foreshoreGeometryMatch = new Regex(foreshoreGeometryPattern).Match(text); + Match foreshoreGeometryMatch = new Regex(@"^VOORLAND(\s+(?.+?)?)?\s*$").Match(text); if (foreshoreGeometryMatch.Success) { string readForeshoreCountText = foreshoreGeometryMatch.Groups["foreshoregeometry"].Value; var numberOfElements = ParseNumberOfForeshoreElements(readForeshoreCountText, lineNumber); - if (numberOfElements < 0) - { - string message = string.Format(Resources.DikeProfileDataReader_ReadDikeProfileData_ForeshoreCount_0_cannot_be_negative, - numberOfElements); - throw CreateCriticalFileReadException(lineNumber, message); - } + ValidateForeshorePointCount(numberOfElements, lineNumber); + if (numberOfElements == 0) { readParameters |= ParametersFoundInFile.VOORLAND; @@ -768,11 +814,10 @@ } /// - /// Parses the number of foreshore roughness points from a piece of text representing - /// a VOORLAND key-value pair. + /// Parses the number of foreshore roughness points from a piece of text. /// /// The line number. - /// The VOORLAND key-value pair text. + /// The text. /// The number of foreshore roughness points. /// When /// does not represent a number. @@ -796,9 +841,26 @@ } } + /// + /// Validates the number of foreshore points. + /// + /// The number of elements. + /// The line number. + /// When + /// is negative. + private void ValidateForeshorePointCount(int numberOfElements, int lineNumber) + { + if (numberOfElements < 0) + { + string message = string.Format(Resources.DikeProfileDataReader_ReadDikeProfileData_ForeshoreCount_0_cannot_be_negative, + numberOfElements); + throw CreateCriticalFileReadException(lineNumber, message); + } + } + private bool TryReadMemo(string text, DikeProfileData data, StreamReader reader) { - Match memoMatch = new Regex(memoPattern).Match(text); + Match memoMatch = new Regex(@"^MEMO\s*$").Match(text); if (memoMatch.Success) { data.Memo = reader.ReadToEnd(); @@ -841,7 +903,7 @@ } /// - /// Throws a configured instance of . + /// Returns a configured instance of . /// /// The line number being read. /// The critical error message. @@ -855,7 +917,6 @@ return new CriticalFileReadException(message, innerException); } - private void ValidateNoMissingParameters() { string[] missingParameters = GetMissingParameterNames(); Index: Ringtoets/GrassCoverErosionInwards/src/Ringtoets.GrassCoverErosionInwards.IO/Properties/Resources.Designer.cs =================================================================== diff -u -r02b9c1938c77fec1599daf755bc1e7e70598621c -r9b2cc43e34e4e4202f435ea6720c8e9637db45a7 --- Ringtoets/GrassCoverErosionInwards/src/Ringtoets.GrassCoverErosionInwards.IO/Properties/Resources.Designer.cs (.../Resources.Designer.cs) (revision 02b9c1938c77fec1599daf755bc1e7e70598621c) +++ Ringtoets/GrassCoverErosionInwards/src/Ringtoets.GrassCoverErosionInwards.IO/Properties/Resources.Designer.cs (.../Resources.Designer.cs) (revision 9b2cc43e34e4e4202f435ea6720c8e9637db45a7) @@ -330,36 +330,36 @@ /// /// Looks up a localized string similar to De ingelezen Id ({0}) bevat spaties, welke niet zijn toegestaan.. /// - internal static string DikeProfileDataReader_TryReadId_Id_0_has_unsupported_white_space { + internal static string DikeProfileDataReader_ValidateId_Id_0_has_unsupported_white_space { get { - return ResourceManager.GetString("DikeProfileDataReader_TryReadId_Id_0_has_unsupported_white_space", resourceCulture); + return ResourceManager.GetString("DikeProfileDataReader_ValidateId_Id_0_has_unsupported_white_space", resourceCulture); } } /// /// Looks up a localized string similar to De ingelezen Id ({0}) is geen geldig id.. /// - internal static string DikeProfileDataReader_TryReadId_Id_0_not_valid { + internal static string DikeProfileDataReader_ValidateId_Id_0_not_valid { get { - return ResourceManager.GetString("DikeProfileDataReader_TryReadId_Id_0_not_valid", resourceCulture); + return ResourceManager.GetString("DikeProfileDataReader_ValidateId_Id_0_not_valid", resourceCulture); } } /// /// Looks up a localized string similar to De ingelezen orientatie waarde ({0}) moet binnen het bereik [0, 360] vallen.. /// - internal static string DikeProfileDataReader_TryReadOrientation_Orientation_0_must_be_in_range { + internal static string DikeProfileDataReader_ValidateOrientation_Orientation_0_must_be_in_range { get { - return ResourceManager.GetString("DikeProfileDataReader_TryReadOrientation_Orientation_0_must_be_in_range", resourceCulture); + return ResourceManager.GetString("DikeProfileDataReader_ValidateOrientation_Orientation_0_must_be_in_range", resourceCulture); } } /// /// Looks up a localized string similar to Enkel bestanden van versie '4.0' worden ondersteund.. /// - internal static string DikeProfileDataReader_TryReadVersion_Only_version_four_zero_supported { + internal static string DikeProfileDataReader_ValidateVersion_Only_version_four_zero_supported { get { - return ResourceManager.GetString("DikeProfileDataReader_TryReadVersion_Only_version_four_zero_supported", resourceCulture); + return ResourceManager.GetString("DikeProfileDataReader_ValidateVersion_Only_version_four_zero_supported", resourceCulture); } } Index: Ringtoets/GrassCoverErosionInwards/src/Ringtoets.GrassCoverErosionInwards.IO/Properties/Resources.resx =================================================================== diff -u -r02b9c1938c77fec1599daf755bc1e7e70598621c -r9b2cc43e34e4e4202f435ea6720c8e9637db45a7 --- Ringtoets/GrassCoverErosionInwards/src/Ringtoets.GrassCoverErosionInwards.IO/Properties/Resources.resx (.../Resources.resx) (revision 02b9c1938c77fec1599daf755bc1e7e70598621c) +++ Ringtoets/GrassCoverErosionInwards/src/Ringtoets.GrassCoverErosionInwards.IO/Properties/Resources.resx (.../Resources.resx) (revision 9b2cc43e34e4e4202f435ea6720c8e9637db45a7) @@ -144,10 +144,10 @@ De volgende parameter(s) zijn niet aanwezig in het bestand: {0} - + Enkel bestanden van versie '4.0' worden ondersteund. - + De ingelezen orientatie waarde ({0}) moet binnen het bereik [0, 360] vallen. @@ -210,10 +210,10 @@ De locatie parameter 'X0' bevat een ongeldig getal. - + De ingelezen Id ({0}) is geen geldig id. - + De ingelezen Id ({0}) bevat spaties, welke niet zijn toegestaan.