Skip to content

Commit 6cda2d0

Browse files
committed
Add generation zip from Aspect Model
1 parent 9db236e commit 6cda2d0

File tree

6 files changed

+208
-3
lines changed

6 files changed

+208
-3
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2024 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+
14+
package org.eclipse.esmf.aspectmodel.generator;
15+
16+
import java.io.BufferedOutputStream;
17+
import java.io.BufferedWriter;
18+
import java.io.FileOutputStream;
19+
import java.io.IOException;
20+
import java.io.OutputStreamWriter;
21+
import java.io.Writer;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import java.nio.file.Paths;
25+
import java.util.zip.ZipEntry;
26+
import java.util.zip.ZipOutputStream;
27+
28+
import org.eclipse.esmf.aspectmodel.AspectModelFile;
29+
import org.eclipse.esmf.metamodel.AspectModel;
30+
31+
public class AspectModelZipGenerator {
32+
33+
private static final int BUFFER_SIZE = 4096;
34+
35+
public static void generateZipAspectModelArchive( final AspectModel aspectModel, final String zipFilePath ) throws IOException {
36+
Path zipFile = Files.createFile( Paths.get( zipFilePath ) );
37+
try ( FileOutputStream fos = new FileOutputStream( zipFilePath );
38+
BufferedOutputStream bos = new BufferedOutputStream( fos );
39+
ZipOutputStream zos = new ZipOutputStream( bos ) ) {
40+
41+
for ( final AspectModelFile aspectModelFile : aspectModel.files() ) {
42+
addFileToZip( aspectModelFile, zos );
43+
}
44+
}
45+
}
46+
47+
private static void addFileToZip( final AspectModelFile file, final ZipOutputStream zos ) throws IOException {
48+
final String aspectString = file.aspect().toString();
49+
// final String aspectString = AspectSerializer.INSTANCE.apply( aspect );
50+
final String fileName = String.format( "%s/%s/%s.ttl", file.aspect().urn().getNamespace(), file.aspect().urn().getVersion(),
51+
file.aspect().getName() );
52+
final ZipEntry zipEntry = new ZipEntry( fileName );
53+
zos.putNextEntry( zipEntry );
54+
55+
try ( Writer writer = new BufferedWriter( new OutputStreamWriter( zos ) ) ) {
56+
writer.write( aspectString );
57+
} catch ( IOException exception ) {
58+
throw new IOException( "Could not write to zip entry: " + fileName, exception );
59+
} finally {
60+
zos.closeEntry();
61+
}
62+
}
63+
}

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/loader/AspectModelLoader.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,19 +192,18 @@ public AspectModel loadFromArchive( final String pathToArchive ) {
192192

193193
while ( entries.hasMoreElements() ) {
194194
ZipEntry entry = entries.nextElement();
195-
System.out.println( entry.getName() ); // Consider removing or modifying this debug statement in production.
196195

197196
if ( entry.getName().endsWith( ".ttl" ) ) {
198197
try ( InputStream inputStream = zip.getInputStream( entry ) ) {
199198
AspectModelFile aspectModelFile = AspectModelFileLoader.load( inputStream );
200199
aspectModelFiles.add( aspectModelFile );
201200
} catch ( IOException e ) {
202-
System.err.println( "Error reading entry: " + entry.getName() + " - " + e.getMessage() );
201+
LOG.error( String.format( "Error reading entry: %s - %s", entry.getName(), e.getMessage() ) );
203202
}
204203
}
205204
}
206205
} catch ( IOException e ) {
207-
System.err.println( "Error reading the ZIP file: " + e.getMessage() );
206+
LOG.error( String.format( "Error reading the ZIP file: %s", e.getMessage() ) );
208207
}
209208

210209
return buildAspectModel( aspectModelFiles );
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2024 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+
14+
package org.eclipse.esmf.aspectmodel.generator;
15+
16+
import java.io.IOException;
17+
18+
import org.eclipse.esmf.aspectmodel.AspectModelFile;
19+
import org.eclipse.esmf.aspectmodel.loader.AspectModelLoader;
20+
import org.eclipse.esmf.aspectmodel.resolver.AspectModelFileLoader;
21+
import org.eclipse.esmf.metamodel.AspectModel;
22+
import org.eclipse.esmf.test.TestAspect;
23+
import org.eclipse.esmf.test.TestResources;
24+
25+
import org.junit.jupiter.api.Test;
26+
27+
public class AspectModelZipGeneratorTest {
28+
29+
@Test
30+
public void testAspectModelZipGeneration() throws IOException {
31+
// final AspectModelFile aspectModelFile_1 = AspectModelFileLoader.load( )
32+
final AspectModel aspectModel = TestResources.load( TestAspect.ASPECT_WITH_MULTIPLE_ENTITIES_SAME_EXTEND );
33+
34+
AspectModelZipGenerator.generateZipAspectModelArchive( aspectModel, "/Users/Yauheni_Kapliarchuk/Downloads/testzip.zip" );
35+
}
36+
}

core/esmf-aspect-model-document-generators/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@
160160
<artifactId>logback-core</artifactId>
161161
<scope>test</scope>
162162
</dependency>
163+
<dependency>
164+
<groupId>org.eclipse.esmf</groupId>
165+
<artifactId>esmf-aspect-model-serializer</artifactId>
166+
</dependency>
163167
</dependencies>
164168

165169
<build>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.eclipse.esmf.aspectmodel.generator.zip;
2+
3+
import java.io.BufferedOutputStream;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.io.OutputStreamWriter;
7+
import java.io.Writer;
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
10+
import java.util.zip.ZipEntry;
11+
import java.util.zip.ZipOutputStream;
12+
13+
import org.eclipse.esmf.aspectmodel.AspectModelFile;
14+
import org.eclipse.esmf.aspectmodel.serializer.AspectSerializer;
15+
import org.eclipse.esmf.metamodel.AspectModel;
16+
17+
public class AspectModelZipGenerator {
18+
19+
public static void generateZipAspectModelArchive( final AspectModel aspectModel, final String zipFilePath ) throws IOException {
20+
final Path zipFile = Paths.get( zipFilePath );
21+
22+
try ( FileOutputStream fos = new FileOutputStream( zipFile.toFile() );
23+
BufferedOutputStream bos = new BufferedOutputStream( fos );
24+
ZipOutputStream zos = new ZipOutputStream( bos ) ) {
25+
26+
for ( final AspectModelFile aspectModelFile : aspectModel.files() ) {
27+
addFileToZip( aspectModelFile, zos );
28+
}
29+
} catch ( IOException e ) {
30+
throw new IOException( "Error creating zip archive: " + zipFilePath, e );
31+
}
32+
}
33+
34+
private static void addFileToZip( final AspectModelFile file, ZipOutputStream zos ) throws IOException {
35+
final String aspectString = AspectSerializer.INSTANCE.apply( file.aspect() );
36+
final String fileName = String.format( "%s/%s/%s.ttl",
37+
file.aspect().urn().getNamespace(),
38+
file.aspect().urn().getVersion(),
39+
file.aspect().getName() );
40+
final ZipEntry zipEntry = new ZipEntry( fileName );
41+
zos.putNextEntry( zipEntry );
42+
43+
Writer writer = new OutputStreamWriter( zos );
44+
writer.write( aspectString );
45+
writer.flush();
46+
47+
zos.closeEntry();
48+
}
49+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2024 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+
14+
package org.eclipse.esmf.aspectmodel.generator.zip;
15+
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
18+
import java.io.File;
19+
import java.io.IOException;
20+
import java.nio.file.Files;
21+
import java.nio.file.Path;
22+
23+
import org.eclipse.esmf.aspectmodel.loader.AspectModelLoader;
24+
import org.eclipse.esmf.metamodel.AspectModel;
25+
import org.eclipse.esmf.test.TestAspect;
26+
import org.eclipse.esmf.test.TestResources;
27+
28+
import org.junit.jupiter.api.BeforeEach;
29+
import org.junit.jupiter.api.Test;
30+
31+
class AspectModelZipGeneratorTest {
32+
33+
Path outputDirectory = null;
34+
35+
@BeforeEach
36+
void beforeEach() throws IOException {
37+
outputDirectory = Files.createTempDirectory( "junit" );
38+
}
39+
40+
@Test
41+
void testAspectModelZipGeneration() throws IOException {
42+
final AspectModel aspectModel = TestResources.load( TestAspect.ASPECT_WITH_PROPERTY );
43+
final String outputFileName = String.format( "%s/%s", outputDirectory.toString(), "/test_zip.zip" );
44+
45+
AspectModelZipGenerator.generateZipAspectModelArchive( aspectModel, outputFileName );
46+
47+
assertThat( new File( outputFileName ) ).exists();
48+
49+
final AspectModel aspectModelResult = new AspectModelLoader().loadFromArchive( outputFileName );
50+
assertThat( aspectModelResult.namespaces() ).hasSize( 1 );
51+
assertThat( aspectModelResult.files() ).hasSize( 1 );
52+
assertThat( aspectModelResult.files().get( 0 ).aspect() ).isEqualTo( aspectModel.aspect() );
53+
}
54+
}

0 commit comments

Comments
 (0)