1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.dependency.tree;
20
21 import java.io.Writer;
22
23 import org.apache.maven.shared.dependency.graph.DependencyNode;
24 import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
25
26
27
28
29
30
31
32
33 public class GraphmlDependencyNodeVisitor extends AbstractSerializingVisitor implements DependencyNodeVisitor {
34
35
36
37
38 private static final String GRAPHML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> "
39 + "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" "
40 + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
41 + "xmlns:y=\"http://www.yworks.com/xml/graphml\" "
42 + "xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns "
43 + "http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">" + System.lineSeparator()
44 + " <key for=\"node\" id=\"d0\" yfiles.type=\"nodegraphics\"/> " + System.lineSeparator()
45 + " <key for=\"edge\" id=\"d1\" yfiles.type=\"edgegraphics\"/> " + System.lineSeparator()
46 + "<graph id=\"dependencies\" edgedefault=\"directed\">" + System.lineSeparator();
47
48
49
50
51 private static final String GRAPHML_FOOTER = "</graph></graphml>";
52
53
54
55
56
57
58 public GraphmlDependencyNodeVisitor(Writer writer) {
59 super(writer);
60 }
61
62
63
64
65 @Override
66 public boolean endVisit(DependencyNode node) {
67 if (node.getParent() == null || node.getParent() == node) {
68 writer.write(GRAPHML_FOOTER);
69 } else {
70 DependencyNode p = node.getParent();
71 writer.print("<edge source=\"" + generateId(p) + "\" target=\"" + generateId(node) + "\">");
72 if (node.getArtifact().getScope() != null) {
73
74 writer.print("<data key=\"d1\"><y:PolyLineEdge><y:EdgeLabel>"
75 + node.getArtifact().getScope() + "</y:EdgeLabel></y:PolyLineEdge></data>");
76 }
77 writer.println("</edge>");
78 }
79 return true;
80 }
81
82
83
84
85 @Override
86 public boolean visit(DependencyNode node) {
87 if (node.getParent() == null || node.getParent() == node) {
88 writer.write(GRAPHML_HEADER);
89 }
90
91 writer.print("<node id=\"" + generateId(node) + "\">");
92
93 writer.print("<data key=\"d0\"><y:ShapeNode><y:NodeLabel>" + node.toNodeString()
94 + "</y:NodeLabel></y:ShapeNode></data>");
95 writer.println("</node>");
96 return true;
97 }
98
99
100
101
102
103
104
105
106
107
108 private static String generateId(DependencyNode node) {
109 return String.valueOf(node.hashCode());
110 }
111 }