|
1 | 1 | package org.eclipse.esmf.aspectmodel.generator.zip;
|
2 | 2 |
|
3 | 3 | import java.io.BufferedOutputStream;
|
4 |
| -import java.io.FileOutputStream; |
| 4 | +import java.io.ByteArrayOutputStream; |
5 | 5 | import java.io.IOException;
|
6 | 6 | import java.io.OutputStream;
|
7 | 7 | import java.io.OutputStreamWriter;
|
8 | 8 | import java.io.Writer;
|
| 9 | +import java.util.stream.Stream; |
9 | 10 | import java.util.zip.ZipEntry;
|
10 | 11 | import java.util.zip.ZipOutputStream;
|
11 | 12 |
|
12 | 13 | import org.eclipse.esmf.aspectmodel.AspectModelFile;
|
| 14 | +import org.eclipse.esmf.aspectmodel.generator.GenerationException; |
| 15 | +import org.eclipse.esmf.aspectmodel.generator.Generator; |
13 | 16 | import org.eclipse.esmf.aspectmodel.serializer.AspectSerializer;
|
14 | 17 | import org.eclipse.esmf.metamodel.AspectModel;
|
15 | 18 |
|
16 |
| -import org.apache.commons.lang3.function.TriConsumer; |
| 19 | +public class AspectModelNamespacePackageCreator |
| 20 | + extends Generator<AspectModel, String, byte[], NamespacePackageGenerationConfig, NamespacePackageArtifact> { |
| 21 | + public static final NamespacePackageGenerationConfig DEFAULT_CONFIG = NamespacePackageGenerationConfigBuilder.builder().build(); |
| 22 | + @Deprecated( forRemoval = true ) |
| 23 | + public static final AspectModelNamespacePackageCreator INSTANCE = new AspectModelNamespacePackageCreator( null ); |
| 24 | + private static final String BASE_ARCHIVE_FORMAT_PATH = "aspect-models/"; |
17 | 25 |
|
18 |
| -public class AspectModelNamespacePackageCreator implements TriConsumer<AspectModel, OutputStream, String> { |
| 26 | + public AspectModelNamespacePackageCreator( final AspectModel aspectModel ) { |
| 27 | + this( aspectModel, DEFAULT_CONFIG ); |
| 28 | + } |
19 | 29 |
|
20 |
| - private static final String BASE_ARCHIVE_FORMAT_PATH = "aspect-models/"; |
| 30 | + public AspectModelNamespacePackageCreator( final AspectModel aspectModel, final NamespacePackageGenerationConfig config ) { |
| 31 | + super( aspectModel, config ); |
| 32 | + } |
21 | 33 |
|
22 |
| - public static final AspectModelNamespacePackageCreator INSTANCE = new AspectModelNamespacePackageCreator(); |
| 34 | + private AspectModel aspectModel() { |
| 35 | + return focus; |
| 36 | + } |
23 | 37 |
|
24 |
| - private AspectModelNamespacePackageCreator() { |
| 38 | + /** |
| 39 | + * @deprecated Use {@link #AspectModelNamespacePackageCreator(AspectModel, NamespacePackageGenerationConfig)} instead |
| 40 | + */ |
| 41 | + @Deprecated( forRemoval = true ) |
| 42 | + public void accept( final AspectModel aspectModel, final OutputStream outputStream, final String rootPath ) { |
| 43 | + final NamespacePackageGenerationConfig config = NamespacePackageGenerationConfigBuilder.builder() |
| 44 | + .rootPath( rootPath ) |
| 45 | + .build(); |
| 46 | + final NamespacePackageArtifact artifact = new AspectModelNamespacePackageCreator( aspectModel, config ).singleResult(); |
| 47 | + write( artifact, x -> outputStream ); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Figures out a fitting name for the generated namespace package |
| 52 | + * |
| 53 | + * @return a name for the generated namespace package |
| 54 | + */ |
| 55 | + private String packageName() { |
| 56 | + if ( aspectModel().aspects().size() == 1 ) { |
| 57 | + return aspectModel().aspect().getName() + ".zip"; |
| 58 | + } |
| 59 | + return "package" + aspectModel().hashCode() + ".zip"; // 🤷 |
25 | 60 | }
|
26 | 61 |
|
27 | 62 | @Override
|
28 |
| - public void accept( final AspectModel aspectModel, final OutputStream outputStream, final String rootPath ) { |
29 |
| - try ( final FileOutputStream fos = (FileOutputStream) outputStream; |
30 |
| - final BufferedOutputStream bos = new BufferedOutputStream( fos ); |
| 63 | + public Stream<NamespacePackageArtifact> generate() { |
| 64 | + final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 65 | + try ( final BufferedOutputStream bos = new BufferedOutputStream( out ); |
31 | 66 | final ZipOutputStream zos = new ZipOutputStream( bos ) ) {
|
32 |
| - |
33 |
| - for ( final AspectModelFile aspectModelFile : aspectModel.files() ) { |
34 |
| - addFileToArchive( aspectModelFile, zos, rootPath ); |
35 |
| - } |
36 |
| - } catch ( final IOException e ) { |
37 |
| - try { |
38 |
| - throw new IOException( "Error creating zip archive!", e ); |
39 |
| - } catch ( final IOException ex ) { |
40 |
| - throw new RuntimeException( ex ); |
| 67 | + for ( final AspectModelFile aspectModelFile : aspectModel().files() ) { |
| 68 | + addFileToArchive( aspectModelFile, zos, config.rootPath() ); |
41 | 69 | }
|
| 70 | + out.close(); |
| 71 | + } catch ( final IOException exception ) { |
| 72 | + throw new GenerationException( "Could not creat namespace package for Aspect Model", exception ); |
42 | 73 | }
|
| 74 | + |
| 75 | + return Stream.of( new NamespacePackageArtifact( packageName(), out.toByteArray() ) ); |
43 | 76 | }
|
44 | 77 |
|
45 | 78 | private static void addFileToArchive( final AspectModelFile file, final ZipOutputStream zos, final String rootPath )
|
|
0 commit comments