Index: Core/Common/src/Core.Common.Utils/Builders/FileReaderErrorMessageBuilder.cs =================================================================== diff -u -r2aa9661130f20e88c41dac921ffb780573dcf799 -r36c6449038e383f66174db2c09a5bff079f758b2 --- Core/Common/src/Core.Common.Utils/Builders/FileReaderErrorMessageBuilder.cs (.../FileReaderErrorMessageBuilder.cs) (revision 2aa9661130f20e88c41dac921ffb780573dcf799) +++ Core/Common/src/Core.Common.Utils/Builders/FileReaderErrorMessageBuilder.cs (.../FileReaderErrorMessageBuilder.cs) (revision 36c6449038e383f66174db2c09a5bff079f758b2) @@ -21,6 +21,7 @@ using System.Globalization; using Core.Common.Utils.Extensions; +using Core.Common.Utils.Properties; namespace Core.Common.Utils.Builders { @@ -50,7 +51,7 @@ public string Build(string errorMessage) { return string.Format(CultureInfo.CurrentCulture, - "Fout bij het lezen van bestand '{0}'{1}{2}: {3}", + Resources.FileReaderErrorMessageBuilder_Build_Error_while_reading_file_0_location_1_subject_2_errorMessage_3, filePath, location ?? string.Empty, subject ?? string.Empty, Index: Core/Common/src/Core.Common.Utils/Properties/Resources.Designer.cs =================================================================== diff -u -ra8bfedc443289dd37ebd3c2304d4c4add4ef7d58 -r36c6449038e383f66174db2c09a5bff079f758b2 --- Core/Common/src/Core.Common.Utils/Properties/Resources.Designer.cs (.../Resources.Designer.cs) (revision a8bfedc443289dd37ebd3c2304d4c4add4ef7d58) +++ Core/Common/src/Core.Common.Utils/Properties/Resources.Designer.cs (.../Resources.Designer.cs) (revision 36c6449038e383f66174db2c09a5bff079f758b2) @@ -22,7 +22,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.42000 +// Runtime Version:4.0.30319.17929 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -190,6 +190,16 @@ } /// + /// Looks up a localized string similar to Fout bij het lezen van bestand '{0}'{1}{2}: {3}. + /// + public static string FileReaderErrorMessageBuilder_Build_Error_while_reading_file_0_location_1_subject_2_errorMessage_3 { + get { + return ResourceManager.GetString("FileReaderErrorMessageBuilder_Build_Error_while_reading_file_0_location_1_subject" + + "_2_errorMessage_3", resourceCulture); + } + } + + /// /// Looks up a localized string similar to Er is een fout opgetreden bij het verwijderen van bestanden in de map '{0}'.. /// public static string FileUtils_DeleteOldFiles_Error_occurred_deleting_files_in_folder_0 { Index: Core/Common/src/Core.Common.Utils/Properties/Resources.resx =================================================================== diff -u -ra8bfedc443289dd37ebd3c2304d4c4add4ef7d58 -r36c6449038e383f66174db2c09a5bff079f758b2 --- Core/Common/src/Core.Common.Utils/Properties/Resources.resx (.../Resources.resx) (revision a8bfedc443289dd37ebd3c2304d4c4add4ef7d58) +++ Core/Common/src/Core.Common.Utils/Properties/Resources.resx (.../Resources.resx) (revision 36c6449038e383f66174db2c09a5bff079f758b2) @@ -162,4 +162,7 @@ Slechts de volgende waardes worden geaccepteerd: {0}. + + Fout bij het lezen van bestand '{0}'{1}{2}: {3} + \ No newline at end of file Index: Core/Components/src/Core.Components.Gis.IO/Importers/FeatureBasedMapDataImporter.cs =================================================================== diff -u -r4a06a70fe07352d26475ba7de536e7f3bf0eaa3c -r36c6449038e383f66174db2c09a5bff079f758b2 --- Core/Components/src/Core.Components.Gis.IO/Importers/FeatureBasedMapDataImporter.cs (.../FeatureBasedMapDataImporter.cs) (revision 4a06a70fe07352d26475ba7de536e7f3bf0eaa3c) +++ Core/Components/src/Core.Components.Gis.IO/Importers/FeatureBasedMapDataImporter.cs (.../FeatureBasedMapDataImporter.cs) (revision 36c6449038e383f66174db2c09a5bff079f758b2) @@ -19,8 +19,14 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. +using System; +using System.IO; using Core.Common.Base.IO; +using Core.Common.IO.Exceptions; +using Core.Common.IO.Readers; +using Core.Common.Utils.Builders; using Core.Components.Gis.Data; +using Core.Components.Gis.IO.Properties; using DotSpatial.Data; using log4net; using ILog = log4net.ILog; @@ -45,7 +51,67 @@ public override bool Import() { + Canceled = false; + + ReadResult readResult = ReadFeatureBasedMapData(); + if (readResult.CriticalErrorOccurred) + { + return false; + } + + if (Canceled) + { + HandleUserCancellingImport(); + return false; + } return true; } + + private ReadResult ReadFeatureBasedMapData() + { + try + { + var featureSet = Shapefile.OpenFile(FilePath); + + var readResult = new ReadResult(false); + + return readResult; + } + catch (ArgumentException) + { + return HandleCriticalFileReadError(Resources.FeatureBasedMapDataImporter_Import_File_does_not_contain_geometries); + } + catch (FileNotFoundException) + { + return HandleCriticalFileReadError(Resources.FeatureBasedMapDataImporter_Import_File_does_not_exist_or_misses_needed_files); + } + catch (IOException) + { + return HandleCriticalFileReadError(Resources.FeatureBasedMapDataImporter_Import_An_error_occurred_when_trying_to_read_the_file); + } + catch (CriticalFileReadException e) + { + return HandleCriticalFileReadError(e.Message); + } + catch (Exception) + { + // Because NullReferenceException or NotImplementedException when reading in a corrupt shape file + // from a third party library is expected, we catch all the exceptions here. + return HandleCriticalFileReadError(Resources.FeatureBasedMapDataImporter_Import_An_error_occurred_when_trying_to_read_the_file); + } + } + + private ReadResult HandleCriticalFileReadError(string message) + { + var errorMessage = string.Format(Resources.FeatureBasedMapDataImporter_HandleCriticalFileReadError_Error_0_no_maplayer_imported, + new FileReaderErrorMessageBuilder(FilePath).Build(message)); + log.Error(errorMessage); + return new ReadResult(true); + } + + private static void HandleUserCancellingImport() + { + log.Info("Kaartlaag toevoegen geannuleerd."); + } } } \ No newline at end of file Index: Core/Components/src/Core.Components.Gis.IO/Properties/Resources.Designer.cs =================================================================== diff -u -r7da60d6a03b1443834aba2cf1e9857351acb5699 -r36c6449038e383f66174db2c09a5bff079f758b2 --- Core/Components/src/Core.Components.Gis.IO/Properties/Resources.Designer.cs (.../Resources.Designer.cs) (revision 7da60d6a03b1443834aba2cf1e9857351acb5699) +++ Core/Components/src/Core.Components.Gis.IO/Properties/Resources.Designer.cs (.../Resources.Designer.cs) (revision 36c6449038e383f66174db2c09a5bff079f758b2) @@ -22,7 +22,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.42000 +// Runtime Version:4.0.30319.17929 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -82,6 +82,54 @@ } /// + /// Looks up a localized string similar to {0} + ///Er is geen kaartlaag geïmporteerd.. + /// + internal static string FeatureBasedMapDataImporter_HandleCriticalFileReadError_Error_0_no_maplayer_imported { + get { + return ResourceManager.GetString("FeatureBasedMapDataImporter_HandleCriticalFileReadError_Error_0_no_maplayer_impor" + + "ted", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Het bestand kon niet worden geopend. Mogelijk is het bestand corrupt of in gebruik door een andere applicatie.. + /// + internal static string FeatureBasedMapDataImporter_Import_An_error_occurred_when_trying_to_read_the_file { + get { + return ResourceManager.GetString("FeatureBasedMapDataImporter_Import_An_error_occurred_when_trying_to_read_the_file" + + "", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Kon geen geometrieën vinden in dit bestand.. + /// + internal static string FeatureBasedMapDataImporter_Import_File_does_not_contain_geometries { + get { + return ResourceManager.GetString("FeatureBasedMapDataImporter_Import_File_does_not_contain_geometries", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Het bestand of andere benodigde bestanden zijn niet gevonden.. + /// + internal static string FeatureBasedMapDataImporter_Import_File_does_not_exist_or_misses_needed_files { + get { + return ResourceManager.GetString("FeatureBasedMapDataImporter_Import_File_does_not_exist_or_misses_needed_files", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to De data in het shapebestand wordt niet ondersteund.. + /// + internal static string MapLegendView_CheckDataFormat_ShapeFile_Contains_Unsupported_Data { + get { + return ResourceManager.GetString("MapLegendView_CheckDataFormat_ShapeFile_Contains_Unsupported_Data", resourceCulture); + } + } + + /// /// Looks up a localized string similar to Kon geen punten vinden in dit bestand.. /// internal static string PointShapeFileReader_File_contains_geometries_not_points { Index: Core/Components/src/Core.Components.Gis.IO/Properties/Resources.resx =================================================================== diff -u -r7da60d6a03b1443834aba2cf1e9857351acb5699 -r36c6449038e383f66174db2c09a5bff079f758b2 --- Core/Components/src/Core.Components.Gis.IO/Properties/Resources.resx (.../Resources.resx) (revision 7da60d6a03b1443834aba2cf1e9857351acb5699) +++ Core/Components/src/Core.Components.Gis.IO/Properties/Resources.resx (.../Resources.resx) (revision 36c6449038e383f66174db2c09a5bff079f758b2) @@ -141,4 +141,20 @@ Een feature mag maar één geometrie bevatten. + + {0} +Er is geen kaartlaag geïmporteerd. + + + Het bestand kon niet worden geopend. Mogelijk is het bestand corrupt of in gebruik door een andere applicatie. + + + Kon geen geometrieën vinden in dit bestand. + + + Het bestand of andere benodigde bestanden zijn niet gevonden. + + + De data in het shapebestand wordt niet ondersteund. + \ No newline at end of file Index: Core/Components/test/Core.Components.Gis.IO.Test/Importers/FeatureBasedMapDataImporterTest.cs =================================================================== diff -u -r8e73bf849271aa3d92c161d88e8f2400a52cf65a -r36c6449038e383f66174db2c09a5bff079f758b2 --- Core/Components/test/Core.Components.Gis.IO.Test/Importers/FeatureBasedMapDataImporterTest.cs (.../FeatureBasedMapDataImporterTest.cs) (revision 8e73bf849271aa3d92c161d88e8f2400a52cf65a) +++ Core/Components/test/Core.Components.Gis.IO.Test/Importers/FeatureBasedMapDataImporterTest.cs (.../FeatureBasedMapDataImporterTest.cs) (revision 36c6449038e383f66174db2c09a5bff079f758b2) @@ -19,7 +19,10 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. +using System; +using System.IO; using Core.Common.Base.IO; +using Core.Common.TestUtil; using Core.Components.Gis.Data; using Core.Components.Gis.IO.Importers; using NUnit.Framework; @@ -41,5 +44,86 @@ // Assert Assert.IsInstanceOf>(importer); } + + [Test] + public void Import_ShapefileDoesNotExist_CancelImportWithErrorMessage() + { + // Setup + var path = TestHelper.GetTestDataPath(TestDataPath.Core.Components.Gis.IO, "I_dont_exist.shp"); + var mapDataCollection = new MapDataCollection("test"); + var importer = new FeatureBasedMapDataImporter(mapDataCollection, path); + + // Call + bool importSuccesful = true; + Action call = () => importSuccesful = importer.Import(); + + // Assert + var expectedMessage = string.Format(@"Fout bij het lezen van bestand '{0}': het bestand of andere benodigde bestanden zijn niet gevonden.", path) + Environment.NewLine + + "Er is geen kaartlaag geïmporteerd."; + TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); + Assert.IsFalse(importSuccesful); + } + + [Test] + public void Import_ShapeFileEmptyFile_CancelImportWithErrorMessage() + { + // Setup + var path = TestHelper.GetTestDataPath(TestDataPath.Core.Components.Gis.IO, "EmptyFile.shp"); + var mapDataCollection = new MapDataCollection("test"); + var importer = new FeatureBasedMapDataImporter(mapDataCollection, path); + + // Call + bool importSuccesful = true; + Action call = () => importSuccesful = importer.Import(); + + // Assert + var expectedMessage = string.Format(@"Fout bij het lezen van bestand '{0}': kon geen geometrieën vinden in dit bestand.", path) + Environment.NewLine + + "Er is geen kaartlaag geïmporteerd."; + TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); + Assert.IsFalse(importSuccesful); + } + + [Test] + public void Import_ShapeFileCorrupt_CancelImportWithErrorMessage() + { + // Setup + var path = TestHelper.GetTestDataPath(TestDataPath.Core.Components.Gis.IO, "CorruptFile.shp"); + var mapDataCollection = new MapDataCollection("test"); + var importer = new FeatureBasedMapDataImporter(mapDataCollection, path); + + // Call + bool importSuccesful = true; + Action call = () => importSuccesful = importer.Import(); + + // Assert + const string message = @"Fout bij het lezen van bestand '{0}': het bestand kon niet worden geopend. Mogelijk is het bestand corrupt of in gebruik door een andere applicatie."; + var expectedMessage = string.Format(message, path) + Environment.NewLine + + "Er is geen kaartlaag geïmporteerd."; + TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); + Assert.IsFalse(importSuccesful); + } + + [Test] + public void Import_ShapeFileInUse_CancelImportWithErrorMessage() + { + // Setup + var path = TestHelper.GetTestDataPath(TestDataPath.Core.Components.Gis.IO, "Single_Point_with_ID.shp"); + var mapDataCollection = new MapDataCollection("test"); + var importer = new FeatureBasedMapDataImporter(mapDataCollection, path); + + using (new StreamReader(new FileStream(path, FileMode.Open))) + { + // Call + bool importSuccesful = true; + Action call = () => importSuccesful = importer.Import(); + + // Assert + const string message = @"Fout bij het lezen van bestand '{0}': het bestand kon niet worden geopend. Mogelijk is het bestand corrupt of in gebruik door een andere applicatie."; + var expectedMessage = string.Format(message, path) + Environment.NewLine + + "Er is geen kaartlaag geïmporteerd."; + TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); + Assert.IsFalse(importSuccesful); + } + } } } \ No newline at end of file