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

Fixes wrong namespace in AASX files #205

Merged
merged 2 commits into from
Nov 20, 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 @@ -15,6 +15,15 @@
*/
package org.eclipse.digitaltwin.aas4j.v3.dataformat.aasx;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
Expand All @@ -28,24 +37,19 @@
import org.eclipse.digitaltwin.aas4j.v3.model.File;
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement;
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementCollection;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The AASX package converter converts a aasx package into a list of aas, a list
* of submodels a list of assets, a list of Concept descriptions
*/
public class AASXDeserializer {
private static Logger logger = LoggerFactory.getLogger(AASXDeserializer.class);

private static final String XML_TYPE = "http://www.admin-shell.io/aasx/relationships/aas-spec";
private static final String AASX_ORIGIN = "/aasx/aasx-origin";
// In an older version of AAS4J/AASX Package Explorer,
// the wrong namespace was used
private static final String AASPEC_RELTYPE_BACKWARDSCOMPATIBLE = "http://www.admin-shell.io/aasx/relationships/aas-spec";
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;


Expand Down Expand Up @@ -107,31 +111,79 @@ public String getXMLResourceString() throws InvalidFormatException, IOException
return getXMLResourceString(this.aasxRoot);
}

private String getXMLResourceString(OPCPackage aasxPackage) throws InvalidFormatException, IOException {
// Get the "/aasx/aasx-origin" Part. It is Relationship source for the
// XML-Document
PackagePart originPart = aasxPackage.getPart(PackagingURIHelper.createPartName(AASX_ORIGIN));
/**
* Retrieves a list of related files from the deserialized aasx package
*
* @return the list of file in memory
* @throws InvalidFormatException
* if aasx package format is invalid
* @throws IOException
* if creating input streams for aasx fails
* @throws DeserializationException
* if deserialization of the serialized aas environment fails
*/
public List<InMemoryFile> getRelatedFiles() throws InvalidFormatException, IOException, DeserializationException {
List<String> filePaths = parseReferencedFilePathsFromAASX();
List<InMemoryFile> files = new ArrayList<>();
for (String filePath : filePaths) {
files.add(readFile(aasxRoot, filePath));
}
return files;
}

// Get the Relation to the XML Document
PackageRelationshipCollection originRelationships = originPart.getRelationshipsByType(XML_TYPE);
private String getXMLResourceString(OPCPackage aasxPackage) throws InvalidFormatException, IOException {
PackagePart originPart = getOriginPart(aasxPackage);

// If there is more than one or no XML-Document that is an error
if (originRelationships.size() > 1) {
throw new RuntimeException("More than one 'aasx-spec' document found in .aasx");
} else if (originRelationships.size() == 0) {
throw new RuntimeException("No 'aasx-spec' document found in .aasx");
}
PackageRelationshipCollection originRelationships = getXMLDocumentRelation(originPart);

// Get the PackagePart of the XML-Document
PackagePart xmlPart = originPart.getRelatedPart(originRelationships.getRelationship(0));

// Read the content from the PackagePart
return readContentFromPackagePart(xmlPart);
}

private String readContentFromPackagePart(PackagePart xmlPart) throws IOException {
InputStream stream = xmlPart.getInputStream();
StringWriter writer = new StringWriter();
IOUtils.copy(stream, writer, DEFAULT_CHARSET);
return writer.toString();
}

private PackagePart getOriginPart(OPCPackage aasxPackage) throws InvalidFormatException {
return aasxPackage.getPart(PackagingURIHelper.createPartName(AASXSerializer.ORIGIN_PATH));
}

private PackageRelationshipCollection getXMLDocumentRelation(PackagePart originPart) throws InvalidFormatException {
String xmlPart = getXMLPart(originPart);
PackageRelationshipCollection originRelationships = originPart.getRelationshipsByType(xmlPart);

checkNumberOfRelationsForValidity(originRelationships);

return originRelationships;
}

private String getXMLPart(PackagePart originPart) throws InvalidFormatException {
if (isCompatibilityModeNeeded(originPart)) {
logger.warn("AASX contains wrong Relationship namespace. This may be related to a bug in AASX Package Explorer or an old version of AAS4J. Future compatibility with the wrong namespace may not be guaranteed");
return AASPEC_RELTYPE_BACKWARDSCOMPATIBLE;
} else {
return AASXSerializer.AASSPEC_RELTYPE;
}
}

private boolean isCompatibilityModeNeeded(PackagePart originPart) throws InvalidFormatException {
PackageRelationshipCollection originRelationshipsBackwardsCompatible = originPart.getRelationshipsByType(AASPEC_RELTYPE_BACKWARDSCOMPATIBLE);

return originRelationshipsBackwardsCompatible.size() > 0;
}

private void checkNumberOfRelationsForValidity(PackageRelationshipCollection originRelationships) throws InvalidFormatException {
if (originRelationships.size() > 1) {
throw new InvalidFormatException("More than one 'aasx-spec' document found in .aasx");
} else if (originRelationships.size() == 0) {
throw new InvalidFormatException("No 'aasx-spec' document found in .aasx");
}
}

/**
* Load the referenced filepaths in the submodels such as PDF, PNG files from
* the package
Expand Down Expand Up @@ -174,23 +226,6 @@ private List<String> parseElements(Collection<SubmodelElement> elements) {
return paths;
}

/**
* Retrieves a list of related files from the deserialized aasx package
*
* @return the list of file in memory
* @throws InvalidFormatException if aasx package format is invalid
* @throws IOException if creating input streams for aasx fails
* @throws DeserializationException if deserialization of the serialized aas environment fails
*/
public List<InMemoryFile> getRelatedFiles() throws InvalidFormatException, IOException, DeserializationException {
List<String> filePaths = parseReferencedFilePathsFromAASX();
List<InMemoryFile> files = new ArrayList<>();
for (String filePath : filePaths) {
files.add(readFile(aasxRoot, filePath));
}
return files;
}

private InMemoryFile readFile(OPCPackage aasxRoot, String filePath) throws InvalidFormatException, IOException {
PackagePart part = aasxRoot.getPart(PackagingURIHelper.createPartName(AASXUtils.getPathFromURL(filePath)));
InputStream stream = part.getInputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
*/
package org.eclipse.digitaltwin.aas4j.v3.dataformat.aasx;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.UUID;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart;
Expand All @@ -34,14 +42,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.UUID;

/**
* This class can be used to generate an .aasx file from Metamodel Objects and
* the Files referred to in the Submodels
Expand All @@ -52,14 +52,16 @@ public class AASXSerializer {
private static final String MIME_PLAINTXT = "text/plain";
private static final String MIME_XML = "application/xml";

private static final String ORIGIN_RELTYPE = "http://www.admin-shell.io/aasx/relationships/aasx-origin";
private static final String ORIGIN_PATH = "/aasx/aasx-origin";
private static final String ORIGIN_CONTENT = "Intentionally empty.";
public static final String AASX_NAMESPACE = "http://admin-shell.io/aasx/relationships";

public static final String ORIGIN_RELTYPE = AASX_NAMESPACE + "/aasx-origin";
public static final String ORIGIN_PATH = "/aasx/aasx-origin";
public static final String ORIGIN_CONTENT = "Intentionally empty.";

private static final String AASSPEC_RELTYPE = "http://www.admin-shell.io/aasx/relationships/aas-spec";
private static final String XML_PATH = "/aasx/xml/content.xml";
public static final String AASSPEC_RELTYPE = AASX_NAMESPACE + "/aas-spec";
public static final String XML_PATH = "/aasx/xml/content.xml";

private static final String AASSUPPL_RELTYPE = "http://www.admin-shell.io/aasx/relationships/aas-suppl";
public static final String AASSUPPL_RELTYPE = AASX_NAMESPACE + "/aas-suppl";

private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;

Expand Down