|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 2022 Robert Bosch Manufacturing Solutions GmbH |
| 3 | + * |
| 4 | + * See the AUTHORS file(s) distributed with this work for additional |
| 5 | + * information regarding authorship. |
| 6 | + * |
| 7 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 8 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 9 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: MPL-2.0 |
| 12 | + */ |
| 13 | +package io.openmanufacturing.sds.aspectmodel.aas; |
| 14 | + |
| 15 | +import java.io.ByteArrayOutputStream; |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.OutputStream; |
| 18 | +import java.util.function.Function; |
| 19 | + |
| 20 | +import io.adminshell.aas.v3.dataformat.SerializationException; |
| 21 | +import io.adminshell.aas.v3.dataformat.aasx.AASXSerializer; |
| 22 | +import io.adminshell.aas.v3.dataformat.xml.XmlSerializer; |
| 23 | +import io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment; |
| 24 | +import io.openmanufacturing.sds.metamodel.Aspect; |
| 25 | + |
| 26 | +/** Generator that generates an AASX file containing an AAS submodel for a given Aspect model */ |
| 27 | +public class AspectModelAASGenerator { |
| 28 | + |
| 29 | + /** |
| 30 | + * Generates an AASX archive file for a given Aspect and writes it to a given OutputStream provided by <code>nameMapper<code/> |
| 31 | + * |
| 32 | + * @param aspect the Aspect for which an AASX archive shall be generated |
| 33 | + * @param nameMapper a Name Mapper implementation, which provides an OutputStream for a given filename |
| 34 | + * @throws IOException in case the generation can not properly be executed |
| 35 | + */ |
| 36 | + public void generateAASXFile( final Aspect aspect, final Function<String, OutputStream> nameMapper ) |
| 37 | + throws IOException { |
| 38 | + try ( final OutputStream output = nameMapper.apply( aspect.getName() ) ) { |
| 39 | + output.write( generateAasxOutput( aspect ).toByteArray() ); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Generates an AAS XML archive file for a given Aspect and writes it to a given OutputStream provided by <code>nameMapper<code/> |
| 45 | + * |
| 46 | + * @param aspect the Aspect for which an AASX archive shall be generated |
| 47 | + * @param nameMapper a Name Mapper implementation, which provides an OutputStream for a given filename |
| 48 | + * @throws IOException in case the generation can not properly be executed |
| 49 | + */ |
| 50 | + public void generateAasXmlFile( |
| 51 | + final Aspect aspect, final Function<String, OutputStream> nameMapper ) throws IOException { |
| 52 | + try ( final OutputStream output = nameMapper.apply( aspect.getName() ) ) { |
| 53 | + output.write( generateXmlOutput( aspect ).toByteArray() ); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + protected ByteArrayOutputStream generateAasxOutput( Aspect aspect ) throws IOException { |
| 58 | + final AspectModelAASVisitor visitor = new AspectModelAASVisitor(); |
| 59 | + AssetAdministrationShellEnvironment environment = visitor.visitAspect( aspect, null ); |
| 60 | + |
| 61 | + try ( ByteArrayOutputStream out = new ByteArrayOutputStream() ) { |
| 62 | + AASXSerializer serializer = new AASXSerializer(); |
| 63 | + serializer.write( environment, null, out ); |
| 64 | + return out; |
| 65 | + } catch ( SerializationException e ) { |
| 66 | + throw new IOException( e ); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + protected ByteArrayOutputStream generateXmlOutput( Aspect aspect ) throws IOException { |
| 71 | + final AspectModelAASVisitor visitor = new AspectModelAASVisitor(); |
| 72 | + AssetAdministrationShellEnvironment environment = visitor.visitAspect( aspect, null ); |
| 73 | + |
| 74 | + try ( ByteArrayOutputStream out = new ByteArrayOutputStream() ) { |
| 75 | + XmlSerializer serializer = new XmlSerializer(); |
| 76 | + serializer.write( out, environment ); |
| 77 | + return out; |
| 78 | + } catch ( SerializationException e ) { |
| 79 | + throw new IOException( e ); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments