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 jalview.jsdev.RegExp;
24 import jalview.jsdev.api.RegExpInterface;
26 import java.util.Vector;
28 //import com.stevesoft.pat.Regex;
33 * helper class to parse URL Link strings taken from applet parameters or
34 * jalview properties file using the Regex implementation. Jalview 2.4
35 * extension allows regular expressions to be used to parse ID strings and
36 * replace the result in the URL. Regex's operate on the whole ID string given
37 * to the matchURL method, if no regex is supplied, then only text following
38 * the first pipe symbol will be susbstituted. Usage documentation todo.
40 private String url_suffix, url_prefix, target, label, regexReplace;
42 private boolean dynamic = false;
44 private String invalidMessage = null;
47 * parse the given linkString of the form '<label>|<url>' into parts url may
48 * contain a string $SEQUENCE_ID<=optional regex=>$ where <=optional regex=>
49 * must be of the form =/<perl style regex>/=$
53 public UrlLink(String link)
55 int sep = link.indexOf("|"), psqid = link.indexOf("$SEQUENCE_ID");
63 p = link.indexOf("|", sep + 1);
64 } while (p > sep && p < psqid);
65 // Assuming that the URL itself does not contain any '|' symbols
66 // sep now contains last pipe symbol position prior to any regex symbols
67 label = link.substring(0, sep);
68 if (label.indexOf("|") > -1)
70 // | terminated database name / www target at start of Label
71 target = label.substring(0, label.indexOf("|"));
73 else if (label.indexOf(" ") > 2)
75 // space separated Label - matches database name
76 target = label.substring(0, label.indexOf(" "));
82 // Parse URL : Whole URL string first
83 url_prefix = link.substring(sep + 1, psqid);
84 if (link.indexOf("$SEQUENCE_ID=/") == psqid
85 && (p = link.indexOf("/=$", psqid + 14)) > psqid + 14)
87 // Extract Regex and suffix
88 url_suffix = link.substring(p + 3);
89 regexReplace = link.substring(psqid + 14, p);
92 RegExpInterface rg = RegExp.perlCode("/"
93 + regexReplace + "/");
96 invalidMessage = "Invalid Regular Expression : '"
97 + regexReplace + "'\n";
101 invalidMessage = "Invalid Regular Expression : '" + regexReplace
108 // verify format is really correct.
109 if (link.indexOf("$SEQUENCE_ID$") == psqid)
111 url_suffix = link.substring(psqid + 13);
116 invalidMessage = "Warning: invalid regex structure for URL link : "
123 target = link.substring(0, sep);
124 label = link.substring(0, sep = link.lastIndexOf("|"));
125 url_prefix = link.substring(sep + 1);
126 regexReplace = null; // implies we trim any prefix if necessary //
127 // regexReplace=".*\\|?(.*)";
133 * @return the url_suffix
135 public String getUrl_suffix()
141 * @return the url_prefix
143 public String getUrl_prefix()
151 public String getTarget()
159 public String getLabel()
165 * @return the regexReplace
167 public String getRegexReplace()
173 * @return the invalidMessage
175 public String getInvalidMessage()
177 return invalidMessage;
181 * Check if URL string was parsed properly.
183 * @return boolean - if false then <code>getInvalidMessage</code> returns an
186 public boolean isValid()
188 return invalidMessage == null;
192 * return one or more URL strings by applying regex to the given idstring
195 * @param onlyIfMatches
196 * - when true url strings are only made if regex is defined and
198 * @return String[] { part of idstring substituted, full substituted url , ..
199 * next part, next url..}
201 public String[] makeUrls(String idstring, boolean onlyIfMatches)
205 if (regexReplace != null)
207 RegExpInterface rg = RegExp.perlCode("/" + regexReplace + "/");
208 if (rg.search(idstring))
210 int ns = rg.numSubs();
215 { rg.stringMatchedI(ns),
216 url_prefix + rg.stringMatched() + url_suffix };
218 * else if (ns==1) { // take only subgroup match return new String[]
219 * { rg.stringMatched(1), url_prefix+rg.stringMatched(1)+url_suffix
225 for (int s = 0; s <= rg.numSubs(); s++)
227 System.err.println("Sub " + s + " : " + rg.matchedFromI(s)
228 + " : " + rg.matchedToI(s) + " : '"
229 + rg.stringMatchedI(s) + "'");
231 // try to collate subgroup matches
232 Vector subs = new Vector();
233 // have to loop through submatches, collating them at top level
238 if (s + 1 <= ns && rg.matchedToI(s) > -1
239 && rg.matchedToI(s + 1) > -1
240 && rg.matchedToI(s + 1) < rg.matchedToI(s))
242 // s is top level submatch. search for submatches enclosed by
246 while (r <= ns && rg.matchedToI(r) <= rg.matchedToI(s))
248 if (rg.matchedFromI(r) > -1)
250 mtch += rg.stringMatchedI(r);
254 if (mtch.length() > 0)
256 subs.addElement(mtch);
257 subs.addElement(url_prefix + mtch + url_suffix);
263 if (rg.matchedFromI(s) > -1)
265 subs.addElement(rg.stringMatchedI(s));
266 subs.addElement(url_prefix + rg.stringMatchedI(s)
273 String[] res = new String[subs.size()];
274 for (int r = 0, rs = subs.size(); r < rs; r++)
276 res[r] = (String) subs.elementAt(r);
278 subs.removeAllElements();
287 /* Otherwise - trim off any 'prefix' - pre 2.4 Jalview behaviour */
288 if (idstring.indexOf("|") > -1)
290 idstring = idstring.substring(idstring.lastIndexOf("|") + 1);
293 // just return simple url substitution.
295 { idstring, url_prefix + idstring + url_suffix };
304 public String toString()
309 + (dynamic ? ("$SEQUENCE_ID" + ((regexReplace != null) ? "="
310 + regexReplace + "=$" : "$")) : "")
311 + ((url_suffix == null) ? "" : url_suffix);
315 private static void testUrls(UrlLink ul, String idstring, String[] urls)
320 System.out.println("Created NO urls.");
324 System.out.println("Created " + (urls.length / 2) + " Urls.");
325 for (int uls = 0; uls < urls.length; uls += 2)
327 System.out.println("URL Replacement text : " + urls[uls]
328 + " : URL : " + urls[uls + 1]);
338 public static void main(String argv[])
340 String[] links = new String[]
343 * "AlinkT|Target|http://foo.foo.soo/",
344 * "myUrl1|http://$SEQUENCE_ID=/[0-9]+/=$.someserver.org/foo",
345 * "myUrl2|http://$SEQUENCE_ID=/(([0-9]+).+([A-Za-z]+))/=$.someserver.org/foo"
347 * "myUrl3|http://$SEQUENCE_ID=/([0-9]+).+([A-Za-z]+)/=$.someserver.org/foo"
348 * , "myUrl4|target|http://$SEQUENCE_ID$.someserver.org/foo|too",
349 * "PF1|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/(?:PFAM:)?(.+)/=$"
351 * "PF2|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/(PFAM:)?(.+)/=$"
353 * "PF3|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/PFAM:(.+)/=$"
354 * , "NOTFER|http://notfer.org/$SEQUENCE_ID=/(?<!\\s)(.+)/=$",
356 "NESTED|http://nested/$SEQUENCE_ID=/^(?:Label:)?(?:(?:gi\\|(\\d+))|([^:]+))/=$/nested" };
357 String[] idstrings = new String[]
360 * //"LGUL_human", //"QWIQW_123123", "uniprot|why_do+_12313_foo",
361 * //"123123312", "123123 ABCDE foo", "PFAM:PF23943",
363 "Label:gi|9234|pdb|102L|A" };
364 // TODO: test the setLabel method.
365 for (int i = 0; i < links.length; i++)
367 UrlLink ul = new UrlLink(links[i]);
370 System.out.println("\n\n\n");
371 System.out.println("Link " + i + " " + links[i] + " : "
373 System.out.println(" pref : "
378 + ((ul.getRegexReplace() != null) ? ul.getRegexReplace()
380 for (int ids = 0; ids < idstrings.length; ids++)
382 System.out.println("ID String : " + idstrings[ids]
383 + "\nWithout onlyIfMatches:");
384 String[] urls = ul.makeUrls(idstrings[ids], false);
385 testUrls(ul, idstrings[ids], urls);
386 System.out.println("With onlyIfMatches set.");
387 urls = ul.makeUrls(idstrings[ids], true);
388 testUrls(ul, idstrings[ids], urls);
393 System.err.println("Invalid URLLink : " + links[i] + " : "
394 + ul.getInvalidMessage());
399 public boolean isDynamic()
401 // TODO Auto-generated method stub
405 public void setLabel(String newlabel)
407 this.label = newlabel;