blob: 6278be2a191389d18a7427fd11dfb606184972b1 [file] [log] [blame]
Stuart McCulloch3fdcd852011-10-17 10:31:43 +00001package aQute.bnd.maven;
2
3import java.net.*;
4import java.util.*;
5
6import javax.xml.parsers.*;
7import javax.xml.xpath.*;
8
9import org.w3c.dom.*;
10
11public class MavenDependencyGraph {
12 final static DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
13 final static XPathFactory xpathFactory = XPathFactory.newInstance();
14 final List<Artifact> dependencies = new ArrayList<Artifact>();
15 final List<URL> repositories = new ArrayList<URL>();
16 final XPath xpath = xpathFactory.newXPath();
17 final Map<URL, Artifact> cache = new HashMap<URL, Artifact>();
18 Artifact root;
19
20 enum Scope {
21 COMPILE, RUNTIME, TEST, PROVIDED, SYSTEM, IMPORT,
22 };
23
24
25 public class Artifact {
26
27 String groupId;
28 String artifactId;
29 String version;
30 Scope scope = Scope.COMPILE;
31 boolean optional;
32 String type;
33 URL url;
34 List<Artifact> dependencies = new ArrayList<Artifact>();
35
36 public Artifact(URL url) throws Exception {
37 if (url != null) {
38 this.url = url;
39 DocumentBuilder db = docFactory.newDocumentBuilder();
40 Document doc = db.parse(url.toString());
41 Node node = (Node) xpath.evaluate("/project", doc, XPathConstants.NODE);
42
43 groupId = xpath.evaluate("groupId", node);
44 artifactId = xpath.evaluate("artifactId", node);
45 version = xpath.evaluate("version", node);
46 type = xpath.evaluate("type", node);
47 optional = (Boolean) xpath.evaluate("optinal", node, XPathConstants.BOOLEAN);
48 String scope = xpath.evaluate("scope", node);
49 if (scope != null && scope.length() > 0) {
50 this.scope = Scope.valueOf(scope.toUpperCase());
51 }
52 NodeList evaluate = (NodeList) xpath.evaluate("//dependencies/dependency", doc,
53 XPathConstants.NODESET);
54
55 for (int i = 0; i < evaluate.getLength(); i++) {
56 Node childNode = evaluate.item(i);
57 Artifact artifact = getArtifact(xpath.evaluate("groupId", childNode), xpath
58 .evaluate("artifactId", childNode), xpath.evaluate("version", childNode));
59 add(artifact);
60 }
61 }
62 }
63
64
65
66 public void add(Artifact artifact) {
67 dependencies.add(artifact);
68 }
69
70
71
72 public String toString() {
73 return groupId + "." + artifactId + "-" + version + "[" + scope + "," + optional + "]";
74 }
75
76 public String getPath() throws URISyntaxException {
77 return groupId.replace('.', '/') + "/" + artifactId + "/" + version + "/" + artifactId
78 + "-" + version;
79 }
80
81 }
82
83 public void addRepository(URL repository) {
84 repositories.add(repository);
85 }
86
87 /**
88 * @param xp
89 * @param node
90 * @param d
91 * @throws XPathExpressionException
92 */
93
94 public Artifact getArtifact(String groupId, String artifactId, String version) {
95 for (URL repository : repositories) {
96 String path = getPath(repository.toString(), groupId, artifactId, version);
97
98 try {
99 URL url = new URL(path + ".pom");
100 if (cache.containsKey(url)) {
101 return cache.get(url);
102 } else {
103 return new Artifact(url);
104 }
105 } catch (Exception e) {
106 System.err.println("Failed to get " + artifactId + " from " + repository);
107 }
108 }
109 return null;
110 }
111
112 private String getPath(String path, String groupId, String artifactId, String version) {
113 StringBuilder sb = new StringBuilder();
114 sb.append(path);
115 if (!path.endsWith("/"))
116 sb.append("/");
117
118 sb.append(groupId.replace('.', '/'));
119 sb.append('/');
120 sb.append(artifactId);
121 sb.append('/');
122 sb.append(version);
123 sb.append('/');
124 sb.append(artifactId);
125 sb.append('-');
126 sb.append(version);
127 return null;
128 }
129
130
131
132 public void addArtifact( Artifact artifact ) throws Exception {
133 if ( root == null)
134 root = new Artifact(null);
135 root.add(artifact);
136 }
137
138
139}