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.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, HashMap<String, String>> urls;
59 // list of selected urls
60 private ArrayList<String> selectedUrls;
62 public IdentifiersUrlProvider(String cachedUrlList, String idFileName)
66 // File idFile = getIdentifiers();
67 urls = readIdentifiers(new FileReader(idFileName));
68 checkSelectionMatchesUrls(cachedUrlList);
70 } catch (IOException e)
76 private File getIdentifiers() throws IOException
78 String identifiersorgUrl = "http://identifiers.org/rest/collections/";
79 String outfile = "identifiers.json";
80 int BUFFER_SIZE = 4096;
82 URL url = new URL(identifiersorgUrl);
83 InputStream is = new BufferedInputStream(url.openStream());
84 FileOutputStream os = new FileOutputStream(outfile);
85 byte[] buffer = new byte[BUFFER_SIZE];
87 while ((bytesRead = is.read(buffer)) != -1)
89 os.write(buffer, 0, bytesRead);
94 return new File(outfile);
97 private HashMap<String, HashMap<String, String>> readIdentifiers(
100 JSONParser parser = new JSONParser();
101 HashMap<String, HashMap<String, String>> idData = new HashMap<String, HashMap<String, String>>();
105 JSONArray jsonarray = (JSONArray) parser.parse(reader);
107 // loop over each entry in JSON array and build HashMap entry
108 for (int i = 0; i < jsonarray.size(); i++)
110 JSONObject item = (JSONObject) jsonarray.get(i);
112 HashMap<String, String> idEntry = new HashMap<String, String>();
113 idEntry.put("name", (String) item.get("name"));
114 idEntry.put("url", (String) item.get("url"));
115 idData.put((String) item.get("id"), idEntry);
117 } catch (FileNotFoundException e)
120 } catch (IOException e)
123 } catch (ParseException e)
130 private void checkSelectionMatchesUrls(String cachedUrlList)
132 selectedUrls = new ArrayList<String>();
133 String[] prevSelected = cachedUrlList.split("\\" + SEP);
134 for (String id : prevSelected)
136 if (urls.containsKey(id))
138 selectedUrls.add(id);
142 // reset defaultUrl in case it is no longer selected
143 setDefaultUrl(defaultUrl);
146 private void checkSelectionMatchesUrls(Vector<String> idList)
148 selectedUrls = new ArrayList<String>();
149 String[] prevSelected = new String[idList.size()];
150 idList.toArray(prevSelected);
151 for (String id : prevSelected)
153 if (urls.containsKey(id))
155 selectedUrls.add(id);
159 // reset defaultUrl in case it is no longer selected
160 setDefaultUrl(defaultUrl);
164 public String getDefaultUrl()
170 public boolean setDefaultUrl(String id)
172 if (selectedUrls.contains(id))
180 return selectedUrls.contains(id);
184 public String writeUrlsAsString()
186 StringBuffer links = new StringBuffer();
187 for (String k : selectedUrls)
191 return links.toString();
195 public Vector<String> getLinksForDisplay()
197 Vector<String> links = new Vector<String>();
198 for (String key : selectedUrls)
200 links.add(key + SEP + urls.get(key).get("url") + "/" + DELIM
201 + DB_ACCESSION + DELIM);
207 public String getDefaultUrl(String seqid)
209 return urls.get(defaultUrl).get("url") + "/" + seqid;
213 public String getDefaultTarget(String seqid)
215 // TODO Auto-generated method stub
220 public void setUrlLinks(Vector<String> names, Vector<String> urls)
222 // ignores urls, only uses names (as ids)
223 checkSelectionMatchesUrls(names);
227 public String chooseDefaultUrl()
229 // TODO Auto-generated method stub