/* * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) * Copyright (C) $$Year-Rel$$ The Jalview Authors * * This file is part of Jalview. * * Jalview is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation, either version 3 * of the License, or (at your option) any later version. * * Jalview is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Jalview. If not, see . * The Jalview Authors are detailed in the 'AUTHORS' file. */ package jalview.urls; import static jalview.util.UrlConstants.DB_ACCESSION; import static jalview.util.UrlConstants.DELIM; import static jalview.util.UrlConstants.SEP; import jalview.util.UrlLink; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.StringTokenizer; import java.util.Vector; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; /** * * Implements the UrlProviderI interface for a UrlProvider object which serves * URLs from identifiers.org * * @author $author$ * @version $Revision$ */ public class IdentifiersUrlProvider extends UrlProviderImpl { // map of string ids to urls private HashMap urls; // list of selected urls private ArrayList selectedUrls; public IdentifiersUrlProvider(String cachedUrlList) { urls = readIdentifiers(IdOrgSettings.getDownloadLocation()); selectedUrls = new ArrayList(); checkSelectionMatchesUrls(cachedUrlList); } /** * Read data from an identifiers.org download file * * @param idFileName * name of identifiers.org download file * @return hashmap of identifiers.org data, keyed by MIRIAM id */ private HashMap readIdentifiers( String idFileName) { JSONParser parser = new JSONParser(); // identifiers.org data HashMap idData = new HashMap(); try { FileReader reader = new FileReader(idFileName); JSONArray jsonarray = (JSONArray) parser.parse(reader); // loop over each entry in JSON array and build HashMap entry for (int i = 0; i < jsonarray.size(); i++) { JSONObject item = (JSONObject) jsonarray.get(i); String url = (String) item.get("url") + "/" + DELIM + DB_ACCESSION + DELIM; UrlLink link = new UrlLink((String) item.get("name") + SEP + url); idData.put((String) item.get("id"), link); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } return idData; } private void checkSelectionMatchesUrls(String cachedUrlList) { StringTokenizer st = new StringTokenizer(cachedUrlList, SEP); while (st.hasMoreElements()) { String id = st.nextToken(); if (isMiriamId(id)) { // this is an identifiers.org MIRIAM id if (urls.containsKey(id)) { selectedUrls.add(id); } } } // reset defaultUrl in case it is no longer selected setDefaultUrl(defaultUrl); } @Override public boolean setDefaultUrl(String id) { if (selectedUrls.contains(id)) { defaultUrl = id; } else { defaultUrl = null; } return selectedUrls.contains(id); } @Override public String writeUrlsAsString(boolean selected) { if (!selected) { return ""; // we don't cache unselected identifiers.org urls } StringBuffer links = new StringBuffer(); if (!selectedUrls.isEmpty()) { for (String k : selectedUrls) { links.append(k); links.append(SEP); } // remove last SEP links.setLength(links.length() - 1); } return links.toString(); } @Override public Vector getLinksForMenu() { Vector links = new Vector(); for (String key : selectedUrls) { links.add(urls.get(key).toString()); } return links; } @Override public List getLinksForTable() { return super.getLinksForTable(urls, selectedUrls, false); } @Override public void setUrlData(List links) { selectedUrls = new ArrayList(); Iterator it = links.iterator(); while (it.hasNext()) { UrlLinkDisplay link = it.next(); // Handle links with MIRIAM ids only if (isMiriamId(link.getId())) { // select/deselect links accordingly and set default url if (link.getIsSelected()) { if (urls.containsKey(link.getId())) { selectedUrls.add(link.getId()); } if (link.getIsDefault()) { setDefaultUrl(link.getId()); } } } } } @Override public String getDefaultUrl(String seqid) { return super.getDefaultUrl(seqid, urls); } @Override public String getDefaultUrlId() { return defaultUrl; } @Override public String getDefaultTarget(String seqid) { // TODO Auto-generated method stub return null; } @Override public String chooseDefaultUrl() { // TODO Auto-generated method stub return null; } }