From ca5acaa026eaad87f8aced5917d4575acf354923 Mon Sep 17 00:00:00 2001 From: AlexanderWollbrink Date: Wed, 7 Feb 2024 12:58:54 +0100 Subject: [PATCH] fixing namespace for relationships and making it backwards compatible --- src/AasxCsharpLibrary/AdminShellPackageEnv.cs | 82 +++++++++++++++++-- 1 file changed, 75 insertions(+), 7 deletions(-) diff --git a/src/AasxCsharpLibrary/AdminShellPackageEnv.cs b/src/AasxCsharpLibrary/AdminShellPackageEnv.cs index bf0a81ef..6e5a8150 100644 --- a/src/AasxCsharpLibrary/AdminShellPackageEnv.cs +++ b/src/AasxCsharpLibrary/AdminShellPackageEnv.cs @@ -380,7 +380,7 @@ private static (AasCore.Aas3_0.Environment, Package) LoadPackageAasx(string fn, // get the origin from the package PackagePart originPart = null; var xs = package.GetRelationshipsByType( - "http://www.admin-shell.io/aasx/relationships/aasx-origin"); + "http://admin-shell.io/aasx/relationships/aasx-origin"); foreach (var x in xs) if (x.SourceUri.ToString() == "/") { @@ -394,11 +394,25 @@ private static (AasCore.Aas3_0.Environment, Package) LoadPackageAasx(string fn, } if (originPart == null) - throw (new Exception("Unable to find AASX origin. Aborting!")); + xs = package.GetRelationshipsByType( + "http://www.admin-shell.io/aasx/relationships/aasx-origin"); + foreach (var x in xs) + if (x.SourceUri.ToString() == "/") + { + //originPart = package.GetPart(x.TargetUri); + var absoluteURI = PackUriHelper.ResolvePartUri(x.SourceUri, x.TargetUri); + if (package.PartExists(absoluteURI)) + { + originPart = package.GetPart(absoluteURI); + } + break; + } + if (originPart == null) + throw (new Exception("Unable to find AASX origin. Aborting!")); // get the specs from the package PackagePart specPart = null; - xs = originPart.GetRelationshipsByType("http://www.admin-shell.io/aasx/relationships/aas-spec"); + xs = originPart.GetRelationshipsByType("http://admin-shell.io/aasx/relationships/aas-spec"); foreach (var x in xs) { //specPart = package.GetPart(x.TargetUri); @@ -411,7 +425,19 @@ private static (AasCore.Aas3_0.Environment, Package) LoadPackageAasx(string fn, } if (specPart == null) - throw (new Exception("Unable to find AASX spec(s). Aborting!")); + xs = originPart.GetRelationshipsByType("http://www.admin-shell.io/aasx/relationships/aas-spec"); + foreach (var x in xs) + { + //specPart = package.GetPart(x.TargetUri); + var absoluteURI = PackUriHelper.ResolvePartUri(x.SourceUri, x.TargetUri); + if (package.PartExists(absoluteURI)) + { + specPart = package.GetPart(absoluteURI); + } + break; + } + if (specPart == null) + throw (new Exception("Unable to find AASX spec(s). Aborting!")); // open spec part to read try @@ -754,6 +780,26 @@ public bool SaveAs(string fn, bool writeFreshly = false, SerializationFormat pre PackagePart originPart = null; var xs = package.GetRelationshipsByType( "http://www.admin-shell.io/aasx/relationships/aasx-origin"); + foreach (var x in xs) + if (x.SourceUri.ToString() == "/") + { + //originPart = package.GetPart(x.TargetUri); + + var absoluteURI = PackUriHelper.ResolvePartUri(x.SourceUri, x.TargetUri); + if (package.PartExists(absoluteURI)) + { + originPart = package.GetPart(absoluteURI); + } + //delete old type, because its not according to spec or something + //then replace with the current type + package.DeleteRelationship(x.Id); + package.CreateRelationship( + originPart.Uri, TargetMode.Internal, + "http://admin-shell.io/aasx/relationships/aasx-origin"); + originPart = null; + break; + } + xs = package.GetRelationshipsByType("http://admin-shell.io/aasx/relationships/aasx-origin"); foreach (var x in xs) if (x.SourceUri.ToString() == "/") { @@ -778,7 +824,7 @@ public bool SaveAs(string fn, bool writeFreshly = false, SerializationFormat pre } package.CreateRelationship( originPart.Uri, TargetMode.Internal, - "http://www.admin-shell.io/aasx/relationships/aasx-origin"); + "http://admin-shell.io/aasx/relationships/aasx-origin"); } // get the specs from the package @@ -794,8 +840,30 @@ public bool SaveAs(string fn, bool writeFreshly = false, SerializationFormat pre { specPart = package.GetPart(absoluteURI); } + //delete old type, because its not according to spec or something + //then replace with the current type + package.DeleteRelationship(x.Id); + originPart.CreateRelationship( + specPart.Uri, TargetMode.Internal, + "http://admin-shell.io/aasx/relationships/aas-spec"); + specPart = null; + specRel = null; break; } + xs = originPart.GetRelationshipsByType("http://admin-shell.io/aasx/relationships/aas-spec"); + foreach (var x in xs) + { + specRel = x; + //specPart = package.GetPart(x.TargetUri); + var absoluteURI = PackUriHelper.ResolvePartUri(x.SourceUri, x.TargetUri); + if (package.PartExists(absoluteURI)) + { + specPart = package.GetPart(absoluteURI); + } + + break; + } + // check, if we have to change the spec part if (specPart != null && specRel != null) @@ -838,7 +906,7 @@ public bool SaveAs(string fn, bool writeFreshly = false, SerializationFormat pre System.Net.Mime.MediaTypeNames.Text.Xml, CompressionOption.Maximum); originPart.CreateRelationship( specPart.Uri, TargetMode.Internal, - "http://www.admin-shell.io/aasx/relationships/aas-spec"); + "http://admin-shell.io/aasx/relationships/aas-spec"); } // now, specPart shall be != null! @@ -1432,7 +1500,7 @@ public ListOfAasSupplementaryFile GetListOfSupplementaryFiles() // get the origin from the package PackagePart originPart = null; xs = _openPackage.GetRelationshipsByType( - "http://www.admin-shell.io/aasx/relationships/aasx-origin"); + "http://admin-shell.io/aasx/relationships/aasx-origin"); foreach (var x in xs) if (x.SourceUri.ToString() == "/") {