2525import org .contextmapper .contextmap .generator .model .*;
2626
2727import java .io .*;
28+ import java .nio .file .Files ;
29+ import java .nio .file .Paths ;
30+ import java .nio .file .StandardCopyOption ;
2831import java .util .*;
2932import java .util .stream .Collectors ;
3033
3942public class ContextMapGenerator {
4043
4144 private static final String EDGE_SPACING_UNIT = " " ;
45+ private static final String TEAM_ICON_FILE_NAME = "team-icon.png" ;
4246
4347 private Map <String , MutableNode > bcNodesMap ;
4448 private Set <MutableNode > genericNodes ;
@@ -146,7 +150,7 @@ public void generateContextMapGraphic(ContextMap contextMap, Format format, Outp
146150
147151 private Renderer generateContextMapGraphic (ContextMap contextMap , Format format ) throws IOException {
148152 exportImages ();
149- MutableGraph graph = createGraph (contextMap );
153+ MutableGraph graph = createGraph (contextMap , format == Format . DOT );
150154
151155 // store file
152156 if (useWidth )
@@ -155,22 +159,22 @@ private Renderer generateContextMapGraphic(ContextMap contextMap, Format format)
155159 return Graphviz .fromGraph (graph ).basedir (baseDir ).height (height ).render (format );
156160 }
157161
158- private MutableGraph createGraph (ContextMap contextMap ) {
162+ private MutableGraph createGraph (ContextMap contextMap , boolean withImagePath ) {
159163 this .bcNodesMap = new TreeMap <>();
160164 this .genericNodes = new HashSet <>();
161165 this .teamNodes = new HashSet <>();
162- MutableGraph rootGraph = createGraph ("ContextMapGraph" );
166+ MutableGraph rootGraph = createGraph ("ContextMapGraph" , withImagePath );
163167
164168 createNodes (contextMap .getBoundedContexts ());
165169
166170 if (!needsSubGraphs (contextMap )) {
167171 addNodesToGraph (rootGraph , bcNodesMap .values ());
168172 createRelationshipLinks4ExistingNodes (contextMap .getRelationships ());
169173 } else {
170- MutableGraph genericGraph = createGraph (getSubgraphName ("GenericSubgraph" ))
174+ MutableGraph genericGraph = createGraph (getSubgraphName ("GenericSubgraph" ), withImagePath )
171175 .graphAttrs ().add ("color" , "white" );
172176 addNodesToGraph (genericGraph , genericNodes );
173- MutableGraph teamGraph = createGraph (getSubgraphName ("Teams_Subgraph" ))
177+ MutableGraph teamGraph = createGraph (getSubgraphName ("Teams_Subgraph" ), withImagePath )
174178 .graphAttrs ().add ("color" , "white" );
175179 addNodesToGraph (teamGraph , teamNodes );
176180 genericGraph .addTo (rootGraph );
@@ -196,10 +200,11 @@ private boolean needsSubGraphs(ContextMap contextMap) {
196200 return hasGenericContexts && hasTeams ;
197201 }
198202
199- private MutableGraph createGraph (String name ) {
203+ private MutableGraph createGraph (String name , boolean withImagePath ) {
200204 MutableGraph rootGraph = mutGraph (name );
201205 rootGraph .setDirected (true );
202- rootGraph .graphAttrs ().add (attr ("imagepath" , baseDir .getAbsolutePath ()));
206+ if (withImagePath )
207+ rootGraph .graphAttrs ().add (attr ("imagepath" , baseDir .getAbsolutePath ()));
203208 return rootGraph ;
204209 }
205210
@@ -284,7 +289,7 @@ private void createTeamImplementationLinks(MutableGraph graph, List<BoundedConte
284289 MutableNode node1 = createNode (team );
285290 MutableNode node2 = createNode (system );
286291 node1 .addLink (to (node2 ).with (
287- Label .lines (" «realizes»" ),
292+ Label .lines (getRealizesLabel () ),
288293 attr ("color" , "#686868" ),
289294 attr ("fontname" , "sans-serif" ),
290295 attr ("fontsize" , "12" ),
@@ -297,9 +302,13 @@ private void createTeamImplementationLinks(MutableGraph graph, List<BoundedConte
297302 }
298303 }
299304
305+ private String getRealizesLabel () {
306+ return (System .getProperty ("os.name" ).toLowerCase ().indexOf ("win" ) >= 0 ) ? " \" realizes\" " : " «realizes»" ;
307+ }
308+
300309 private Label createNodeLabel (BoundedContext boundedContext ) {
301310 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\" >" +
303312 "</td><td><b>Team</b></td></tr><tr><td width=\" 10px\" ></td><td>" + boundedContext .getName () + "</td></tr></table>" );
304313 return Label .lines (boundedContext .getName ());
305314 }
@@ -367,15 +376,9 @@ private Label getEdgeHTMLLabel(String upstreamDownstreamLabel, Set<String> patte
367376 private void exportImages () throws IOException {
368377 if (!baseDir .exists ())
369378 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 );
379382 }
380383 }
381384
0 commit comments