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;
27 import static jalview.util.UrlConstants.SEQUENCE_ID;
29 import jalview.util.MessageManager;
30 import jalview.util.UrlLink;
32 import java.util.HashMap;
33 import java.util.Iterator;
35 import java.util.StringTokenizer;
36 import java.util.Vector;
40 * Implements the UrlProviderI interface for a UrlProvider object which serves
41 * custom URLs defined by the user
46 public class CustomUrlProvider extends UrlProviderImpl
49 // minimum length of substitution in url link string
50 private static final int MIN_SUBST_LENGTH = 4;
52 // Default sequence URL link label for SRS
53 private static final String SRS_LABEL = "SRS";
55 // map of string ids to urlLinks
56 private HashMap<String, UrlLink> urls;
59 * Construct UrlProvider for custom (user-entered) URLs
61 * @param cachedUrlList
62 * list of URLs in form stored in Cache. i.e. SEP delimited string
64 public CustomUrlProvider(String cachedUrlList)
68 urls = new HashMap<String, UrlLink>();
70 // cachedUrlList is in form <label>|<url>|<label>|<url>...
71 // parse cachedUrlList into labels (used as id) and url links
72 StringTokenizer st = new StringTokenizer(cachedUrlList, SEP);
73 while (st.hasMoreElements())
75 String name = st.nextToken();
76 String url = st.nextToken();
77 // check for '|' within a regex
78 int rxstart = url.indexOf(DELIM + DB_ACCESSION + DELIM);
81 rxstart = url.indexOf(DELIM + SEQUENCE_ID + DELIM);
83 while (rxstart == -1 && url.indexOf("/=" + DELIM) == -1
84 && st.hasMoreTokens())
86 url = url + SEP + st.nextToken();
88 urls.put(name, new UrlLink(name + SEP + url));
90 } catch (Exception ex)
92 System.out.println(ex + "\nError parsing sequence links");
99 * Construct UrlProvider for custom (user-entered) URLs
102 * list of URLs as (label,url) pairs
104 public CustomUrlProvider(Map<String, String> urlList)
108 urls = new HashMap<String, UrlLink>();
109 Iterator<Map.Entry<String, String>> it = urlList.entrySet()
113 Map.Entry<String, String> pair = it.next();
114 urls.put(pair.getKey(),
115 new UrlLink(pair.getKey() + SEP + pair.getValue()));
117 } catch (Exception ex)
119 System.out.println(ex + "\nError parsing sequence links");
125 * Upgrade any legacy links which may have been left lying around
127 private void upgradeOldLinks()
129 // upgrade old SRS link
130 if (urls.containsKey(SRS_LABEL))
132 urls.remove(SRS_LABEL);
133 urls.put(DEFAULT_LABEL, new UrlLink(DEFAULT_STRING));
138 public Vector<String> getLinksForDisplay()
140 Vector<String> links = new Vector<String>();
141 Iterator<Map.Entry<String, UrlLink>> it = urls.entrySet().iterator();
144 Map.Entry<String, UrlLink> pair = it.next();
145 links.add(pair.getValue().toString());
151 public boolean setDefaultUrl(String id)
153 if (urls.containsKey(id))
161 return urls.containsKey(id);
165 public String writeUrlsAsString()
167 StringBuffer links = new StringBuffer();
170 for (UrlLink link : urls.values())
172 links.append(link.toString());
177 links.setLength(links.length() - 1);
183 return links.toString();
187 public String getDefaultUrl(String seqid)
189 if (defaultUrl == null)
195 UrlLink urlLink = urls.get(defaultUrl);
196 String[] defaultUrls = urlLink.makeUrls(seqid, true);
197 if (defaultUrls == null || defaultUrls[0] == null
198 || defaultUrls[0].length() < MIN_SUBST_LENGTH)
204 // just take first URL made from regex
205 url = defaultUrls[1];
211 public String getDefaultTarget(String seqid)
213 return urls.get(defaultUrl).getTarget();
217 public void setUrlLinks(Vector<String> names, Vector<String> urlstrings)
219 HashMap<String, UrlLink> newurls = new HashMap<String, UrlLink>();
221 // should check that lists are same length but this function is likely
222 // to change once the Preferences dialog is rebuilt
224 for (int i = 0; i < names.size(); ++i)
226 // don't allow MIRIAM ids as custom url names (as the links will overwrite
228 // unlikely user would try to do this, but...
229 if (isMiriamId(names.elementAt(i)))
231 throw new IllegalArgumentException(MessageManager.formatMessage(
232 "exception.url_cannot_have_miriam_id", names.elementAt(i)));
234 // don't allow duplicate key names as entries will be overwritten
235 if (newurls.containsKey(names.elementAt(i)))
237 throw new IllegalArgumentException(MessageManager.formatMessage(
238 "exception.url_cannot_have_duplicate_id",
239 names.elementAt(i)));
241 newurls.put(names.elementAt(i), new UrlLink(names.elementAt(i) + SEP
242 + urlstrings.elementAt(i)));
245 // don't update until we're sure this set is ok
251 public String chooseDefaultUrl()
253 // unilaterally set the default id to the EMBL_EBI link
255 if (!urls.containsKey(DEFAULT_LABEL))
257 urls.put(DEFAULT_LABEL, new UrlLink(DEFAULT_STRING));
259 defaultUrl = DEFAULT_LABEL;
260 return DEFAULT_LABEL;