|
| 1 | +package io.adminshell.aas.v3.dataformat.aasx; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.io.StringWriter; |
| 6 | +import java.nio.charset.StandardCharsets; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Collection; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import javax.xml.parsers.ParserConfigurationException; |
| 12 | + |
| 13 | +import org.apache.commons.io.IOUtils; |
| 14 | +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; |
| 15 | +import org.apache.poi.openxml4j.opc.OPCPackage; |
| 16 | +import org.apache.poi.openxml4j.opc.PackagePart; |
| 17 | +import org.apache.poi.openxml4j.opc.PackageRelationshipCollection; |
| 18 | +import org.apache.poi.openxml4j.opc.PackagingURIHelper; |
| 19 | +import org.xml.sax.SAXException; |
| 20 | + |
| 21 | +import io.adminshell.aas.v3.dataformat.DeserializationException; |
| 22 | +import io.adminshell.aas.v3.dataformat.xml.XmlDeserializer; |
| 23 | +import io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment; |
| 24 | +import io.adminshell.aas.v3.model.File; |
| 25 | +import io.adminshell.aas.v3.model.Submodel; |
| 26 | +import io.adminshell.aas.v3.model.SubmodelElement; |
| 27 | +import io.adminshell.aas.v3.model.SubmodelElementCollection; |
| 28 | + |
| 29 | +/** |
| 30 | + * The AASX package converter converts a aasx package into a list of aas, a list |
| 31 | + * of submodels a list of assets, a list of Concept descriptions |
| 32 | + * |
| 33 | + * The aas provides the references to the submodels and assets |
| 34 | + * |
| 35 | + * @author zhangzai, conradi |
| 36 | + * |
| 37 | + */ |
| 38 | +public class AASXDeserializer { |
| 39 | + |
| 40 | + private static final String XML_TYPE = "http://www.admin-shell.io/aasx/relationships/aas-spec"; |
| 41 | + private static final String AASX_ORIGIN = "/aasx/aasx-origin"; |
| 42 | + |
| 43 | + private XmlDeserializer deserializer = new XmlDeserializer(); |
| 44 | + |
| 45 | + private AssetAdministrationShellEnvironment environment; |
| 46 | + private OPCPackage aasxRoot; |
| 47 | + |
| 48 | + public AASXDeserializer(InputStream inputStream) throws InvalidFormatException, IOException { |
| 49 | + aasxRoot = OPCPackage.open(inputStream); |
| 50 | + } |
| 51 | + |
| 52 | + public AASXDeserializer(XmlDeserializer deserializer, InputStream inputStream) throws InvalidFormatException, IOException { |
| 53 | + aasxRoot = OPCPackage.open(inputStream); |
| 54 | + this.deserializer = deserializer; |
| 55 | + } |
| 56 | + |
| 57 | + public AssetAdministrationShellEnvironment read() throws InvalidFormatException, IOException, DeserializationException { |
| 58 | + |
| 59 | + // If the XML was already parsed return cached environment |
| 60 | + if (environment != null) { |
| 61 | + return environment; |
| 62 | + } |
| 63 | + |
| 64 | + environment = deserializer.read(getXMLResourceString(aasxRoot)); |
| 65 | + |
| 66 | + return environment; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Return the Content of the xml file in the aasx-package as String |
| 71 | + * |
| 72 | + * @param aasxPackage |
| 73 | + * - the root package of the AASX |
| 74 | + * @return Content of XML as String |
| 75 | + * @throws InvalidFormatException |
| 76 | + * @throws IOException |
| 77 | + */ |
| 78 | + private String getXMLResourceString(OPCPackage aasxPackage) throws InvalidFormatException, IOException { |
| 79 | + |
| 80 | + // Get the "/aasx/aasx-origin" Part. It is Relationship source for the |
| 81 | + // XML-Document |
| 82 | + PackagePart originPart = aasxPackage.getPart(PackagingURIHelper.createPartName(AASX_ORIGIN)); |
| 83 | + |
| 84 | + // Get the Relation to the XML Document |
| 85 | + PackageRelationshipCollection originRelationships = originPart.getRelationshipsByType(XML_TYPE); |
| 86 | + |
| 87 | + // If there is more than one or no XML-Document that is an error |
| 88 | + if (originRelationships.size() > 1) { |
| 89 | + throw new RuntimeException("More than one 'aasx-spec' document found in .aasx"); |
| 90 | + } else if (originRelationships.size() == 0) { |
| 91 | + throw new RuntimeException("No 'aasx-spec' document found in .aasx"); |
| 92 | + } |
| 93 | + |
| 94 | + // Get the PackagePart of the XML-Document |
| 95 | + PackagePart xmlPart = originPart.getRelatedPart(originRelationships.getRelationship(0)); |
| 96 | + |
| 97 | + // Read the content from the PackagePart |
| 98 | + InputStream stream = xmlPart.getInputStream(); |
| 99 | + StringWriter writer = new StringWriter(); |
| 100 | + IOUtils.copy(stream, writer, StandardCharsets.UTF_8); |
| 101 | + return writer.toString(); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Load the referenced filepaths in the submodels such as PDF, PNG files from |
| 106 | + * the package |
| 107 | + * |
| 108 | + * @return a map of the folder name and folder path, the folder holds the files |
| 109 | + * @throws IOException |
| 110 | + * @throws SAXException |
| 111 | + * @throws ParserConfigurationException |
| 112 | + * @throws InvalidFormatException |
| 113 | + * @throws DeserializationException |
| 114 | + * |
| 115 | + */ |
| 116 | + private List<String> parseReferencedFilePathsFromAASX() throws IOException, InvalidFormatException, DeserializationException { |
| 117 | + |
| 118 | + AssetAdministrationShellEnvironment environment = read(); |
| 119 | + |
| 120 | + List<String> paths = new ArrayList<String>(); |
| 121 | + |
| 122 | + for (Submodel sm : environment.getSubmodels()) { |
| 123 | + paths.addAll(parseElements(sm.getSubmodelElements())); |
| 124 | + } |
| 125 | + return paths; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Gets the paths from a collection of ISubmodelElement |
| 130 | + * |
| 131 | + * @param elements |
| 132 | + * @return the Paths from the File elements |
| 133 | + */ |
| 134 | + private List<String> parseElements(Collection<SubmodelElement> elements) { |
| 135 | + List<String> paths = new ArrayList<String>(); |
| 136 | + |
| 137 | + for (SubmodelElement element : elements) { |
| 138 | + if (element instanceof File) { |
| 139 | + File file = (File) element; |
| 140 | + // If the path contains a "://", we can assume, that the Path is a link to an |
| 141 | + // other server |
| 142 | + // e.g. http://localhost:8080/aasx/... |
| 143 | + if (!file.getValue().contains("://")) { |
| 144 | + paths.add(file.getValue()); |
| 145 | + } |
| 146 | + } else if (element instanceof SubmodelElementCollection) { |
| 147 | + SubmodelElementCollection collection = (SubmodelElementCollection) element; |
| 148 | + paths.addAll(parseElements(collection.getValues())); |
| 149 | + } |
| 150 | + } |
| 151 | + return paths; |
| 152 | + } |
| 153 | + |
| 154 | + public List<InMemoryFile> getRelatedFiles() throws InvalidFormatException, IOException, DeserializationException { |
| 155 | + List<String> filePaths = parseReferencedFilePathsFromAASX(); |
| 156 | + |
| 157 | + List<InMemoryFile> files = new ArrayList<>(); |
| 158 | + |
| 159 | + for (String filePath : filePaths) { |
| 160 | + files.add(readFile(aasxRoot, filePath)); |
| 161 | + } |
| 162 | + |
| 163 | + return files; |
| 164 | + } |
| 165 | + |
| 166 | + private InMemoryFile readFile(OPCPackage aasxRoot, String filePath) throws InvalidFormatException, IOException { |
| 167 | + PackagePart part = aasxRoot.getPart(PackagingURIHelper.createPartName(filePath)); |
| 168 | + InputStream stream = part.getInputStream(); |
| 169 | + return new InMemoryFile(stream.readAllBytes(), filePath); |
| 170 | + } |
| 171 | +} |
0 commit comments