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

Commit 493838e

Browse files
Working on deserialization, add deserialization of File
1 parent 2f80fe3 commit 493838e

3 files changed

Lines changed: 87 additions & 0 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public AssetAdministrationShellEnvironment map(CAEXFile aml) throws MappingExcep
6969
mappingProvider.register(new ConstraintCollectionMapper());
7070
mappingProvider.register(new QualifierMapper());
7171
mappingProvider.register(new OperationCollectionMapper());
72+
mappingProvider.register(new FileMapper());
7273
AbstractClassNamingStrategy classNamingStrategy = new NumberingClassNamingStrategy();
7374

7475
PropertyNamingStrategy propertyNamingStrategy = new PropertyNamingStrategy();

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.adminshell.aas.v3.dataformat.aml.model.caex.CAEXObject;
2020
import io.adminshell.aas.v3.dataformat.aml.model.caex.InternalElementType;
2121
import io.adminshell.aas.v3.dataformat.aml.model.caex.SystemUnitClassType;
22+
import io.adminshell.aas.v3.dataformat.aml.model.caex.InterfaceClassType;
2223
import io.adminshell.aas.v3.dataformat.core.ReflectionHelper;
2324
import io.adminshell.aas.v3.dataformat.core.util.AasUtils;
2425
import io.adminshell.aas.v3.dataformat.mapping.MappingException;
@@ -426,4 +427,15 @@ protected List<InternalElementType> findInternalElements(InternalElementType par
426427
}
427428
return parent.getInternalElement().stream().filter(filter).collect(Collectors.toList());
428429
}
430+
431+
protected List<InterfaceClassType> findExternalInterface(CAEXObject parent, Predicate<InterfaceClassType> filter) {
432+
if (parent == null || filter == null) {
433+
return List.of();
434+
}
435+
if (InternalElementType.class.isAssignableFrom(parent.getClass())) {
436+
return ((InternalElementType) parent).getExternalInterface().stream().filter(filter).collect(Collectors.toList());
437+
}
438+
return List.of();
439+
440+
}
429441
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.aml.deserialization.mappers;
17+
18+
import io.adminshell.aas.v3.dataformat.aml.deserialization.AmlParser;
19+
import io.adminshell.aas.v3.dataformat.aml.deserialization.DefaultMapper;
20+
import io.adminshell.aas.v3.dataformat.aml.deserialization.MappingContext;
21+
import io.adminshell.aas.v3.dataformat.aml.model.caex.AttributeType;
22+
import io.adminshell.aas.v3.dataformat.aml.model.caex.InterfaceClassType;
23+
import io.adminshell.aas.v3.dataformat.core.util.AasUtils;
24+
import io.adminshell.aas.v3.dataformat.mapping.MappingException;
25+
import io.adminshell.aas.v3.model.File;
26+
import java.beans.PropertyDescriptor;
27+
import java.util.List;
28+
29+
/**
30+
*
31+
*/
32+
public class FileMapper extends DefaultMapper<File> {
33+
34+
protected static PropertyDescriptor PROPERTY_VALUE = AasUtils.getProperty(File.class, "value");
35+
protected static PropertyDescriptor PROPERTY_MIME_TYPE = AasUtils.getProperty(File.class, "mimeType");
36+
37+
private static final String FILE_DATA_REFERENCE = "FileDataReference";
38+
private static final String MIME_TYPE_ATTRIBUTE_NAME = "MIMEType";
39+
private static final String REF_URI_ATTRIBUTE_NAME = "refUri";
40+
41+
public FileMapper() {
42+
super(PROPERTY_VALUE.getName(), PROPERTY_MIME_TYPE.getName());
43+
}
44+
45+
@Override
46+
protected void mapProperties(Object parent, AmlParser parser, MappingContext context) throws MappingException {
47+
if (parent == null || context == null) {
48+
return;
49+
}
50+
51+
List<InterfaceClassType> externalInterfaces = findExternalInterface(parser.getCurrent(), x -> x.getName().equalsIgnoreCase(FILE_DATA_REFERENCE));
52+
if (externalInterfaces == null || externalInterfaces.size() == 0)
53+
throw new MappingException(String.format("no external interfaces are found in file %s %s", parser.getCurrent().getID(), parser.getCurrent().getName()));
54+
55+
if (externalInterfaces.size() > 1)
56+
throw new MappingException(String.format("multiple external interfaces are found in file %s %s", parser.getCurrent().getID(), parser.getCurrent().getName()));
57+
58+
List<AttributeType> attributeTypes = externalInterfaces.get(0).getAttribute();
59+
AttributeType mimeTypeAttribute = attributeTypes.stream()
60+
.filter(x -> x.getName().equalsIgnoreCase(MIME_TYPE_ATTRIBUTE_NAME))
61+
.findFirst().orElse(null);
62+
AttributeType refUriAttribute = attributeTypes.stream()
63+
.filter(x -> x.getName().equalsIgnoreCase(REF_URI_ATTRIBUTE_NAME)).findFirst().orElse(null);
64+
65+
if (refUriAttribute != null)
66+
((File) parent).setValue(refUriAttribute.getValue() == null ? null : refUriAttribute.getValue().toString());
67+
68+
if (mimeTypeAttribute != null)
69+
((File) parent).setMimeType(mimeTypeAttribute.getValue() == null ? null : mimeTypeAttribute.getValue().toString());
70+
71+
super.mapProperties(parent, parser, context);
72+
}
73+
74+
}

0 commit comments

Comments
 (0)