1 package org.apache.maven.plugin.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 =
43 "<?xml version=\"1.0\" encoding=\"UTF-8\"?> "
44 + "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" "
45 + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
46 + "xmlns:y=\"http://www.yworks.com/xml/graphml\" "
47 + "xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns "
48 + "http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">\n"
49 + " <key for=\"node\" id=\"d0\" yfiles.type=\"nodegraphics\"/> \n"
50 + " <key for=\"edge\" id=\"d1\" yfiles.type=\"edgegraphics\"/> \n"
51 + "<graph id=\"dependencies\" edgedefault=\"directed\">\n";
52
53
54
55
56 private static final String GRAPHML_FOOTER = "</graph></graphml>";
57
58
59
60
61
62
63 public GraphmlDependencyNodeVisitor( Writer writer )
64 {
65 super( writer );
66 }
67
68
69
70
71 public boolean endVisit( DependencyNode node )
72 {
73 if ( node.getParent() == null || node.getParent() == node )
74 {
75 writer.write( GRAPHML_FOOTER );
76 }
77 else
78 {
79 DependencyNode p = node.getParent();
80 writer.print( "<edge source=\"" + generateId( p ) + "\" target=\"" + generateId( node ) + "\">" );
81 if ( node.getArtifact().getScope() != null )
82 {
83
84 writer.print( "<data key=\"d1\"><y:PolyLineEdge><y:EdgeLabel>" + node.getArtifact().getScope()
85 + "</y:EdgeLabel></y:PolyLineEdge></data>" );
86 }
87 writer.println( "</edge>" );
88 }
89 return true;
90 }
91
92
93
94
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 }