/*
* 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 java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
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;
/*
* Location of file for identifiers.org download
*/
public static final String ID_ORG_FILE = System.getProperty("user.home")
+ File.separatorChar + ".jalview_identifiers";
public IdentifiersUrlProvider(String cachedUrlList, String idFileName)
{
try
{
// File idFile = getIdentifiers();
urls = readIdentifiers(new FileReader(idFileName));
selectedUrls = new ArrayList();
checkSelectionMatchesUrls(cachedUrlList);
} catch (IOException e)
{
System.out.println("Exception reading URLs from identifiers.org");
System.out.println(e.getMessage());
}
}
private File getIdentifiers() throws IOException
{
String identifiersorgUrl = "http://identifiers.org/rest/collections/";
String outfile = "identifiers.json";
int BUFFER_SIZE = 4096;
URL url = new URL(identifiersorgUrl);
InputStream is = new BufferedInputStream(url.openStream());
FileOutputStream os = new FileOutputStream(outfile);
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = -1;
while ((bytesRead = is.read(buffer)) != -1)
{
os.write(buffer, 0, bytesRead);
}
os.close();
is.close();
return new File(outfile);
}
private HashMap> readIdentifiers(
FileReader reader)
{
JSONParser parser = new JSONParser();
HashMap> idData = new HashMap>();
try
{
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);
HashMap idEntry = new HashMap();
idEntry.put("name", (String) item.get("name"));
idEntry.put("url", (String) item.get("url"));
idData.put((String) item.get("id"), idEntry);
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (ParseException e)
{
e.printStackTrace();
}
return idData;
}
private void checkSelectionMatchesUrls(String cachedUrlList)
{
String[] prevSelected = cachedUrlList.split("\\" + SEP);
for (String id : prevSelected)
{
if (urls.containsKey(id))
{
selectedUrls.add(id);
}
}
// reset defaultUrl in case it is no longer selected
setDefaultUrl(defaultUrl);
}
private void checkSelectionMatchesUrls(Vector idList)
{
String[] prevSelected = new String[idList.size()];
idList.toArray(prevSelected);
for (String id : prevSelected)
{
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()
{
if (!selectedUrls.isEmpty())
{
StringBuffer links = new StringBuffer();
for (String k : selectedUrls)
{
links.append(k);
links.append(SEP);
}
// remove last SEP
links.setLength(links.length() - 1);
return links.toString();
}
return "";
}
@Override
public Vector getLinksForMenu()
{
Vector links = new Vector();
for (String key : selectedUrls)
{
links.add(urls.get(key).get("name") + SEP + urls.get(key).get("url")
+ "/" + DELIM
+ DB_ACCESSION + DELIM);
}
return links;
}
@Override
public List getLinksForTable()
{
ArrayList displayLinks = new ArrayList();
for (Entry> entry : urls.entrySet())
{
String key = entry.getKey();
boolean isDefault = (key == defaultUrl);
boolean isSelected = (selectedUrls.contains(key));
displayLinks.add(new UrlLinkDisplay(key,
entry.getValue().get("name"),
entry.getValue().get("url")
+ "/" + DELIM + DB_ACCESSION + DELIM, isSelected, isDefault));
}
return displayLinks;
}
@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())) // TODO separate use of name and id
{
// 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)
{
if (seqid.length() < MIN_SUBST_LENGTH)
{
return null;
}
else if (defaultUrl == null)
{
return null;
}
else
{
return urls.get(defaultUrl).get("url") + "/" + seqid;
}
}
@Override
public String getDefaultTarget(String seqid)
{
// TODO Auto-generated method stub
return null;
}
@Override
public String chooseDefaultUrl()
{
// TODO Auto-generated method stub
return null;
}
}