|
| 1 | +package io.adminshell.aas.v3.dataformat.xml; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.Objects; |
| 6 | +import java.util.Optional; |
| 7 | + |
| 8 | +import io.adminshell.aas.v3.model.DataSpecificationContent; |
| 9 | +import io.adminshell.aas.v3.model.DataSpecificationIEC61360; |
| 10 | +import io.adminshell.aas.v3.model.KeyElements; |
| 11 | +import io.adminshell.aas.v3.model.KeyType; |
| 12 | +import io.adminshell.aas.v3.model.Reference; |
| 13 | +import io.adminshell.aas.v3.model.impl.DefaultKey; |
| 14 | +import io.adminshell.aas.v3.model.impl.DefaultReference; |
| 15 | + |
| 16 | +public class DataSpecificationManager { |
| 17 | + |
| 18 | + public static final String PROP_DATA_SPECIFICATION = "dataSpecification"; |
| 19 | + public static final String PROP_DATA_SPECIFICATION_CONTENT = "dataSpecificationContent"; |
| 20 | + |
| 21 | + private static final Map<Reference, Class<? extends DataSpecificationContent>> KNOWN_IMPLEMENTATIONS = Map.of( |
| 22 | + createGlobalIri("http://admin-shell.io/DataSpecificationTemplates/DataSpecificationIEC61360/2/0"), |
| 23 | + DataSpecificationIEC61360.class); |
| 24 | + |
| 25 | + public static void register(Reference reference, Class<? extends DataSpecificationContent> implementation) { |
| 26 | + KNOWN_IMPLEMENTATIONS.put(reference, implementation); |
| 27 | + } |
| 28 | + |
| 29 | + public static void registerGlobalIri(String iri, Class<? extends DataSpecificationContent> implementation) { |
| 30 | + KNOWN_IMPLEMENTATIONS.put(createGlobalIri(iri), implementation); |
| 31 | + } |
| 32 | + |
| 33 | + private static Reference createGlobalIri(String iri) { |
| 34 | + return new DefaultReference.Builder().keys(Arrays.asList( |
| 35 | + new DefaultKey.Builder().idType(KeyType.IRI).type(KeyElements.GLOBAL_REFERENCE).value(iri).build())) |
| 36 | + .build(); |
| 37 | + } |
| 38 | + |
| 39 | + public static Reference getReference(Class<? extends DataSpecificationContent> implementation) { |
| 40 | + Reference result = getReferenceSafe(implementation); |
| 41 | + if (result == null) { |
| 42 | + throw new IllegalArgumentException( |
| 43 | + String.format("unknown data specification implementation '%s'", implementation.getName())); |
| 44 | + } |
| 45 | + return result; |
| 46 | + } |
| 47 | + |
| 48 | + private static Reference getReferenceSafe(Class<? extends DataSpecificationContent> implementation) { |
| 49 | + Optional<Reference> exactMatch = KNOWN_IMPLEMENTATIONS.entrySet().stream() |
| 50 | + .filter(x -> Objects.equals(x.getValue(), implementation)).map(x -> x.getKey()).findFirst(); |
| 51 | + if (exactMatch.isPresent()) { |
| 52 | + return exactMatch.get(); |
| 53 | + } |
| 54 | + Optional<Reference> inheritanceMatch = KNOWN_IMPLEMENTATIONS.entrySet().stream() |
| 55 | + .filter(x -> x.getValue().isAssignableFrom(implementation)).map(x -> x.getKey()).findFirst(); |
| 56 | + if (inheritanceMatch.isPresent()) { |
| 57 | + return inheritanceMatch.get(); |
| 58 | + } |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + public static Class<? extends DataSpecificationContent> getImplementation(Reference reference) { |
| 63 | + if (KNOWN_IMPLEMENTATIONS.containsKey(reference)) { |
| 64 | + return KNOWN_IMPLEMENTATIONS.get(reference); |
| 65 | + } |
| 66 | + throw new IllegalArgumentException(String.format("unknown data specification reference '%s'", reference)); |
| 67 | + } |
| 68 | +} |
0 commit comments