Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for thumbnails in AASXSerializer / AASXDeserializer #165

Merged
merged 6 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
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.model.AssetInformation;

/**
* The AASX package converter converts a aasx package into a list of aas, a list
Expand Down Expand Up @@ -148,9 +150,11 @@ private List<String> parseReferencedFilePathsFromAASX() throws IOException, Inva
read();

List<String> paths = new ArrayList<>();
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
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.model.AssetInformation;

import org.eclipse.digitaltwin.aas4j.v3.dataformat.SerializationException;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.aasx.internal.AASXUtils;
Expand Down Expand Up @@ -110,34 +112,50 @@ public void write(Environment environment, Collection<InMemoryFile> 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);
}

/**
* 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<Submodel> submodelList, Collection<InMemoryFile> files, OPCPackage rootPackage,
private void storeFilesInAASX(Environment environment, Collection<InMemoryFile> files, OPCPackage rootPackage,
PackagePart xmlPart) {
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())));
}

for (Submodel sm : submodelList) {
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.");
}
}
/**
* 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<InMemoryFile> 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.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -50,8 +52,11 @@ public void testRoundTrip() throws SerializationException, IOException, InvalidF

List<InMemoryFile> 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");

Expand All @@ -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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down