|
| 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