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

Commit 8dd79ee

Browse files
Arno WeissDaespen
authored andcommitted
Pull request #2: Feature/validate aasx
Merge in EAR/aas-serializer from feature/validate-aasx to master * commit '957f83e2913ac699a8979da294e1b3729e35d7f1': Modify internal state of AASXDeserializer to be private Copy aasx-creation from deserializer-test and call validation on it. Implement AASX Validation, set a couple of methods in Deserializer to public Add Arno's gh account Add basic structure of the aasx-validator
2 parents 6da8b65 + 957f83e commit 8dd79ee

4 files changed

Lines changed: 124 additions & 17 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ We always look for contributions, bug reports, feature requests etc. Simply open
5555
| Jens Müller | Fraunhofer IOSB | [JensMueller2709](https://github.com/JensMueller2709) | | | | x | |
5656
| Bastian Rössl | Fraunhofer IOSB-INA | [br-iosb](https://github.com/br-iosb) | | | | x | |
5757
| Frank Schnicke | Fraunhofer IESE | [frankschnicke](https://github.com/frankschnicke) | | | x | | x |
58-
| Arno Weiss | Fraunhofer IWU | []() | | | | x | |
58+
| Arno Weiss | Fraunhofer IWU | [alw-iwu](https://github.com/alw-iwu) | | | | x | |

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

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class AASXDeserializer {
5858
private XmlDeserializer deserializer = new XmlDeserializer();
5959

6060
private AssetAdministrationShellEnvironment environment;
61-
private OPCPackage aasxRoot;
61+
private final OPCPackage aasxRoot;
6262

6363
public AASXDeserializer(InputStream inputStream) throws InvalidFormatException, IOException {
6464
aasxRoot = OPCPackage.open(inputStream);
@@ -70,28 +70,27 @@ public AASXDeserializer(XmlDeserializer deserializer, InputStream inputStream) t
7070
}
7171

7272
public AssetAdministrationShellEnvironment read() throws InvalidFormatException, IOException, DeserializationException {
73-
7473
// If the XML was already parsed return cached environment
7574
if (environment != null) {
7675
return environment;
7776
}
78-
7977
environment = deserializer.read(getXMLResourceString(aasxRoot));
80-
8178
return environment;
8279
}
8380

8481
/**
8582
* Return the Content of the xml file in the aasx-package as String
8683
*
87-
* @param aasxPackage
88-
* - the root package of the AASX
84+
* @param aasxPackage - the root package of the AASX
8985
* @return Content of XML as String
9086
* @throws InvalidFormatException
9187
* @throws IOException
9288
*/
93-
private String getXMLResourceString(OPCPackage aasxPackage) throws InvalidFormatException, IOException {
89+
public String getXMLResourceString() throws InvalidFormatException, IOException {
90+
return getXMLResourceString(this.aasxRoot);
91+
}
9492

93+
private String getXMLResourceString(OPCPackage aasxPackage) throws InvalidFormatException, IOException {
9594
// Get the "/aasx/aasx-origin" Part. It is Relationship source for the
9695
// XML-Document
9796
PackagePart originPart = aasxPackage.getPart(PackagingURIHelper.createPartName(AASX_ORIGIN));
@@ -129,11 +128,9 @@ private String getXMLResourceString(OPCPackage aasxPackage) throws InvalidFormat
129128
*
130129
*/
131130
private List<String> parseReferencedFilePathsFromAASX() throws IOException, InvalidFormatException, DeserializationException {
131+
read();
132132

133-
AssetAdministrationShellEnvironment environment = read();
134-
135-
List<String> paths = new ArrayList<String>();
136-
133+
List<String> paths = new ArrayList<>();
137134
for (Submodel sm : environment.getSubmodels()) {
138135
paths.addAll(parseElements(sm.getSubmodelElements()));
139136
}
@@ -147,8 +144,7 @@ private List<String> parseReferencedFilePathsFromAASX() throws IOException, Inva
147144
* @return the Paths from the File elements
148145
*/
149146
private List<String> parseElements(Collection<SubmodelElement> elements) {
150-
List<String> paths = new ArrayList<String>();
151-
147+
List<String> paths = new ArrayList<>();
152148
for (SubmodelElement element : elements) {
153149
if (element instanceof File) {
154150
File file = (File) element;
@@ -168,13 +164,10 @@ private List<String> parseElements(Collection<SubmodelElement> elements) {
168164

169165
public List<InMemoryFile> getRelatedFiles() throws InvalidFormatException, IOException, DeserializationException {
170166
List<String> filePaths = parseReferencedFilePathsFromAASX();
171-
172167
List<InMemoryFile> files = new ArrayList<>();
173-
174168
for (String filePath : filePaths) {
175169
files.add(readFile(aasxRoot, filePath));
176170
}
177-
178171
return files;
179172
}
180173

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2021 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e. V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.adminshell.aas.v3.dataformat.aasx;
17+
18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
import java.util.Set;
21+
22+
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
23+
import org.xml.sax.SAXException;
24+
25+
import io.adminshell.aas.v3.dataformat.xml.XmlSchemaValidator;
26+
27+
/**
28+
* Class to validate the XML file inside an AASX-package
29+
*/
30+
public class AASXValidator {
31+
32+
private XmlSchemaValidator xmlValidator;
33+
private AASXDeserializer deserializer;
34+
35+
public AASXValidator(InputStream is) throws SAXException, IOException, InvalidFormatException {
36+
this.xmlValidator = new XmlSchemaValidator();
37+
this.deserializer = new AASXDeserializer(is);
38+
}
39+
40+
/**
41+
* Calls XML-Validator
42+
*
43+
* @return Set of Strings containing message on AASX-XML-Validation result
44+
* @throws IOException
45+
* @throws InvalidFormatException
46+
*/
47+
public Set<String> validateSchema() throws IOException, InvalidFormatException {
48+
String file = deserializer.getXMLResourceString();
49+
return xmlValidator.validateSchema(file);
50+
}
51+
52+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2021 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e. V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.adminshell.aas.v3.dataformat.aasx.deserialization;
17+
18+
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertNull;
20+
21+
import io.adminshell.aas.v3.dataformat.DeserializationException;
22+
import io.adminshell.aas.v3.dataformat.SerializationException;
23+
import io.adminshell.aas.v3.dataformat.aasx.AASXDeserializer;
24+
import io.adminshell.aas.v3.dataformat.aasx.AASXSerializer;
25+
import io.adminshell.aas.v3.dataformat.aasx.AASXValidator;
26+
import io.adminshell.aas.v3.dataformat.aasx.InMemoryFile;
27+
import io.adminshell.aas.v3.dataformat.aasx.serialization.AASSimple;
28+
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
29+
import org.junit.Rule;
30+
import org.junit.Test;
31+
import org.junit.rules.TemporaryFolder;
32+
import org.xml.sax.SAXException;
33+
34+
import javax.xml.parsers.ParserConfigurationException;
35+
import java.io.*;
36+
import java.util.ArrayList;
37+
import java.util.List;
38+
import java.util.Set;
39+
40+
public class ValidationTest {
41+
42+
@Rule
43+
public TemporaryFolder tempFolder = new TemporaryFolder();
44+
45+
@Test
46+
public void validateXmlInsideAasx() throws SerializationException, IOException, InvalidFormatException, DeserializationException, ParserConfigurationException, SAXException {
47+
List<InMemoryFile> fileList = new ArrayList<>();
48+
byte[] operationManualContent = { 0, 1, 2, 3, 4 };
49+
InMemoryFile inMemoryFile = new InMemoryFile(operationManualContent, "/aasx/OperatingManual.pdf");
50+
fileList.add(inMemoryFile);
51+
52+
File file = tempFolder.newFile("output.aasx");
53+
54+
new AASXSerializer().write(AASSimple.ENVIRONMENT, fileList, new FileOutputStream(file));
55+
56+
InputStream in = new FileInputStream(file);
57+
AASXValidator v = new AASXValidator(in);
58+
Set<String> validationResult = v.validateSchema();
59+
System.out.println(validationResult);
60+
assertEquals(validationResult.size(),0);
61+
}
62+
}

0 commit comments

Comments
 (0)