X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Furls%2FIdentifiersUrlProvider.java;h=850a2308c6d8692fe8d441f85e57528e604984f0;hb=be87ee66ed7952a13c1b402257b8a270ba9168e0;hp=51d181d0bb7337eb1ad008a8f62c397ba3a4a4ec;hpb=a1bf90058be8be92554ecbc5868982dcdbed5a80;p=jalview.git diff --git a/src/jalview/urls/IdentifiersUrlProvider.java b/src/jalview/urls/IdentifiersUrlProvider.java index 51d181d..850a230 100644 --- a/src/jalview/urls/IdentifiersUrlProvider.java +++ b/src/jalview/urls/IdentifiersUrlProvider.java @@ -25,21 +25,18 @@ import static jalview.util.UrlConstants.DB_ACCESSION; import static jalview.util.UrlConstants.DELIM; import static jalview.util.UrlConstants.SEP; +import jalview.util.JSONUtils; import jalview.util.UrlLink; -import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.StringTokenizer; -import java.util.Vector; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; /** @@ -53,6 +50,10 @@ import org.json.simple.parser.ParseException; public class IdentifiersUrlProvider extends UrlProviderImpl { + private static final String LOCAL_KEY = "Local"; + + private static final String ID_ORG_KEY = "identifiers.org"; + // map of string ids to urls private HashMap urls; @@ -62,7 +63,7 @@ public class IdentifiersUrlProvider extends UrlProviderImpl public IdentifiersUrlProvider(String cachedUrlList) { urls = readIdentifiers(IdOrgSettings.getDownloadLocation()); - selectedUrls = new ArrayList(); + selectedUrls = new ArrayList<>(); checkSelectionMatchesUrls(cachedUrlList); } @@ -73,39 +74,66 @@ public class IdentifiersUrlProvider extends UrlProviderImpl * name of identifiers.org download file * @return hashmap of identifiers.org data, keyed by MIRIAM id */ - private HashMap readIdentifiers( - String idFileName) + @SuppressWarnings("unchecked") + private HashMap readIdentifiers(String idFileName) { - JSONParser parser = new JSONParser(); - // identifiers.org data - HashMap idData = new HashMap(); + HashMap idData = new HashMap<>(); + String errorMessage = null; try { + // NOTE: THIS WILL FAIL IN SWINGJS BECAUSE IT INVOLVES A FILE READER + FileReader reader = new FileReader(idFileName); + String key = ""; + Map obj = (Map) JSONUtils + .parse(reader); + if (obj.containsKey(ID_ORG_KEY)) + { + key = ID_ORG_KEY; + } + else if (obj.containsKey(LOCAL_KEY)) + { + key = LOCAL_KEY; + } + else + { + System.out.println( + "Unexpected key returned from identifiers jalview service"); + return idData; + } - JSONArray jsonarray = (JSONArray) parser.parse(reader); + List jsonarray = (List) obj.get(key); // loop over each entry in JSON array and build HashMap entry for (int i = 0; i < jsonarray.size(); i++) { - JSONObject item = (JSONObject) jsonarray.get(i); + Map item = (Map) jsonarray.get(i); String url = (String) item.get("url") + "/" + DELIM + DB_ACCESSION + DELIM; - UrlLink link = new UrlLink((String) item.get("name") + SEP + url); + UrlLink link = new UrlLink((String) item.get("name"), url, + (String) item.get("prefix")); idData.put((String) item.get("id"), link); } - } catch (FileNotFoundException e) - { - e.printStackTrace(); - } catch (IOException e) + } catch (IOException | ParseException e) { - e.printStackTrace(); - } catch (ParseException e) + // unnecessary e.printStackTrace(); + // Note how in JavaScript we can grab the first bytes from any file + // reader. + // Typical report here is "NetworkError" because the file does not exist. + // "https://." is coming from System.getProperty("user.home"), but this + // could + // be set by the page developer to anything, of course. + errorMessage = e.toString(); + idData.clear(); + } + // BH 2018 -- added more valuable report + if (errorMessage != null) { - e.printStackTrace(); + System.err.println("IdentifiersUrlProvider: cannot read " + idFileName + + ": " + errorMessage); } return idData; } @@ -169,12 +197,12 @@ public class IdentifiersUrlProvider extends UrlProviderImpl } @Override - public Vector getLinksForMenu() + public List getLinksForMenu() { - Vector links = new Vector(); + List links = new ArrayList<>(); for (String key : selectedUrls) { - links.add(urls.get(key).toString()); + links.add(urls.get(key).toStringWithTarget()); } return links; } @@ -188,7 +216,7 @@ public class IdentifiersUrlProvider extends UrlProviderImpl @Override public void setUrlData(List links) { - selectedUrls = new ArrayList(); + selectedUrls = new ArrayList<>(); Iterator it = links.iterator(); while (it.hasNext())