Url links changes - incomplete checkin as battery low
[jalview.git] / src / jalview / util / CustomUrlProvider.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
22 package jalview.util;
23
24 import static jalview.util.UrlConstants.DB_ACCESSION;
25 import static jalview.util.UrlConstants.EMBLEBI_STRING;
26 import static jalview.util.UrlConstants.SEQUENCE_ID;
27 import static jalview.util.UrlConstants.SRS_STRING;
28
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.Map;
32 import java.util.StringTokenizer;
33 import java.util.Vector;
34
35 /**
36  * 
37  * Implements the UrlProviderI interface for a UrlProvider object which serves
38  * custom URLs defined by the user
39  * 
40  * @author $author$
41  * @version $Revision$
42  */
43 public class CustomUrlProvider implements UrlProviderI
44 {
45
46   private HashMap<String, UrlLink> urls;
47
48   public CustomUrlProvider(String cachedUrlList)
49   {
50     try
51     {
52       StringTokenizer st = new StringTokenizer(cachedUrlList, "|");
53       while (st.hasMoreElements())
54       {
55         String name = st.nextToken();
56         String url = st.nextToken();
57         // check for '|' within a regex
58         int rxstart = url.indexOf("$" + DB_ACCESSION + "$");
59         if (rxstart == -1)
60         {
61           rxstart = url.indexOf("$" + SEQUENCE_ID + "$");
62         }
63         while (rxstart == -1 && url.indexOf("/=$") == -1)
64         {
65           url = url + "|" + st.nextToken();
66         }
67         urls.put(name, new UrlLink(url));
68         // sequenceURLLinks.addElement(name + "|" + url);
69       }
70     } catch (Exception ex)
71     {
72       System.out.println(ex + "\nError parsing sequence links");
73     }
74
75     // upgrade old SRS link
76     String srs_key = SRS_STRING.split("\\|")[0];
77     if (urls.containsKey(srs_key))
78     {
79       urls.remove(srs_key);
80       urls.put(EMBLEBI_STRING.split("//|")[0],
81               new UrlLink(EMBLEBI_STRING.split("//|")[1]));
82     }
83   }
84
85   @Override
86   public HashMap<String, UrlLink> getUrlLinks()
87   {
88     // TODO Auto-generated method stub
89     return null;
90   }
91
92   @Override
93   public void getLinksForDisplay(Vector<String> nameLinks,
94           Vector<String> urlLinks)
95   {
96     Iterator<Map.Entry<String, UrlLink>> it = urls.entrySet().iterator();
97     while (it.hasNext())
98     {
99       Map.Entry<String, UrlLink> pair = it.next();
100       nameLinks.addElement(pair.getKey());
101       urlLinks.addElement(pair.getValue().toString()); // TODO check this is
102                                                        // right
103     }
104   }
105
106   @Override
107   public String getDefaultUrl()
108   {
109     // TODO Auto-generated method stub
110     return null;
111   }
112
113   @Override
114   public void setDefaultUrl(String id)
115   {
116     // TODO Auto-generated method stub
117
118   }
119
120   @Override
121   public String writeUrlsAsString()
122   {
123     StringBuffer links = new StringBuffer();
124     if (urls.size() > 0)
125     {
126       for (UrlLink link : urls.values())
127       {
128         links.append(link.toString());
129         links.append("|");
130       }
131
132       // remove last "|"
133       links.setLength(links.length() - 1);
134     }
135     else
136     {
137       urls.clear();
138     }
139     return links.toString();
140   }
141
142   @Override
143   public void setUrlLinks(String cachedUrl)
144   {
145     // TODO Auto-generated method stub, maybe don't need this? watch out for
146     // superclass interactions
147
148   }
149
150   @Override
151   public void setUrlLinks(HashMap<String, UrlLink> links)
152   {
153     // TODO Auto-generated method stub
154
155   }
156
157 }