blob: e9e928e4ae8df58dc197f404a41f2148de1d0f69 [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 McCulloch55d4dfe2012-08-07 10:57:21 +000066 @Override
Stuart McCullochbb014372012-06-07 21:57:32 +000067 public String toString() {
68 return groupId + "." + artifactId + "-" + version + "[" + scope + "," + optional + "]";
69 }
70
71 public String getPath() throws URISyntaxException {
Stuart McCulloch2286f232012-06-15 13:27:53 +000072 return groupId.replace('.', '/') + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version;
Stuart McCullochbb014372012-06-07 21:57:32 +000073 }
74
75 }
76
77 public void addRepository(URL repository) {
78 repositories.add(repository);
79 }
80
81 /**
82 * @param xp
83 * @param node
84 * @param d
85 * @throws XPathExpressionException
86 */
87
88 public Artifact getArtifact(String groupId, String artifactId, String version) {
89 for (URL repository : repositories) {
90 String path = getPath(repository.toString(), groupId, artifactId, version);
91
92 try {
Stuart McCullochd4826102012-06-26 16:34:24 +000093 URI url = new URL(path + ".pom").toURI();
Stuart McCullochbb014372012-06-07 21:57:32 +000094 if (cache.containsKey(url)) {
95 return cache.get(url);
Stuart McCullochbb014372012-06-07 21:57:32 +000096 }
Stuart McCullochd4826102012-06-26 16:34:24 +000097 return new Artifact(url.toURL());
Stuart McCulloch2286f232012-06-15 13:27:53 +000098 }
99 catch (Exception e) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000100 System.err.println("Failed to get " + artifactId + " from " + repository);
101 }
102 }
103 return null;
104 }
105
106 private String getPath(String path, String groupId, String artifactId, String version) {
107 StringBuilder sb = new StringBuilder();
108 sb.append(path);
109 if (!path.endsWith("/"))
110 sb.append("/");
111
112 sb.append(groupId.replace('.', '/'));
113 sb.append('/');
114 sb.append(artifactId);
115 sb.append('/');
116 sb.append(version);
117 sb.append('/');
118 sb.append(artifactId);
119 sb.append('-');
120 sb.append(version);
121 return null;
122 }
123
Stuart McCulloch2286f232012-06-15 13:27:53 +0000124 public void addArtifact(Artifact artifact) throws Exception {
125 if (root == null)
Stuart McCullochbb014372012-06-07 21:57:32 +0000126 root = new Artifact(null);
127 root.add(artifact);
128 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000129
Stuart McCullochbb014372012-06-07 21:57:32 +0000130}