Skip to content

Commit ef8861c

Browse files
committed
Turn aspectModelDocumentationGenerator into a standard Generator
I.e., have it inherit from Generator and use the same configuration approach as the other generators
1 parent 2c617c8 commit ef8861c

File tree

13 files changed

+211
-274
lines changed

13 files changed

+211
-274
lines changed

core/esmf-aspect-model-document-generators/src/main/java/org/eclipse/esmf/aspectmodel/generator/JsonArtifact.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
package org.eclipse.esmf.aspectmodel.generator;
1515

16+
import java.nio.charset.StandardCharsets;
17+
1618
import com.fasterxml.jackson.core.JsonProcessingException;
1719
import com.fasterxml.jackson.databind.JsonNode;
1820
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
@@ -46,6 +48,11 @@ public String getContentAsYaml() {
4648
return jsonToYaml( getContent() );
4749
}
4850

51+
@Override
52+
public byte[] serialize() {
53+
return getContent().toPrettyString().getBytes( StandardCharsets.UTF_8 );
54+
}
55+
4956
protected String jsonToYaml( final JsonNode json ) {
5057
try {
5158
final YAMLFactory yamlFactory = YAMLFactory.builder()

core/esmf-aspect-model-document-generators/src/main/java/org/eclipse/esmf/aspectmodel/generator/JsonGenerator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,10 @@ public void generateJson( final Function<String, OutputStream> nameMapper ) {
100100
@Override
101101
protected void write( final Artifact<String, R> artifact, final Function<String, OutputStream> nameMapper ) {
102102
try ( final OutputStream output = nameMapper.apply( aspect.getName() ) ) {
103-
output.write( artifact.getContent().toPrettyString().getBytes() );
103+
output.write( artifact.serialize() );
104+
output.flush();
104105
} catch ( final IOException exception ) {
105-
throw new RuntimeException( exception );
106+
throw new DocumentGenerationException( exception );
106107
}
107108
}
108109
}

core/esmf-aspect-model-document-generators/src/main/java/org/eclipse/esmf/aspectmodel/generator/docu/AspectModelDocumentationGenerator.java

Lines changed: 95 additions & 215 deletions
Large diffs are not rendered by default.
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.docu;
15+
16+
import org.eclipse.esmf.aspectmodel.generator.Artifact;
17+
18+
public class DocumentationArtifact implements Artifact<String, String> {
19+
private final String id;
20+
private final String content;
21+
22+
public DocumentationArtifact( final String id, final String content ) {
23+
this.id = id;
24+
this.content = content;
25+
}
26+
27+
@Override
28+
public String getId() {
29+
return "";
30+
}
31+
32+
@Override
33+
public String getContent() {
34+
return "";
35+
}
36+
}
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.docu;
15+
16+
import java.util.Locale;
17+
18+
import org.eclipse.esmf.aspectmodel.generator.GenerationConfig;
19+
20+
import io.soabase.recordbuilder.core.RecordBuilder;
21+
22+
/**
23+
* @param locale The target language to generate for
24+
* @param stylesheet the CSS stylesheet to use in the generated HTML
25+
*/
26+
@RecordBuilder
27+
public record DocumentationGenerationConfig(
28+
Locale locale,
29+
String stylesheet
30+
) implements GenerationConfig {
31+
public DocumentationGenerationConfig {
32+
if ( locale == null ) {
33+
locale = Locale.ENGLISH;
34+
}
35+
}
36+
}

core/esmf-aspect-model-document-generators/src/main/java/org/eclipse/esmf/aspectmodel/generator/jsonld/AspectModelToJsonLdGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.slf4j.LoggerFactory;
2727

2828
public class AspectModelToJsonLdGenerator extends JsonGenerator<JsonLdGenerationConfig, JsonNode, JsonLdArtifact> {
29-
public static final JsonLdGenerationConfig DEFAULT_CONFIG = null;
29+
public static final JsonLdGenerationConfig DEFAULT_CONFIG = JsonLdGenerationConfigBuilder.builder().build();
3030
private static final Logger LOG = LoggerFactory.getLogger( AspectModelToJsonLdGenerator.class );
3131

3232
public AspectModelToJsonLdGenerator( final Aspect aspect ) {

core/esmf-aspect-model-document-generators/src/test/java/org/eclipse/esmf/aspectmodel/generator/docu/AspectModelDocumentationGeneratorTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.io.IOException;
2121
import java.io.PrintStream;
2222
import java.nio.charset.StandardCharsets;
23-
import java.util.Map;
2423

2524
import org.eclipse.esmf.metamodel.Aspect;
2625
import org.eclipse.esmf.test.TestAspect;
@@ -186,7 +185,7 @@ private String generateHtmlDocumentation( final TestAspect testAspect ) throws I
186185
final AspectModelDocumentationGenerator aspectModelDocumentationGenerator = new AspectModelDocumentationGenerator( aspect );
187186

188187
try ( final ByteArrayOutputStream result = new ByteArrayOutputStream() ) {
189-
aspectModelDocumentationGenerator.generate( name -> result, Map.of() );
188+
aspectModelDocumentationGenerator.generate();
190189
return result.toString( StandardCharsets.UTF_8 );
191190
}
192191
}

core/esmf-aspect-model-generator/src/main/java/org/eclipse/esmf/aspectmodel/generator/Artifact.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
package org.eclipse.esmf.aspectmodel.generator;
1515

16+
import java.nio.charset.StandardCharsets;
17+
1618
/**
1719
* Represents an artifact during generation, for example a single generated class.
1820
*
@@ -23,4 +25,8 @@ public interface Artifact<I, T> {
2325
I getId();
2426

2527
T getContent();
28+
29+
default byte[] serialize() {
30+
return getContent().toString().getBytes( StandardCharsets.UTF_8 );
31+
}
2632
}

core/esmf-aspect-model-generator/src/main/java/org/eclipse/esmf/aspectmodel/generator/Generator.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,26 @@
1919
import java.io.Writer;
2020
import java.nio.charset.StandardCharsets;
2121
import java.util.Comparator;
22-
import java.util.List;
2322
import java.util.function.Function;
2423
import java.util.stream.Stream;
2524

2625
import org.eclipse.esmf.aspectmodel.visitor.AspectStreamTraversalVisitor;
2726
import org.eclipse.esmf.metamodel.Aspect;
2827
import org.eclipse.esmf.metamodel.ModelElement;
2928

29+
import org.slf4j.Logger;
30+
import org.slf4j.LoggerFactory;
31+
3032
/**
3133
* Base class for the generation of {@link Artifact}s.
3234
*
3335
* @param <I> the type that uniquely identifies the artifact in the scope of the generation process
3436
* @param <T> the artifact's content type, e.g. String or byte[]
3537
* @param <C> the config object for the genererator
38+
* @param <A> the type of the artifact that is generated
3639
*/
3740
public abstract class Generator<I, T, C extends GenerationConfig, A extends Artifact<I, T>> {
41+
private static final Logger LOG = LoggerFactory.getLogger( Generator.class );
3842
protected final C config;
3943
protected final Aspect aspect;
4044
protected final Comparator<ModelElement> uniqueByModelElementIdentifier = ( modelElementOne, modelElementTwo ) -> {
@@ -83,8 +87,7 @@ protected void writeCharSequenceToOutputStream( final CharSequence charSequence,
8387
* @param nameMapper the callback function that maps artifact identifiers to OutputStreams
8488
*/
8589
public void generate( final Function<I, OutputStream> nameMapper ) {
86-
final List<A> artifacts = generate().toList();
87-
artifacts.forEach( generationResult -> write( generationResult, nameMapper ) );
90+
generate().toList().forEach( generationResult -> write( generationResult, nameMapper ) );
8891
}
8992

9093
/**
@@ -120,5 +123,12 @@ public T getContent() {
120123
* @param artifact the artifact
121124
* @param nameMapper the function that provides the output stream for the artifact
122125
*/
123-
protected abstract void write( final Artifact<I, T> artifact, final Function<I, OutputStream> nameMapper );
126+
protected void write( final Artifact<I, T> artifact, final Function<I, OutputStream> nameMapper ) {
127+
try ( final OutputStream output = nameMapper.apply( artifact.getId() ) ) {
128+
output.write( artifact.serialize() );
129+
output.flush();
130+
} catch ( final IOException exception ) {
131+
LOG.error( "Failure during writing of generated artifact", exception );
132+
}
133+
}
124134
}

core/esmf-aspect-model-java-generator/src/main/java/org/eclipse/esmf/aspectmodel/java/JavaGenerator.java

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,15 @@
1212
*/
1313
package org.eclipse.esmf.aspectmodel.java;
1414

15-
import java.io.IOException;
16-
import java.io.OutputStream;
17-
import java.io.OutputStreamWriter;
18-
import java.io.Writer;
19-
import java.nio.charset.StandardCharsets;
20-
import java.util.function.Function;
21-
2215
import org.eclipse.esmf.aspectmodel.generator.Artifact;
2316
import org.eclipse.esmf.aspectmodel.generator.Generator;
2417
import org.eclipse.esmf.metamodel.Aspect;
2518

26-
import org.slf4j.Logger;
27-
import org.slf4j.LoggerFactory;
28-
2919
/**
3020
* Base class for all generators that want to create Java source code.
3121
*/
3222
public abstract class JavaGenerator extends Generator<QualifiedName, String, JavaCodeGenerationConfig, Artifact<QualifiedName, String>> {
33-
private static final Logger LOG = LoggerFactory.getLogger( JavaGenerator.class );
34-
3523
public JavaGenerator( final Aspect aspect, final JavaCodeGenerationConfig config ) {
3624
super( aspect, config );
3725
}
38-
39-
@Override
40-
public void write( final Artifact<QualifiedName, String> artifact, final Function<QualifiedName, OutputStream> nameMapper ) {
41-
final QualifiedName qualifiedName = artifact.getId();
42-
final OutputStream outputStream = nameMapper.apply( qualifiedName );
43-
final String content = artifact.getContent();
44-
45-
try {
46-
try ( final Writer writer = new OutputStreamWriter( outputStream, StandardCharsets.UTF_8 ) ) {
47-
for ( int i = 0; i < content.length(); i++ ) {
48-
writer.write( content.charAt( i ) );
49-
}
50-
writer.flush();
51-
}
52-
} catch ( final IOException e ) {
53-
LOG.error( "Failure during writing of generated code.", e );
54-
}
55-
}
5626
}

0 commit comments

Comments
 (0)