regular expression based URL link generation
[jalview.git] / src / jalview / util / UrlLink.java
1 package jalview.util;\r
2 \r
3 import java.util.Vector;\r
4 \r
5 public class UrlLink\r
6 {\r
7   /**\r
8    * helper class to parse URL Link strings taken from applet parameters or\r
9    * jalview properties file using the com.stevesoft.pat.Regex implementation.\r
10    * Jalview 2.4 extension allows regular expressions to be used to parse ID\r
11    * strings and replace the result in the URL. Regex's operate on the whole ID\r
12    * string given to the matchURL method, if no regex is supplied, then only\r
13    * text following the first pipe symbol will be susbstituted.\r
14    */\r
15   private String url_suffix, url_prefix, target, label, regexReplace;\r
16 \r
17   private boolean dynamic = false;\r
18 \r
19   private String invalidMessage = null;\r
20 \r
21   /**\r
22    * parse the given linkString of the form '<label>|<url>' into parts url may\r
23    * contain a string $SEQUENCE_ID<=optional regex=>$ where <=optional regex=>\r
24    * must be of the form =/<perl style regex>/=$\r
25    * \r
26    * @param link\r
27    */\r
28   public UrlLink(String link)\r
29   {\r
30     int sep = link.indexOf("|"), psqid = link.indexOf("$SEQUENCE_ID");\r
31     if (psqid > -1)\r
32     {\r
33       dynamic = true;\r
34       int p = sep;\r
35       do\r
36       {\r
37         sep = p;\r
38         p = link.indexOf("|", sep + 1);\r
39       } while (p > sep && p < psqid);\r
40       // Assuming that the URL itself does not contain any '|' symbols\r
41       // sep now contains last pipe symbol position prior to any regex symbols\r
42       label = link.substring(0, sep);\r
43       if (label.indexOf("|") > -1)\r
44       {\r
45         // | terminated database name / www target at start of Label\r
46         target = label.substring(0, label.indexOf("|"));\r
47       }\r
48       else if (label.indexOf(" ") > 2)\r
49       {\r
50         // space separated Label - matches database name\r
51         target = label.substring(0, label.indexOf(" "));\r
52       }\r
53       else\r
54       {\r
55         target = label;\r
56       }\r
57       // Parse URL : Whole URL string first\r
58       url_prefix = link.substring(sep + 1, psqid);\r
59       if (link.indexOf("$SEQUENCE_ID=/") == psqid\r
60               && (p = link.indexOf("/=$", psqid + 14)) > psqid + 14)\r
61       {\r
62         // Extract Regex and suffix\r
63         url_suffix = link.substring(p + 3);\r
64         regexReplace = link.substring(psqid + 14, p);\r
65         try\r
66         {\r
67           com.stevesoft.pat.Regex rg = com.stevesoft.pat.Regex.perlCode("/"\r
68                   + regexReplace + "/");\r
69           if (rg == null)\r
70           {\r
71             invalidMessage = "Invalid Regular Expression : '"\r
72                     + regexReplace + "'\n";\r
73           }\r
74         } catch (Exception e)\r
75         {\r
76           invalidMessage = "Invalid Regular Expression : '" + regexReplace\r
77                   + "'\n";\r
78         }\r
79       }\r
80       else\r
81       {\r
82         regexReplace = null;\r
83         // verify format is really correct.\r
84         if (link.indexOf("$SEQUENCE_ID$") == psqid)\r
85         {\r
86           url_suffix = link.substring(psqid + 13);\r
87           regexReplace = null;\r
88         }\r
89         else\r
90         {\r
91           invalidMessage = "Warning: invalid regex structure for URL link : "\r
92                   + link;\r
93         }\r
94       }\r
95     }\r
96     else\r
97     {\r
98       target = link.substring(0, sep);\r
99       label = link.substring(0, sep = link.lastIndexOf("|"));\r
100       url_prefix = link.substring(sep + 1);\r
101       regexReplace = null; // implies we trim any prefix if necessary //\r
102                             // regexReplace=".*\\|?(.*)";\r
103       url_suffix = null;\r
104     }\r
105   }\r
106 \r
107   /**\r
108    * @return the url_suffix\r
109    */\r
110   public String getUrl_suffix()\r
111   {\r
112     return url_suffix;\r
113   }\r
114 \r
115   /**\r
116    * @return the url_prefix\r
117    */\r
118   public String getUrl_prefix()\r
119   {\r
120     return url_prefix;\r
121   }\r
122 \r
123   /**\r
124    * @return the target\r
125    */\r
126   public String getTarget()\r
127   {\r
128     return target;\r
129   }\r
130 \r
131   /**\r
132    * @return the label\r
133    */\r
134   public String getLabel()\r
135   {\r
136     return label;\r
137   }\r
138 \r
139   /**\r
140    * @return the regexReplace\r
141    */\r
142   public String getRegexReplace()\r
143   {\r
144     return regexReplace;\r
145   }\r
146 \r
147   /**\r
148    * @return the invalidMessage\r
149    */\r
150   public String getInvalidMessage()\r
151   {\r
152     return invalidMessage;\r
153   }\r
154 \r
155   /**\r
156    * \r
157    * @return true if URL string could not be parsed properly.\r
158    */\r
159   public boolean isValid()\r
160   {\r
161     return invalidMessage == null;\r
162   }\r
163 \r
164   /**\r
165    * return one or more URL strings by applying regex to the given idstring\r
166    * \r
167    * @param idstring\r
168    * @param onlyIfMatches -\r
169    *                when true url strings are only made if regex is defined and\r
170    *                matches\r
171    * @return String[] { part of idstring substituted, full substituted url , ..\r
172    *         next part, next url..}\r
173    */\r
174   public String[] makeUrls(String idstring, boolean onlyIfMatches)\r
175   {\r
176     if (dynamic)\r
177     {\r
178       if (regexReplace != null)\r
179       {\r
180         com.stevesoft.pat.Regex rg = com.stevesoft.pat.Regex.perlCode("/"\r
181                 + regexReplace + "/");\r
182         if (rg.search(idstring))\r
183         {\r
184           int ns = rg.numSubs();\r
185           if (ns == 0)\r
186           {\r
187             // take whole regex\r
188             return new String[]\r
189             { rg.stringMatched(),\r
190                 url_prefix + rg.stringMatched() + url_suffix };\r
191           } /*\r
192              * else if (ns==1) { // take only subgroup match return new String[] {\r
193              * rg.stringMatched(1), url_prefix+rg.stringMatched(1)+url_suffix }; }\r
194              */\r
195           else\r
196           {\r
197             // debug\r
198             for (int s = 0; s <= rg.numSubs(); s++)\r
199             {\r
200               System.err.println("Sub " + s + " : " + rg.matchedFrom(s)\r
201                       + " : " + rg.matchedTo(s) + " : '"\r
202                       + rg.stringMatched(s) + "'");\r
203             }\r
204             // try to collate subgroup matches\r
205             Vector subs = new Vector();\r
206             // have to loop through submatches, collating them at top level\r
207             // match\r
208             int s = 0; // 1;\r
209             while (s <= ns)\r
210             {\r
211               if (s + 1 <= ns && rg.matchedTo(s) > -1\r
212                       && rg.matchedTo(s + 1) > -1\r
213                       && rg.matchedTo(s + 1) < rg.matchedTo(s))\r
214               {\r
215                 // s is top level submatch. search for submatches enclosed by\r
216                 // this one\r
217                 int r = s + 1;\r
218                 String mtch = "";\r
219                 while (r <= ns && rg.matchedTo(r) <= rg.matchedTo(s))\r
220                 {\r
221                   if (rg.matchedFrom(r) > -1)\r
222                   {\r
223                     mtch += rg.stringMatched(r);\r
224                   }\r
225                   r++;\r
226                 }\r
227                 if (mtch.length() > 0)\r
228                 {\r
229                   subs.addElement(mtch);\r
230                   subs.addElement(url_prefix + mtch + url_suffix);\r
231                 }\r
232                 s = r;\r
233               }\r
234               else\r
235               {\r
236                 if (rg.matchedFrom(s) > -1)\r
237                 {\r
238                   subs.addElement(rg.stringMatched(s));\r
239                   subs.addElement(url_prefix + rg.stringMatched(s)\r
240                           + url_suffix);\r
241                 }\r
242                 s++;\r
243               }\r
244             }\r
245 \r
246             String[] res = new String[subs.size()];\r
247             for (int r = 0, rs = subs.size(); r < rs; r++)\r
248             {\r
249               res[r] = (String) subs.elementAt(r);\r
250             }\r
251             subs.removeAllElements();\r
252             return res;\r
253           }\r
254         }\r
255         if (onlyIfMatches)\r
256         {\r
257           return null;\r
258         }\r
259       }\r
260       /* Otherwise - trim off any 'prefix' - pre 2.4 Jalview behaviour */\r
261       if (idstring.indexOf("|") > -1)\r
262       {\r
263         idstring = idstring.substring(idstring.lastIndexOf("|") + 1);\r
264       }\r
265 \r
266       // just return simple url substitution.\r
267       return new String[]\r
268       { idstring, url_prefix + idstring + url_suffix };\r
269     }\r
270     else\r
271     {\r
272       return new String[]\r
273       { "", url_prefix };\r
274     }\r
275   }\r
276 \r
277   public String toString()\r
278   {\r
279     return label\r
280             + "|"\r
281             + url_prefix\r
282             + (dynamic ? ("$SEQUENCE_ID" + ((regexReplace != null) ? "="\r
283                     + regexReplace + "=$" : "$")) : "")\r
284             + ((url_suffix == null) ? "" : url_suffix);\r
285 \r
286   }\r
287 \r
288   private static void testUrls(UrlLink ul, String idstring, String[] urls)\r
289   {\r
290 \r
291     if (urls == null)\r
292     {\r
293       System.out.println("Created NO urls.");\r
294     }\r
295     else\r
296     {\r
297       System.out.println("Created " + (urls.length / 2) + " Urls.");\r
298       for (int uls = 0; uls < urls.length; uls += 2)\r
299       {\r
300         System.out.println("URL Replacement text : " + urls[uls]\r
301                 + " : URL : " + urls[uls + 1]);\r
302       }\r
303     }\r
304   }\r
305 \r
306   public static void main(String argv[])\r
307   {\r
308     String[] links = new String[]\r
309     {\r
310     /*\r
311      * "AlinkT|Target|http://foo.foo.soo/",\r
312      * "myUrl1|http://$SEQUENCE_ID=/[0-9]+/=$.someserver.org/foo",\r
313      * "myUrl2|http://$SEQUENCE_ID=/(([0-9]+).+([A-Za-z]+))/=$.someserver.org/foo",\r
314      * "myUrl3|http://$SEQUENCE_ID=/([0-9]+).+([A-Za-z]+)/=$.someserver.org/foo",\r
315      * "myUrl4|target|http://$SEQUENCE_ID$.someserver.org/foo|too",\r
316      * "PF1|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/(?:PFAM:)?(.+)/=$",\r
317      * "PF2|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/(PFAM:)?(.+)/=$",\r
318      * "PF3|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/PFAM:(.+)/=$",\r
319      * "NOTFER|http://notfer.org/$SEQUENCE_ID=/(?<!\\s)(.+)/=$",\r
320      */\r
321     "NESTED|http://nested/$SEQUENCE_ID=/^(?:Label:)?(?:(?:gi\\|(\\d+))|([^:]+))/=$/nested" };\r
322     String[] idstrings = new String[]\r
323     {\r
324     /*\r
325      * //"LGUL_human", //"QWIQW_123123", "uniprot|why_do+_12313_foo",\r
326      * //"123123312", "123123 ABCDE foo", "PFAM:PF23943",\r
327      */\r
328     "Label:gi|9234|pdb|102L|A" };\r
329 \r
330     for (int i = 0; i < links.length; i++)\r
331     {\r
332       UrlLink ul = new UrlLink(links[i]);\r
333       if (ul.isValid())\r
334       {\r
335         System.out.println("\n\n\n");\r
336         System.out.println("Link " + i + " " + links[i] + " : "\r
337                 + ul.toString());\r
338         System.out.println(" pref : "\r
339                 + ul.getUrl_prefix()\r
340                 + "\n suf : "\r
341                 + ul.getUrl_suffix()\r
342                 + "\n : "\r
343                 + ((ul.getRegexReplace() != null) ? ul.getRegexReplace()\r
344                         : ""));\r
345         for (int ids = 0; ids < idstrings.length; ids++)\r
346         {\r
347           System.out.println("ID String : " + idstrings[ids]\r
348                   + "\nWithout onlyIfMatches:");\r
349           String[] urls = ul.makeUrls(idstrings[ids], false);\r
350           testUrls(ul, idstrings[ids], urls);\r
351           System.out.println("With onlyIfMatches set.");\r
352           urls = ul.makeUrls(idstrings[ids], true);\r
353           testUrls(ul, idstrings[ids], urls);\r
354         }\r
355       }\r
356       else\r
357       {\r
358         System.err.println("Invalid URLLink : " + links[i] + " : "\r
359                 + ul.getInvalidMessage());\r
360       }\r
361     }\r
362   }\r
363 \r
364   public boolean isDynamic()\r
365   {\r
366     // TODO Auto-generated method stub\r
367     return dynamic;\r
368   }\r
369 }\r