2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
24 import static jalview.util.UrlConstants.DB_ACCESSION;
25 import static jalview.util.UrlConstants.DELIM;
26 import static jalview.util.UrlConstants.SEP;
28 import jalview.util.UrlLink;
30 import java.io.FileNotFoundException;
31 import java.io.FileReader;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.List;
37 import java.util.StringTokenizer;
38 import java.util.Vector;
40 import org.json.simple.JSONArray;
41 import org.json.simple.JSONObject;
42 import org.json.simple.parser.JSONParser;
43 import org.json.simple.parser.ParseException;
47 * Implements the UrlProviderI interface for a UrlProvider object which serves
48 * URLs from identifiers.org
53 public class IdentifiersUrlProvider extends UrlProviderImpl
56 // map of string ids to urls
57 private HashMap<String, UrlLink> urls;
59 // list of selected urls
60 private ArrayList<String> selectedUrls;
62 public IdentifiersUrlProvider(String cachedUrlList)
64 urls = readIdentifiers(IdOrgSettings.getDownloadLocation());
65 selectedUrls = new ArrayList<String>();
66 checkSelectionMatchesUrls(cachedUrlList);
70 * Read data from an identifiers.org download file
73 * name of identifiers.org download file
74 * @return hashmap of identifiers.org data, keyed by MIRIAM id
76 private HashMap<String, UrlLink> readIdentifiers(
79 JSONParser parser = new JSONParser();
81 // identifiers.org data
82 HashMap<String, UrlLink> idData = new HashMap<String, UrlLink>();
86 FileReader reader = new FileReader(idFileName);
88 JSONArray jsonarray = (JSONArray) parser.parse(reader);
90 // loop over each entry in JSON array and build HashMap entry
91 for (int i = 0; i < jsonarray.size(); i++)
93 JSONObject item = (JSONObject) jsonarray.get(i);
95 String url = (String) item.get("url") + "/" + DELIM + DB_ACCESSION
97 UrlLink link = new UrlLink((String) item.get("name"), url,
98 (String) item.get("prefix"));
99 idData.put((String) item.get("id"), link);
101 } catch (FileNotFoundException e)
104 } catch (IOException e)
107 } catch (ParseException e)
114 private void checkSelectionMatchesUrls(String cachedUrlList)
116 StringTokenizer st = new StringTokenizer(cachedUrlList, SEP);
117 while (st.hasMoreElements())
119 String id = st.nextToken();
123 // this is an identifiers.org MIRIAM id
124 if (urls.containsKey(id))
126 selectedUrls.add(id);
131 // reset defaultUrl in case it is no longer selected
132 setPrimaryUrl(primaryUrl);
136 public boolean setPrimaryUrl(String id)
138 if (urls.containsKey(id))
147 return urls.containsKey(id);
151 public String writeUrlsAsString(boolean selected)
155 return ""; // we don't cache unselected identifiers.org urls
158 StringBuffer links = new StringBuffer();
159 if (!selectedUrls.isEmpty())
161 for (String k : selectedUrls)
167 links.setLength(links.length() - 1);
169 return links.toString();
173 public Vector<String> getLinksForMenu()
175 Vector<String> links = new Vector<String>();
176 for (String key : selectedUrls)
178 links.add(urls.get(key).toString());
184 public List<UrlLinkDisplay> getLinksForTable()
186 return super.getLinksForTable(urls, selectedUrls, false);
190 public void setUrlData(List<UrlLinkDisplay> links)
192 selectedUrls = new ArrayList<String>();
194 Iterator<UrlLinkDisplay> it = links.iterator();
197 UrlLinkDisplay link = it.next();
199 // Handle links with MIRIAM ids only
200 if (isMiriamId(link.getId()))
202 // select/deselect links accordingly and set default url
203 if (urls.containsKey(link.getId()))
205 if (link.getIsSelected())
207 selectedUrls.add(link.getId());
209 if (link.getIsPrimary())
211 setPrimaryUrl(link.getId());
219 public String getPrimaryUrl(String seqid)
221 return super.getPrimaryUrl(seqid, urls);
225 public String getPrimaryUrlId()
231 public String getPrimaryTarget(String seqid)
237 public String choosePrimaryUrl()
243 public boolean contains(String id)
245 return (urls.containsKey(id));