package aQute.bnd.maven;

import java.net.*;
import java.util.*;

import javax.xml.parsers.*;
import javax.xml.xpath.*;

import org.w3c.dom.*;

public class MavenDependencyGraph {
	final static DocumentBuilderFactory	docFactory		= DocumentBuilderFactory.newInstance();
	final static XPathFactory			xpathFactory	= XPathFactory.newInstance();
	final List<Artifact>				dependencies	= new ArrayList<Artifact>();
	final List<URL>						repositories	= new ArrayList<URL>();
	final XPath							xpath			= xpathFactory.newXPath();
	final Map<URI,Artifact>				cache			= new HashMap<URI,Artifact>();
	Artifact							root;

	enum Scope {
		COMPILE, RUNTIME, TEST, PROVIDED, SYSTEM, IMPORT,
	}

	public class Artifact {

		String			groupId;
		String			artifactId;
		String			version;
		Scope			scope			= Scope.COMPILE;
		boolean			optional;
		String			type;
		URL				url;
		List<Artifact>	dependencies	= new ArrayList<Artifact>();

		public Artifact(URL url) throws Exception {
			if (url != null) {
				this.url = url;
				DocumentBuilder db = docFactory.newDocumentBuilder();
				Document doc = db.parse(url.toString());
				Node node = (Node) xpath.evaluate("/project", doc, XPathConstants.NODE);

				groupId = xpath.evaluate("groupId", node);
				artifactId = xpath.evaluate("artifactId", node);
				version = xpath.evaluate("version", node);
				type = xpath.evaluate("type", node);
				optional = (Boolean) xpath.evaluate("optinal", node, XPathConstants.BOOLEAN);
				String scope = xpath.evaluate("scope", node);
				if (scope != null && scope.length() > 0) {
					this.scope = Scope.valueOf(scope.toUpperCase());
				}
				NodeList evaluate = (NodeList) xpath.evaluate("//dependencies/dependency", doc, XPathConstants.NODESET);

				for (int i = 0; i < evaluate.getLength(); i++) {
					Node childNode = evaluate.item(i);
					Artifact artifact = getArtifact(xpath.evaluate("groupId", childNode),
							xpath.evaluate("artifactId", childNode), xpath.evaluate("version", childNode));
					add(artifact);
				}
			}
		}

		public void add(Artifact artifact) {
			dependencies.add(artifact);
		}

		@Override
		public String toString() {
			return groupId + "." + artifactId + "-" + version + "[" + scope + "," + optional + "]";
		}

		public String getPath() throws URISyntaxException {
			return groupId.replace('.', '/') + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version;
		}

	}

	public void addRepository(URL repository) {
		repositories.add(repository);
	}

	/**
	 * @param xp @param node @param d @throws XPathExpressionException
	 */

	public Artifact getArtifact(String groupId, String artifactId, String version) {
		for (URL repository : repositories) {
			String path = getPath(repository.toString(), groupId, artifactId, version);

			try {
				URI url = new URL(path + ".pom").toURI();
				if (cache.containsKey(url)) {
					return cache.get(url);
				}
				return new Artifact(url.toURL());
			}
			catch (Exception e) {
				System.err.println("Failed to get " + artifactId + " from " + repository);
			}
		}
		return null;
	}

	private String getPath(String path, String groupId, String artifactId, String version) {
		StringBuilder sb = new StringBuilder();
		sb.append(path);
		if (!path.endsWith("/"))
			sb.append("/");

		sb.append(groupId.replace('.', '/'));
		sb.append('/');
		sb.append(artifactId);
		sb.append('/');
		sb.append(version);
		sb.append('/');
		sb.append(artifactId);
		sb.append('-');
		sb.append(version);
		return null;
	}

	public void addArtifact(Artifact artifact) throws Exception {
		if (root == null)
			root = new Artifact(null);
		root.add(artifact);
	}

}

@@ -39,107 +39,449 @@
 net.
-*;%0Aimport java.util.*;%0A%0Aimport javax.xml.parsers.*;%0Aimport javax.xml.xpath.*;%0A%0Aimport org.w3c.dom.*
+URI;%0Aimport java.net.URISyntaxException;%0Aimport java.net.URL;%0Aimport java.util.ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0Aimport javax.xml.parsers.DocumentBuilder;%0Aimport javax.xml.parsers.DocumentBuilderFactory;%0Aimport javax.xml.xpath.XPath;%0Aimport javax.xml.xpath.XPathConstants;%0Aimport javax.xml.xpath.XPathFactory;%0A%0Aimport org.w3c.dom.Document;%0Aimport org.w3c.dom.Node;%0Aimport org.w3c.dom.NodeList
 ;%0A%0Ap
@@ -2817,87 +2817,8 @@
 %09%7D%0A%0A
-%09/**%0A%09 * @param xp @param node @param d @throws XPathExpressionException%0A%09 */%0A%0A
 %09pub

Fix javadoc warnings.