Skip to content

Commit

Permalink
Support loading IDS zips from file path.
Browse files Browse the repository at this point in the history
Improved error checking / logging
  • Loading branch information
andyward committed Aug 30, 2023
1 parent fa35cd5 commit 69101f3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
16 changes: 16 additions & 0 deletions Xbim.InformationSpecifications.NewTests/IoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
26 changes: 23 additions & 3 deletions Xbim.InformationSpecifications/IO/Xids.Io.xml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -493,7 +497,7 @@ static private void WriteFacetBaseElements(FacetBase cf, XmlWriter xmlWriter)
}

/// <summary>
/// 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.
/// </summary>
/// <param name="fileName">File name of the Xids to load</param>
/// <param name="logger">The logger to send any errors and warnings to.</param>
Expand All @@ -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);
}
}

/// <summary>
Expand Down

0 comments on commit 69101f3

Please sign in to comment.