Index: DamClients/DamUI/trunk/src/Dam/Deltares.Dam.Tests/CsvParserTest.cs =================================================================== diff -u -r4259 -r4287 --- DamClients/DamUI/trunk/src/Dam/Deltares.Dam.Tests/CsvParserTest.cs (.../CsvParserTest.cs) (revision 4259) +++ DamClients/DamUI/trunk/src/Dam/Deltares.Dam.Tests/CsvParserTest.cs (.../CsvParserTest.cs) (revision 4287) @@ -39,7 +39,7 @@ { testFile = "test.csv"; using StreamWriter writer = File.CreateText(testFile); - writer.WriteLine("name;age"); //<- header contains name and age + writer.WriteLine("name;age"); writer.WriteLine("Anouar;31"); writer.WriteLine("Barry;39"); writer.WriteLine("Tom;48"); @@ -58,26 +58,25 @@ [Test] public void ThrowsArgumentExceptionWhenHeaderStringIsNullByCallingParseHeader() { - var expectedMessage = "The header in the import file is not valid"; - Assert.That(() => CsvParser.ParseHeader(null, new Regex("")), Throws.ArgumentException.With.Message.EqualTo(expectedMessage)); + const string expectedMessage = "The header in the import file is not valid"; + Assert.That(() => CsvParser.ParseHeader(null, new Regex("")), Throws.InstanceOf().With.Message.EqualTo(expectedMessage)); } [Test] public void ThrowsArgumentExceptionSplitterIsNull() { string expectedMessage = "The given pattern to split the data values of the CSV files is not valid" + Environment.NewLine + "Parameter name: splitter"; - Assert.That(() => CsvParser.ParseHeader("test;test", null), Throws.ArgumentNullException.With.Message.EqualTo(expectedMessage)); + Assert.That(() => CsvParser.ParseHeader("test;test", null), Throws.InstanceOf().With.Message.EqualTo(expectedMessage)); } [Test] public void ThrowsWhenSplitPatternIsEmpty() { - var expectedMessage = "The given pattern to split the data values of the CSV files is not valid"; - Assert.That(() => CsvParser.ParseHeader("test", new Regex("")), Throws.ArgumentException.With.Message.EqualTo(expectedMessage)); + const string expectedMessage = "The given pattern to split the data values of the CSV files is not valid"; + Assert.That(() => CsvParser.ParseHeader("test", new Regex("")), Throws.InstanceOf().With.Message.EqualTo(expectedMessage)); } [Test] - [Ignore("[ExpectedException(typeof(CsvParserException))]")] public void ThrowsWhenNoHeaderItemsAreParsed() { Assert.That(() => CsvParser.ParseHeader("test", new Regex("")), Throws.InstanceOf()); @@ -154,7 +153,7 @@ { using (StreamWriter writer = File.CreateText(testFile)) { - writer.WriteLine("name;age"); //<- header contains name and age + writer.WriteLine("name;age"); writer.WriteLine("Barry;39;"); } @@ -169,7 +168,7 @@ { using (StreamWriter writer = File.CreateText(testFile)) { - writer.WriteLine("name;age"); //<- header contains name and age + writer.WriteLine("name;age"); writer.WriteLine("Anouar;31"); writer.WriteLine("Barry;39"); writer.WriteLine("Tom;48"); @@ -180,7 +179,7 @@ loader.Add("name", (entity, value) => entity.Name = value); loader.Add("age", (entity, value) => entity.Age = int.Parse(value)); List items = loader.LoadFromCsvFile(testFile).ToList(); - Assert.AreEqual(4, items.Count()); + Assert.AreEqual(4, items.Count); Assert.AreEqual("Tom", items.ElementAt(2).Name); Assert.AreEqual(48, items.ElementAt(2).Age); } Index: DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/CsvParser.cs =================================================================== diff -u -r4277 -r4287 --- DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/CsvParser.cs (.../CsvParser.cs) (revision 4277) +++ DamClients/DamUI/trunk/src/DamClientsLibrary/Deltares.Dam.Data/CsvParser.cs (.../CsvParser.cs) (revision 4287) @@ -28,53 +28,61 @@ namespace Deltares.Dam.Data; /// -/// Parses a csv file and returns a sequence of entities of a specific type +/// Parses a csv file and returns a sequence of entities of a specific type /// public static class CsvParser { /// - /// This regex splits semicolon separated lists of optionally quoted strings. It handles quoted delimiters and escaped quotes. - /// Whitespace inside quotes is preserved, outside is eaten. - /// See: http://regexlib.com/REDetails.aspx?regexp_id=1325 + /// This regex splits semicolon separated lists of optionally quoted strings. It handles quoted delimiters and escaped + /// quotes. + /// Whitespace inside quotes is preserved, outside is eaten. + /// See: http://regexlib.com/REDetails.aspx?regexp_id=1325 /// public static readonly string CsvSplitPatternSemicolonOnly = @"\s*[;]\s*(?!(?<=(?:^|[;])\s*""(?:[^""]|""""|\\"")*[;]\s*)(?:[^""]|""""|\\"")*""\s*(?:[;]|$))"; /// - /// Parses the header of an csv import file + /// Parses the header of an csv import file /// /// The header string /// The regular expression /// An array of strings containing the header names public static string[] ParseHeader(string header, Regex splitter) { - ThrowHelper.ThrowIfStringArgumentNullOrEmpty(header, StringResourceNames.CsvHeaderNullOrEmpty); - ThrowHelper.ThrowIfArgumentNull(splitter, "splitter", StringResourceNames.CsvSplitterPatternNotValid); - ThrowHelper.ThrowIfStringArgumentNullOrEmpty(splitter.ToString(), StringResourceNames.CsvSplitterPatternNotValid); + try + { + ThrowHelper.ThrowIfStringArgumentNullOrEmpty(header, StringResourceNames.CsvHeaderNullOrEmpty); + ThrowHelper.ThrowIfArgumentNull(splitter, "splitter", StringResourceNames.CsvSplitterPatternNotValid); + ThrowHelper.ThrowIfStringArgumentNullOrEmpty(splitter.ToString(), StringResourceNames.CsvSplitterPatternNotValid); + } + catch (Exception e) + { + throw new CsvParserException(e.Message); + } return splitter.Split(header); } /// - /// Loads entities of an specific type from a csv file + /// Loads entities of an specific type from a csv file /// /// The type to construct /// The object materializer (property setters) /// The file name to parse - /// A sequance containing materialized intances of the type to construct from the csv data + /// A sequence containing materialized instances of the type to construct from the csv data public static IEnumerable LoadFromCsvFile(this ObjectMaterializer setter, string fileName) where T : new() { return LoadFromCsvFile(setter, fileName, true); } /// - /// Loads entities of an specific type from a csv file. Also returns the parsed headers + /// Loads entities of an specific type from a csv file. Also returns the parsed headers /// /// The type to construct /// The object materializer (property setters) /// The file name to parse - /// A sequance containing materialized intances of the type to construct from the csv data + /// A sequance containing materialized intances of the type to construct from the csv data public static IEnumerable LoadFromCsvFile(this ObjectMaterializer setter, string fileName, ref string[] headers) where T : new() { @@ -97,42 +105,42 @@ } /// - /// Loads entities of an specific type from a csv file + /// Loads entities of an specific type from a csv file /// /// The type to construct /// The object materializer (property setters) /// The file name to parse /// The regular expression for splitting the items in a string - /// A sequance containing materialized intances of the type to construct from the csv data + /// A sequance containing materialized intances of the type to construct from the csv data public static IEnumerable LoadFromCsvFile(this ObjectMaterializer setter, string fileName, string splitPattern) where T : new() { return LoadFromCsvFile(setter, fileName, true, splitPattern); } /// - /// Loads entities of an specific type from a csv file + /// Loads entities of an specific type from a csv file /// /// The type to construct /// The object materializer (property setters) /// The file name to parse /// Indicates if the first line in the csv file contains the header info - /// A sequance containing materialized intances of the type to construct from the csv data + /// A sequance containing materialized intances of the type to construct from the csv data public static IEnumerable LoadFromCsvFile(this ObjectMaterializer setter, string fileName, bool headerInfoInFirstLine) where T : new() { return LoadFromCsvFile(setter, fileName, true, CsvSplitPatternSemicolonOnly); } /// - /// Loads entities of an specific type from a csv file + /// Loads entities of an specific type from a csv file /// /// The type to construct /// The object materializer (property setters) /// The file name to parse /// Indicates if the first line in the csv file contains the header info /// The regular expression for splitting the items in a string - /// A sequance containing materialized intances of the type to construct from the csv data + /// A sequance containing materialized intances of the type to construct from the csv data public static IEnumerable LoadFromCsvFile(this ObjectMaterializer materializer, string fileName, bool headerInfoInFirstLine, string splitPattern) where T : new() {