Skip to content
This repository was archived by the owner on Feb 15, 2024. It is now read-only.

Commit 8235ecc

Browse files
DaespenMatthias Böckmann
authored andcommitted
Pull request #7: Fix/aasx comments
Merge in EAR/aas-serializer from fix/aasx-comments to master * commit 'd694395454c0cf10dfeeec646b462df2b088d9da': Adds missing comments for aasx dataformat Remove unnecessary utility class in AASX serializer
2 parents aeedf00 + d694395 commit 8235ecc

5 files changed

Lines changed: 92 additions & 262 deletions

File tree

dataformat-aasx/src/main/java/io/adminshell/aas/v3/dataformat/aasx/AASXDeserializer.java

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,12 @@
2323
import java.util.Collection;
2424
import java.util.List;
2525

26-
import javax.xml.parsers.ParserConfigurationException;
27-
2826
import org.apache.commons.io.IOUtils;
2927
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
3028
import org.apache.poi.openxml4j.opc.OPCPackage;
3129
import org.apache.poi.openxml4j.opc.PackagePart;
3230
import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
3331
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
34-
import org.xml.sax.SAXException;
3532

3633
import io.adminshell.aas.v3.dataformat.DeserializationException;
3734
import io.adminshell.aas.v3.dataformat.xml.XmlDeserializer;
@@ -44,11 +41,6 @@
4441
/**
4542
* The AASX package converter converts a aasx package into a list of aas, a list
4643
* of submodels a list of assets, a list of Concept descriptions
47-
*
48-
* The aas provides the references to the submodels and assets
49-
*
50-
* @author zhangzai, conradi
51-
*
5244
*/
5345
public class AASXDeserializer {
5446

@@ -60,15 +52,39 @@ public class AASXDeserializer {
6052
private AssetAdministrationShellEnvironment environment;
6153
private final OPCPackage aasxRoot;
6254

55+
/**
56+
* Constructor that takes the aasx package for this deserializer
57+
*
58+
* @param inputStream an input stream to an aasx package that can be read with this instance
59+
* @throws InvalidFormatException if aasx package format is invalid
60+
* @throws IOException if creating input streams for aasx fails
61+
*/
6362
public AASXDeserializer(InputStream inputStream) throws InvalidFormatException, IOException {
6463
aasxRoot = OPCPackage.open(inputStream);
6564
}
6665

66+
/**
67+
* Constructor for custom XML deserialization
68+
*
69+
* @param deserializer a custom deserializer used for deserializing the aas environment
70+
* @param inputStream an input stream to an aasx package that can be read with this instance
71+
* @throws InvalidFormatException if aasx package format is invalid
72+
* @throws IOException if creating input streams for aasx fails
73+
*/
6774
public AASXDeserializer(XmlDeserializer deserializer, InputStream inputStream) throws InvalidFormatException, IOException {
6875
aasxRoot = OPCPackage.open(inputStream);
6976
this.deserializer = deserializer;
7077
}
7178

79+
/**
80+
* Reads the AASX package that belongs to this deserializer
81+
*
82+
* @return The deserialized aas environment from the aasx package given in the constructor
83+
*
84+
* @throws InvalidFormatException if aasx package format is invalid
85+
* @throws IOException if creating input streams for aasx fails
86+
* @throws DeserializationException if deserialization of the serialized aas environment fails
87+
*/
7288
public AssetAdministrationShellEnvironment read() throws InvalidFormatException, IOException, DeserializationException {
7389
// If the XML was already parsed return cached environment
7490
if (environment != null) {
@@ -81,9 +97,8 @@ public AssetAdministrationShellEnvironment read() throws InvalidFormatException,
8197
/**
8298
* Return the Content of the xml file in the aasx-package as String
8399
*
84-
* @return Content of XML as String
85-
* @throws InvalidFormatException
86-
* @throws IOException
100+
* @throws InvalidFormatException if aasx package format is invalid
101+
* @throws IOException if creating input streams for aasx fails
87102
*/
88103
public String getXMLResourceString() throws InvalidFormatException, IOException {
89104
return getXMLResourceString(this.aasxRoot);
@@ -119,11 +134,9 @@ private String getXMLResourceString(OPCPackage aasxPackage) throws InvalidFormat
119134
* the package
120135
*
121136
* @return a map of the folder name and folder path, the folder holds the files
122-
* @throws IOException
123-
* @throws SAXException
124-
* @throws ParserConfigurationException
125-
* @throws InvalidFormatException
126-
* @throws DeserializationException
137+
* @throws IOException if creating input streams for aasx fails
138+
* @throws InvalidFormatException if aasx package format is invalid
139+
* @throws DeserializationException if deserialization of the serialized aas environment fails
127140
*
128141
*/
129142
private List<String> parseReferencedFilePathsFromAASX() throws IOException, InvalidFormatException, DeserializationException {
@@ -137,9 +150,9 @@ private List<String> parseReferencedFilePathsFromAASX() throws IOException, Inva
137150
}
138151

139152
/**
140-
* Gets the paths from a collection of ISubmodelElement
153+
* Gets the file paths from a collection of ISubmodelElement
141154
*
142-
* @param elements
155+
* @param elements the submodel elements to process
143156
* @return the Paths from the File elements
144157
*/
145158
private List<String> parseElements(Collection<SubmodelElement> elements) {
@@ -161,6 +174,14 @@ private List<String> parseElements(Collection<SubmodelElement> elements) {
161174
return paths;
162175
}
163176

177+
/**
178+
* Retrieves a list of related files from the deserialized aasx package
179+
*
180+
* @return the list of file in memory
181+
* @throws InvalidFormatException if aasx package format is invalid
182+
* @throws IOException if creating input streams for aasx fails
183+
* @throws DeserializationException if deserialization of the serialized aas environment fails
184+
*/
164185
public List<InMemoryFile> getRelatedFiles() throws InvalidFormatException, IOException, DeserializationException {
165186
List<String> filePaths = parseReferencedFilePathsFromAASX();
166187
List<InMemoryFile> files = new ArrayList<>();

dataformat-aasx/src/main/java/io/adminshell/aas/v3/dataformat/aasx/AASXSerializer.java

Lines changed: 47 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,39 @@ public class AASXSerializer {
6363

6464
private Serializer xmlSerializer = new XmlSerializer();
6565

66-
public AASXSerializer(Serializer xmlSerializer) {
67-
this.xmlSerializer = xmlSerializer;
68-
}
69-
66+
/**
67+
* Default constructor
68+
*/
7069
public AASXSerializer() {
70+
}
7171

72+
/**
73+
* Constructor with a custom serializer for serializing the aas environment
74+
*
75+
* @param xmlSerializer a custom serializer used for serializing the aas environment
76+
*/
77+
public AASXSerializer(Serializer xmlSerializer) {
78+
this.xmlSerializer = xmlSerializer;
7279
}
7380

7481
/**
7582
* Generates the .aasx file and writes it to the given OutputStream
7683
*
77-
* @throws SerializationException
78-
* @throws IOException
84+
* @param environment the aas environment that will be included in the aasx package as an xml serialization
85+
* @param files related inMemory files that belong to the given aas environment
86+
* @param os an output stream for writing the aasx package
87+
* @throws SerializationException if serializing the given elements fails
88+
* @throws IOException if creating output streams for aasx fails
7989
*/
80-
public void write(AssetAdministrationShellEnvironment environment, Collection<InMemoryFile> files, OutputStream os) throws SerializationException, IOException {
90+
public void write(AssetAdministrationShellEnvironment environment, Collection<InMemoryFile> files, OutputStream os)
91+
throws SerializationException, IOException {
8192
prepareFilePaths(environment.getSubmodels());
8293

8394
OPCPackage rootPackage = OPCPackage.create(os);
8495

8596
// Create the empty aasx-origin file
86-
PackagePart origin = createAASXPart(rootPackage, rootPackage, ORIGIN_PATH, MIME_PLAINTXT, ORIGIN_RELTYPE, ORIGIN_CONTENT.getBytes());
97+
PackagePart origin = createAASXPart(rootPackage, rootPackage, ORIGIN_PATH, MIME_PLAINTXT, ORIGIN_RELTYPE,
98+
ORIGIN_CONTENT.getBytes());
8799

88100
// Convert the given Metamodels to XML
89101
String xml = xmlSerializer.write(environment);
@@ -99,16 +111,13 @@ public void write(AssetAdministrationShellEnvironment environment, Collection<In
99111
/**
100112
* Stores the files from the Submodels in the .aasx file
101113
*
102-
* @param submodelList
103-
* the Submodels
104-
* @param files
105-
* the content of the files
106-
* @param rootPackage
107-
* the OPCPackage
108-
* @param xmlPart
109-
* the Part the files should be related to
114+
* @param submodelList the Submodels
115+
* @param files the content of the files
116+
* @param rootPackage the OPCPackage
117+
* @param xmlPart the Part the files should be related to
110118
*/
111-
private void storeFilesInAASX(List<Submodel> submodelList, Collection<InMemoryFile> files, OPCPackage rootPackage, PackagePart xmlPart) {
119+
private void storeFilesInAASX(List<Submodel> submodelList, Collection<InMemoryFile> files, OPCPackage rootPackage,
120+
PackagePart xmlPart) {
112121

113122
for (Submodel sm : submodelList) {
114123
for (File file : findFileElements(sm.getSubmodelElements())) {
@@ -128,11 +137,9 @@ private void storeFilesInAASX(List<Submodel> submodelList, Collection<InMemoryFi
128137
/**
129138
* Saves the OPCPackage to the given OutputStream
130139
*
131-
* @param os
132-
* the Stream to be saved to
133-
* @param rootPackage
134-
* the Package to be saved
135-
* @throws IOException
140+
* @param os the Stream to be saved to
141+
* @param rootPackage the Package to be saved
142+
* @throws IOException if creating output streams for aasx fails
136143
*/
137144
private void saveAASX(OutputStream os, OPCPackage rootPackage) throws IOException {
138145
rootPackage.flush();
@@ -152,23 +159,16 @@ private String createUniqueID() {
152159
/**
153160
* Creates a Part (a file in the .aasx) of the .aasx and adds it to the Package
154161
*
155-
* @param root
156-
* the OPCPackage
157-
* @param relateTo
158-
* the Part of the OPC the relationship of the new Part should be
159-
* added to
160-
* @param path
161-
* the path inside the .aasx where the new Part should be created
162-
* @param mimeType
163-
* the mime-type of the file
164-
* @param relType
165-
* the type of the Relationship
166-
* @param content
167-
* the data the new part should contain
168-
* @return the created PackagePart; Returned in case it is needed late as a Part
169-
* to relate to
162+
* @param root the OPCPackage
163+
* @param relateTo the Part of the OPC the relationship of the new Part should be added to
164+
* @param path the path inside the .aasx where the new Part should be created
165+
* @param mimeType the mime-type of the file
166+
* @param relType the type of the Relationship
167+
* @param content the data the new part should contain
168+
* @return the created PackagePart; Returned in case it is needed late as a Part to relate to
170169
*/
171-
private PackagePart createAASXPart(OPCPackage root, RelationshipSource relateTo, String path, String mimeType, String relType, byte[] content) {
170+
private PackagePart createAASXPart(OPCPackage root, RelationshipSource relateTo, String path, String mimeType, String relType,
171+
byte[] content) {
172172
if (mimeType == null || mimeType.equals("")) {
173173
throw new RuntimeException("Could not create AASX Part '" + path + "'. No MIME_TYPE specified.");
174174
}
@@ -191,10 +191,8 @@ private PackagePart createAASXPart(OPCPackage root, RelationshipSource relateTo,
191191
/**
192192
* Writes the content of a byte[] to a Part
193193
*
194-
* @param part
195-
* the Part to be written to
196-
* @param content
197-
* the content to be written to the part
194+
* @param part the Part to be written to
195+
* @param content the content to be written to the part
198196
*/
199197
private void writeDataToPart(PackagePart part, byte[] content) {
200198
try (OutputStream ostream = part.getOutputStream();) {
@@ -209,8 +207,7 @@ private void writeDataToPart(PackagePart part, byte[] content) {
209207
* Gets the File elements from a collection of elements Also recursively
210208
* searches in SubmodelElementCollections
211209
*
212-
* @param elements
213-
* the Elements to be searched for File elements
210+
* @param elements the Elements to be searched for File elements
214211
* @return the found Files
215212
*/
216213
private Collection<File> findFileElements(Collection<SubmodelElement> elements) {
@@ -231,20 +228,18 @@ private Collection<File> findFileElements(Collection<SubmodelElement> elements)
231228
/**
232229
* Replaces the path in all File Elements with the result of preparePath
233230
*
234-
* @param submodels
235-
* the Submodels
231+
* @param submodels the Submodels
236232
*/
237233
private void prepareFilePaths(Collection<Submodel> submodels) {
238-
submodels.stream().forEach(sm -> findFileElements(sm.getSubmodelElements()).stream().forEach(f -> f.setValue(preparePath(f.getValue()))));
234+
submodels.stream()
235+
.forEach(sm -> findFileElements(sm.getSubmodelElements()).stream().forEach(f -> f.setValue(preparePath(f.getValue()))));
239236
}
240237

241238
/**
242239
* Finds an InMemoryFile by its path
243240
*
244-
* @param files
245-
* the InMemoryFiles
246-
* @param path
247-
* the path of the wanted file
241+
* @param files the InMemoryFiles
242+
* @param path the path of the wanted file
248243
* @return the InMemoryFile if it was found; else null
249244
*/
250245
private InMemoryFile findFileByPath(Collection<InMemoryFile> files, String path) {
@@ -259,8 +254,7 @@ private InMemoryFile findFileByPath(Collection<InMemoryFile> files, String path)
259254
/**
260255
* Removes the serverpart from a path and ensures it starts with a slash
261256
*
262-
* @param path
263-
* the path to be prepared
257+
* @param path the path to be prepared
264258
* @return the prepared path
265259
*/
266260
private String preparePath(String path) {

dataformat-aasx/src/main/java/io/adminshell/aas/v3/dataformat/aasx/InMemoryFile.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@
1919

2020
/**
2121
* Container class for the content of a File and its Path
22-
*
2322
*/
2423
public class InMemoryFile {
2524

2625
private byte[] fileContent;
2726
private String path;
2827

28+
/**
29+
* Constructor for directly setting the InMemoryFile contents
30+
*
31+
* @param fileContent byte-array content of the represented file
32+
* @param path relative or absolute path of the represented file
33+
*/
2934
public InMemoryFile(byte[] fileContent, String path) {
3035
this.fileContent = fileContent;
3136
this.path = path;

0 commit comments

Comments
 (0)