From 003f5c3a49025cff9e91604d3de42c2060f2ba8e Mon Sep 17 00:00:00 2001 From: Marta <84308731+martafullen@users.noreply.github.com> Date: Wed, 17 Apr 2024 09:20:44 +0200 Subject: [PATCH] =?UTF-8?q?fixing=20namespace=20for=20relationships=20and?= =?UTF-8?q?=20making=20it=20backwards=20compatible=E2=80=A6=20(#197)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fixing namespace for relationships and making it backwards compatible (#690) * added fix for bug regarding two relationship entries * Support for "aas-suppl" from V2 to V3 --------- Co-authored-by: AlexanderWollbrink Co-authored-by: juileetikekar --- src/AasxCsharpLibrary/AdminShellPackageEnv.cs | 129 ++++++++++++++++-- 1 file changed, 116 insertions(+), 13 deletions(-) diff --git a/src/AasxCsharpLibrary/AdminShellPackageEnv.cs b/src/AasxCsharpLibrary/AdminShellPackageEnv.cs index be34eb05..14316aba 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,26 @@ 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 + //first try to find it without www, the "updated" ns 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); @@ -410,9 +425,23 @@ private static (AasCore.Aas3_0.Environment, Package) LoadPackageAasx(string fn, break; } - if (specPart == null) - throw (new Exception("Unable to find AASX spec(s). Aborting!")); - + //if its still null, then try with the old + //this is to make it backwards compatible + if (specPart == null) { + 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 { @@ -752,6 +781,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() == "/") { @@ -776,7 +825,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 @@ -792,8 +841,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 + originPart.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) @@ -836,7 +907,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! @@ -875,6 +946,34 @@ public bool SaveAs(string fn, bool writeFreshly = false, SerializationFormat pre } } + //Handling of aas_suppl namespace from v2 to v3 + //Need to check/test in detail, with thumbnails as well + if(specPart != null) + { + + xs = specPart.GetRelationshipsByType("http://www.admin-shell.io/aasx/relationships/aas-suppl"); + if(xs != null) + { + foreach(var x in xs.ToList()) + { + var uri = x.TargetUri; + PackagePart filePart = null; + var absoluteURI = PackUriHelper.ResolvePartUri(x.SourceUri, x.TargetUri); + if (package.PartExists(absoluteURI)) + { + filePart = package.GetPart(absoluteURI); + } + //delete old type, because its not according to spec or something + //then replace with the current type + specPart.DeleteRelationship(x.Id); + specPart.CreateRelationship( + filePart.Uri, TargetMode.Internal, + "http://admin-shell.io/aasx/relationships/aas-suppl"); + } + } + } + + // there might be pending files to be deleted (first delete, then add, // in case of identical files in both categories) foreach (var psfDel in _pendingFilesToDelete) @@ -883,7 +982,10 @@ public bool SaveAs(string fn, bool writeFreshly = false, SerializationFormat pre var found = false; // normal files - xs = specPart.GetRelationshipsByType("http://www.admin-shell.io/aasx/relationships/aas-suppl"); + xs = specPart.GetRelationshipsByType("http://admin-shell.io/aasx/relationships/aas-suppl"); + //if its not found, use the "old" namespace, this is to make it backwards compatible + if(xs == null) xs = specPart.GetRelationshipsByType("http://www.admin-shell.io/aasx/relationships/aas-suppl"); + foreach (var x in xs) if (x.TargetUri == psfDel.Uri) { @@ -933,8 +1035,8 @@ public bool SaveAs(string fn, bool writeFreshly = false, SerializationFormat pre PackagePart filePart = null; if (psfAdd.SpecialHandling == AdminShellPackageSupplementaryFile.SpecialHandlingType.None) { - xs = specPart.GetRelationshipsByType( - "http://www.admin-shell.io/aasx/relationships/aas-suppl"); + xs = specPart.GetRelationshipsByType("http://admin-shell.io/aasx/relationships/aas-suppl"); + if(xs == null) xs = specPart.GetRelationshipsByType("http://www.admin-shell.io/aasx/relationships/aas-suppl"); foreach (var x in xs) if (x.TargetUri == psfAdd.Uri) { @@ -984,7 +1086,7 @@ public bool SaveAs(string fn, bool writeFreshly = false, SerializationFormat pre AdminShellPackageSupplementaryFile.SpecialHandlingType.None) specPart.CreateRelationship( filePart.Uri, TargetMode.Internal, - "http://www.admin-shell.io/aasx/relationships/aas-suppl"); + "http://admin-shell.io/aasx/relationships/aas-suppl"); if (psfAdd.SpecialHandling == AdminShellPackageSupplementaryFile.SpecialHandlingType.EmbedAsThumbnail) package.CreateRelationship( @@ -1434,7 +1536,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() == "/") { @@ -1467,6 +1569,7 @@ public ListOfAasSupplementaryFile GetListOfSupplementaryFiles() { // get the supplementaries from the package, derived from spec xs = specPart.GetRelationshipsByType("http://www.admin-shell.io/aasx/relationships/aas-suppl"); + if(xs == null) xs = specPart.GetRelationshipsByType("http://www.admin-shell.io/aasx/relationships/aas-suppl"); foreach (var x in xs) { result.Add(