blob: bbc45b8202847eaf0b04807f64fec90d1cc26dad [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +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();
Stuart McCullochd4826102012-06-26 16:34:24 +000017 final Map<URI,Artifact> cache = new HashMap<URI,Artifact>();
Stuart McCulloch2286f232012-06-15 13:27:53 +000018 Artifact root;
Stuart McCullochbb014372012-06-07 21:57:32 +000019
20 enum Scope {
21 COMPILE, RUNTIME, TEST, PROVIDED, SYSTEM, IMPORT,
22 }
23
Stuart McCullochbb014372012-06-07 21:57:32 +000024 public class Artifact {
25
26 String groupId;
27 String artifactId;
28 String version;
29 Scope scope = Scope.COMPILE;
30 boolean optional;
31 String type;
32 URL url;
33 List<Artifact> dependencies = new ArrayList<Artifact>();
34
35 public Artifact(URL url) throws Exception {
36 if (url != null) {
37 this.url = url;
38 DocumentBuilder db = docFactory.newDocumentBuilder();
39 Document doc = db.parse(url.toString());
40 Node node = (Node) xpath.evaluate("/project", doc, XPathConstants.NODE);
41
42 groupId = xpath.evaluate("groupId", node);
43 artifactId = xpath.evaluate("artifactId", node);
44 version = xpath.evaluate("version", node);
45 type = xpath.evaluate("type", node);
46 optional = (Boolean) xpath.evaluate("optinal", node, XPathConstants.BOOLEAN);
47 String scope = xpath.evaluate("scope", node);
48 if (scope != null && scope.length() > 0) {
49 this.scope = Scope.valueOf(scope.toUpperCase());
50 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000051 NodeList evaluate = (NodeList) xpath.evaluate("//dependencies/dependency", doc, XPathConstants.NODESET);
Stuart McCullochbb014372012-06-07 21:57:32 +000052
53 for (int i = 0; i < evaluate.getLength(); i++) {
54 Node childNode = evaluate.item(i);
Stuart McCulloch2286f232012-06-15 13:27:53 +000055 Artifact artifact = getArtifact(xpath.evaluate("groupId", childNode),
56 xpath.evaluate("artifactId", childNode), xpath.evaluate("version", childNode));
Stuart McCullochbb014372012-06-07 21:57:32 +000057 add(artifact);
58 }
59 }
60 }
Stuart McCullochbb014372012-06-07 21:57:32 +000061
62 public void add(Artifact artifact) {
63 dependencies.add(artifact);
64 }
65
Stuart McCullochbb014372012-06-07 21:57:32 +000066 public String toString() {
67 return groupId + "." + artifactId + "-" + version + "[" + scope + "," + optional + "]";
68 }
69
70 public String getPath() throws URISyntaxException {
Stuart McCulloch2286f232012-06-15 13:27:53 +000071 return groupId.replace('.', '/') + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version;
Stuart McCullochbb014372012-06-07 21:57:32 +000072 }
73
74 }
75
76 public void addRepository(URL repository) {
77 repositories.add(repository);
78 }
79
80 /**
81 * @param xp
82 * @param node
83 * @param d
84 * @throws XPathExpressionException
85 */
86
87 public Artifact getArtifact(String groupId, String artifactId, String version) {
88 for (URL repository : repositories) {
89 String path = getPath(repository.toString(), groupId, artifactId, version);
90
91 try {
Stuart McCullochd4826102012-06-26 16:34:24 +000092 URI url = new URL(path + ".pom").toURI();
Stuart McCullochbb014372012-06-07 21:57:32 +000093 if (cache.containsKey(url)) {
94 return cache.get(url);
Stuart McCullochbb014372012-06-07 21:57:32 +000095 }
Stuart McCullochd4826102012-06-26 16:34:24 +000096 return new Artifact(url.toURL());
Stuart McCulloch2286f232012-06-15 13:27:53 +000097 }
98 catch (Exception e) {
Stuart McCullochbb014372012-06-07 21:57:32 +000099 System.err.println("Failed to get " + artifactId + " from " + repository);
100 }
101 }
102 return null;
103 }
104
105 private String getPath(String path, String groupId, String artifactId, String version) {
106 StringBuilder sb = new StringBuilder();
107 sb.append(path);
108 if (!path.endsWith("/"))
109 sb.append("/");
110
111 sb.append(groupId.replace('.', '/'));
112 sb.append('/');
113 sb.append(artifactId);
114 sb.append('/');
115 sb.append(version);
116 sb.append('/');
117 sb.append(artifactId);
118 sb.append('-');
119 sb.append(version);
120 return null;
121 }
122
Stuart McCulloch2286f232012-06-15 13:27:53 +0000123 public void addArtifact(Artifact artifact) throws Exception {
124 if (root == null)
Stuart McCullochbb014372012-06-07 21:57:32 +0000125 root = new Artifact(null);
126 root.add(artifact);
127 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000128
Stuart McCullochbb014372012-06-07 21:57:32 +0000129}