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

Commit 18ef0d1

Browse files
committed
Bugfix in MappingProvider and refactoring
1 parent 2584d94 commit 18ef0d1

5 files changed

Lines changed: 48 additions & 7 deletions

File tree

dataformat-aml/src/main/java/io/adminshell/aas/v3/dataformat/aml/AmlDeserializer.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import io.adminshell.aas.v3.dataformat.DeserializationException;
1919
import io.adminshell.aas.v3.dataformat.Deserializer;
20+
import io.adminshell.aas.v3.dataformat.aml.deserialization.AasTypeFactory;
2021
import io.adminshell.aas.v3.dataformat.aml.deserialization.Aml2AasMapper;
2122
import io.adminshell.aas.v3.dataformat.aml.model.caex.CAEXFile;
2223
import io.adminshell.aas.v3.dataformat.mapping.MappingException;
@@ -34,14 +35,17 @@
3435
public class AmlDeserializer implements Deserializer {
3536

3637
private static final Logger log = LoggerFactory.getLogger(AmlDeserializer.class);
38+
private AasTypeFactory typeFactory = new AasTypeFactory();
3739

3840
@Override
3941
public AssetAdministrationShellEnvironment read(String value) throws DeserializationException {
4042
try {
4143
Unmarshaller unmarshaller = JAXBContextFactory.createContext(new Class[]{CAEXFile.class}, null).createUnmarshaller();
4244
StringReader reader = new StringReader(value);
4345
CAEXFile aml = (CAEXFile) unmarshaller.unmarshal(reader);
44-
Aml2AasMapper mapper = new Aml2AasMapper(new AmlDeserializationConfig.Builder().build());
46+
Aml2AasMapper mapper = new Aml2AasMapper(new AmlDeserializationConfig.Builder()
47+
.typeFactory(typeFactory)
48+
.build());
4549
return mapper.map(aml);
4650
} catch (JAXBException ex) {
4751
throw new DeserializationException("error deserializing AssetAdministrationShellEnvironment", ex);
@@ -52,6 +56,6 @@ public AssetAdministrationShellEnvironment read(String value) throws Deserializa
5256

5357
@Override
5458
public <T> void useImplementation(Class<T> aasInterface, Class<? extends T> implementation) {
55-
throw new UnsupportedOperationException("Not supported yet.");
59+
typeFactory.useImplementation(aasInterface, implementation);
5660
}
5761
}

dataformat-aml/src/main/java/io/adminshell/aas/v3/dataformat/aml/deserialization/Aml2AasMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public AssetAdministrationShellEnvironment map(CAEXFile aml) throws MappingExcep
7979
// propertyNamingStrategy.registerCustomNaming(Qualifier.class, x -> "qualifier:" + x.getType() + "=" + x.getValue(), false);
8080
MappingContext context = new MappingContext(mappingProvider, classNamingStrategy, propertyNamingStrategy, config.getTypeFactory());
8181
context.setDocumentInfo(AmlDocumentInfo.fromFile(aml));
82-
AssetAdministrationShellEnvironment result = (AssetAdministrationShellEnvironment) context.getMappingProvider().getMapper(AssetAdministrationShellEnvironment.class).map(parser, context);
82+
AssetAdministrationShellEnvironment result = context.map(AssetAdministrationShellEnvironment.class, parser);
8383
parser.resolveIdsToReferences(result);
8484
return (AssetAdministrationShellEnvironment) result;
8585
}

dataformat-aml/src/main/java/io/adminshell/aas/v3/dataformat/aml/deserialization/mappers/IdentifiableMapper.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.adminshell.aas.v3.dataformat.mapping.MappingException;
2525
import io.adminshell.aas.v3.model.Identifiable;
2626
import io.adminshell.aas.v3.model.Identifier;
27+
import io.adminshell.aas.v3.model.Key;
2728
import io.adminshell.aas.v3.model.Reference;
2829
import java.beans.PropertyDescriptor;
2930

@@ -49,7 +50,9 @@ protected void mapProperties(Object parent, AmlParser parser, MappingContext con
4950
Identifier identification = context.map(Identifier.class, parser);
5051
parser.setCurrent(temp);
5152
((Identifiable) parent).setIdentification(identification);
52-
Reference reference = AasUtils.toReference((Identifiable) parent);
53+
Reference reference = AasUtils.toReference((Identifiable) parent,
54+
context.getTypeFactory().getImplementationType(Reference.class),
55+
context.getTypeFactory().getImplementationType(Key.class));
5356
parser.collectIdMapping(parser.getCurrent().getID(), reference);
5457
super.mapProperties(parent, parser, context.with(reference));
5558
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.core.util;
17+
18+
import java.util.Comparator;
19+
20+
/**
21+
* Comparator comparing two classes regarding which type is more specific.
22+
*/
23+
public class MostSpecificClassComparator implements Comparator<Class<?>> {
24+
25+
@Override
26+
public int compare(Class<?> x, Class<?> y) {
27+
if (y.isAssignableFrom(x)) {
28+
if (x.isAssignableFrom(y)) {
29+
return 0;
30+
}
31+
return -1;
32+
}
33+
return 1;
34+
}
35+
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.adminshell.aas.v3.dataformat.mapping;
1717

1818
import com.google.common.reflect.TypeToken;
19+
import io.adminshell.aas.v3.dataformat.core.util.MostSpecificClassComparator;
1920
import io.adminshell.aas.v3.dataformat.core.util.MostSpecificTypeTokenComparator;
2021
import java.lang.reflect.Type;
2122
import java.util.ArrayList;
@@ -25,8 +26,6 @@
2526
import java.util.Map;
2627
import java.util.Objects;
2728
import java.util.Optional;
28-
import java.util.stream.Collectors;
29-
import java.util.stream.Stream;
3029
import org.slf4j.Logger;
3130
import org.slf4j.LoggerFactory;
3231

@@ -115,7 +114,7 @@ public T getMapper(Type type) {
115114
if (customMapper.isEmpty() && !TypeToken.of(Collection.class).isSupertypeOf(type)) {
116115
customMapper = mappings.entrySet().stream()
117116
.filter(x -> x.getKey().getRawType().isAssignableFrom(TypeToken.of(type).getRawType()))
118-
.sorted((x, y) -> Objects.compare(x.getKey(), y.getKey(), new MostSpecificTypeTokenComparator()))
117+
.sorted((x, y) -> Objects.compare(x.getKey().getRawType(), y.getKey().getRawType(), new MostSpecificClassComparator()))
119118
.map(x -> x.getValue())
120119
.findFirst();
121120
}

0 commit comments

Comments
 (0)