Skip to content

Commit f5d196a

Browse files
authored
Bugfix/win team map (#7)
1 parent 904a1ec commit f5d196a

File tree

4 files changed

+37
-22
lines changed

4 files changed

+37
-22
lines changed

src/main/java/guru/nidi/graphviz/engine/Graphviz.java

+4
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ Format format() {
209209
return options.format;
210210
}
211211

212+
public Options getOptions() {
213+
return options;
214+
}
215+
212216
double dpi() {
213217
final Matcher matcher = DPI_PATTERN.matcher(src);
214218
return matcher.find() ? Double.parseDouble(matcher.group(1)) : 72;

src/main/java/guru/nidi/graphviz/engine/GraphvizCmdLineEngine.java

+3
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ public EngineResult execute(String src, Options options, Rasterizer rasterizer)
6868
new OutputStreamWriter(new FileOutputStream(dotFile), StandardCharsets.UTF_8))) {
6969
bw.write(preprocessCode(src, options));
7070
}
71+
System.out.println("try to call: path=" + path.toString() + "; dotFile=" + dotFile.getAbsolutePath() + "; options=" + options.toString());
7172
return doExecute(path, dotFile, options, rasterizer);
7273
} catch (IOException | InterruptedException e) {
74+
System.out.println("following exception was thrown:");
75+
e.printStackTrace();
7376
throw new GraphvizException(e.getMessage(), e);
7477
}
7578
}

src/main/java/guru/nidi/graphviz/engine/SvgRasterizer.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.awt.*;
1919
import java.awt.image.BufferedImage;
20+
import java.io.File;
2021
import java.util.function.Consumer;
2122

2223
abstract class SvgRasterizer implements Rasterizer {
@@ -27,14 +28,18 @@ public Format format() {
2728

2829
@Override
2930
public BufferedImage rasterize(Graphviz graphviz, Consumer<Graphics2D> graphicsConfigurer, String input) {
30-
final String svg = input
31+
String svg = input
3132
.replace("xlink:href=\"", "xlink:href=\"file://")
3233
.replace("stroke=\"transparent\"", "stroke=\"#fff\" stroke-opacity=\"0.0\"")
3334
.replace("fill=\"transparent\"", "fill=\"#fff\" fill-opacity=\"0.0\"");
35+
36+
String baseDirStringOrig = graphviz.getOptions().basedir.getAbsolutePath() + File.separator;
37+
String baseDirString = baseDirStringOrig.replace("\\", "/");
38+
if(System.getProperty("os.name").toLowerCase().indexOf("win") >= 0)
39+
svg = svg.replace("xlink:href=\"file://" + baseDirStringOrig, "xlink:href=\"file:///" + baseDirString);
40+
3441
return doRasterize(graphviz, graphicsConfigurer, svg);
3542
}
3643

37-
abstract BufferedImage doRasterize(Graphviz graphviz,
38-
Consumer<Graphics2D> graphicsConfigurer,
39-
String svg);
44+
abstract BufferedImage doRasterize(Graphviz graphviz, Consumer<Graphics2D> graphicsConfigurer, String svg);
4045
}

src/main/java/org/contextmapper/contextmap/generator/ContextMapGenerator.java

+21-18
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
import org.contextmapper.contextmap.generator.model.*;
2626

2727
import java.io.*;
28+
import java.nio.file.Files;
29+
import java.nio.file.Paths;
30+
import java.nio.file.StandardCopyOption;
2831
import java.util.*;
2932
import java.util.stream.Collectors;
3033

@@ -39,6 +42,7 @@
3942
public 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

Comments
 (0)