25
25
import org .contextmapper .contextmap .generator .model .*;
26
26
27
27
import java .io .*;
28
+ import java .nio .file .Files ;
29
+ import java .nio .file .Paths ;
30
+ import java .nio .file .StandardCopyOption ;
28
31
import java .util .*;
29
32
import java .util .stream .Collectors ;
30
33
39
42
public class ContextMapGenerator {
40
43
41
44
private static final String EDGE_SPACING_UNIT = " " ;
45
+ private static final String TEAM_ICON_FILE_NAME = "team-icon.png" ;
42
46
43
47
private Map <String , MutableNode > bcNodesMap ;
44
48
private Set <MutableNode > genericNodes ;
@@ -146,7 +150,7 @@ public void generateContextMapGraphic(ContextMap contextMap, Format format, Outp
146
150
147
151
private Renderer generateContextMapGraphic (ContextMap contextMap , Format format ) throws IOException {
148
152
exportImages ();
149
- MutableGraph graph = createGraph (contextMap );
153
+ MutableGraph graph = createGraph (contextMap , format == Format . DOT );
150
154
151
155
// store file
152
156
if (useWidth )
@@ -155,22 +159,22 @@ private Renderer generateContextMapGraphic(ContextMap contextMap, Format format)
155
159
return Graphviz .fromGraph (graph ).basedir (baseDir ).height (height ).render (format );
156
160
}
157
161
158
- private MutableGraph createGraph (ContextMap contextMap ) {
162
+ private MutableGraph createGraph (ContextMap contextMap , boolean withImagePath ) {
159
163
this .bcNodesMap = new TreeMap <>();
160
164
this .genericNodes = new HashSet <>();
161
165
this .teamNodes = new HashSet <>();
162
- MutableGraph rootGraph = createGraph ("ContextMapGraph" );
166
+ MutableGraph rootGraph = createGraph ("ContextMapGraph" , withImagePath );
163
167
164
168
createNodes (contextMap .getBoundedContexts ());
165
169
166
170
if (!needsSubGraphs (contextMap )) {
167
171
addNodesToGraph (rootGraph , bcNodesMap .values ());
168
172
createRelationshipLinks4ExistingNodes (contextMap .getRelationships ());
169
173
} else {
170
- MutableGraph genericGraph = createGraph (getSubgraphName ("GenericSubgraph" ))
174
+ MutableGraph genericGraph = createGraph (getSubgraphName ("GenericSubgraph" ), withImagePath )
171
175
.graphAttrs ().add ("color" , "white" );
172
176
addNodesToGraph (genericGraph , genericNodes );
173
- MutableGraph teamGraph = createGraph (getSubgraphName ("Teams_Subgraph" ))
177
+ MutableGraph teamGraph = createGraph (getSubgraphName ("Teams_Subgraph" ), withImagePath )
174
178
.graphAttrs ().add ("color" , "white" );
175
179
addNodesToGraph (teamGraph , teamNodes );
176
180
genericGraph .addTo (rootGraph );
@@ -196,10 +200,11 @@ private boolean needsSubGraphs(ContextMap contextMap) {
196
200
return hasGenericContexts && hasTeams ;
197
201
}
198
202
199
- private MutableGraph createGraph (String name ) {
203
+ private MutableGraph createGraph (String name , boolean withImagePath ) {
200
204
MutableGraph rootGraph = mutGraph (name );
201
205
rootGraph .setDirected (true );
202
- rootGraph .graphAttrs ().add (attr ("imagepath" , baseDir .getAbsolutePath ()));
206
+ if (withImagePath )
207
+ rootGraph .graphAttrs ().add (attr ("imagepath" , baseDir .getAbsolutePath ()));
203
208
return rootGraph ;
204
209
}
205
210
@@ -284,7 +289,7 @@ private void createTeamImplementationLinks(MutableGraph graph, List<BoundedConte
284
289
MutableNode node1 = createNode (team );
285
290
MutableNode node2 = createNode (system );
286
291
node1 .addLink (to (node2 ).with (
287
- Label .lines (" «realizes»" ),
292
+ Label .lines (getRealizesLabel () ),
288
293
attr ("color" , "#686868" ),
289
294
attr ("fontname" , "sans-serif" ),
290
295
attr ("fontsize" , "12" ),
@@ -297,9 +302,13 @@ private void createTeamImplementationLinks(MutableGraph graph, List<BoundedConte
297
302
}
298
303
}
299
304
305
+ private String getRealizesLabel () {
306
+ return (System .getProperty ("os.name" ).toLowerCase ().indexOf ("win" ) >= 0 ) ? " \" realizes\" " : " «realizes»" ;
307
+ }
308
+
300
309
private Label createNodeLabel (BoundedContext boundedContext ) {
301
310
if (boundedContext .getType () == BoundedContextType .TEAM )
302
- return Label .html ("<table cellspacing=\" 0\" cellborder=\" 0\" border=\" 0\" ><tr><td rowspan=\" 2\" ><img src='team-icon.png ' /></td><td width=\" 10px\" >" +
311
+ return Label .html ("<table cellspacing=\" 0\" cellborder=\" 0\" border=\" 0\" ><tr><td rowspan=\" 2\" ><img src='" + TEAM_ICON_FILE_NAME + " ' /></td><td width=\" 10px\" >" +
303
312
"</td><td><b>Team</b></td></tr><tr><td width=\" 10px\" ></td><td>" + boundedContext .getName () + "</td></tr></table>" );
304
313
return Label .lines (boundedContext .getName ());
305
314
}
@@ -367,15 +376,9 @@ private Label getEdgeHTMLLabel(String upstreamDownstreamLabel, Set<String> patte
367
376
private void exportImages () throws IOException {
368
377
if (!baseDir .exists ())
369
378
baseDir .mkdir ();
370
- if (!new File (baseDir , "team-icon.png" ).exists ()) {
371
- InputStream teamIconInputStream = ContextMapGenerator .class .getClassLoader ().getResourceAsStream ("team-icon.png" );
372
- byte [] buffer = new byte [teamIconInputStream .available ()];
373
- teamIconInputStream .read (buffer );
374
- File targetFile = new File (baseDir , "team-icon.png" );
375
- OutputStream outStream = new FileOutputStream (targetFile );
376
- outStream .write (buffer );
377
- outStream .flush ();
378
- outStream .close ();
379
+ if (!new File (baseDir , TEAM_ICON_FILE_NAME ).exists ()) {
380
+ InputStream teamIconInputStream = ContextMapGenerator .class .getClassLoader ().getResourceAsStream (TEAM_ICON_FILE_NAME );
381
+ Files .copy (teamIconInputStream , Paths .get (baseDir .getAbsolutePath (), TEAM_ICON_FILE_NAME ), StandardCopyOption .REPLACE_EXISTING );
379
382
}
380
383
}
381
384
0 commit comments