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