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 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.maven.shared.dependency.graph.DependencyNode;
26 import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
27
28
29
30
31
32
33
34
35 public class TGFDependencyNodeVisitor extends AbstractSerializingVisitor implements DependencyNodeVisitor {
36
37
38
39
40
41
42 static final class EdgeAppender {
43
44
45
46 private final DependencyNode from;
47
48
49
50
51 private final DependencyNode to;
52
53
54
55
56 private final String label;
57
58
59
60
61
62
63
64
65 EdgeAppender(DependencyNode from, DependencyNode to, String label) {
66 super();
67 this.from = from;
68 this.to = to;
69 this.label = label;
70 }
71
72
73
74
75 @Override
76 public String toString() {
77 StringBuilder result = new StringBuilder(generateId(from));
78 result.append(' ').append(generateId(to));
79 if (label != null) {
80 result.append(' ').append(label);
81 }
82 return result.toString();
83 }
84 }
85
86
87
88
89 private final List<EdgeAppender> edges = new ArrayList<>();
90
91
92
93
94
95
96 public TGFDependencyNodeVisitor(Writer writer) {
97 super(writer);
98 }
99
100
101
102
103 @Override
104 public boolean endVisit(DependencyNode node) {
105 if (node.getParent() == null || node.getParent() == node) {
106
107 writer.println("#");
108 for (EdgeAppender edge : edges) {
109 writer.println(edge.toString());
110 }
111 } else {
112 DependencyNode p = node.getParent();
113
114 edges.add(new EdgeAppender(p, node, node.getArtifact().getScope()));
115 }
116 return true;
117 }
118
119
120
121
122 @Override
123 public boolean visit(DependencyNode node) {
124
125 writer.write(generateId(node));
126 writer.write(" ");
127 writer.println(node.toNodeString());
128 return true;
129 }
130
131
132
133
134
135
136
137
138
139
140 private static String generateId(DependencyNode node) {
141 return String.valueOf(node.hashCode());
142 }
143 }