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

Commit f58c8dd

Browse files
author
Matthias Böckmann
committed
Merge branch 'master' of https://jira.iais.fraunhofer.de/stash/scm/ear/aas-serializer into feature/rdfSerializer
� Conflicts: � dataformat-json/src/main/java/io/adminshell/aas/v3/dataformat/json/ReflectionAnnotationIntrospector.java
2 parents f6ef214 + 8dd79ee commit f58c8dd

8 files changed

Lines changed: 188 additions & 64 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+
}

dataformat-core/src/main/java/io/adminshell/aas/v3/dataformat/core/ReflectionHelper.java

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package io.adminshell.aas.v3.dataformat.core;
1717

1818
import io.adminshell.aas.v3.model.Constraint;
19-
import io.adminshell.aas.v3.model.DataSpecificationContent;
2019
import io.adminshell.aas.v3.model.Referable;
2120
import io.github.classgraph.ClassGraph;
2221
import io.github.classgraph.ClassInfo;
@@ -162,6 +161,62 @@ public static boolean isModelInterfaceOrDefaultImplementation(Class<?> type) {
162161
return isModelInterface(type) || isDefaultImplementation(type);
163162
}
164163

164+
/**
165+
* Returns the AAS type information used for de-/serialization for a given
166+
* class or null if type information should not be included
167+
*
168+
* @param clazz the class to find the type information for
169+
* @return the type information for the given class or null if there is no
170+
* type information or type information should not be included
171+
*/
172+
public static String getModelType(Class<?> clazz) {
173+
Class<?> type = getMostSpecificTypeWithModelType(clazz);
174+
if (type != null) {
175+
return type.getSimpleName();
176+
}
177+
for (Class<?> interfaceClass : clazz.getInterfaces()) {
178+
String result = getModelType(interfaceClass);
179+
if (result != null) {
180+
return result;
181+
}
182+
}
183+
Class<?> superClass = clazz.getSuperclass();
184+
if (superClass != null) {
185+
return getModelType(superClass);
186+
}
187+
return null;
188+
}
189+
190+
/**
191+
* Returns the most specific supertype that contains some AAS type
192+
* information or null if there is none
193+
*
194+
* @param clazz the class to find the type for
195+
* @return the most specific supertype of given class that contains some AAS
196+
* type information or null if there is none
197+
*/
198+
public static Class<?> getMostSpecificTypeWithModelType(Class<?> clazz) {
199+
return TYPES_WITH_MODEL_TYPE.stream()
200+
.filter(x -> clazz.isInterface() ? x.equals(clazz) : x.isAssignableFrom(clazz))
201+
.sorted((Class<?> o1, Class<?> o2) -> {
202+
// -1: o1 more special than o2
203+
// 0: o1 equals o2 or on same samelevel
204+
// 1: o2 more special than o1
205+
if (o1.isAssignableFrom(o2)) {
206+
if (o2.isAssignableFrom(o1)) {
207+
return 0;
208+
}
209+
return 1;
210+
}
211+
if (o2.isAssignableFrom(o1)) {
212+
return -1;
213+
}
214+
return 0;
215+
})
216+
.findFirst()
217+
.orElse(null);
218+
}
219+
165220
static {
166221
ScanResult modelScan = new ClassGraph()
167222
.enableClassInfo()

dataformat-json/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<dependency>
2020
<groupId>io.admin-shell.aas</groupId>
2121
<artifactId>dataformat-core</artifactId>
22-
<version>${model.version}</version>
22+
<version>${revision}</version>
2323
<scope>compile</scope>
2424
</dependency>
2525
<dependency>

dataformat-json/src/main/java/io/adminshell/aas/v3/dataformat/json/ReflectionAnnotationIntrospector.java

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@
3434
* This class helps to dynamically decide how to de-/serialize classes and
3535
* properties defined in the AAS model library.
3636
*
37-
* This is equivalent to adding the following annotations
37+
* This is equivialent to adding the following annotations
3838
* <ul>
3939
* <li> to all interfaces defined in the AAS model:
4040
* <ul>
4141
* <li> @JsonTypeName([interface name])
4242
* <li> @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "modelType")
43-
* <li> @JsonSubTypes({@Type(value = [sub-interface].class, name = "[sub-interface name]"), ...})
43+
* <li> @JsonSubTypes({
44+
*
45+
* @Type(value = [sub-interface].class, name = "[sub-interface name]"), ...})
4446
* for each sub-interface
4547
* </ul>
4648
* <li> to all getter methods returning any type of Collection<?> defined in the
@@ -58,55 +60,15 @@ public class ReflectionAnnotationIntrospector extends JacksonAnnotationIntrospec
5860

5961
@Override
6062
public String findTypeName(AnnotatedClass ac) {
61-
String customType = findModelType(ac.getRawType());
63+
String customType = ReflectionHelper.getModelType(ac.getRawType());
6264
return customType != null
6365
? customType
6466
: super.findTypeName(ac);
6567
}
6668

67-
private Class<?> getMostSpecificTypeWithModelType(Class<?> clazz) {
68-
return ReflectionHelper.TYPES_WITH_MODEL_TYPE.stream()
69-
.filter(x -> clazz.isInterface() ? x.equals(clazz) : x.isAssignableFrom(clazz))
70-
.sorted((Class<?> o1, Class<?> o2) -> {
71-
// -1: o1 more special than o2
72-
// 0: o1 equals o2 or on same same level
73-
// 1: o2 more special than o1
74-
if (o1.isAssignableFrom(o2)) {
75-
if (o2.isAssignableFrom(o1)) {
76-
return 0;
77-
}
78-
return 1;
79-
}
80-
if (o2.isAssignableFrom(o1)) {
81-
return -1;
82-
}
83-
return 0;
84-
})
85-
.findFirst()
86-
.orElse(null);
87-
}
88-
89-
private String findModelType(Class<?> clazz) {
90-
Class<?> type = getMostSpecificTypeWithModelType(clazz);
91-
if (type != null) {
92-
return type.getSimpleName();
93-
}
94-
for (Class<?> interfaceClass : clazz.getInterfaces()) {
95-
String result = findModelType(interfaceClass);
96-
if (result != null) {
97-
return result;
98-
}
99-
}
100-
Class<?> superClass = clazz.getSuperclass();
101-
if (superClass != null) {
102-
return findModelType(superClass);
103-
}
104-
return null;
105-
}
106-
10769
@Override
10870
public TypeResolverBuilder<?> findTypeResolver(MapperConfig<?> config, AnnotatedClass ac, JavaType baseType) {
109-
String modelType = findModelType(ac.getRawType());
71+
String modelType = ReflectionHelper.getModelType(ac.getRawType());
11072
if (modelType != null) {
11173
TypeResolverBuilder<?> result = _constructStdTypeResolverBuilder();
11274
result = result.init(JsonTypeInfo.Id.NAME, null);

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
<licenseSet>
6060
<header>license-header.txt</header>
6161
<includes>
62-
<include>**/*.java</include>
62+
<include>**/src/**/*.java</include>
6363
</includes>
6464
</licenseSet>
6565
</licenseSets>

0 commit comments

Comments
 (0)