+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
+
+package jalview.util;
+
+import static jalview.util.UrlConstants.DB_ACCESSION;
+import static jalview.util.UrlConstants.EMBLEBI_STRING;
+import static jalview.util.UrlConstants.SEQUENCE_ID;
+import static jalview.util.UrlConstants.SRS_STRING;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.StringTokenizer;
+import java.util.Vector;
+
+/**
+ *
+ * Implements the UrlProviderI interface for a UrlProvider object which serves
+ * custom URLs defined by the user
+ *
+ * @author $author$
+ * @version $Revision$
+ */
+public class CustomUrlProvider implements UrlProviderI
+{
+
+ private HashMap<String, UrlLink> urls;
+
+ public CustomUrlProvider(String cachedUrlList)
+ {
+ try
+ {
+ StringTokenizer st = new StringTokenizer(cachedUrlList, "|");
+ while (st.hasMoreElements())
+ {
+ String name = st.nextToken();
+ String url = st.nextToken();
+ // check for '|' within a regex
+ int rxstart = url.indexOf("$" + DB_ACCESSION + "$");
+ if (rxstart == -1)
+ {
+ rxstart = url.indexOf("$" + SEQUENCE_ID + "$");
+ }
+ while (rxstart == -1 && url.indexOf("/=$") == -1)
+ {
+ url = url + "|" + st.nextToken();
+ }
+ urls.put(name, new UrlLink(url));
+ // sequenceURLLinks.addElement(name + "|" + url);
+ }
+ } catch (Exception ex)
+ {
+ System.out.println(ex + "\nError parsing sequence links");
+ }
+
+ // upgrade old SRS link
+ String srs_key = SRS_STRING.split("\\|")[0];
+ if (urls.containsKey(srs_key))
+ {
+ urls.remove(srs_key);
+ urls.put(EMBLEBI_STRING.split("//|")[0],
+ new UrlLink(EMBLEBI_STRING.split("//|")[1]));
+ }
+ }
+
+ @Override
+ public HashMap<String, UrlLink> getUrlLinks()
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public void getLinksForDisplay(Vector<String> nameLinks,
+ Vector<String> urlLinks)
+ {
+ Iterator<Map.Entry<String, UrlLink>> it = urls.entrySet().iterator();
+ while (it.hasNext())
+ {
+ Map.Entry<String, UrlLink> pair = it.next();
+ nameLinks.addElement(pair.getKey());
+ urlLinks.addElement(pair.getValue().toString()); // TODO check this is
+ // right
+ }
+ }
+
+ @Override
+ public String getDefaultUrl()
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public void setDefaultUrl(String id)
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public String writeUrlsAsString()
+ {
+ StringBuffer links = new StringBuffer();
+ if (urls.size() > 0)
+ {
+ for (UrlLink link : urls.values())
+ {
+ links.append(link.toString());
+ links.append("|");
+ }
+
+ // remove last "|"
+ links.setLength(links.length() - 1);
+ }
+ else
+ {
+ urls.clear();
+ }
+ return links.toString();
+ }
+
+ @Override
+ public void setUrlLinks(String cachedUrl)
+ {
+ // TODO Auto-generated method stub, maybe don't need this? watch out for
+ // superclass interactions
+
+ }
+
+ @Override
+ public void setUrlLinks(HashMap<String, UrlLink> links)
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+}