87e40c6ddf9265c7891ecbc7376fd51007b889f5
[jalview.git] / src / jalview / urls / UrlProviderImpl.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.urls;
22
23 import jalview.urls.api.UrlProviderI;
24 import jalview.util.UrlLink;
25
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map.Entry;
30 import java.util.regex.Pattern;
31
32 /**
33  * Leaf node of UrlProvider composite
34  * 
35  * @author $author$
36  * @version $Revision$
37  */
38
39 public abstract class UrlProviderImpl implements UrlProviderI
40 {
41   // minimum length of substitution in url link string
42   protected static final int MIN_SUBST_LENGTH = 4;
43
44   private static final Pattern MIRIAM_PATTERN = Pattern
45           .compile("^MIR:\\d{8}$");
46
47   protected String defaultUrl;
48
49   protected String getDefaultUrl(String seqid, HashMap<String, UrlLink> urls)
50   {
51     if (seqid.length() < MIN_SUBST_LENGTH)
52     {
53       return null;
54     }
55     else if (defaultUrl == null)
56     {
57       return null;
58     }
59     else if (!urls.containsKey(defaultUrl))
60     {
61       return null;
62     }
63     else
64     {
65       String url = null;
66       UrlLink urlLink = urls.get(defaultUrl);
67       String[] defaultUrls = urlLink.makeUrls(seqid, true);
68       if (defaultUrls == null || defaultUrls[0] == null
69               || defaultUrls[0].length() < MIN_SUBST_LENGTH)
70       {
71         url = null;
72       }
73       else
74       {
75         // just take first URL made from regex
76         url = defaultUrls[1];
77       }
78       return url;
79     }
80   }
81
82   @Override
83   public List<UrlLinkDisplay> getLinksForTable()
84   {
85     return null;
86   }
87
88   protected ArrayList<UrlLinkDisplay> getLinksForTable(
89           HashMap<String, UrlLink> urls, ArrayList<String> selectedUrls,
90           boolean selected)
91   {
92     ArrayList<UrlLinkDisplay> displayLinks = new ArrayList<UrlLinkDisplay>();
93     for (Entry<String, UrlLink> entry : urls.entrySet())
94     {
95       String key = entry.getKey();
96       boolean isDefault = (key.equals(defaultUrl));
97       boolean isSelected;
98       if (selectedUrls != null)
99       {
100         isSelected = selectedUrls.contains(key);
101       }
102       else
103       {
104         isSelected = selected;
105       }
106       displayLinks.add(new UrlLinkDisplay(key, entry.getValue(),
107               isSelected, isDefault));
108     }
109     return displayLinks;
110   }
111
112   protected boolean isMiriamId(String id)
113   {
114     return MIRIAM_PATTERN.matcher(id).matches();
115   }
116
117   @Override
118   public boolean isUserEntry(String id)
119   {
120     return !isMiriamId(id);
121   }
122 }
123