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;
65 public IdentifiersUrlProvider(String cachedUrlList, String idFileName)
69 // File idFile = getIdentifiers();
70 urls = readIdentifiers(new FileReader(idFileName));
71 selectedUrls = new ArrayList<String>();
72 checkSelectionMatchesUrls(cachedUrlList);
74 } catch (IOException e)
76 System.out.println("Exception reading URLs from identifiers.org");
77 System.out.println(e.getMessage());
81 private File getIdentifiers() throws IOException
83 String identifiersorgUrl = "http://identifiers.org/rest/collections/";
84 String outfile = "identifiers.json";
85 int BUFFER_SIZE = 4096;
87 URL url = new URL(identifiersorgUrl);
88 InputStream is = new BufferedInputStream(url.openStream());
89 FileOutputStream os = new FileOutputStream(outfile);
90 byte[] buffer = new byte[BUFFER_SIZE];
92 while ((bytesRead = is.read(buffer)) != -1)
94 os.write(buffer, 0, bytesRead);
99 return new File(outfile);
102 private HashMap<String, HashMap<String, String>> readIdentifiers(
105 JSONParser parser = new JSONParser();
106 HashMap<String, HashMap<String, String>> idData = new HashMap<String, HashMap<String, String>>();
110 JSONArray jsonarray = (JSONArray) parser.parse(reader);
112 // loop over each entry in JSON array and build HashMap entry
113 for (int i = 0; i < jsonarray.size(); i++)
115 JSONObject item = (JSONObject) jsonarray.get(i);
117 HashMap<String, String> idEntry = new HashMap<String, String>();
118 idEntry.put("name", (String) item.get("name"));
119 idEntry.put("url", (String) item.get("url"));
120 idData.put((String) item.get("id"), idEntry);
122 } catch (FileNotFoundException e)
125 } catch (IOException e)
128 } catch (ParseException e)
135 private void checkSelectionMatchesUrls(String cachedUrlList)
138 String[] prevSelected = cachedUrlList.split("\\" + SEP);
139 for (String id : prevSelected)
141 if (urls.containsKey(id))
143 selectedUrls.add(id);
147 // reset defaultUrl in case it is no longer selected
148 setDefaultUrl(defaultUrl);
151 private void checkSelectionMatchesUrls(Vector<String> idList)
153 String[] prevSelected = new String[idList.size()];
154 idList.toArray(prevSelected);
155 for (String id : prevSelected)
157 if (urls.containsKey(id))
159 selectedUrls.add(id);
163 // reset defaultUrl in case it is no longer selected
164 setDefaultUrl(defaultUrl);
168 public String getDefaultUrl()
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> getLinksForDisplay()
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 return urls.get(defaultUrl).get("url") + "/" + seqid;
274 public String getDefaultTarget(String seqid)
276 // TODO Auto-generated method stub
281 public void setUrlLinks(Vector<String> names, Vector<String> urls)
283 // ignores urls, only uses names (as ids)
284 checkSelectionMatchesUrls(names);
288 public String chooseDefaultUrl()
290 // TODO Auto-generated method stub