2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 import java.util.Vector;
26 * helper class to parse URL Link strings taken from applet parameters or
27 * jalview properties file using the com.stevesoft.pat.Regex implementation.
28 * Jalview 2.4 extension allows regular expressions to be used to parse ID
29 * strings and replace the result in the URL. Regex's operate on the whole ID
30 * string given to the matchURL method, if no regex is supplied, then only
31 * text following the first pipe symbol will be susbstituted. Usage
34 private String url_suffix, url_prefix, target, label, regexReplace;
36 private boolean dynamic = false;
38 private String invalidMessage = null;
41 * parse the given linkString of the form '<label>|<url>' into parts url may
42 * contain a string $SEQUENCE_ID<=optional regex=>$ where <=optional regex=>
43 * must be of the form =/<perl style regex>/=$
47 public UrlLink(String link)
49 int sep = link.indexOf("|"), psqid = link.indexOf("$SEQUENCE_ID");
57 p = link.indexOf("|", sep + 1);
58 } while (p > sep && p < psqid);
59 // Assuming that the URL itself does not contain any '|' symbols
60 // sep now contains last pipe symbol position prior to any regex symbols
61 label = link.substring(0, sep);
62 if (label.indexOf("|") > -1)
64 // | terminated database name / www target at start of Label
65 target = label.substring(0, label.indexOf("|"));
67 else if (label.indexOf(" ") > 2)
69 // space separated Label - matches database name
70 target = label.substring(0, label.indexOf(" "));
76 // Parse URL : Whole URL string first
77 url_prefix = link.substring(sep + 1, psqid);
78 if (link.indexOf("$SEQUENCE_ID=/") == psqid
79 && (p = link.indexOf("/=$", psqid + 14)) > psqid + 14)
81 // Extract Regex and suffix
82 url_suffix = link.substring(p + 3);
83 regexReplace = link.substring(psqid + 14, p);
86 com.stevesoft.pat.Regex rg = com.stevesoft.pat.Regex.perlCode("/"
87 + regexReplace + "/");
90 invalidMessage = "Invalid Regular Expression : '"
91 + regexReplace + "'\n";
95 invalidMessage = "Invalid Regular Expression : '" + regexReplace
102 // verify format is really correct.
103 if (link.indexOf("$SEQUENCE_ID$") == psqid)
105 url_suffix = link.substring(psqid + 13);
110 invalidMessage = "Warning: invalid regex structure for URL link : "
117 target = link.substring(0, sep);
118 label = link.substring(0, sep = link.lastIndexOf("|"));
119 url_prefix = link.substring(sep + 1);
120 regexReplace = null; // implies we trim any prefix if necessary //
121 // regexReplace=".*\\|?(.*)";
127 * @return the url_suffix
129 public String getUrl_suffix()
135 * @return the url_prefix
137 public String getUrl_prefix()
145 public String getTarget()
153 public String getLabel()
159 * @return the regexReplace
161 public String getRegexReplace()
167 * @return the invalidMessage
169 public String getInvalidMessage()
171 return invalidMessage;
175 * Check if URL string was parsed properly.
177 * @return boolean - if false then <code>getInvalidMessage</code> returns an
180 public boolean isValid()
182 return invalidMessage == null;
186 * return one or more URL strings by applying regex to the given idstring
189 * @param onlyIfMatches
190 * - when true url strings are only made if regex is defined and
192 * @return String[] { part of idstring substituted, full substituted url , ..
193 * next part, next url..}
195 public String[] makeUrls(String idstring, boolean onlyIfMatches)
199 if (regexReplace != null)
201 com.stevesoft.pat.Regex rg = com.stevesoft.pat.Regex.perlCode("/"
202 + regexReplace + "/");
203 if (rg.search(idstring))
205 int ns = rg.numSubs();
210 { rg.stringMatched(),
211 url_prefix + rg.stringMatched() + url_suffix };
213 * else if (ns==1) { // take only subgroup match return new String[]
214 * { rg.stringMatched(1), url_prefix+rg.stringMatched(1)+url_suffix
220 for (int s = 0; s <= rg.numSubs(); s++)
222 System.err.println("Sub " + s + " : " + rg.matchedFrom(s)
223 + " : " + rg.matchedTo(s) + " : '"
224 + rg.stringMatched(s) + "'");
226 // try to collate subgroup matches
227 Vector subs = new Vector();
228 // have to loop through submatches, collating them at top level
233 if (s + 1 <= ns && rg.matchedTo(s) > -1
234 && rg.matchedTo(s + 1) > -1
235 && rg.matchedTo(s + 1) < rg.matchedTo(s))
237 // s is top level submatch. search for submatches enclosed by
241 while (r <= ns && rg.matchedTo(r) <= rg.matchedTo(s))
243 if (rg.matchedFrom(r) > -1)
245 mtch += rg.stringMatched(r);
249 if (mtch.length() > 0)
251 subs.addElement(mtch);
252 subs.addElement(url_prefix + mtch + url_suffix);
258 if (rg.matchedFrom(s) > -1)
260 subs.addElement(rg.stringMatched(s));
261 subs.addElement(url_prefix + rg.stringMatched(s)
268 String[] res = new String[subs.size()];
269 for (int r = 0, rs = subs.size(); r < rs; r++)
271 res[r] = (String) subs.elementAt(r);
273 subs.removeAllElements();
282 /* Otherwise - trim off any 'prefix' - pre 2.4 Jalview behaviour */
283 if (idstring.indexOf("|") > -1)
285 idstring = idstring.substring(idstring.lastIndexOf("|") + 1);
288 // just return simple url substitution.
290 { idstring, url_prefix + idstring + url_suffix };
299 public String toString()
304 + (dynamic ? ("$SEQUENCE_ID" + ((regexReplace != null) ? "="
305 + regexReplace + "=$" : "$")) : "")
306 + ((url_suffix == null) ? "" : url_suffix);
310 private static void testUrls(UrlLink ul, String idstring, String[] urls)
315 System.out.println("Created NO urls.");
319 System.out.println("Created " + (urls.length / 2) + " Urls.");
320 for (int uls = 0; uls < urls.length; uls += 2)
322 System.out.println("URL Replacement text : " + urls[uls]
323 + " : URL : " + urls[uls + 1]);
328 public static void main(String argv[])
330 String[] links = new String[]
333 * "AlinkT|Target|http://foo.foo.soo/",
334 * "myUrl1|http://$SEQUENCE_ID=/[0-9]+/=$.someserver.org/foo",
335 * "myUrl2|http://$SEQUENCE_ID=/(([0-9]+).+([A-Za-z]+))/=$.someserver.org/foo"
337 * "myUrl3|http://$SEQUENCE_ID=/([0-9]+).+([A-Za-z]+)/=$.someserver.org/foo"
338 * , "myUrl4|target|http://$SEQUENCE_ID$.someserver.org/foo|too",
339 * "PF1|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/(?:PFAM:)?(.+)/=$"
341 * "PF2|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/(PFAM:)?(.+)/=$"
343 * "PF3|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/PFAM:(.+)/=$"
344 * , "NOTFER|http://notfer.org/$SEQUENCE_ID=/(?<!\\s)(.+)/=$",
346 "NESTED|http://nested/$SEQUENCE_ID=/^(?:Label:)?(?:(?:gi\\|(\\d+))|([^:]+))/=$/nested" };
347 String[] idstrings = new String[]
350 * //"LGUL_human", //"QWIQW_123123", "uniprot|why_do+_12313_foo",
351 * //"123123312", "123123 ABCDE foo", "PFAM:PF23943",
353 "Label:gi|9234|pdb|102L|A" };
354 // TODO: test the setLabel method.
355 for (int i = 0; i < links.length; i++)
357 UrlLink ul = new UrlLink(links[i]);
360 System.out.println("\n\n\n");
361 System.out.println("Link " + i + " " + links[i] + " : "
363 System.out.println(" pref : "
368 + ((ul.getRegexReplace() != null) ? ul.getRegexReplace()
370 for (int ids = 0; ids < idstrings.length; ids++)
372 System.out.println("ID String : " + idstrings[ids]
373 + "\nWithout onlyIfMatches:");
374 String[] urls = ul.makeUrls(idstrings[ids], false);
375 testUrls(ul, idstrings[ids], urls);
376 System.out.println("With onlyIfMatches set.");
377 urls = ul.makeUrls(idstrings[ids], true);
378 testUrls(ul, idstrings[ids], urls);
383 System.err.println("Invalid URLLink : " + links[i] + " : "
384 + ul.getInvalidMessage());
389 public boolean isDynamic()
391 // TODO Auto-generated method stub
395 public void setLabel(String newlabel)
397 this.label = newlabel;