Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.bnd.maven; |
| 2 | |
| 3 | import java.net.*; |
| 4 | import java.util.*; |
| 5 | |
| 6 | import javax.xml.parsers.*; |
| 7 | import javax.xml.xpath.*; |
| 8 | |
| 9 | import org.w3c.dom.*; |
| 10 | |
| 11 | public 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 McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 17 | final Map<URI,Artifact> cache = new HashMap<URI,Artifact>(); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 18 | Artifact root; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 19 | |
| 20 | enum Scope { |
| 21 | COMPILE, RUNTIME, TEST, PROVIDED, SYSTEM, IMPORT, |
| 22 | } |
| 23 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 24 | 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 51 | NodeList evaluate = (NodeList) xpath.evaluate("//dependencies/dependency", doc, XPathConstants.NODESET); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 52 | |
| 53 | for (int i = 0; i < evaluate.getLength(); i++) { |
| 54 | Node childNode = evaluate.item(i); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 55 | Artifact artifact = getArtifact(xpath.evaluate("groupId", childNode), |
| 56 | xpath.evaluate("artifactId", childNode), xpath.evaluate("version", childNode)); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 57 | add(artifact); |
| 58 | } |
| 59 | } |
| 60 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 61 | |
| 62 | public void add(Artifact artifact) { |
| 63 | dependencies.add(artifact); |
| 64 | } |
| 65 | |
Stuart McCulloch | 55d4dfe | 2012-08-07 10:57:21 +0000 | [diff] [blame^] | 66 | @Override |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 67 | public String toString() { |
| 68 | return groupId + "." + artifactId + "-" + version + "[" + scope + "," + optional + "]"; |
| 69 | } |
| 70 | |
| 71 | public String getPath() throws URISyntaxException { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 72 | return groupId.replace('.', '/') + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 73 | } |
| 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 McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 93 | URI url = new URL(path + ".pom").toURI(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 94 | if (cache.containsKey(url)) { |
| 95 | return cache.get(url); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 96 | } |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 97 | return new Artifact(url.toURL()); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 98 | } |
| 99 | catch (Exception e) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 100 | 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 124 | public void addArtifact(Artifact artifact) throws Exception { |
| 125 | if (root == null) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 126 | root = new Artifact(null); |
| 127 | root.add(artifact); |
| 128 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 129 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 130 | } |