From f2dfc31c2aa94279d1a7cf924647d466c476a6d4 Mon Sep 17 00:00:00 2001 From: fvolz Date: Thu, 31 Aug 2023 16:41:32 +0200 Subject: [PATCH 1/6] add support for thumbnails in AASXSerializer / AASXDeserializer --- .../v3/dataformat/aasx/AASXDeserializer.java | 4 ++++ .../v3/dataformat/aasx/AASXSerializer.java | 21 ++++++++++++++----- .../deserialization/AASXDeserializerTest.java | 7 ++++++- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java index d2ce12523..689a03158 100644 --- a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java +++ b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java @@ -39,6 +39,7 @@ import org.eclipse.digitaltwin.aas4j.v3.model.Submodel; import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement; import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementCollection; +import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; /** * The AASX package converter converts a aasx package into a list of aas, a list @@ -148,6 +149,9 @@ private List parseReferencedFilePathsFromAASX() throws IOException, Inva read(); List paths = new ArrayList<>(); + for (AssetAdministrationShell aas : environment.getAssetAdministrationShells()) { + paths.add(aas.getAssetInformation().getDefaultThumbnail().getPath()); + } for (Submodel sm : environment.getSubmodels()) { paths.addAll(parseElements(sm.getSubmodelElements())); } diff --git a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java index 095c964e2..4cf400afb 100644 --- a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java +++ b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java @@ -41,6 +41,7 @@ import org.eclipse.digitaltwin.aas4j.v3.model.Submodel; import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement; import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementCollection; +import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; import org.eclipse.digitaltwin.aas4j.v3.dataformat.SerializationException; import org.eclipse.digitaltwin.aas4j.v3.dataformat.aasx.internal.AASXUtils; @@ -110,7 +111,7 @@ public void write(Environment environment, Collection files, Outpu // Save the XML to aasx/xml/content.xml PackagePart xmlPart = createAASXPart(rootPackage, origin, XML_PATH, MIME_XML, AASSPEC_RELTYPE, xml.getBytes(DEFAULT_CHARSET)); - storeFilesInAASX(environment.getSubmodels(), files, rootPackage, xmlPart); + storeFilesInAASX(environment, files, rootPackage, xmlPart); saveAASX(os, rootPackage); } @@ -118,15 +119,25 @@ public void write(Environment environment, Collection files, Outpu /** * Stores the files from the Submodels in the .aasx file * - * @param submodelList the Submodels + * @param environment the Environment * @param files the content of the files * @param rootPackage the OPCPackage * @param xmlPart the Part the files should be related to */ - private void storeFilesInAASX(List submodelList, Collection files, OPCPackage rootPackage, + private void storeFilesInAASX(Environment environment, Collection files, OPCPackage rootPackage, PackagePart xmlPart) { - - for (Submodel sm : submodelList) { + for (AssetAdministrationShell aas : environment.getAssetAdministrationShells()) { + String filePath = AASXUtils.getPathFromURL(aas.getAssetInformation().getDefaultThumbnail().getPath()); + try { + InMemoryFile content = findFileByPath(files, filePath); + logger.trace("Writing file '" + filePath + "' to .aasx."); + createAASXPart(rootPackage, xmlPart, filePath, aas.getAssetInformation().getDefaultThumbnail().getContentType(), AASSUPPL_RELTYPE, content.getFileContent()); + } catch (RuntimeException e) { + // Log that a file is missing and continue building the .aasx + logger.warn("Could not add File '" + filePath + "'. It was not contained in given InMemoryFiles."); + } + } + for (Submodel sm : environment.getSubmodels()) { for (File file : findFileElements(sm.getSubmodelElements())) { String filePath = AASXUtils.getPathFromURL(file.getValue()); try { diff --git a/dataformat-aasx/src/test/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/deserialization/AASXDeserializerTest.java b/dataformat-aasx/src/test/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/deserialization/AASXDeserializerTest.java index 77425197e..0c274625e 100644 --- a/dataformat-aasx/src/test/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/deserialization/AASXDeserializerTest.java +++ b/dataformat-aasx/src/test/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/deserialization/AASXDeserializerTest.java @@ -16,6 +16,7 @@ package org.eclipse.digitaltwin.aas4j.v3.dataformat.aasx.deserialization; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.io.File; import java.io.FileInputStream; @@ -27,6 +28,7 @@ import javax.xml.parsers.ParserConfigurationException; +import org.apache.commons.collections4.CollectionUtils; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.eclipse.digitaltwin.aas4j.v3.dataformat.aasx.AASXDeserializer; import org.eclipse.digitaltwin.aas4j.v3.dataformat.aasx.AASXSerializer; @@ -50,8 +52,11 @@ public void testRoundTrip() throws SerializationException, IOException, InvalidF List fileList = new ArrayList<>(); byte[] operationManualContent = { 0, 1, 2, 3, 4 }; + byte[] thumbnail = { 0, 1, 2, 3, 4 }; InMemoryFile inMemoryFile = new InMemoryFile(operationManualContent, "file:///aasx/OperatingManual.pdf"); + InMemoryFile inMemoryFileThumbnail = new InMemoryFile(thumbnail, "file:///master/verwaltungsschale-detail-part1.png"); fileList.add(inMemoryFile); + fileList.add(inMemoryFileThumbnail); File file = tempFolder.newFile("output.aasx"); @@ -61,6 +66,6 @@ public void testRoundTrip() throws SerializationException, IOException, InvalidF AASXDeserializer deserializer = new AASXDeserializer(in); assertEquals(AASSimple.createEnvironment(), deserializer.read()); - assertEquals(fileList, deserializer.getRelatedFiles()); + assertTrue(CollectionUtils.isEqualCollection(fileList, deserializer.getRelatedFiles())); } } From 3bac665e7bb0eb4a50a2a17b3f02662ed831cbf6 Mon Sep 17 00:00:00 2001 From: fvolz Date: Thu, 31 Aug 2023 16:52:08 +0200 Subject: [PATCH 2/6] Update AASXSerializerTest.java --- .../v3/dataformat/aasx/serialization/AASXSerializerTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dataformat-aasx/src/test/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/serialization/AASXSerializerTest.java b/dataformat-aasx/src/test/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/serialization/AASXSerializerTest.java index cfce0d5c1..e8388f3a9 100644 --- a/dataformat-aasx/src/test/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/serialization/AASXSerializerTest.java +++ b/dataformat-aasx/src/test/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/serialization/AASXSerializerTest.java @@ -48,8 +48,11 @@ public class AASXSerializerTest { @Before public void setup() throws IOException { byte[] operationManualContent = { 0, 1, 2, 3, 4 }; + byte[] thumbnail = { 0, 1, 2, 3, 4 }; InMemoryFile file = new InMemoryFile(operationManualContent, "file:///aasx/OperatingManual.pdf"); + InMemoryFile inMemoryFileThumbnail = new InMemoryFile(thumbnail, "file:///master/verwaltungsschale-detail-part1.png"); fileList.add(file); + fileList.add(inMemoryFileThumbnail); } @Test From 91ec09d185a6de87db4b4c1f09e063ac239fca0b Mon Sep 17 00:00:00 2001 From: fvolz Date: Mon, 4 Sep 2023 19:56:55 +0200 Subject: [PATCH 3/6] add null check --- .../aas4j/v3/dataformat/aasx/AASXDeserializer.java | 4 ++++ .../digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java index 689a03158..f23616874 100644 --- a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java +++ b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java @@ -150,6 +150,10 @@ private List parseReferencedFilePathsFromAASX() throws IOException, Inva List paths = new ArrayList<>(); for (AssetAdministrationShell aas : environment.getAssetAdministrationShells()) { + AssetInformation information = aas.getAssetInformation(); + if(information == null || information.getDefaultThumbnail() == null || information.getDefaultThumbnail().getPath() == null) { + continue; + } paths.add(aas.getAssetInformation().getDefaultThumbnail().getPath()); } for (Submodel sm : environment.getSubmodels()) { diff --git a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java index 4cf400afb..254de6646 100644 --- a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java +++ b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java @@ -127,6 +127,10 @@ public void write(Environment environment, Collection files, Outpu private void storeFilesInAASX(Environment environment, Collection files, OPCPackage rootPackage, PackagePart xmlPart) { for (AssetAdministrationShell aas : environment.getAssetAdministrationShells()) { + AssetInformation information = aas.getAssetInformation(); + if(information == null || information.getDefaultThumbnail() == null || information.getDefaultThumbnail().getPath() == null) { + continue; + } String filePath = AASXUtils.getPathFromURL(aas.getAssetInformation().getDefaultThumbnail().getPath()); try { InMemoryFile content = findFileByPath(files, filePath); From 8c7277eda26cf6b582066d157797a283d0983923 Mon Sep 17 00:00:00 2001 From: fvolz Date: Wed, 6 Sep 2023 08:04:10 +0200 Subject: [PATCH 4/6] fix depedency --- .../digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java | 1 + .../digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java | 1 + 2 files changed, 2 insertions(+) diff --git a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java index f23616874..5a7edbd8c 100644 --- a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java +++ b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java @@ -40,6 +40,7 @@ import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement; import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementCollection; import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; +import org.eclipse.digitaltwin.aas4j.v3.model.AssetInformation; /** * The AASX package converter converts a aasx package into a list of aas, a list diff --git a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java index 254de6646..5851c4a64 100644 --- a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java +++ b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java @@ -42,6 +42,7 @@ import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement; import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementCollection; import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; +import org.eclipse.digitaltwin.aas4j.v3.model.AssetInformation; import org.eclipse.digitaltwin.aas4j.v3.dataformat.SerializationException; import org.eclipse.digitaltwin.aas4j.v3.dataformat.aasx.internal.AASXUtils; From 1e1f396e19805a1a3707ae95410000c3a1feb74d Mon Sep 17 00:00:00 2001 From: fvolz Date: Thu, 5 Oct 2023 09:28:07 +0200 Subject: [PATCH 5/6] syntax --- .../v3/dataformat/aasx/AASXDeserializer.java | 15 ++--- .../v3/dataformat/aasx/AASXSerializer.java | 56 ++++++++++--------- 2 files changed, 34 insertions(+), 37 deletions(-) diff --git a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java index 5a7edbd8c..24f82a315 100644 --- a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java +++ b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java @@ -150,16 +150,11 @@ private List parseReferencedFilePathsFromAASX() throws IOException, Inva read(); List paths = new ArrayList<>(); - for (AssetAdministrationShell aas : environment.getAssetAdministrationShells()) { - AssetInformation information = aas.getAssetInformation(); - if(information == null || information.getDefaultThumbnail() == null || information.getDefaultThumbnail().getPath() == null) { - continue; - } - paths.add(aas.getAssetInformation().getDefaultThumbnail().getPath()); - } - for (Submodel sm : environment.getSubmodels()) { - paths.addAll(parseElements(sm.getSubmodelElements())); - } + environment.getAssetAdministrationShells().stream().filter(aas -> aas.getAssetInformation() != null + || aas.getAssetInformation().getDefaultThumbnail() != null + || aas.getAssetInformation().getDefaultThumbnail().getPath() != null) + .forEach(aas -> paths.add(aas.getAssetInformation().getDefaultThumbnail().getPath())); + environment.getSubmodels().forEach(sm -> paths.addAll(parseElements(sm.getSubmodelElements()))); return paths; } diff --git a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java index 5851c4a64..cda123067 100644 --- a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java +++ b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java @@ -127,33 +127,35 @@ public void write(Environment environment, Collection files, Outpu */ private void storeFilesInAASX(Environment environment, Collection files, OPCPackage rootPackage, PackagePart xmlPart) { - for (AssetAdministrationShell aas : environment.getAssetAdministrationShells()) { - AssetInformation information = aas.getAssetInformation(); - if(information == null || information.getDefaultThumbnail() == null || information.getDefaultThumbnail().getPath() == null) { - continue; - } - String filePath = AASXUtils.getPathFromURL(aas.getAssetInformation().getDefaultThumbnail().getPath()); - try { - InMemoryFile content = findFileByPath(files, filePath); - logger.trace("Writing file '" + filePath + "' to .aasx."); - createAASXPart(rootPackage, xmlPart, filePath, aas.getAssetInformation().getDefaultThumbnail().getContentType(), AASSUPPL_RELTYPE, content.getFileContent()); - } catch (RuntimeException e) { - // Log that a file is missing and continue building the .aasx - logger.warn("Could not add File '" + filePath + "'. It was not contained in given InMemoryFiles."); - } - } - for (Submodel sm : environment.getSubmodels()) { - for (File file : findFileElements(sm.getSubmodelElements())) { - String filePath = AASXUtils.getPathFromURL(file.getValue()); - try { - InMemoryFile content = findFileByPath(files, filePath); - logger.trace("Writing file '" + filePath + "' to .aasx."); - createAASXPart(rootPackage, xmlPart, filePath, file.getContentType(), AASSUPPL_RELTYPE, content.getFileContent()); - } catch (RuntimeException e) { - // Log that a file is missing and continue building the .aasx - logger.warn("Could not add File '" + filePath + "'. It was not contained in given InMemoryFiles."); - } - } + environment.getAssetAdministrationShells().stream().filter(aas -> aas.getAssetInformation() != null + || aas.getAssetInformation().getDefaultThumbnail() != null + || aas.getAssetInformation().getDefaultThumbnail().getPath() != null) + .forEach(aas -> createParts(files, + AASXUtils.getPathFromURL(aas.getAssetInformation().getDefaultThumbnail().getPath()), + rootPackage, xmlPart, aas.getAssetInformation().getDefaultThumbnail().getContentType())); + environment.getSubmodels().forEach(sm -> + findFileElements(sm.getSubmodelElements()).forEach(file -> createParts(files, + AASXUtils.getPathFromURL(file.getValue()), rootPackage, xmlPart, file.getContentType()))); + } + + /** + * Adds a part to the .aasx file with the given file, filePath and contentType + * + * @param files the content of the files + * @param filePath the path of the file + * @param rootPackage the OPCPackage + * @param xmlPart the Part the files should be related to + * @param contentType the contentType of the file + */ + private void createParts(Collection files, String filePath, OPCPackage rootPackage, + PackagePart xmlPart, String contentType) { + try { + InMemoryFile content = findFileByPath(files, filePath); + logger.trace("Writing file '" + filePath + "' to .aasx."); + createAASXPart(rootPackage, xmlPart, filePath, contentType, AASSUPPL_RELTYPE, content.getFileContent()); + } catch (RuntimeException e) { + // Log that a file is missing and continue building the .aasx + logger.warn("Could not add File '" + filePath + "'. It was not contained in given InMemoryFiles."); } } From 1a32c4bda66383364d0bcb16d5ac088978e4ea52 Mon Sep 17 00:00:00 2001 From: fvolz Date: Thu, 5 Oct 2023 09:34:18 +0200 Subject: [PATCH 6/6] change filter to && --- .../aas4j/v3/dataformat/aasx/AASXDeserializer.java | 4 ++-- .../digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java index 24f82a315..c068ad20b 100644 --- a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java +++ b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXDeserializer.java @@ -151,8 +151,8 @@ private List parseReferencedFilePathsFromAASX() throws IOException, Inva List paths = new ArrayList<>(); environment.getAssetAdministrationShells().stream().filter(aas -> aas.getAssetInformation() != null - || aas.getAssetInformation().getDefaultThumbnail() != null - || aas.getAssetInformation().getDefaultThumbnail().getPath() != null) + && aas.getAssetInformation().getDefaultThumbnail() != null + && aas.getAssetInformation().getDefaultThumbnail().getPath() != null) .forEach(aas -> paths.add(aas.getAssetInformation().getDefaultThumbnail().getPath())); environment.getSubmodels().forEach(sm -> paths.addAll(parseElements(sm.getSubmodelElements()))); return paths; diff --git a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java index cda123067..71e61ccb8 100644 --- a/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java +++ b/dataformat-aasx/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/aasx/AASXSerializer.java @@ -128,8 +128,8 @@ public void write(Environment environment, Collection files, Outpu private void storeFilesInAASX(Environment environment, Collection files, OPCPackage rootPackage, PackagePart xmlPart) { environment.getAssetAdministrationShells().stream().filter(aas -> aas.getAssetInformation() != null - || aas.getAssetInformation().getDefaultThumbnail() != null - || aas.getAssetInformation().getDefaultThumbnail().getPath() != null) + && aas.getAssetInformation().getDefaultThumbnail() != null + && aas.getAssetInformation().getDefaultThumbnail().getPath() != null) .forEach(aas -> createParts(files, AASXUtils.getPathFromURL(aas.getAssetInformation().getDefaultThumbnail().getPath()), rootPackage, xmlPart, aas.getAssetInformation().getDefaultThumbnail().getContentType()));