JAL-2316 Unit testing, relax constraint that default url is in menu
[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.util.UrlLink;
24
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map.Entry;
29 import java.util.regex.Pattern;
30
31 /**
32  * Leaf node of UrlProvider composite
33  * 
34  * @author $author$
35  * @version $Revision$
36  */
37
38 public abstract class UrlProviderImpl implements UrlProviderI
39 {
40   // minimum length of substitution in url link string
41   protected static final int MIN_SUBST_LENGTH = 4;
42
43   private static final Pattern MIRIAM_PATTERN = Pattern
44           .compile("^MIR:\\d{8}$");
45
46   protected String defaultUrl;
47
48   protected String getDefaultUrl(String seqid, HashMap<String, UrlLink> urls)
49   {
50     if (seqid.length() < MIN_SUBST_LENGTH)
51     {
52       return null;
53     }
54     else if (defaultUrl == null)
55     {
56       return null;
57     }
58     else if (!urls.containsKey(defaultUrl))
59     {
60       return null;
61     }
62     else
63     {
64       String url = null;
65       UrlLink urlLink = urls.get(defaultUrl);
66       String[] defaultUrls = urlLink.makeUrls(seqid, true);
67       if (defaultUrls == null || defaultUrls[0] == null
68               || defaultUrls[0].length() < MIN_SUBST_LENGTH)
69       {
70         url = null;
71       }
72       else
73       {
74         // just take first URL made from regex
75         url = defaultUrls[1];
76       }
77       return url;
78     }
79   }
80
81   @Override
82   public List<UrlLinkDisplay> getLinksForTable()
83   {
84     return null;
85   }
86
87   protected ArrayList<UrlLinkDisplay> getLinksForTable(
88           HashMap<String, UrlLink> urls, ArrayList<String> selectedUrls,
89           boolean selected)
90   {
91     ArrayList<UrlLinkDisplay> displayLinks = new ArrayList<UrlLinkDisplay>();
92     for (Entry<String, UrlLink> entry : urls.entrySet())
93     {
94       String key = entry.getKey();
95       boolean isDefault = (key.equals(defaultUrl));
96       boolean isSelected;
97       if (selectedUrls != null)
98       {
99         isSelected = selectedUrls.contains(key);
100       }
101       else
102       {
103         isSelected = selected;
104       }
105       String displayLink = entry.getValue().getUrlWithToken();
106       displayLinks.add(new UrlLinkDisplay(key, entry.getValue().getLabel(),
107               displayLink, 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