|
| 1 | +package io.adminshell.aas.v3.dataformat; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.FileNotFoundException; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.io.InputStreamReader; |
| 8 | +import java.nio.charset.Charset; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.util.stream.Collectors; |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +import de.fraunhofer.iais.eis.util.*; |
| 15 | +import io.adminshell.aas.v3.model.*; |
| 16 | +import io.adminshell.aas.v3.model.builder.*; |
| 17 | +import io.adminshell.aas.v3.model.impl.*; |
| 18 | + |
| 19 | +/** |
| 20 | + * * Generic deserializer interface to deserialize a given string, Outputstream or java.io.File into |
| 21 | + * an instance of AssetAdministrationShellEnvironment |
| 22 | + */ |
| 23 | +public interface Deserializer { |
| 24 | + |
| 25 | + /*** Default charset that will be used when no charset is specified */ |
| 26 | + Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; |
| 27 | + |
| 28 | + /*** |
| 29 | + * Deserializes a given string into an instance of AssetAdministrationShellEnvironment |
| 30 | + * |
| 31 | + * @param value a string representation of the AssetAdministrationShellEnvironment |
| 32 | + * @return an instance of AssetAdministrationShellEnvironment |
| 33 | + * @throws DeserializationException if deserialization fails |
| 34 | + */ |
| 35 | + AssetAdministrationShellEnvironment read(String value) throws DeserializationException; |
| 36 | + |
| 37 | + /*** |
| 38 | + * Deserializes a given InputStream into an instance of AssetAdministrationShellEnvironment using |
| 39 | + * DEFAULT_CHARSET |
| 40 | + * |
| 41 | + * @param src an InputStream containing the string representation of the |
| 42 | + * AssetAdministrationShellEnvironment |
| 43 | + * @return an instance of AssetAdministrationShellEnvironment |
| 44 | + * @throws DeserializationException if deserialization fails |
| 45 | + */ |
| 46 | + default AssetAdministrationShellEnvironment read(InputStream src) throws DeserializationException { |
| 47 | + return read(src, DEFAULT_CHARSET); |
| 48 | + } |
| 49 | + |
| 50 | + /*** |
| 51 | + * Deserializes a given InputStream into an instance of AssetAdministrationShellEnvironment using a |
| 52 | + * given charset |
| 53 | + * |
| 54 | + * @param src An InputStream containing the string representation of the |
| 55 | + * AssetAdministrationShellEnvironment |
| 56 | + * @param charset the charset to use for deserialization |
| 57 | + * @return an instance of AssetAdministrationShellEnvironment |
| 58 | + * @throws DeserializationException if deserialization fails |
| 59 | + */ |
| 60 | + default AssetAdministrationShellEnvironment read(InputStream src, Charset charset) throws DeserializationException { |
| 61 | + return read(new BufferedReader( |
| 62 | + new InputStreamReader(src, charset)) |
| 63 | + .lines() |
| 64 | + .collect(Collectors.joining(System.lineSeparator()))); |
| 65 | + } |
| 66 | + |
| 67 | + // Note that the AAS also defines a file class |
| 68 | + |
| 69 | + /*** |
| 70 | + * Deserializes a given File into an instance of AssetAdministrationShellEnvironment using |
| 71 | + * DEFAULT_CHARSET |
| 72 | + * |
| 73 | + * @param file A java.io.File containing the string representation of the |
| 74 | + * AssetAdministrationShellEnvironment |
| 75 | + * @param charset the charset to use for deserialization |
| 76 | + * @return an instance of AssetAdministrationShellEnvironment |
| 77 | + * @throws FileNotFoundException if file is not present |
| 78 | + * @throws DeserializationException if deserialization fails |
| 79 | + */ |
| 80 | + default AssetAdministrationShellEnvironment read(java.io.File file, Charset charset) |
| 81 | + throws FileNotFoundException, DeserializationException { |
| 82 | + return read(new FileInputStream(file), charset); |
| 83 | + } |
| 84 | + |
| 85 | + /*** |
| 86 | + * Deserializes a given File into an instance of AssetAdministrationShellEnvironment using a given |
| 87 | + * charset |
| 88 | + * |
| 89 | + * @param file a java.io.File containing the string representation of the |
| 90 | + * AssetAdministrationShellEnvironment |
| 91 | + * @return an instance of AssetAdministrationShellEnvironment |
| 92 | + * @throws FileNotFoundException if the file is not present |
| 93 | + * @throws DeserializationException if deserialization fails |
| 94 | + */ |
| 95 | + default AssetAdministrationShellEnvironment read(java.io.File file) throws FileNotFoundException, DeserializationException { |
| 96 | + return read(file, DEFAULT_CHARSET); |
| 97 | + } |
| 98 | + |
| 99 | + /*** |
| 100 | + * Enables usage of custom implementation to be used for deserialization instead of default |
| 101 | + * implementation, e.g. defining a custom implementation of the Submodel interface {@code class |
| 102 | + * CustomSubmodel implements Submodel {}} and calling |
| 103 | + * {@code useImplementation(Submodel.class, CustomSubmodel.class);} will result in all instances of |
| 104 | + * Submodel will be deserialized as CustomSubmodel. Subsequent class with the same aasInterface |
| 105 | + * parameter will override the effects of all previous calls. |
| 106 | + * |
| 107 | + * @param <T> the type of the interface to replace |
| 108 | + * @param aasInterface the class of the interface to replace |
| 109 | + * @param implementation the class implementing the interface that should be used for |
| 110 | + * deserialization. |
| 111 | + */ |
| 112 | + <T> void useImplementation(Class<T> aasInterface, Class<? extends T> implementation); |
| 113 | + |
| 114 | +} |
0 commit comments