24
24
import java .io .OutputStream ;
25
25
import java .io .UncheckedIOException ;
26
26
import java .nio .charset .StandardCharsets ;
27
- import java .nio .file .Files ;
28
27
import java .util .Base64 ;
29
28
import java .util .EnumMap ;
30
29
import java .util .List ;
33
32
import java .util .Scanner ;
34
33
import java .util .Set ;
35
34
import java .util .function .Function ;
36
- import java .util .stream .Collectors ;
37
35
import java .util .stream .StreamSupport ;
38
36
39
- import org .apache .commons .text .StringEscapeUtils ;
40
- import org .apache .commons .text .WordUtils ;
41
- import org .apache .jena .query .ARQ ;
42
- import org .apache .jena .query .Query ;
43
- import org .apache .jena .query .QueryFactory ;
44
- import org .apache .jena .rdf .model .Model ;
45
- import org .apache .jena .rdf .model .ModelFactory ;
46
- import org .apache .jena .rdf .model .Property ;
47
- import org .apache .jena .rdf .model .RDFNode ;
48
- import org .apache .jena .rdf .model .Resource ;
49
- import org .apache .jena .rdf .model .ResourceFactory ;
50
- import org .apache .jena .rdf .model .Statement ;
51
- import org .apache .jena .vocabulary .RDF ;
52
37
import org .eclipse .esmf .aspectmodel .UnsupportedVersionException ;
53
38
import org .eclipse .esmf .aspectmodel .generator .LanguageCollector ;
54
39
import org .eclipse .esmf .aspectmodel .resolver .services .MetaModelUrls ;
59
44
import org .eclipse .esmf .samm .KnownVersion ;
60
45
61
46
import com .google .common .collect .ImmutableList ;
62
-
63
47
import guru .nidi .graphviz .engine .Graphviz ;
64
48
import guru .nidi .graphviz .model .MutableGraph ;
65
49
import guru .nidi .graphviz .parse .Parser ;
50
+ import org .apache .commons .text .StringEscapeUtils ;
51
+ import org .apache .commons .text .WordUtils ;
52
+ import org .apache .jena .query .ARQ ;
53
+ import org .apache .jena .query .Query ;
54
+ import org .apache .jena .query .QueryFactory ;
55
+ import org .apache .jena .rdf .model .Model ;
56
+ import org .apache .jena .rdf .model .ModelFactory ;
57
+ import org .apache .jena .rdf .model .Property ;
58
+ import org .apache .jena .rdf .model .RDFNode ;
59
+ import org .apache .jena .rdf .model .Resource ;
60
+ import org .apache .jena .rdf .model .ResourceFactory ;
61
+ import org .apache .jena .rdf .model .Statement ;
62
+ import org .apache .jena .vocabulary .RDF ;
66
63
67
64
public class AspectModelDiagramGenerator {
68
65
public enum Format {
@@ -140,6 +137,11 @@ ImmutableList.<String> builder().addAll( queryFilesForAllMetaModelVersions )
140
137
.addAll ( queryFilesForMetaModelVersionsAsOf2_0_0 )
141
138
.build () );
142
139
140
+ aspectToBoxmodelQueryFiles .put ( KnownVersion .SAMM_2_1_0 ,
141
+ ImmutableList .<String > builder ().addAll ( queryFilesForAllMetaModelVersions )
142
+ .addAll ( queryFilesForMetaModelVersionsAsOf2_0_0 )
143
+ .build () );
144
+
143
145
ARQ .init ();
144
146
model = versionedModel .getModel ();
145
147
metaModelVersion = KnownVersion .fromVersionString ( versionedModel .getMetaModelVersion ().toString () )
@@ -171,7 +173,7 @@ String getInputStreamAsString( final String resource ) {
171
173
private void generatePng ( final String dotInput , final OutputStream output ) throws IOException {
172
174
// To make the font available during PNG generation, it needs to be registered
173
175
// in Java Runtime's graphics environment
174
- try {
176
+ try {
175
177
final File tmpFontFile = generateTmpFontFile ();
176
178
final Font f = Font .createFont ( Font .TRUETYPE_FONT , tmpFontFile );
177
179
final GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment ();
@@ -184,17 +186,19 @@ private void generatePng( final String dotInput, final OutputStream output ) thr
184
186
final Graphviz graphviz = Graphviz .fromGraph ( g );
185
187
graphviz .render ( guru .nidi .graphviz .engine .Format .PNG ).toOutputStream ( output );
186
188
}
189
+
187
190
private File generateTmpFontFile () throws IOException {
188
- File tempFontFile = new File ( System .getProperty ( "java.io.tmpdir" ) + File .separator + "aspect-model-diagram.tmp" );
189
- if ( !tempFontFile .exists () ){
191
+ final File tempFontFile = new File ( System .getProperty ( "java.io.tmpdir" ) + File .separator + "aspect-model-diagram.tmp" );
192
+ if ( !tempFontFile .exists () ) {
190
193
try ( final InputStream fontStream = getInputStream ( FONT_FILE );
191
- final OutputStream output = new FileOutputStream ( tempFontFile , false ) ){
192
- fontStream .transferTo ( output );
193
- }
194
+ final OutputStream output = new FileOutputStream ( tempFontFile , false ) ) {
195
+ fontStream .transferTo ( output );
196
+ }
194
197
}
195
198
tempFontFile .deleteOnExit ();
196
- return tempFontFile ;
199
+ return tempFontFile ;
197
200
}
201
+
198
202
private String base64EncodeInputStream ( final InputStream in ) throws IOException {
199
203
try ( final ByteArrayOutputStream os = new ByteArrayOutputStream () ) {
200
204
final byte [] buffer = new byte [1024 ];
@@ -223,16 +227,16 @@ private void generateSvg( final String dotInput, final OutputStream output ) thr
223
227
+ "\" );\n "
224
228
+ "}\n "
225
229
+ "</style>" ;
226
- final String result = svgOutput .toString ( StandardCharsets .UTF_8 . name () )
230
+ final String result = svgOutput .toString ( StandardCharsets .UTF_8 )
227
231
.replaceFirst ( ">" , ">" + css );
228
- output .write ( result .getBytes ( StandardCharsets .UTF_8 . name () ) );
232
+ output .write ( result .getBytes ( StandardCharsets .UTF_8 ) );
229
233
}
230
234
}
231
235
232
236
private void breakLongLinesAndEscapeTexts ( final Model model ) {
233
237
final Property text = boxModelNamespace .text ();
234
238
final Iterable <Statement > statementIterable = () -> model .listStatements ( null , text , (RDFNode ) null );
235
- StreamSupport .stream ( statementIterable .spliterator (), false ).collect ( Collectors . toList () )
239
+ StreamSupport .stream ( statementIterable .spliterator (), false ).toList ()
236
240
.forEach ( oldStatement -> {
237
241
final String newValue = WordUtils .wrap ( oldStatement .getLiteral ().getString (), 60 , "\\ l " , false );
238
242
final String escapedValue = StringEscapeUtils .escapeHtml4 ( newValue );
@@ -297,15 +301,9 @@ public void generateDiagram( final Format outputFormat, final Locale language, f
297
301
final String dotResult = generateDot ( language );
298
302
299
303
switch ( outputFormat ) {
300
- case DOT :
301
- out .write ( dotResult .getBytes ( StandardCharsets .UTF_8 ) );
302
- break ;
303
- case PNG :
304
- generatePng ( dotResult , out );
305
- break ;
306
- case SVG :
307
- generateSvg ( dotResult , out );
308
- break ;
304
+ case DOT -> out .write ( dotResult .getBytes ( StandardCharsets .UTF_8 ) );
305
+ case PNG -> generatePng ( dotResult , out );
306
+ case SVG -> generateSvg ( dotResult , out );
309
307
}
310
308
}
311
309
@@ -349,15 +347,9 @@ public void generateDiagrams( final Set<Format> targetFormats, final Locale lang
349
347
try ( final OutputStream outputStream = nameMapper
350
348
.apply ( format .getArtifactFilename ( aspectName , language ) ) ) {
351
349
switch ( format ) {
352
- case DOT :
353
- outputStream .write ( dotResult .getBytes ( StandardCharsets .UTF_8 .name () ) );
354
- break ;
355
- case PNG :
356
- generatePng ( dotResult , outputStream );
357
- break ;
358
- case SVG :
359
- generateSvg ( dotResult , outputStream );
360
- break ;
350
+ case DOT -> outputStream .write ( dotResult .getBytes ( StandardCharsets .UTF_8 ) );
351
+ case PNG -> generatePng ( dotResult , outputStream );
352
+ case SVG -> generateSvg ( dotResult , outputStream );
361
353
}
362
354
}
363
355
}
0 commit comments