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 java.io.BufferedInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.FileOutputStream;
32 import java.io.FileReader;
33 import java.io.IOException;
34 import java.io.InputStream;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.Iterator;
39 import java.util.List;
40 import java.util.Map.Entry;
41 import java.util.Vector;
43 import org.json.simple.JSONArray;
44 import org.json.simple.JSONObject;
45 import org.json.simple.parser.JSONParser;
46 import org.json.simple.parser.ParseException;
50 * Implements the UrlProviderI interface for a UrlProvider object which serves
51 * URLs from identifiers.org
56 public class IdentifiersUrlProvider extends UrlProviderImpl
59 // map of string ids to urls
60 private HashMap<String, HashMap<String, String>> urls;
62 // list of selected urls
63 private ArrayList<String> selectedUrls;
66 * Location of file for identifiers.org download
68 public static final String ID_ORG_FILE = System.getProperty("user.home")
69 + File.separatorChar + ".jalview_identifiers";
71 public IdentifiersUrlProvider(String cachedUrlList, String idFileName)
75 // File idFile = getIdentifiers();
76 urls = readIdentifiers(new FileReader(idFileName));
77 selectedUrls = new ArrayList<String>();
78 checkSelectionMatchesUrls(cachedUrlList);
80 } catch (IOException e)
82 System.out.println("Exception reading URLs from identifiers.org");
83 System.out.println(e.getMessage());
87 private File getIdentifiers() throws IOException
89 String identifiersorgUrl = "http://identifiers.org/rest/collections/";
90 String outfile = "identifiers.json";
91 int BUFFER_SIZE = 4096;
93 URL url = new URL(identifiersorgUrl);
94 InputStream is = new BufferedInputStream(url.openStream());
95 FileOutputStream os = new FileOutputStream(outfile);
96 byte[] buffer = new byte[BUFFER_SIZE];
98 while ((bytesRead = is.read(buffer)) != -1)
100 os.write(buffer, 0, bytesRead);
105 return new File(outfile);
108 private HashMap<String, HashMap<String, String>> readIdentifiers(
111 JSONParser parser = new JSONParser();
112 HashMap<String, HashMap<String, String>> idData = new HashMap<String, HashMap<String, String>>();
116 JSONArray jsonarray = (JSONArray) parser.parse(reader);
118 // loop over each entry in JSON array and build HashMap entry
119 for (int i = 0; i < jsonarray.size(); i++)
121 JSONObject item = (JSONObject) jsonarray.get(i);
123 HashMap<String, String> idEntry = new HashMap<String, String>();
124 idEntry.put("name", (String) item.get("name"));
125 idEntry.put("url", (String) item.get("url"));
126 idData.put((String) item.get("id"), idEntry);
128 } catch (FileNotFoundException e)
131 } catch (IOException e)
134 } catch (ParseException e)
141 private void checkSelectionMatchesUrls(String cachedUrlList)
144 String[] prevSelected = cachedUrlList.split("\\" + SEP);
145 for (String id : prevSelected)
147 if (urls.containsKey(id))
149 selectedUrls.add(id);
153 // reset defaultUrl in case it is no longer selected
154 setDefaultUrl(defaultUrl);
157 private void checkSelectionMatchesUrls(Vector<String> idList)
159 String[] prevSelected = new String[idList.size()];
160 idList.toArray(prevSelected);
161 for (String id : prevSelected)
163 if (urls.containsKey(id))
165 selectedUrls.add(id);
169 // reset defaultUrl in case it is no longer selected
170 setDefaultUrl(defaultUrl);
174 public boolean setDefaultUrl(String id)
176 if (selectedUrls.contains(id))
184 return selectedUrls.contains(id);
188 public String writeUrlsAsString()
190 if (!selectedUrls.isEmpty())
192 StringBuffer links = new StringBuffer();
193 for (String k : selectedUrls)
199 links.setLength(links.length() - 1);
200 return links.toString();
206 public Vector<String> getLinksForMenu()
208 Vector<String> links = new Vector<String>();
209 for (String key : selectedUrls)
211 links.add(urls.get(key).get("name") + SEP + urls.get(key).get("url")
213 + DB_ACCESSION + DELIM);
219 public List<UrlLinkDisplay> getLinksForTable()
221 ArrayList<UrlLinkDisplay> displayLinks = new ArrayList<UrlLinkDisplay>();
222 for (Entry<String, HashMap<String, String>> entry : urls.entrySet())
224 String key = entry.getKey();
225 boolean isDefault = (key == defaultUrl);
226 boolean isSelected = (selectedUrls.contains(key));
227 displayLinks.add(new UrlLinkDisplay(key,
228 entry.getValue().get("name"),
229 entry.getValue().get("url")
230 + "/" + DELIM + DB_ACCESSION + DELIM, isSelected, isDefault));
236 public void setUrlData(List<UrlLinkDisplay> links)
238 selectedUrls = new ArrayList<String>();
240 Iterator<UrlLinkDisplay> it = links.iterator();
243 UrlLinkDisplay link = it.next();
245 // Handle links with MIRIAM ids only
246 if (isMiriamId(link.getId())) // TODO separate use of name and id
248 // select/deselect links accordingly and set default url
250 if (link.getIsSelected())
252 if (urls.containsKey(link.getId()))
254 selectedUrls.add(link.getId());
256 if (link.getIsDefault())
258 setDefaultUrl(link.getId());
268 public String getDefaultUrl(String seqid)
270 if (seqid.length() < MIN_SUBST_LENGTH)
274 else if (defaultUrl == null)
280 return urls.get(defaultUrl).get("url") + "/" + seqid;
285 public String getDefaultTarget(String seqid)
287 // TODO Auto-generated method stub
292 public String chooseDefaultUrl()
294 // TODO Auto-generated method stub