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 parseUrl(link, SEQUENCE_ID, psqid, sep);
71 sep = parseTargetAndLabel(sep, nsqid, link);
73 parseUrl(link, SEQUENCE_NAME, nsqid, sep);
77 target = link.substring(0, sep);
78 sep = link.lastIndexOf("|");
79 label = link.substring(0, sep);
80 url_prefix = link.substring(sep + 1);
81 regexReplace = null; // implies we trim any prefix if necessary //
82 // regexReplace=".*\\|?(.*)";
87 target = target.trim();
88 target = target.toUpperCase(); // DBRefEntry uppercases DB names
89 // NB getCanonicalName might be better but does not currently change case
93 * @return the url_suffix
95 public String getUrl_suffix()
101 * @return the url_prefix
103 public String getUrl_prefix()
111 public String getTarget()
119 public String getLabel()
125 * @return the regexReplace
127 public String getRegexReplace()
133 * @return the invalidMessage
135 public String getInvalidMessage()
137 return invalidMessage;
141 * Check if URL string was parsed properly.
143 * @return boolean - if false then <code>getInvalidMessage</code> returns an
146 public boolean isValid()
148 return invalidMessage == null;
152 * return one or more URL strings by applying regex to the given idstring
155 * @param onlyIfMatches
156 * - when true url strings are only made if regex is defined and
158 * @return String[] { part of idstring substituted, full substituted url , ..
159 * next part, next url..}
161 public String[] makeUrls(String idstring, boolean onlyIfMatches)
165 if (regexReplace != null)
167 com.stevesoft.pat.Regex rg = com.stevesoft.pat.Regex.perlCode("/"
168 + regexReplace + "/");
169 if (rg.search(idstring))
171 int ns = rg.numSubs();
175 return new String[] { rg.stringMatched(),
176 url_prefix + rg.stringMatched() + url_suffix };
178 * else if (ns==1) { // take only subgroup match return new String[]
179 * { rg.stringMatched(1), url_prefix+rg.stringMatched(1)+url_suffix
185 for (int s = 0; s <= rg.numSubs(); s++)
187 System.err.println("Sub " + s + " : " + rg.matchedFrom(s)
188 + " : " + rg.matchedTo(s) + " : '"
189 + rg.stringMatched(s) + "'");
191 // try to collate subgroup matches
192 Vector subs = new Vector();
193 // have to loop through submatches, collating them at top level
198 if (s + 1 <= ns && rg.matchedTo(s) > -1
199 && rg.matchedTo(s + 1) > -1
200 && rg.matchedTo(s + 1) < rg.matchedTo(s))
202 // s is top level submatch. search for submatches enclosed by
206 while (r <= ns && rg.matchedTo(r) <= rg.matchedTo(s))
208 if (rg.matchedFrom(r) > -1)
210 mtch += rg.stringMatched(r);
214 if (mtch.length() > 0)
216 subs.addElement(mtch);
217 subs.addElement(url_prefix + mtch + url_suffix);
223 if (rg.matchedFrom(s) > -1)
225 subs.addElement(rg.stringMatched(s));
226 subs.addElement(url_prefix + rg.stringMatched(s)
233 String[] res = new String[subs.size()];
234 for (int r = 0, rs = subs.size(); r < rs; r++)
236 res[r] = (String) subs.elementAt(r);
238 subs.removeAllElements();
247 /* Otherwise - trim off any 'prefix' - pre 2.4 Jalview behaviour */
248 if (idstring.indexOf("|") > -1)
250 idstring = idstring.substring(idstring.lastIndexOf("|") + 1);
253 // just return simple url substitution.
254 return new String[] { idstring, url_prefix + idstring + url_suffix };
258 return new String[] { "", url_prefix };
263 public String toString()
265 String var = (uses_seq_id ? SEQUENCE_ID : SEQUENCE_NAME);
270 + (dynamic ? ("$" + var + ((regexReplace != null) ? "="
271 + regexReplace + "=$" : "$")) : "")
272 + ((url_suffix == null) ? "" : url_suffix);
278 * Location of first occurrence of separator in link string
280 * Position of sequence id or name in link string
282 * Link string containing database name and url
283 * @return Position of last separator symbol prior to any regex symbols
285 protected int parseTargetAndLabel(int firstSep, int psqid, String link)
292 p = link.indexOf("|", sep + 1);
293 } while (p > sep && p < psqid);
294 // Assuming that the URL itself does not contain any '|' symbols
295 // sep now contains last pipe symbol position prior to any regex symbols
296 label = link.substring(0, sep);
297 if (label.indexOf("|") > -1)
299 // | terminated database name / www target at start of Label
300 target = label.substring(0, label.indexOf("|"));
302 else if (label.indexOf(" ") > 2)
304 // space separated Label - matches database name
305 target = label.substring(0, label.indexOf(" "));
315 * Parse the URL part of the link string
318 * Link string containing database name and url
320 * Name of variable in url string (e.g. SEQUENCE_ID, SEQUENCE_NAME)
322 * Position of id or name in link string
324 * Position of separator in link string
326 protected void parseUrl(String link, String varName, int sqidPos, int sep)
328 url_prefix = link.substring(sep + 1, sqidPos);
330 // delimiter at start of regex: e.g. $SEQUENCE_ID=/
331 String startDelimiter = "$" + varName + "=/";
333 // delimiter at end of regex: /=$
334 String endDelimiter = "/=$";
336 int startLength = startDelimiter.length();
338 // Parse URL : Whole URL string first
339 int p = link.indexOf(endDelimiter, sqidPos + startLength);
341 if (link.indexOf(startDelimiter) == sqidPos
342 && (p > sqidPos + startLength))
344 // Extract Regex and suffix
345 url_suffix = link.substring(p + endDelimiter.length());
346 regexReplace = link.substring(sqidPos + startLength, p);
349 com.stevesoft.pat.Regex rg = com.stevesoft.pat.Regex.perlCode("/"
350 + regexReplace + "/");
353 invalidMessage = "Invalid Regular Expression : '" + regexReplace
356 } catch (Exception e)
358 invalidMessage = "Invalid Regular Expression : '" + regexReplace
366 // verify format is really correct.
367 if (link.indexOf("$" + varName + "$") == sqidPos)
369 url_suffix = link.substring(sqidPos + startLength - 1);
374 invalidMessage = "Warning: invalid regex structure for URL link : "
380 private static void testUrls(UrlLink ul, String idstring, String[] urls)
385 System.out.println("Created NO urls.");
389 System.out.println("Created " + (urls.length / 2) + " Urls.");
390 for (int uls = 0; uls < urls.length; uls += 2)
392 System.out.println("URL Replacement text : " + urls[uls]
393 + " : URL : " + urls[uls + 1]);
398 public static void main(String argv[])
400 String[] links = new String[] {
402 * "AlinkT|Target|http://foo.foo.soo/",
403 * "myUrl1|http://$SEQUENCE_ID=/[0-9]+/=$.someserver.org/foo",
404 * "myUrl2|http://$SEQUENCE_ID=/(([0-9]+).+([A-Za-z]+))/=$.someserver.org/foo"
406 * "myUrl3|http://$SEQUENCE_ID=/([0-9]+).+([A-Za-z]+)/=$.someserver.org/foo"
407 * , "myUrl4|target|http://$SEQUENCE_ID$.someserver.org/foo|too",
408 * "PF1|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/(?:PFAM:)?(.+)/=$"
410 * "PF2|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/(PFAM:)?(.+)/=$"
412 * "PF3|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/PFAM:(.+)/=$"
413 * , "NOTFER|http://notfer.org/$SEQUENCE_ID=/(?<!\\s)(.+)/=$",
415 "NESTED|http://nested/$" + SEQUENCE_ID
416 + "=/^(?:Label:)?(?:(?:gi\\|(\\d+))|([^:]+))/=$/nested" };
417 String[] idstrings = new String[] {
419 * //"LGUL_human", //"QWIQW_123123", "uniprot|why_do+_12313_foo",
420 * //"123123312", "123123 ABCDE foo", "PFAM:PF23943",
422 "Label:gi|9234|pdb|102L|A" };
423 // TODO: test the setLabel method.
424 for (int i = 0; i < links.length; i++)
426 UrlLink ul = new UrlLink(links[i]);
429 System.out.println("\n\n\n");
430 System.out.println("Link " + i + " " + links[i] + " : "
432 System.out.println(" pref : "
437 + ((ul.getRegexReplace() != null) ? ul.getRegexReplace()
439 for (int ids = 0; ids < idstrings.length; ids++)
441 System.out.println("ID String : " + idstrings[ids]
442 + "\nWithout onlyIfMatches:");
443 String[] urls = ul.makeUrls(idstrings[ids], false);
444 testUrls(ul, idstrings[ids], urls);
445 System.out.println("With onlyIfMatches set.");
446 urls = ul.makeUrls(idstrings[ids], true);
447 testUrls(ul, idstrings[ids], urls);
452 System.err.println("Invalid URLLink : " + links[i] + " : "
453 + ul.getInvalidMessage());
458 public boolean isDynamic()
460 // TODO Auto-generated method stub
464 public boolean usesSeqId()
469 public void setLabel(String newlabel)
471 this.label = newlabel;