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.UrlConstants;
31 import jalview.util.UrlLink;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.List;
38 import java.util.Map.Entry;
39 import java.util.StringTokenizer;
40 import java.util.Vector;
44 * Implements the UrlProviderI interface for a UrlProvider object which serves
45 * custom URLs defined by the user
50 public class CustomUrlProvider extends UrlProviderImpl
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();
77 if (!isMiriamId(name))
79 // this one of our custom urls
80 String url = st.nextToken();
81 // check for '|' within a regex
82 int rxstart = url.indexOf(DELIM + DB_ACCESSION + DELIM);
85 rxstart = url.indexOf(DELIM + SEQUENCE_ID + DELIM);
87 while (rxstart == -1 && url.indexOf("/=" + DELIM) == -1
88 && st.hasMoreTokens())
90 url = url + SEP + st.nextToken();
92 urls.put(name, new UrlLink(name + SEP + url));
95 } catch (Exception ex)
97 System.out.println(ex + "\nError parsing sequence links");
104 * Construct UrlProvider for custom (user-entered) URLs
107 * list of URLs as (label,url) pairs
109 public CustomUrlProvider(Map<String, String> urlList)
113 urls = new HashMap<String, UrlLink>();
114 Iterator<Map.Entry<String, String>> it = urlList.entrySet()
118 Map.Entry<String, String> pair = it.next();
119 urls.put(pair.getKey(),
120 new UrlLink(pair.getKey() + SEP + pair.getValue()));
122 } catch (Exception ex)
124 System.out.println(ex + "\nError parsing sequence links");
130 * Upgrade any legacy links which may have been left lying around
132 private void upgradeOldLinks()
134 // upgrade old SRS link
135 if (urls.containsKey(SRS_LABEL))
137 urls.remove(SRS_LABEL);
138 urls.put(UrlConstants.DEFAULT_LABEL, new UrlLink(UrlConstants.DEFAULT_STRING));
143 public Vector<String> getLinksForMenu()
145 Vector<String> links = new Vector<String>();
146 Iterator<Map.Entry<String, UrlLink>> it = urls.entrySet().iterator();
149 Map.Entry<String, UrlLink> pair = it.next();
150 links.add(pair.getValue().toString());
156 public List<UrlLinkDisplay> getLinksForTable()
158 ArrayList<UrlLinkDisplay> displayLinks = new ArrayList<UrlLinkDisplay>();
159 for (Entry<String, UrlLink> entry : urls.entrySet())
161 String key = entry.getKey();
162 boolean isDefault = (key.equals(defaultUrl));
163 boolean isSelected = true; // custom urls always selected
164 String displayLink = entry.getValue().toString().split("\\|")[1]; // TODO
169 displayLinks.add(new UrlLinkDisplay(key, key, displayLink,
170 isSelected, isDefault));
176 public boolean setDefaultUrl(String id)
178 if (urls.containsKey(id))
186 return urls.containsKey(id);
190 public String writeUrlsAsString()
192 StringBuffer links = new StringBuffer();
195 for (UrlLink link : urls.values())
197 links.append(link.toString());
202 links.setLength(links.length() - 1);
208 return links.toString();
212 public String getDefaultUrl(String seqid)
214 if (defaultUrl == null)
220 UrlLink urlLink = urls.get(defaultUrl);
221 String[] defaultUrls = urlLink.makeUrls(seqid, true);
222 if (defaultUrls == null || defaultUrls[0] == null
223 || defaultUrls[0].length() < MIN_SUBST_LENGTH)
229 // just take first URL made from regex
230 url = defaultUrls[1];
236 public String getDefaultTarget(String seqid)
238 return urls.get(defaultUrl).getTarget();
242 public void setUrlData(List<UrlLinkDisplay> links)
244 HashMap<String, UrlLink> newurls = new HashMap<String, UrlLink>();
246 Iterator<UrlLinkDisplay> it = links.iterator();
249 UrlLinkDisplay link = it.next();
251 // MIRIAM ids will be handled by a different UrlProvider class
252 if (!isMiriamId(link.getId()))
255 // don't allow duplicate key names as entries will be overwritten
256 if (newurls.containsKey(link.getId()))
258 throw new IllegalArgumentException(MessageManager.formatMessage(
259 "exception.url_cannot_have_duplicate_id", link.getId()));
261 if (link.getIsSelected())
263 newurls.put(link.getId(),
264 new UrlLink(link.getId() + SEP + link.getUrl()));
266 // sort out default and selected ids
267 if (link.getIsDefault())
269 setDefaultUrl(link.getId());
272 // TODO KM make it possible to set and save selected custom urls
281 public String chooseDefaultUrl()
283 // unilaterally set the default id to the EMBL_EBI link
285 if (!urls.containsKey(UrlConstants.DEFAULT_LABEL))
287 urls.put(UrlConstants.DEFAULT_LABEL, new UrlLink(UrlConstants.DEFAULT_STRING));
289 defaultUrl = UrlConstants.DEFAULT_LABEL;
290 return UrlConstants.DEFAULT_LABEL;