-
Notifications
You must be signed in to change notification settings - Fork 3
/
patch.txt
327 lines (305 loc) · 16.8 KB
/
patch.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
diff --git a/src/main/java/com/mauvaisetroupe/eadesignit/service/diagram/drawio/MXFileSerializer.java b/src/main/java/com/mauvaisetroupe/eadesignit/service/diagram/drawio/MXFileSerializer.java
index 913eea8..0fc3428 100644
--- a/src/main/java/com/mauvaisetroupe/eadesignit/service/diagram/drawio/MXFileSerializer.java
+++ b/src/main/java/com/mauvaisetroupe/eadesignit/service/diagram/drawio/MXFileSerializer.java
@@ -11,6 +11,7 @@ import java.io.StringWriter;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
+import java.util.Random;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
@@ -23,7 +24,6 @@ import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
-import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
@@ -48,7 +48,7 @@ public class MXFileSerializer {
this.landscapeView = landscapeView;
}
- public String createMXFileXML(String svgXML) throws ParserConfigurationException {
+ public String createMXFileXML(String svgXML) throws ParserConfigurationException, SAXException, IOException {
GraphBuilder graphBuilder = new GraphBuilder();
GraphDTO graphDTO = graphBuilder.createGraph(landscapeView);
@@ -74,8 +74,9 @@ public class MXFileSerializer {
createRectangle(doc, root, application.getId().toString(), application.getName());
}
- PLantumlToDrawioPositioner drawioPositioner = new PLantumlToDrawioPositioner();
- doc = drawioPositioner.addPositions(doc, svgXML, graphDTO.getApplications());
+ PLantumlToDrawioPositioner drawioPositioner = new PLantumlToDrawioPositioner(svgXML, graphDTO.getApplications(), graphDTO.getBidirectionalConsolidatedEdges());
+ doc = drawioPositioner.addPositions(doc);
+ doc = drawioPositioner.addConnectionPoints(doc, graphDTO.getBidirectionalConsolidatedEdges());
return getStringFromDocument(doc);
}
@@ -225,11 +226,14 @@ public class MXFileSerializer {
}
private Element createEdge(Document doc, Element root, Edge edge) {
+
+ edge.getSourceId();
+
Element mxCell = createElement(doc, root, "mxCell");
//mxCell.setAttribute("id", EDGE_ID_PREFIX + edgeId);
mxCell.setAttribute("elementId", EDGE_ID_PREFIX + edge.getId());
mxCell.setAttribute("value", edge.getLabelsAsString());
- mxCell.setAttribute("style", "endArrow=classic;html=1;rounded=0;");
+ mxCell.setAttribute("style", "endArrow=classic;html=1;rounded=0;edgeStyle=elbowEdgeStyle;elbow=vertical;entryX=" + new Random().nextFloat() + ";entryY=0;entryDx=0;entryDy=0;exitX=" + new Random().nextFloat() + ";exitY=1;exitDx=0;exitDy=0;");
mxCell.setAttribute("parent", "1");
mxCell.setAttribute("edge", "1");
mxCell.setAttribute("source", APP_ID_PREFIX + edge.getSourceId());
diff --git a/src/main/java/com/mauvaisetroupe/eadesignit/service/diagram/drawio/PLantumlToDrawioPositioner.java b/src/main/java/com/mauvaisetroupe/eadesignit/service/diagram/drawio/PLantumlToDrawioPositioner.java
index 0151024..005524f 100644
--- a/src/main/java/com/mauvaisetroupe/eadesignit/service/diagram/drawio/PLantumlToDrawioPositioner.java
+++ b/src/main/java/com/mauvaisetroupe/eadesignit/service/diagram/drawio/PLantumlToDrawioPositioner.java
@@ -1,11 +1,13 @@
package com.mauvaisetroupe.eadesignit.service.diagram.drawio;
import com.mauvaisetroupe.eadesignit.service.diagram.dto.Application;
+import com.mauvaisetroupe.eadesignit.service.diagram.dto.Edge;
import com.mauvaisetroupe.eadesignit.service.diagram.dto.PositionAndSize;
import java.io.IOException;
import java.io.StringReader;
import java.util.Collection;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@@ -25,27 +27,34 @@ import org.xml.sax.SAXException;
public class PLantumlToDrawioPositioner {
- // public static void main(String[] args) throws Exception {
-
- // PLantumlToDrawioPositioner app = new PLantumlToDrawioPositioner();
-
- // ClassLoader classLoader = PLantumlToDrawioPositioner.class.getClassLoader();
- // File svgFile = new File(classLoader.getResource("svg-formatted.svg").getFile());
- // Document svgDoc = createDocFromFile(svgFile);
- // Map<String, Point> pointFromSVGMap = app.getPointFromSVG(svgDoc);
-
- // File drawioFile = new File(classLoader.getResource("draw-io.xml").getFile());
- // Document drawioDoc = createDocFromFile(drawioFile);
- // app.addPosition(drawioDoc, pointFromSVGMap);
+ // appliName -> PositionAndSize
+ private Map<String, PositionAndSize> mapApplicationPosition= new HashMap<>();
+ // applicationId -> Application
+ private Map<Long, Application> mapApplicationNames= new HashMap<>();
+ // Ancestors above application(ID), from left to right
+ private Map<Long, List<Long>> upAncestors= new HashMap<>();
+ // Ancestors below application(ID), from left to right
+ private Map<Long, List<Long>> downAncestors= new HashMap<>();
+
+ public Map<String, PositionAndSize> getMapApplicationPosition() {
+ return mapApplicationPosition;
+ }
- // FileOutputStream output = new FileOutputStream("c:\\workspaces\\staff-dom.xml");
- // writeXml(drawioDoc, output);
+ public PLantumlToDrawioPositioner(String svgXML, Collection<Application> applications, Collection<Edge> edges) throws SAXException, IOException, ParserConfigurationException {
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ DocumentBuilder db = dbf.newDocumentBuilder();
+ Document docSvgXML = db.parse(new InputSource(new StringReader(svgXML)));
+ NodeList textNodes = docSvgXML.getElementsByTagName("text");
+ NodeList rectNodes = docSvgXML.getElementsByTagName("rect");
+ populateMaps(textNodes, rectNodes, applications);
+ populateUpAndDownAncestors(edges);
+ }
- // }
+ private void populateUpAndDownAncestors(Collection<Edge> edges) {
- public Document addPositions(Document drawDocument, String svgXML, Collection<Application> applications) {
- if (svgXML == null) return drawDocument;
+ }
+ public Document addPositions(Document drawDocument) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document copiedDocument;
try {
@@ -54,24 +63,15 @@ public class PLantumlToDrawioPositioner {
copiedDocument = db.newDocument();
Node copiedRoot = copiedDocument.importNode(originalRoot, true);
copiedDocument.appendChild(copiedRoot);
- } catch (ParserConfigurationException e) {
- return drawDocument;
- }
-
- try {
- DocumentBuilder db = dbf.newDocumentBuilder();
- Document docSvgXML = db.parse(new InputSource(new StringReader(svgXML)));
-
- Map<String, PositionAndSize> pointFromSVGMap = getPointFromSVG(docSvgXML, applications);
- addPositions(copiedDocument, pointFromSVGMap);
+ _addPositions(copiedDocument);
return copiedDocument;
- } catch (XPathExpressionException | ParserConfigurationException | SAXException | IOException e) {
+ } catch (XPathExpressionException | ParserConfigurationException e ) {
e.printStackTrace();
return drawDocument;
}
}
- private void addPositions(Document doc, Map<String, PositionAndSize> pointFromSVGMap) throws XPathExpressionException {
+ private void _addPositions(Document doc) throws XPathExpressionException {
XPathFactory xpathfactory = XPathFactory.newInstance();
XPath xpath = xpathfactory.newXPath();
NodeList nodeList = (NodeList) xpath.evaluate(
@@ -82,7 +82,7 @@ public class PLantumlToDrawioPositioner {
for (int i = 0; i < nodeList.getLength(); i++) {
Element mxcellElement = ((Element) nodeList.item(i));
String applicationName = mxcellElement.getAttribute("value");
- PositionAndSize position = pointFromSVGMap.get(applicationName);
+ PositionAndSize position = this.mapApplicationPosition.get(applicationName);
NodeList textNodeList = mxcellElement.getElementsByTagName("mxGeometry");
Element mxGeometryElement = extractFirstChild(textNodeList);
@@ -95,42 +95,22 @@ public class PLantumlToDrawioPositioner {
}
}
- // private static Document createDocFromFile(File file)
- // throws ParserConfigurationException, SAXException, IOException {
- // DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- // DocumentBuilder db = factory.newDocumentBuilder();
- // Document doc = db.parse(file);
- // return doc;
- // }
-
- protected Map<String, PositionAndSize> getPointFromSVG(Document doc, Collection<Application> applications)
- throws XPathExpressionException {
- Map<String, PositionAndSize> map = new HashMap<>();
+ public Document addConnectionPoints(Document doc, Collection<Edge> edges) {
+ for (Edge edge : edges) {
+ Application source = mapApplicationNames.get(edge.getSourceId());
+ Application target = mapApplicationNames.get(edge.getTargetId());
- XPathFactory xpathfactory = XPathFactory.newInstance();
- XPath xpath = xpathfactory.newXPath();
-
- // <g id="elem_Trust Service">
- // <rect fill="#F1F1F1" height="46.2969" rx="2.5" ry="2.5"
- // style="stroke:#181818;stroke-width:0.5;" width="124" x="2986.5" y="463.543"/>
- // <text fill="#000000" font-family="sans-serif" font-size="14"
- // lengthAdjust="spacing" textLength="94" x="3001.5"
- // y="491.5381">TrustService</text>
- // </g>
-
- NodeList textNodes = doc.getElementsByTagName("text");
- NodeList rectNodes = doc.getElementsByTagName("rect");
- populateMap(map, textNodes, rectNodes, applications);
- return map;
+ }
+ return doc;
}
- private void populateMap(
- Map<String, PositionAndSize> mapToComplete,
+ private void populateMaps(
NodeList textNodes,
NodeList rectNodes,
Collection<Application> applications
) {
Set<String> appliNames = applications.stream().map(a -> a.getName()).collect(Collectors.toSet());
+ applications.stream().forEach(a -> this.mapApplicationNames.put(a.getId(), a));
Map<String, PositionAndSize> textPostion = new HashMap<>();
for (int i = 0; i < textNodes.getLength(); i++) {
Element textElement = (Element) textNodes.item(i);
@@ -171,7 +151,8 @@ public class PLantumlToDrawioPositioner {
txtPos.getY() > rectPos.getY() &&
txtPos.getY() < rectPos.getY() + rectPos.getHeight()
) {
- mapToComplete.put(app, rectPos);
+ this.mapApplicationPosition.put(app, rectPos);
+ //this.mapApplicationNames.put(app.get, _height)
}
i++;
}
diff --git a/src/main/java/com/mauvaisetroupe/eadesignit/web/rest/DrawIOResource.java b/src/main/java/com/mauvaisetroupe/eadesignit/web/rest/DrawIOResource.java
index f94203d..27d7a12 100644
--- a/src/main/java/com/mauvaisetroupe/eadesignit/web/rest/DrawIOResource.java
+++ b/src/main/java/com/mauvaisetroupe/eadesignit/web/rest/DrawIOResource.java
@@ -53,7 +53,7 @@ public class DrawIOResource {
} else {
// check if drawio is uptodate, if not remove SVG from database
// and send updated xml
- String svgXML = plantUMLSerializer.getLandscapeDiagramSVG(landscapeView.get(), Layout.elk, false, false);
+ String svgXML = plantUMLSerializer.getLandscapeDiagramSVG(landscapeView.get(), Layout.elk, false, true);
return fileSerializer.createMXFileXML(svgXML);
}
}
diff --git a/src/test/java/com/mauvaisetroupe/eadesignit/service/diagram/drawio/PLantumlToDrawioPositionerTest.java b/src/test/java/com/mauvaisetroupe/eadesignit/service/diagram/drawio/PLantumlToDrawioPositionerTest.java
index 2234c58..025c6c0 100644
--- a/src/test/java/com/mauvaisetroupe/eadesignit/service/diagram/drawio/PLantumlToDrawioPositionerTest.java
+++ b/src/test/java/com/mauvaisetroupe/eadesignit/service/diagram/drawio/PLantumlToDrawioPositionerTest.java
@@ -6,6 +6,15 @@ import com.mauvaisetroupe.eadesignit.service.diagram.dto.Application;
import com.mauvaisetroupe.eadesignit.service.diagram.dto.PositionAndSize;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.UncheckedIOException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
@@ -15,20 +24,17 @@ import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import org.junit.jupiter.api.Test;
+import org.springframework.core.io.DefaultResourceLoader;
+import org.springframework.core.io.ResourceLoader;
+import org.springframework.util.FileCopyUtils;
+import org.springframework.core.io.Resource;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class PLantumlToDrawioPositionerTest {
@Test
- void testGetPosition() throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
- InputStream stream =
- PLantumlToDrawioPositionerTest.class.getResourceAsStream("/com/mauvaisetroupe/eadesignit/service/diagram/drawio/svg.xml");
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- factory.setValidating(false);
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document doc = builder.parse(stream);
-
+ void testGetPosition() throws ParserConfigurationException, SAXException, IOException, XPathExpressionException, URISyntaxException {
Collection<Application> applications = new ArrayList<>();
applications.add(new Application(1L, "Accounts Service", null, null));
applications.add(new Application(2L, "Apple pay", null, null));
@@ -64,8 +70,14 @@ public class PLantumlToDrawioPositionerTest {
applications.add(new Application(32L, "Trust Service", null, null));
applications.add(new Application(33L, "User settings service", null, null));
- PLantumlToDrawioPositioner drawioPositioner = new PLantumlToDrawioPositioner();
- Map<String, PositionAndSize> map = drawioPositioner.getPointFromSVG(doc, applications);
+ //InputStream stream = PLantumlToDrawioPositionerTest.class.getResourceAsStream("/com/mauvaisetroupe/eadesignit/service/diagram/drawio/svg.xml");
+ ResourceLoader resourceLoader = new DefaultResourceLoader();
+ Resource resource = resourceLoader.getResource("classpath:/com/mauvaisetroupe/eadesignit/service/diagram/drawio/svg.xml");
+ String svg = asString(resource);
+
+
+ PLantumlToDrawioPositioner drawioPositioner = new PLantumlToDrawioPositioner(svg, applications, null);
+ Map<String, PositionAndSize> map = drawioPositioner.getMapApplicationPosition();
// All application should have been found in <text>
assertEquals(applications.size(), map.size());
@@ -80,4 +92,12 @@ public class PLantumlToDrawioPositionerTest {
assertEquals((Double) 4178.5, map.get("Trading service").getX());
assertEquals((Double) 546.9766, map.get("Trading service").getY());
}
+
+ public static String asString(Resource resource) {
+ try (Reader reader = new InputStreamReader(resource.getInputStream())) {
+ return FileCopyUtils.copyToString(reader);
+ } catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
+ }
}
diff --git a/src/test/java/com/mauvaisetroupe/eadesignit/service/drawio/MXFileSerializerTest.java b/src/test/java/com/mauvaisetroupe/eadesignit/service/drawio/MXFileSerializerTest.java
index a6e1b56..0e876cc 100644
--- a/src/test/java/com/mauvaisetroupe/eadesignit/service/drawio/MXFileSerializerTest.java
+++ b/src/test/java/com/mauvaisetroupe/eadesignit/service/drawio/MXFileSerializerTest.java
@@ -24,7 +24,7 @@ import org.xml.sax.SAXException;
public class MXFileSerializerTest {
@Test
- public void test() throws ParserConfigurationException {
+ public void test() throws ParserConfigurationException, SAXException, IOException {
LandscapeView landscapeView = new LandscapeView();
landscapeView.setId(1L);
landscapeView.setDiagramName("diagramName");