From 69101f3ff30e43b13fb77f33b98feef97bb31b26 Mon Sep 17 00:00:00 2001 From: Andy Ward Date: Wed, 30 Aug 2023 14:55:22 +0100 Subject: [PATCH] Support loading IDS zips from file path. Improved error checking / logging --- .../IoTests.cs | 16 ++++++++++++ .../IO/Xids.Io.xml.cs | 26 ++++++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/Xbim.InformationSpecifications.NewTests/IoTests.cs b/Xbim.InformationSpecifications.NewTests/IoTests.cs index 708816b..f9788d5 100644 --- a/Xbim.InformationSpecifications.NewTests/IoTests.cs +++ b/Xbim.InformationSpecifications.NewTests/IoTests.cs @@ -237,6 +237,22 @@ public void CanLoadXmlAsZip() newIds!.SpecificationsGroups.Should().HaveCount(2); } + [Fact] + public void CanLoadXmlFromZipFile() + { + Xids? x = BuildMultiSpecGroupIDS(); + var tempXmlFile = Path.ChangeExtension(Path.GetTempFileName(), "zip"); + using (var fs = new FileStream(tempXmlFile, FileMode.Create)) + { + x.ExportBuildingSmartIDS(fs); + } + + var newIds = Xids.LoadBuildingSmartIDS(tempXmlFile); + + newIds.Should().NotBeNull(); + newIds!.SpecificationsGroups.Should().HaveCount(2); + } + private static Xids BuildMultiSpecGroupIDS() { var file = new FileInfo(@"bsFiles/bsFilesSelf/TestFile.ids"); diff --git a/Xbim.InformationSpecifications/IO/Xids.Io.xml.cs b/Xbim.InformationSpecifications/IO/Xids.Io.xml.cs index 223bf00..53e7f52 100644 --- a/Xbim.InformationSpecifications/IO/Xids.Io.xml.cs +++ b/Xbim.InformationSpecifications/IO/Xids.Io.xml.cs @@ -473,6 +473,10 @@ static private void WriteFacetBaseElements(FacetBase cf, XmlWriter xmlWriter) logger?.LogError(ex, "Failed to load IDS file from zip stream"); } } + if(!xids.AllSpecifications().Any()) + { + logger?.LogWarning("No specifications found in this zip file. Ensure the zip contains *.ids files"); + } return xids; } } @@ -493,7 +497,7 @@ static private void WriteFacetBaseElements(FacetBase cf, XmlWriter xmlWriter) } /// - /// Attempts to unpersist an XIDS from a file, given the file name. + /// Attempts to unpersist an XIDS from the provider IDS XML file or zip file containing IDS files. /// /// File name of the Xids to load /// The logger to send any errors and warnings to. @@ -506,8 +510,24 @@ static private void WriteFacetBaseElements(FacetBase cf, XmlWriter xmlWriter) logger?.LogError("File '{fileName}' not found from executing directory '{fullDirectoryName}'", fileName, d.FullName); return null; } - var main = XElement.Parse(File.ReadAllText(fileName)); - return LoadBuildingSmartIDS(main, logger); + if(fileName.EndsWith(".zip", StringComparison.InvariantCultureIgnoreCase)) + { + using var stream = File.OpenRead(fileName); + if(IsZipped(stream)) + { + return LoadBuildingSmartIDS(stream, logger); + } + else + { + logger?.LogError("Not a valid zip file"); + return null; + } + } + else + { + var main = XElement.Parse(File.ReadAllText(fileName)); + return LoadBuildingSmartIDS(main, logger); + } } ///