@@ -334,20 +334,34 @@ private class FindRelTuple
334
334
public PackageRelationship Rel { get ; set ; }
335
335
}
336
336
337
- private static IEnumerable < FindRelTuple > FindAllRelationships ( Package package , string [ ] relTypes )
337
+ private static IEnumerable < FindRelTuple > FindAllRelationships (
338
+ Package package , string [ ] relTypes ,
339
+ bool ? filterForDeprecatedEquals = null )
338
340
{
339
- for ( int i = 0 ; i < relTypes . Length ; i ++ )
341
+ for ( int i = 0 ; i < relTypes . Length ; i ++ )
340
342
foreach ( var x in package . GetRelationshipsByType ( relTypes [ i ] ) )
341
- yield return new FindRelTuple ( ) { Deprecated = ( i > 0 ) , Rel = x } ;
343
+ {
344
+ var res = new FindRelTuple ( ) { Deprecated = ( i > 0 ) , Rel = x } ;
345
+ if ( filterForDeprecatedEquals . HasValue &&
346
+ filterForDeprecatedEquals . Value != res . Deprecated )
347
+ continue ;
348
+ yield return res ;
349
+ }
342
350
}
343
351
344
352
private static IEnumerable < FindRelTuple > FindAllRelationships (
345
353
PackagePart part , string [ ] relTypes ,
346
- bool onlyDeprecated = false )
354
+ bool ? filterForDeprecatedEquals = null )
347
355
{
348
- for ( int i = ( onlyDeprecated ? 1 : 0 ) ; i < relTypes . Length ; i ++ )
356
+ for ( int i = 0 ; i < relTypes . Length ; i ++ )
349
357
foreach ( var x in part . GetRelationshipsByType ( relTypes [ i ] ) )
350
- yield return new FindRelTuple ( ) { Deprecated = ( i > 0 ) , Rel = x } ;
358
+ {
359
+ var res = new FindRelTuple ( ) { Deprecated = ( i > 0 ) , Rel = x } ;
360
+ if ( filterForDeprecatedEquals . HasValue &&
361
+ filterForDeprecatedEquals . Value != res . Deprecated )
362
+ continue ;
363
+ yield return res ;
364
+ }
351
365
}
352
366
353
367
private static AasCore . Aas3_0 . Environment LoadXml ( string fn )
@@ -772,41 +786,52 @@ public bool SaveAs(string fn, bool writeFreshly = false, SerializationFormat pre
772
786
}
773
787
_fn = fn ;
774
788
775
- // get the origin from the package
776
- PackagePart originPart = null ;
777
- foreach ( var x in FindAllRelationships ( package , relTypesOrigin ) )
789
+ // first check, if we have deprecated origin
790
+ foreach ( var x in FindAllRelationships ( package , relTypesOrigin ,
791
+ filterForDeprecatedEquals : true ) )
778
792
if ( x . Rel . SourceUri . ToString ( ) == "/" )
779
793
{
780
- //originPart = package.GetPart(x.TargetUri);
781
-
782
794
var absoluteURI = PackUriHelper . ResolvePartUri ( x . Rel . SourceUri , x . Rel . TargetUri ) ;
783
795
if ( package . PartExists ( absoluteURI ) )
784
796
{
785
- originPart = package . GetPart ( absoluteURI ) ;
797
+ var tempPart = package . GetPart ( absoluteURI ) ;
786
798
787
- // check if it a deprecated URI
788
- if ( x . Deprecated )
789
- {
790
- //delete old type, because its not according to spec or something
791
- //then replace with the current type
792
- package . DeleteRelationship ( x . Rel . Id ) ;
793
- package . DeletePart ( absoluteURI ) ;
794
- //package.CreateRelationship(
795
- // originPart.Uri, TargetMode.Internal,
796
- // relTypesOrigin.FirstOrDefault());
797
- originPart = null ;
798
- break ;
799
- }
799
+ // delete old type, because its not according to spec or something
800
+ // then replace with the current type
801
+ package . DeleteRelationship ( x . Rel . Id ) ;
802
+ package . CreateRelationship (
803
+ tempPart . Uri , TargetMode . Internal ,
804
+ relTypesOrigin . FirstOrDefault ( ) ) ;
805
+ break ;
800
806
}
801
807
}
802
808
809
+ // now check, if the origin could be found with correct relationships
810
+ PackagePart originPart = null ;
811
+ foreach ( var x in FindAllRelationships ( package , relTypesOrigin ,
812
+ filterForDeprecatedEquals : false ) )
813
+ if ( x . Rel . SourceUri . ToString ( ) == "/" )
814
+ {
815
+ var absoluteURI = PackUriHelper . ResolvePartUri ( x . Rel . SourceUri , x . Rel . TargetUri ) ;
816
+ if ( package . PartExists ( absoluteURI ) )
817
+ {
818
+ originPart = package . GetPart ( absoluteURI ) ;
819
+ }
820
+ }
821
+
803
822
// MIHO, 2024-05-29
804
823
// fix the case, that part exists but is not really associated by a
805
824
// relationship
806
825
var uriOrigin = new Uri ( "/aasx/aasx-origin" , UriKind . RelativeOrAbsolute ) ;
807
- if ( package . PartExists ( uriOrigin ) )
826
+ if ( originPart == null && package . PartExists ( uriOrigin ) )
808
827
{
828
+ // get the part
809
829
originPart = package . GetPart ( uriOrigin ) ;
830
+
831
+ // make the relationship
832
+ package . CreateRelationship (
833
+ originPart . Uri , TargetMode . Internal ,
834
+ relTypesOrigin . FirstOrDefault ( ) ) ;
810
835
}
811
836
812
837
if ( originPart == null )
@@ -825,35 +850,40 @@ public bool SaveAs(string fn, bool writeFreshly = false, SerializationFormat pre
825
850
relTypesOrigin . FirstOrDefault ( ) ) ;
826
851
}
827
852
828
- // get the specs from the package
853
+ // get the specs from the package, first again: deprecated
854
+ foreach ( var x in FindAllRelationships (
855
+ originPart , relTypesSpec ,
856
+ filterForDeprecatedEquals : true ) )
857
+ {
858
+ var absoluteURI = PackUriHelper . ResolvePartUri ( x . Rel . SourceUri , x . Rel . TargetUri ) ;
859
+ if ( package . PartExists ( absoluteURI ) )
860
+ {
861
+ var tempPart = package . GetPart ( absoluteURI ) ;
862
+
863
+ //delete old type, because its not according to spec or something
864
+ //then replace with the current type
865
+ originPart . DeleteRelationship ( x . Rel . Id ) ;
866
+ originPart . CreateRelationship (
867
+ tempPart . Uri , TargetMode . Internal ,
868
+ relTypesSpec . FirstOrDefault ( ) ) ;
869
+ break ;
870
+ }
871
+ }
872
+
873
+ // now check, if the specs could be found with correct relationships
829
874
PackagePart specPart = null ;
830
875
PackageRelationship specRel = null ;
831
- foreach ( var x in FindAllRelationships ( originPart , relTypesSpec ) )
876
+ foreach ( var x in FindAllRelationships ( originPart , relTypesSpec ,
877
+ filterForDeprecatedEquals : false ) )
832
878
{
833
879
specRel = x . Rel ;
834
- //specPart = package.GetPart(x.TargetUri);
835
880
var absoluteURI = PackUriHelper . ResolvePartUri ( x . Rel . SourceUri , x . Rel . TargetUri ) ;
836
881
if ( package . PartExists ( absoluteURI ) )
837
882
{
838
883
specPart = package . GetPart ( absoluteURI ) ;
839
-
840
- // check if it a deprecated URI
841
- if ( x . Deprecated )
842
- {
843
- //delete old type, because its not according to spec or something
844
- //then replace with the current type
845
- package . DeleteRelationship ( x . Rel . Id ) ;
846
- package . DeletePart ( absoluteURI ) ;
847
- //package.CreateRelationship(
848
- // specPart.Uri, TargetMode.Internal,
849
- // relTypesSpec.FirstOrDefault());
850
- specPart = null ;
851
- specRel = null ;
852
- break ;
853
- }
854
884
}
855
885
}
856
-
886
+
857
887
// check, if we have to change the spec part
858
888
if ( specPart != null && specRel != null )
859
889
{
@@ -941,11 +971,13 @@ public bool SaveAs(string fn, bool writeFreshly = false, SerializationFormat pre
941
971
}
942
972
}
943
973
944
- //Handling of aas_suppl namespace from v2 to v3
945
- //Need to check/test in detail, with thumbnails as well
946
- if ( specPart != null )
974
+ // Handling of aas_suppl namespace from v2 to v3
975
+ // Need to check/test in detail, with thumbnails as well
976
+ if ( specPart != null )
947
977
{
948
- foreach ( var x in FindAllRelationships ( specPart , relTypesSuppl , onlyDeprecated : true ) . ToList ( ) )
978
+ foreach ( var x in FindAllRelationships (
979
+ specPart , relTypesSuppl ,
980
+ filterForDeprecatedEquals : true ) . ToList ( ) )
949
981
{
950
982
var uri = x . Rel . TargetUri ;
951
983
PackagePart filePart = null ;
0 commit comments