X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Furls%2FIdentifiersUrlProvider.java;h=850a2308c6d8692fe8d441f85e57528e604984f0;hb=5a352aa2f3330ae269d9b70c4a7374c2518bfb2e;hp=3501169ada665b9ea75647166aeb9856901b6fbd;hpb=a889a61789f15e79ecde7043cddfdea18ad00166;p=jalview.git diff --git a/src/jalview/urls/IdentifiersUrlProvider.java b/src/jalview/urls/IdentifiersUrlProvider.java index 3501169..850a230 100644 --- a/src/jalview/urls/IdentifiersUrlProvider.java +++ b/src/jalview/urls/IdentifiersUrlProvider.java @@ -25,24 +25,18 @@ import static jalview.util.UrlConstants.DB_ACCESSION; import static jalview.util.UrlConstants.DELIM; import static jalview.util.UrlConstants.SEP; -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; +import jalview.util.JSONUtils; +import jalview.util.UrlLink; + import java.io.FileReader; import java.io.IOException; -import java.io.InputStream; -import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; -import java.util.Map.Entry; -import java.util.Vector; +import java.util.Map; +import java.util.StringTokenizer; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; /** @@ -56,140 +50,141 @@ 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; + private HashMap urls; // list of selected urls private ArrayList selectedUrls; - /* - * Location of file for identifiers.org download - */ - public static final String ID_ORG_FILE = System.getProperty("user.home") - + File.separatorChar + ".jalview_identifiers"; - - public IdentifiersUrlProvider(String cachedUrlList, String idFileName) + public IdentifiersUrlProvider(String cachedUrlList) { - try - { - // File idFile = getIdentifiers(); - urls = readIdentifiers(new FileReader(idFileName)); - selectedUrls = new ArrayList(); - checkSelectionMatchesUrls(cachedUrlList); - - } catch (IOException e) - { - System.out.println("Exception reading URLs from identifiers.org"); - System.out.println(e.getMessage()); - } - } - - private File getIdentifiers() throws IOException - { - String identifiersorgUrl = "http://identifiers.org/rest/collections/"; - String outfile = "identifiers.json"; - int BUFFER_SIZE = 4096; - - URL url = new URL(identifiersorgUrl); - InputStream is = new BufferedInputStream(url.openStream()); - FileOutputStream os = new FileOutputStream(outfile); - byte[] buffer = new byte[BUFFER_SIZE]; - int bytesRead = -1; - while ((bytesRead = is.read(buffer)) != -1) - { - os.write(buffer, 0, bytesRead); - } - os.close(); - is.close(); - - return new File(outfile); + urls = readIdentifiers(IdOrgSettings.getDownloadLocation()); + selectedUrls = new ArrayList<>(); + checkSelectionMatchesUrls(cachedUrlList); } - private HashMap> readIdentifiers( - FileReader reader) + /** + * Read data from an identifiers.org download file + * + * @param idFileName + * name of identifiers.org download file + * @return hashmap of identifiers.org data, keyed by MIRIAM id + */ + @SuppressWarnings("unchecked") + private HashMap readIdentifiers(String idFileName) { - JSONParser parser = new JSONParser(); - HashMap> idData = new HashMap>(); + // identifiers.org data + HashMap idData = new HashMap<>(); + String errorMessage = null; try { - JSONArray jsonarray = (JSONArray) parser.parse(reader); + // 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; + } + + 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); - HashMap idEntry = new HashMap(); - idEntry.put("name", (String) item.get("name")); - idEntry.put("url", (String) item.get("url")); - idData.put((String) item.get("id"), idEntry); + String url = (String) item.get("url") + "/" + DELIM + DB_ACCESSION + + DELIM; + 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; } private void checkSelectionMatchesUrls(String cachedUrlList) { - - String[] prevSelected = cachedUrlList.split("\\" + SEP); - for (String id : prevSelected) + StringTokenizer st = new StringTokenizer(cachedUrlList, SEP); + while (st.hasMoreElements()) { - if (urls.containsKey(id)) - { - selectedUrls.add(id); - } - } + String id = st.nextToken(); - // reset defaultUrl in case it is no longer selected - setDefaultUrl(defaultUrl); - } - - private void checkSelectionMatchesUrls(Vector idList) - { - String[] prevSelected = new String[idList.size()]; - idList.toArray(prevSelected); - for (String id : prevSelected) - { - if (urls.containsKey(id)) + if (isMiriamId(id)) { - selectedUrls.add(id); + // this is an identifiers.org MIRIAM id + if (urls.containsKey(id)) + { + selectedUrls.add(id); + } } } // reset defaultUrl in case it is no longer selected - setDefaultUrl(defaultUrl); + setPrimaryUrl(primaryUrl); } @Override - public boolean setDefaultUrl(String id) + public boolean setPrimaryUrl(String id) { - if (selectedUrls.contains(id)) + if (urls.containsKey(id)) { - defaultUrl = id; + primaryUrl = id; } else { - defaultUrl = null; + primaryUrl = null; } - return selectedUrls.contains(id); + + return urls.containsKey(id); } @Override - public String writeUrlsAsString() + public String writeUrlsAsString(boolean selected) { + if (!selected) + { + return ""; // we don't cache unselected identifiers.org urls + } + + StringBuffer links = new StringBuffer(); if (!selectedUrls.isEmpty()) { - StringBuffer links = new StringBuffer(); for (String k : selectedUrls) { links.append(k); @@ -197,20 +192,17 @@ public class IdentifiersUrlProvider extends UrlProviderImpl } // remove last SEP links.setLength(links.length() - 1); - return links.toString(); } - return ""; + return links.toString(); } @Override - public Vector getLinksForMenu() + public List getLinksForMenu() { - Vector links = new Vector(); + List links = new ArrayList<>(); for (String key : selectedUrls) { - links.add(urls.get(key).get("name") + SEP + urls.get(key).get("url") - + "/" + DELIM - + DB_ACCESSION + DELIM); + links.add(urls.get(key).toStringWithTarget()); } return links; } @@ -218,24 +210,13 @@ public class IdentifiersUrlProvider extends UrlProviderImpl @Override public List getLinksForTable() { - ArrayList displayLinks = new ArrayList(); - for (Entry> entry : urls.entrySet()) - { - String key = entry.getKey(); - boolean isDefault = (key == defaultUrl); - boolean isSelected = (selectedUrls.contains(key)); - displayLinks.add(new UrlLinkDisplay(key, - entry.getValue().get("name"), - entry.getValue().get("url") - + "/" + DELIM + DB_ACCESSION + DELIM, isSelected, isDefault)); - } - return displayLinks; + return super.getLinksForTable(urls, selectedUrls, false); } @Override public void setUrlData(List links) { - selectedUrls = new ArrayList(); + selectedUrls = new ArrayList<>(); Iterator it = links.iterator(); while (it.hasNext()) @@ -243,56 +224,51 @@ public class IdentifiersUrlProvider extends UrlProviderImpl UrlLinkDisplay link = it.next(); // Handle links with MIRIAM ids only - if (isMiriamId(link.getId())) // TODO separate use of name and id + if (isMiriamId(link.getId())) { // select/deselect links accordingly and set default url - - if (link.getIsSelected()) + if (urls.containsKey(link.getId())) { - if (urls.containsKey(link.getId())) + if (link.getIsSelected()) { selectedUrls.add(link.getId()); } - if (link.getIsDefault()) + if (link.getIsPrimary()) { - setDefaultUrl(link.getId()); + setPrimaryUrl(link.getId()); } - } } - } } @Override - public String getDefaultUrl(String seqid) + public String getPrimaryUrl(String seqid) { - if (seqid.length() < MIN_SUBST_LENGTH) - { - return null; - } - else if (defaultUrl == null) - { - return null; - } - else - { - return urls.get(defaultUrl).get("url") + "/" + seqid; - } + return super.getPrimaryUrl(seqid, urls); } @Override - public String getDefaultTarget(String seqid) + public String getPrimaryUrlId() + { + return primaryUrl; + } + + @Override + public String getPrimaryTarget(String seqid) { - // TODO Auto-generated method stub return null; } @Override - public String chooseDefaultUrl() + public String choosePrimaryUrl() { - // TODO Auto-generated method stub return null; } + @Override + public boolean contains(String id) + { + return (urls.containsKey(id)); + } }