2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
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.
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.
23 import static jalview.util.UrlConstants.SEQUENCE_ID;
24 import static jalview.util.UrlConstants.SEQUENCE_NAME;
26 import java.util.Vector;
31 * helper class to parse URL Link strings taken from applet parameters or
32 * jalview properties file using the com.stevesoft.pat.Regex implementation.
33 * Jalview 2.4 extension allows regular expressions to be used to parse ID
34 * strings and replace the result in the URL. Regex's operate on the whole ID
35 * string given to the matchURL method, if no regex is supplied, then only
36 * text following the first pipe symbol will be susbstituted. Usage
39 private String url_suffix, url_prefix, target, label, regexReplace;
41 private boolean dynamic = false;
43 private boolean uses_seq_id = false;
45 private String invalidMessage = null;
48 * parse the given linkString of the form '<label>|<url>' into parts url may
49 * contain a string $SEQUENCE_ID<=optional regex=>$ where <=optional regex=>
50 * must be of the form =/<perl style regex>/=$
54 public UrlLink(String link)
56 int sep = link.indexOf("|");
57 int psqid = link.indexOf("$" + SEQUENCE_ID);
58 int nsqid = link.indexOf("$" + SEQUENCE_NAME);
64 sep = parseTargetAndLabel(sep, psqid, link);
66 // Parse URL : Whole URL string first
68 url_prefix = link.substring(sep + 1, psqid);
69 if (link.indexOf("$" + SEQUENCE_ID + "=/") == psqid
70 && (p = link.indexOf("/=$", psqid + 14)) > psqid + 14)
72 // Extract Regex and suffix
73 url_suffix = link.substring(p + 3);
74 regexReplace = link.substring(psqid + 14, p);
77 com.stevesoft.pat.Regex rg = com.stevesoft.pat.Regex.perlCode("/"
78 + regexReplace + "/");
81 invalidMessage = "Invalid Regular Expression : '"
82 + regexReplace + "'\n";
86 invalidMessage = "Invalid Regular Expression : '" + regexReplace
93 // verify format is really correct.
94 if (link.indexOf("$" + SEQUENCE_ID + "$") == psqid)
96 url_suffix = link.substring(psqid + 13);
101 invalidMessage = "Warning: invalid regex structure for URL link : "
109 sep = parseTargetAndLabel(sep, nsqid, link);
112 url_prefix = link.substring(sep + 1, nsqid);
113 if (link.indexOf("$" + SEQUENCE_NAME + "=/") == nsqid
114 && (p = link.indexOf("/=$", nsqid + 16)) > nsqid + 16)
116 // Extract Regex and suffix
117 url_suffix = link.substring(p + 3);
118 regexReplace = link.substring(nsqid + 16, p);
121 com.stevesoft.pat.Regex rg = com.stevesoft.pat.Regex.perlCode("/"
122 + regexReplace + "/");
125 invalidMessage = "Invalid Regular Expression : '"
126 + regexReplace + "'\n";
128 } catch (Exception e)
130 invalidMessage = "Invalid Regular Expression : '" + regexReplace
137 // verify format is really correct.
138 if (link.indexOf("$" + SEQUENCE_NAME + "$") == nsqid)
140 url_suffix = link.substring(nsqid + 15);
145 invalidMessage = "Warning: invalid regex structure for URL link : "
152 target = link.substring(0, sep);
153 label = link.substring(0, sep = link.lastIndexOf("|"));
154 url_prefix = link.substring(sep + 1);
155 regexReplace = null; // implies we trim any prefix if necessary //
156 // regexReplace=".*\\|?(.*)";
162 * @return the url_suffix
164 public String getUrl_suffix()
170 * @return the url_prefix
172 public String getUrl_prefix()
180 public String getTarget()
188 public String getLabel()
194 * @return the regexReplace
196 public String getRegexReplace()
202 * @return the invalidMessage
204 public String getInvalidMessage()
206 return invalidMessage;
210 * Check if URL string was parsed properly.
212 * @return boolean - if false then <code>getInvalidMessage</code> returns an
215 public boolean isValid()
217 return invalidMessage == null;
221 * return one or more URL strings by applying regex to the given idstring
224 * @param onlyIfMatches
225 * - when true url strings are only made if regex is defined and
227 * @return String[] { part of idstring substituted, full substituted url , ..
228 * next part, next url..}
230 public String[] makeUrls(String idstring, boolean onlyIfMatches)
234 if (regexReplace != null)
236 com.stevesoft.pat.Regex rg = com.stevesoft.pat.Regex.perlCode("/"
237 + regexReplace + "/");
238 if (rg.search(idstring))
240 int ns = rg.numSubs();
244 return new String[] { rg.stringMatched(),
245 url_prefix + rg.stringMatched() + url_suffix };
247 * else if (ns==1) { // take only subgroup match return new String[]
248 * { rg.stringMatched(1), url_prefix+rg.stringMatched(1)+url_suffix
254 for (int s = 0; s <= rg.numSubs(); s++)
256 System.err.println("Sub " + s + " : " + rg.matchedFrom(s)
257 + " : " + rg.matchedTo(s) + " : '"
258 + rg.stringMatched(s) + "'");
260 // try to collate subgroup matches
261 Vector subs = new Vector();
262 // have to loop through submatches, collating them at top level
267 if (s + 1 <= ns && rg.matchedTo(s) > -1
268 && rg.matchedTo(s + 1) > -1
269 && rg.matchedTo(s + 1) < rg.matchedTo(s))
271 // s is top level submatch. search for submatches enclosed by
275 while (r <= ns && rg.matchedTo(r) <= rg.matchedTo(s))
277 if (rg.matchedFrom(r) > -1)
279 mtch += rg.stringMatched(r);
283 if (mtch.length() > 0)
285 subs.addElement(mtch);
286 subs.addElement(url_prefix + mtch + url_suffix);
292 if (rg.matchedFrom(s) > -1)
294 subs.addElement(rg.stringMatched(s));
295 subs.addElement(url_prefix + rg.stringMatched(s)
302 String[] res = new String[subs.size()];
303 for (int r = 0, rs = subs.size(); r < rs; r++)
305 res[r] = (String) subs.elementAt(r);
307 subs.removeAllElements();
316 /* Otherwise - trim off any 'prefix' - pre 2.4 Jalview behaviour */
317 if (idstring.indexOf("|") > -1)
319 idstring = idstring.substring(idstring.lastIndexOf("|") + 1);
322 // just return simple url substitution.
323 return new String[] { idstring, url_prefix + idstring + url_suffix };
327 return new String[] { "", url_prefix };
332 public String toString()
337 + (dynamic ? ("$" + SEQUENCE_ID + ((regexReplace != null) ? "="
338 + regexReplace + "=$" : "$")) : "")
339 + ((url_suffix == null) ? "" : url_suffix);
343 private int parseTargetAndLabel(int sep, int psqid, String link)
349 p = link.indexOf("|", sep + 1);
350 } while (p > sep && p < psqid);
351 // Assuming that the URL itself does not contain any '|' symbols
352 // sep now contains last pipe symbol position prior to any regex symbols
353 label = link.substring(0, sep);
354 if (label.indexOf("|") > -1)
356 // | terminated database name / www target at start of Label
357 target = label.substring(0, label.indexOf("|"));
359 else if (label.indexOf(" ") > 2)
361 // space separated Label - matches database name
362 target = label.substring(0, label.indexOf(" "));
371 private static void testUrls(UrlLink ul, String idstring, String[] urls)
376 System.out.println("Created NO urls.");
380 System.out.println("Created " + (urls.length / 2) + " Urls.");
381 for (int uls = 0; uls < urls.length; uls += 2)
383 System.out.println("URL Replacement text : " + urls[uls]
384 + " : URL : " + urls[uls + 1]);
389 public static void main(String argv[])
391 String[] links = new String[] {
393 * "AlinkT|Target|http://foo.foo.soo/",
394 * "myUrl1|http://$SEQUENCE_ID=/[0-9]+/=$.someserver.org/foo",
395 * "myUrl2|http://$SEQUENCE_ID=/(([0-9]+).+([A-Za-z]+))/=$.someserver.org/foo"
397 * "myUrl3|http://$SEQUENCE_ID=/([0-9]+).+([A-Za-z]+)/=$.someserver.org/foo"
398 * , "myUrl4|target|http://$SEQUENCE_ID$.someserver.org/foo|too",
399 * "PF1|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/(?:PFAM:)?(.+)/=$"
401 * "PF2|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/(PFAM:)?(.+)/=$"
403 * "PF3|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/PFAM:(.+)/=$"
404 * , "NOTFER|http://notfer.org/$SEQUENCE_ID=/(?<!\\s)(.+)/=$",
406 "NESTED|http://nested/$" + SEQUENCE_ID
407 + "=/^(?:Label:)?(?:(?:gi\\|(\\d+))|([^:]+))/=$/nested" };
408 String[] idstrings = new String[] {
410 * //"LGUL_human", //"QWIQW_123123", "uniprot|why_do+_12313_foo",
411 * //"123123312", "123123 ABCDE foo", "PFAM:PF23943",
413 "Label:gi|9234|pdb|102L|A" };
414 // TODO: test the setLabel method.
415 for (int i = 0; i < links.length; i++)
417 UrlLink ul = new UrlLink(links[i]);
420 System.out.println("\n\n\n");
421 System.out.println("Link " + i + " " + links[i] + " : "
423 System.out.println(" pref : "
428 + ((ul.getRegexReplace() != null) ? ul.getRegexReplace()
430 for (int ids = 0; ids < idstrings.length; ids++)
432 System.out.println("ID String : " + idstrings[ids]
433 + "\nWithout onlyIfMatches:");
434 String[] urls = ul.makeUrls(idstrings[ids], false);
435 testUrls(ul, idstrings[ids], urls);
436 System.out.println("With onlyIfMatches set.");
437 urls = ul.makeUrls(idstrings[ids], true);
438 testUrls(ul, idstrings[ids], urls);
443 System.err.println("Invalid URLLink : " + links[i] + " : "
444 + ul.getInvalidMessage());
449 public boolean isDynamic()
451 // TODO Auto-generated method stub
455 public boolean usesSeqId()
460 public void setLabel(String newlabel)
462 this.label = newlabel;