2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
20 import java.util.Vector;
25 * helper class to parse URL Link strings taken from applet parameters or
26 * jalview properties file using the com.stevesoft.pat.Regex implementation.
27 * Jalview 2.4 extension allows regular expressions to be used to parse ID
28 * strings and replace the result in the URL. Regex's operate on the whole ID
29 * string given to the matchURL method, if no regex is supplied, then only
30 * text following the first pipe symbol will be susbstituted. Usage
33 private String url_suffix, url_prefix, target, label, regexReplace;
35 private boolean dynamic = false;
37 private String invalidMessage = null;
40 * parse the given linkString of the form '<label>|<url>' into parts url may
41 * contain a string $SEQUENCE_ID<=optional regex=>$ where <=optional regex=>
42 * must be of the form =/<perl style regex>/=$
46 public UrlLink(String link)
48 int sep = link.indexOf("|"), psqid = link.indexOf("$SEQUENCE_ID");
56 p = link.indexOf("|", sep + 1);
57 } while (p > sep && p < psqid);
58 // Assuming that the URL itself does not contain any '|' symbols
59 // sep now contains last pipe symbol position prior to any regex symbols
60 label = link.substring(0, sep);
61 if (label.indexOf("|") > -1)
63 // | terminated database name / www target at start of Label
64 target = label.substring(0, label.indexOf("|"));
66 else if (label.indexOf(" ") > 2)
68 // space separated Label - matches database name
69 target = label.substring(0, label.indexOf(" "));
75 // Parse URL : Whole URL string first
76 url_prefix = link.substring(sep + 1, psqid);
77 if (link.indexOf("$SEQUENCE_ID=/") == psqid
78 && (p = link.indexOf("/=$", psqid + 14)) > psqid + 14)
80 // Extract Regex and suffix
81 url_suffix = link.substring(p + 3);
82 regexReplace = link.substring(psqid + 14, p);
85 com.stevesoft.pat.Regex rg = com.stevesoft.pat.Regex.perlCode("/"
86 + regexReplace + "/");
89 invalidMessage = "Invalid Regular Expression : '"
90 + regexReplace + "'\n";
94 invalidMessage = "Invalid Regular Expression : '" + regexReplace
101 // verify format is really correct.
102 if (link.indexOf("$SEQUENCE_ID$") == psqid)
104 url_suffix = link.substring(psqid + 13);
109 invalidMessage = "Warning: invalid regex structure for URL link : "
116 target = link.substring(0, sep);
117 label = link.substring(0, sep = link.lastIndexOf("|"));
118 url_prefix = link.substring(sep + 1);
119 regexReplace = null; // implies we trim any prefix if necessary //
120 // regexReplace=".*\\|?(.*)";
126 * @return the url_suffix
128 public String getUrl_suffix()
134 * @return the url_prefix
136 public String getUrl_prefix()
144 public String getTarget()
152 public String getLabel()
158 * @return the regexReplace
160 public String getRegexReplace()
166 * @return the invalidMessage
168 public String getInvalidMessage()
170 return invalidMessage;
174 * Check if URL string was parsed properly.
176 * @return boolean - if false then <code>getInvalidMessage</code> returns an
179 public boolean isValid()
181 return invalidMessage == null;
185 * return one or more URL strings by applying regex to the given idstring
188 * @param onlyIfMatches
189 * - when true url strings are only made if regex is defined and
191 * @return String[] { part of idstring substituted, full substituted url , ..
192 * next part, next url..}
194 public String[] makeUrls(String idstring, boolean onlyIfMatches)
198 if (regexReplace != null)
200 com.stevesoft.pat.Regex rg = com.stevesoft.pat.Regex.perlCode("/"
201 + regexReplace + "/");
202 if (rg.search(idstring))
204 int ns = rg.numSubs();
209 { rg.stringMatched(),
210 url_prefix + rg.stringMatched() + url_suffix };
212 * else if (ns==1) { // take only subgroup match return new String[]
213 * { rg.stringMatched(1), url_prefix+rg.stringMatched(1)+url_suffix
219 for (int s = 0; s <= rg.numSubs(); s++)
221 System.err.println("Sub " + s + " : " + rg.matchedFrom(s)
222 + " : " + rg.matchedTo(s) + " : '"
223 + rg.stringMatched(s) + "'");
225 // try to collate subgroup matches
226 Vector subs = new Vector();
227 // have to loop through submatches, collating them at top level
232 if (s + 1 <= ns && rg.matchedTo(s) > -1
233 && rg.matchedTo(s + 1) > -1
234 && rg.matchedTo(s + 1) < rg.matchedTo(s))
236 // s is top level submatch. search for submatches enclosed by
240 while (r <= ns && rg.matchedTo(r) <= rg.matchedTo(s))
242 if (rg.matchedFrom(r) > -1)
244 mtch += rg.stringMatched(r);
248 if (mtch.length() > 0)
250 subs.addElement(mtch);
251 subs.addElement(url_prefix + mtch + url_suffix);
257 if (rg.matchedFrom(s) > -1)
259 subs.addElement(rg.stringMatched(s));
260 subs.addElement(url_prefix + rg.stringMatched(s)
267 String[] res = new String[subs.size()];
268 for (int r = 0, rs = subs.size(); r < rs; r++)
270 res[r] = (String) subs.elementAt(r);
272 subs.removeAllElements();
281 /* Otherwise - trim off any 'prefix' - pre 2.4 Jalview behaviour */
282 if (idstring.indexOf("|") > -1)
284 idstring = idstring.substring(idstring.lastIndexOf("|") + 1);
287 // just return simple url substitution.
289 { idstring, url_prefix + idstring + url_suffix };
298 public String toString()
303 + (dynamic ? ("$SEQUENCE_ID" + ((regexReplace != null) ? "="
304 + regexReplace + "=$" : "$")) : "")
305 + ((url_suffix == null) ? "" : url_suffix);
309 private static void testUrls(UrlLink ul, String idstring, String[] urls)
314 System.out.println("Created NO urls.");
318 System.out.println("Created " + (urls.length / 2) + " Urls.");
319 for (int uls = 0; uls < urls.length; uls += 2)
321 System.out.println("URL Replacement text : " + urls[uls]
322 + " : URL : " + urls[uls + 1]);
327 public static void main(String argv[])
329 String[] links = new String[]
332 * "AlinkT|Target|http://foo.foo.soo/",
333 * "myUrl1|http://$SEQUENCE_ID=/[0-9]+/=$.someserver.org/foo",
334 * "myUrl2|http://$SEQUENCE_ID=/(([0-9]+).+([A-Za-z]+))/=$.someserver.org/foo"
336 * "myUrl3|http://$SEQUENCE_ID=/([0-9]+).+([A-Za-z]+)/=$.someserver.org/foo"
337 * , "myUrl4|target|http://$SEQUENCE_ID$.someserver.org/foo|too",
338 * "PF1|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/(?:PFAM:)?(.+)/=$"
340 * "PF2|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/(PFAM:)?(.+)/=$"
342 * "PF3|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/PFAM:(.+)/=$"
343 * , "NOTFER|http://notfer.org/$SEQUENCE_ID=/(?<!\\s)(.+)/=$",
345 "NESTED|http://nested/$SEQUENCE_ID=/^(?:Label:)?(?:(?:gi\\|(\\d+))|([^:]+))/=$/nested" };
346 String[] idstrings = new String[]
349 * //"LGUL_human", //"QWIQW_123123", "uniprot|why_do+_12313_foo",
350 * //"123123312", "123123 ABCDE foo", "PFAM:PF23943",
352 "Label:gi|9234|pdb|102L|A" };
353 // TODO: test the setLabel method.
354 for (int i = 0; i < links.length; i++)
356 UrlLink ul = new UrlLink(links[i]);
359 System.out.println("\n\n\n");
360 System.out.println("Link " + i + " " + links[i] + " : "
362 System.out.println(" pref : "
367 + ((ul.getRegexReplace() != null) ? ul.getRegexReplace()
369 for (int ids = 0; ids < idstrings.length; ids++)
371 System.out.println("ID String : " + idstrings[ids]
372 + "\nWithout onlyIfMatches:");
373 String[] urls = ul.makeUrls(idstrings[ids], false);
374 testUrls(ul, idstrings[ids], urls);
375 System.out.println("With onlyIfMatches set.");
376 urls = ul.makeUrls(idstrings[ids], true);
377 testUrls(ul, idstrings[ids], urls);
382 System.err.println("Invalid URLLink : " + links[i] + " : "
383 + ul.getInvalidMessage());
388 public boolean isDynamic()
390 // TODO Auto-generated method stub
394 public void setLabel(String newlabel)
396 this.label = newlabel;