package ch.tkuhn.bel2nanopub;

import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;

import org.nanopub.NanopubCreator;
import org.openrdf.model.URI;
import org.openrdf.model.impl.URIImpl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class IdSchemes {

	private static IdSchemes singleton;

	static {
		try {
			Reader reader = new InputStreamReader(Bel2Nanopub.class.getResourceAsStream("/idschemes.json"), "UTF-8");
			Gson gson = new GsonBuilder().create();
			singleton = gson.fromJson(reader, IdSchemes.class);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static List<IdScheme> getSchemes() {
		return singleton.schemes;
	}

	public static URI makeUri(String schemeNameOrBelNs, String belLabel, NanopubCreator npCreator) {
		return makeUri(schemeNameOrBelNs, belLabel, npCreator, null);
	}

	public static URI makeUri(String schemeNameOrBelNs, String belLabel, NanopubCreator npCreator, String prefix) {
		if (belLabel.startsWith("\"") && belLabel.endsWith("\"") && belLabel.length() > 1) {
			belLabel = belLabel.substring(1, belLabel.length()-1);
		}
		String uriString = null;
		IdScheme sc = getScheme(schemeNameOrBelNs);
		if (sc != null) {
			String n = sc.getRdfNs();
			if (n.contains(" ")) {
				n = n.substring(0, n.indexOf(" "));
			}
			String id = sc.getId(belLabel);
			String ns;
			if (id == null) {
				if (!sc.getMappingType().endsWith("partial")) {
					return null;
				}
			} else {
				ns = sc.getRdfNs();
				if (ns.contains(" ")) {
					uriString = ns.replaceAll(" ", id);
					int i = ns.indexOf(" ");
					ns = ns.substring(0, i);
				} else {
					uriString = ns + id;
				}
				npCreator.addNamespace(sc.getRdfPrefix(), ns);
			}
		}
		String ns = schemeNameOrBelNs;
		if (uriString == null && ns.contains("://")) {
			ns = ns.replaceFirst("\\.bel(ns|anno)$", "");
			if (!ns.endsWith("/")) ns += "/";
			uriString = ns + Utils.encodeUrlString(belLabel);
			if (prefix != null) {
				npCreator.addNamespace(prefix.toLowerCase(), ns);
			}
		}
		return new URIImpl(uriString);
	}

	public static IdScheme getScheme(String schemeNameOrBelNs) {
		for (IdScheme sc : getSchemes()) {
			if (sc.getName().equals(schemeNameOrBelNs) || sc.hasBelNs(schemeNameOrBelNs)) {
				return sc;
			}
		}
		return null;
	}

	private IdSchemes() {}  // created by GSON

	private List<IdScheme> schemes;

}

@@ -1679,16 +1679,42 @@
 ;%0A%09%09%09%09%7D%0A
+%09%09%09%09if (!ns.isEmpty()) %7B%0A%09
 %09%09%09%09npCr
@@ -1748,32 +1748,38 @@
 fPrefix(), ns);%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D%0A%09%09Strin

Fix small bug by ignoring empty namespaces