2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
\r
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.util;
\r
21 import java.util.Vector;
\r
23 public class UrlLink
\r
26 * helper class to parse URL Link strings taken from applet parameters or
\r
27 * jalview properties file using the com.stevesoft.pat.Regex implementation.
\r
28 * Jalview 2.4 extension allows regular expressions to be used to parse ID
\r
29 * strings and replace the result in the URL. Regex's operate on the whole ID
\r
30 * string given to the matchURL method, if no regex is supplied, then only
\r
31 * text following the first pipe symbol will be susbstituted.
\r
33 private String url_suffix, url_prefix, target, label, regexReplace;
\r
35 private boolean dynamic = false;
\r
37 private String invalidMessage = null;
\r
40 * parse the given linkString of the form '<label>|<url>' into parts url may
\r
41 * contain a string $SEQUENCE_ID<=optional regex=>$ where <=optional regex=>
\r
42 * must be of the form =/<perl style regex>/=$
\r
46 public UrlLink(String link)
\r
48 int sep = link.indexOf("|"), psqid = link.indexOf("$SEQUENCE_ID");
\r
56 p = link.indexOf("|", sep + 1);
\r
57 } while (p > sep && p < psqid);
\r
58 // Assuming that the URL itself does not contain any '|' symbols
\r
59 // sep now contains last pipe symbol position prior to any regex symbols
\r
60 label = link.substring(0, sep);
\r
61 if (label.indexOf("|") > -1)
\r
63 // | terminated database name / www target at start of Label
\r
64 target = label.substring(0, label.indexOf("|"));
\r
66 else if (label.indexOf(" ") > 2)
\r
68 // space separated Label - matches database name
\r
69 target = label.substring(0, label.indexOf(" "));
\r
75 // Parse URL : Whole URL string first
\r
76 url_prefix = link.substring(sep + 1, psqid);
\r
77 if (link.indexOf("$SEQUENCE_ID=/") == psqid
\r
78 && (p = link.indexOf("/=$", psqid + 14)) > psqid + 14)
\r
80 // Extract Regex and suffix
\r
81 url_suffix = link.substring(p + 3);
\r
82 regexReplace = link.substring(psqid + 14, p);
\r
85 com.stevesoft.pat.Regex rg = com.stevesoft.pat.Regex.perlCode("/"
\r
86 + regexReplace + "/");
\r
89 invalidMessage = "Invalid Regular Expression : '"
\r
90 + regexReplace + "'\n";
\r
92 } catch (Exception e)
\r
94 invalidMessage = "Invalid Regular Expression : '" + regexReplace
\r
100 regexReplace = null;
\r
101 // verify format is really correct.
\r
102 if (link.indexOf("$SEQUENCE_ID$") == psqid)
\r
104 url_suffix = link.substring(psqid + 13);
\r
105 regexReplace = null;
\r
109 invalidMessage = "Warning: invalid regex structure for URL link : "
\r
116 target = link.substring(0, sep);
\r
117 label = link.substring(0, sep = link.lastIndexOf("|"));
\r
118 url_prefix = link.substring(sep + 1);
\r
119 regexReplace = null; // implies we trim any prefix if necessary //
\r
120 // regexReplace=".*\\|?(.*)";
\r
126 * @return the url_suffix
\r
128 public String getUrl_suffix()
\r
134 * @return the url_prefix
\r
136 public String getUrl_prefix()
\r
142 * @return the target
\r
144 public String getTarget()
\r
150 * @return the label
\r
152 public String getLabel()
\r
158 * @return the regexReplace
\r
160 public String getRegexReplace()
\r
162 return regexReplace;
\r
166 * @return the invalidMessage
\r
168 public String getInvalidMessage()
\r
170 return invalidMessage;
\r
175 * @return true if URL string could not be parsed properly.
\r
177 public boolean isValid()
\r
179 return invalidMessage == null;
\r
183 * return one or more URL strings by applying regex to the given idstring
\r
186 * @param onlyIfMatches -
\r
187 * when true url strings are only made if regex is defined and
\r
189 * @return String[] { part of idstring substituted, full substituted url , ..
\r
190 * next part, next url..}
\r
192 public String[] makeUrls(String idstring, boolean onlyIfMatches)
\r
196 if (regexReplace != null)
\r
198 com.stevesoft.pat.Regex rg = com.stevesoft.pat.Regex.perlCode("/"
\r
199 + regexReplace + "/");
\r
200 if (rg.search(idstring))
\r
202 int ns = rg.numSubs();
\r
205 // take whole regex
\r
206 return new String[]
\r
207 { rg.stringMatched(),
\r
208 url_prefix + rg.stringMatched() + url_suffix };
\r
210 * else if (ns==1) { // take only subgroup match return new String[] {
\r
211 * rg.stringMatched(1), url_prefix+rg.stringMatched(1)+url_suffix }; }
\r
216 for (int s = 0; s <= rg.numSubs(); s++)
\r
218 System.err.println("Sub " + s + " : " + rg.matchedFrom(s)
\r
219 + " : " + rg.matchedTo(s) + " : '"
\r
220 + rg.stringMatched(s) + "'");
\r
222 // try to collate subgroup matches
\r
223 Vector subs = new Vector();
\r
224 // have to loop through submatches, collating them at top level
\r
229 if (s + 1 <= ns && rg.matchedTo(s) > -1
\r
230 && rg.matchedTo(s + 1) > -1
\r
231 && rg.matchedTo(s + 1) < rg.matchedTo(s))
\r
233 // s is top level submatch. search for submatches enclosed by
\r
237 while (r <= ns && rg.matchedTo(r) <= rg.matchedTo(s))
\r
239 if (rg.matchedFrom(r) > -1)
\r
241 mtch += rg.stringMatched(r);
\r
245 if (mtch.length() > 0)
\r
247 subs.addElement(mtch);
\r
248 subs.addElement(url_prefix + mtch + url_suffix);
\r
254 if (rg.matchedFrom(s) > -1)
\r
256 subs.addElement(rg.stringMatched(s));
\r
257 subs.addElement(url_prefix + rg.stringMatched(s)
\r
264 String[] res = new String[subs.size()];
\r
265 for (int r = 0, rs = subs.size(); r < rs; r++)
\r
267 res[r] = (String) subs.elementAt(r);
\r
269 subs.removeAllElements();
\r
278 /* Otherwise - trim off any 'prefix' - pre 2.4 Jalview behaviour */
\r
279 if (idstring.indexOf("|") > -1)
\r
281 idstring = idstring.substring(idstring.lastIndexOf("|") + 1);
\r
284 // just return simple url substitution.
\r
285 return new String[]
\r
286 { idstring, url_prefix + idstring + url_suffix };
\r
290 return new String[]
\r
291 { "", url_prefix };
\r
295 public String toString()
\r
300 + (dynamic ? ("$SEQUENCE_ID" + ((regexReplace != null) ? "="
\r
301 + regexReplace + "=$" : "$")) : "")
\r
302 + ((url_suffix == null) ? "" : url_suffix);
\r
306 private static void testUrls(UrlLink ul, String idstring, String[] urls)
\r
311 System.out.println("Created NO urls.");
\r
315 System.out.println("Created " + (urls.length / 2) + " Urls.");
\r
316 for (int uls = 0; uls < urls.length; uls += 2)
\r
318 System.out.println("URL Replacement text : " + urls[uls]
\r
319 + " : URL : " + urls[uls + 1]);
\r
324 public static void main(String argv[])
\r
326 String[] links = new String[]
\r
329 * "AlinkT|Target|http://foo.foo.soo/",
\r
330 * "myUrl1|http://$SEQUENCE_ID=/[0-9]+/=$.someserver.org/foo",
\r
331 * "myUrl2|http://$SEQUENCE_ID=/(([0-9]+).+([A-Za-z]+))/=$.someserver.org/foo",
\r
332 * "myUrl3|http://$SEQUENCE_ID=/([0-9]+).+([A-Za-z]+)/=$.someserver.org/foo",
\r
333 * "myUrl4|target|http://$SEQUENCE_ID$.someserver.org/foo|too",
\r
334 * "PF1|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/(?:PFAM:)?(.+)/=$",
\r
335 * "PF2|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/(PFAM:)?(.+)/=$",
\r
336 * "PF3|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/PFAM:(.+)/=$",
\r
337 * "NOTFER|http://notfer.org/$SEQUENCE_ID=/(?<!\\s)(.+)/=$",
\r
339 "NESTED|http://nested/$SEQUENCE_ID=/^(?:Label:)?(?:(?:gi\\|(\\d+))|([^:]+))/=$/nested" };
\r
340 String[] idstrings = new String[]
\r
343 * //"LGUL_human", //"QWIQW_123123", "uniprot|why_do+_12313_foo",
\r
344 * //"123123312", "123123 ABCDE foo", "PFAM:PF23943",
\r
346 "Label:gi|9234|pdb|102L|A" };
\r
347 // TODO: test the setLabel method.
\r
348 for (int i = 0; i < links.length; i++)
\r
350 UrlLink ul = new UrlLink(links[i]);
\r
353 System.out.println("\n\n\n");
\r
354 System.out.println("Link " + i + " " + links[i] + " : "
\r
356 System.out.println(" pref : "
\r
357 + ul.getUrl_prefix()
\r
359 + ul.getUrl_suffix()
\r
361 + ((ul.getRegexReplace() != null) ? ul.getRegexReplace()
\r
363 for (int ids = 0; ids < idstrings.length; ids++)
\r
365 System.out.println("ID String : " + idstrings[ids]
\r
366 + "\nWithout onlyIfMatches:");
\r
367 String[] urls = ul.makeUrls(idstrings[ids], false);
\r
368 testUrls(ul, idstrings[ids], urls);
\r
369 System.out.println("With onlyIfMatches set.");
\r
370 urls = ul.makeUrls(idstrings[ids], true);
\r
371 testUrls(ul, idstrings[ids], urls);
\r
376 System.err.println("Invalid URLLink : " + links[i] + " : "
\r
377 + ul.getInvalidMessage());
\r
382 public boolean isDynamic()
\r
384 // TODO Auto-generated method stub
\r
388 public void setLabel(String newlabel)
\r
390 this.label = newlabel;
\r