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;
39 import org.json.simple.JSONArray;
40 import org.json.simple.JSONObject;
41 import org.json.simple.parser.JSONParser;
42 import org.json.simple.parser.ParseException;
46 * Implements the UrlProviderI interface for a UrlProvider object which serves
47 * URLs from identifiers.org
52 public class IdentifiersUrlProvider extends UrlProviderImpl
55 // map of string ids to urls
56 private HashMap<String, UrlLink> urls;
58 // list of selected urls
59 private ArrayList<String> selectedUrls;
61 public IdentifiersUrlProvider(String cachedUrlList)
63 urls = readIdentifiers(IdOrgSettings.getDownloadLocation());
64 selectedUrls = new ArrayList<String>();
65 checkSelectionMatchesUrls(cachedUrlList);
69 * Read data from an identifiers.org download file
72 * name of identifiers.org download file
73 * @return hashmap of identifiers.org data, keyed by MIRIAM id
75 private HashMap<String, UrlLink> readIdentifiers(
78 JSONParser parser = new JSONParser();
80 // identifiers.org data
81 HashMap<String, UrlLink> idData = new HashMap<String, UrlLink>();
85 FileReader reader = new FileReader(idFileName);
87 JSONArray jsonarray = (JSONArray) parser.parse(reader);
89 // loop over each entry in JSON array and build HashMap entry
90 for (int i = 0; i < jsonarray.size(); i++)
92 JSONObject item = (JSONObject) jsonarray.get(i);
94 String url = (String) item.get("url") + "/" + DELIM + DB_ACCESSION
96 UrlLink link = new UrlLink((String) item.get("name"), url,
97 (String) item.get("prefix"));
98 idData.put((String) item.get("id"), link);
100 } catch (FileNotFoundException e)
103 } catch (IOException e)
106 } catch (ParseException e)
113 private void checkSelectionMatchesUrls(String cachedUrlList)
115 StringTokenizer st = new StringTokenizer(cachedUrlList, SEP);
116 while (st.hasMoreElements())
118 String id = st.nextToken();
122 // this is an identifiers.org MIRIAM id
123 if (urls.containsKey(id))
125 selectedUrls.add(id);
130 // reset defaultUrl in case it is no longer selected
131 setPrimaryUrl(primaryUrl);
135 public boolean setPrimaryUrl(String id)
137 if (urls.containsKey(id))
146 return urls.containsKey(id);
150 public String writeUrlsAsString(boolean selected)
154 return ""; // we don't cache unselected identifiers.org urls
157 StringBuffer links = new StringBuffer();
158 if (!selectedUrls.isEmpty())
160 for (String k : selectedUrls)
166 links.setLength(links.length() - 1);
168 return links.toString();
172 public List<String> getLinksForMenu()
174 List<String> links = new ArrayList<String>();
175 for (String key : selectedUrls)
177 links.add(urls.get(key).toStringWithTarget());
183 public List<UrlLinkDisplay> getLinksForTable()
185 return super.getLinksForTable(urls, selectedUrls, false);
189 public void setUrlData(List<UrlLinkDisplay> links)
191 selectedUrls = new ArrayList<String>();
193 Iterator<UrlLinkDisplay> it = links.iterator();
196 UrlLinkDisplay link = it.next();
198 // Handle links with MIRIAM ids only
199 if (isMiriamId(link.getId()))
201 // select/deselect links accordingly and set default url
202 if (urls.containsKey(link.getId()))
204 if (link.getIsSelected())
206 selectedUrls.add(link.getId());
208 if (link.getIsPrimary())
210 setPrimaryUrl(link.getId());
218 public String getPrimaryUrl(String seqid)
220 return super.getPrimaryUrl(seqid, urls);
224 public String getPrimaryUrlId()
230 public String getPrimaryTarget(String seqid)
236 public String choosePrimaryUrl()
242 public boolean contains(String id)
244 return (urls.containsKey(id));