1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.maven;
19
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
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 public class CollectingRootDependencyGraphVisitor implements DependencyNodeVisitor {
33
34
35
36
37 private final Map<DependencyNode, List<DependencyNode>> nodes = new HashMap<>();
38
39
40
41 private DependencyNode root;
42
43
44
45 private int depth = 0;
46
47 @Override
48 public boolean visit(DependencyNode node) {
49 if (depth == 0) {
50 root = node;
51 if (!nodes.containsKey(root)) {
52 nodes.put(root, new ArrayList<>());
53 }
54 } else {
55
56 nodes.get(root).add(node);
57 }
58 depth += 1;
59 return true;
60 }
61
62 @Override
63 public boolean endVisit(DependencyNode node) {
64 depth -= 1;
65 return true;
66 }
67
68 public Map<DependencyNode, List<DependencyNode>> getNodes() {
69 return Collections.unmodifiableMap(nodes);
70 }
71
72 }