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.EMBLEBI_LABEL;
25 //import static jalview.util.UrlConstants.EMBLEBI_STRING;
26 //import static jalview.util.UrlConstants.SRS_LABEL;
29 import static jalview.util.UrlConstants.DB_ACCESSION;
30 import static jalview.util.UrlConstants.DELIM;
31 import static jalview.util.UrlConstants.SEP;
32 import static jalview.util.UrlConstants.SEQUENCE_ID;
34 import jalview.util.MessageManager;
35 import jalview.util.UrlLink;
37 import java.util.HashMap;
38 import java.util.Iterator;
40 import java.util.StringTokenizer;
41 import java.util.Vector;
45 * Implements the UrlProviderI interface for a UrlProvider object which serves
46 * custom URLs defined by the user
51 public class CustomUrlProvider extends UrlProviderImpl
54 // minimum length of substitution in url link string
55 private static final int MIN_SUBST_LENGTH = 4;
57 // Default sequence URL link label for SRS
58 private static final String SRS_LABEL = "SRS";
60 // map of string ids to urlLinks
61 private HashMap<String, UrlLink> urls;
64 * Construct UrlProvider for custom (user-entered) URLs
66 * @param cachedUrlList
67 * list of URLs in form stored in Cache. i.e. SEP delimited string
69 public CustomUrlProvider(String cachedUrlList)
73 urls = new HashMap<String, UrlLink>();
75 // cachedUrlList is in form <label>|<url>|<label>|<url>...
76 // parse cachedUrlList into labels (used as id) and url links
77 StringTokenizer st = new StringTokenizer(cachedUrlList, SEP);
78 while (st.hasMoreElements())
80 String name = st.nextToken();
81 String url = st.nextToken();
82 // check for '|' within a regex
83 int rxstart = url.indexOf(DELIM + DB_ACCESSION + DELIM);
86 rxstart = url.indexOf(DELIM + SEQUENCE_ID + DELIM);
88 while (rxstart == -1 && url.indexOf("/=" + DELIM) == -1)
90 url = url + SEP + st.nextToken();
92 urls.put(name, new UrlLink(name + SEP + url));
94 } catch (Exception ex)
96 System.out.println(ex + "\nError parsing sequence links");
103 * Construct UrlProvider for custom (user-entered) URLs
106 * list of URLs as (label,url) pairs
108 public CustomUrlProvider(Map<String, String> urlList)
112 urls = new HashMap<String, UrlLink>();
113 Iterator<Map.Entry<String, String>> it = urlList.entrySet()
117 Map.Entry<String, String> pair = it.next();
118 urls.put(pair.getKey(),
119 new UrlLink(pair.getKey() + SEP + pair.getValue()));
121 } catch (Exception ex)
123 System.out.println(ex + "\nError parsing sequence links");
129 * Upgrade any legacy links which may have been left lying around
131 private void upgradeOldLinks()
133 // upgrade old SRS link
134 if (urls.containsKey(SRS_LABEL))
136 urls.remove(SRS_LABEL);
137 urls.put(DEFAULT_LABEL, new UrlLink(DEFAULT_STRING));
142 public Vector<String> getLinksForDisplay()
144 Vector<String> links = new Vector<String>();
145 Iterator<Map.Entry<String, UrlLink>> it = urls.entrySet().iterator();
148 Map.Entry<String, UrlLink> pair = it.next();
149 links.add(pair.getValue().toString());
155 public boolean setDefaultUrl(String id)
157 if (urls.containsKey(id))
165 return urls.containsKey(id);
169 public String writeUrlsAsString()
171 StringBuffer links = new StringBuffer();
174 for (UrlLink link : urls.values())
176 links.append(link.toString());
181 links.setLength(links.length() - 1);
187 return links.toString();
191 public String getDefaultUrl(String seqid)
193 if (defaultUrl == null)
199 UrlLink urlLink = urls.get(defaultUrl);
200 String[] defaultUrls = urlLink.makeUrls(seqid, true);
201 if (defaultUrls == null || defaultUrls[0] == null
202 || defaultUrls[0].length() < MIN_SUBST_LENGTH)
208 // just take first URL made from regex
209 url = defaultUrls[1];
215 public String getDefaultTarget(String seqid)
217 return urls.get(defaultUrl).getTarget();
221 public void setUrlLinks(Vector<String> names, Vector<String> urlstrings)
223 HashMap<String, UrlLink> newurls = new HashMap<String, UrlLink>();
225 // should check that lists are same length but this function is likely
226 // to change once the Preferences dialog is rebuilt
228 for (int i = 0; i < names.size(); ++i)
230 // don't allow MIRIAM ids as custom url names (as the links will overwrite
232 // unlikely user would try to do this, but...
233 if (isMiriamId(names.elementAt(i)))
235 throw new IllegalArgumentException(MessageManager.formatMessage(
236 "exception.url_cannot_have_miriam_id", names.elementAt(i)));
238 // don't allow duplicate key names as entries will be overwritten
239 if (newurls.containsKey(names.elementAt(i)))
241 throw new IllegalArgumentException(MessageManager.formatMessage(
242 "exception.url_cannot_have_duplicate_id",
243 names.elementAt(i)));
245 newurls.put(names.elementAt(i), new UrlLink(names.elementAt(i) + SEP
246 + urlstrings.elementAt(i)));
249 // don't update until we're sure this set is ok
255 public String chooseDefaultUrl()
257 // unilaterally set the default id to the EMBL_EBI link
259 if (!urls.containsKey(DEFAULT_LABEL))
261 urls.put(DEFAULT_LABEL, new UrlLink(DEFAULT_STRING));
263 return DEFAULT_LABEL;