X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2FUrlLink.java;h=429709097569f5c7db4bfb4a46df116f3e86e747;hb=fc9aecdad8fb4d6ac38e504b094b29cf2223b32b;hp=a6ef8d235753b0f0ca007f904a2c38eeca03f7c4;hpb=838e4f91d4a53dd315640dbc9ff6ef7a815ee576;p=jalview.git diff --git a/src/jalview/util/UrlLink.java b/src/jalview/util/UrlLink.java index a6ef8d2..872f432 100644 --- a/src/jalview/util/UrlLink.java +++ b/src/jalview/util/UrlLink.java @@ -1,6 +1,6 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b1) - * Copyright (C) 2015 The Jalview Authors + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors * * This file is part of Jalview. * @@ -20,6 +20,9 @@ */ package jalview.util; +import static jalview.util.UrlConstants.SEQUENCE_ID; +import static jalview.util.UrlConstants.SEQUENCE_NAME; + import java.util.Vector; public class UrlLink @@ -37,6 +40,8 @@ public class UrlLink private boolean dynamic = false; + private boolean uses_seq_id = false; + private String invalidMessage = null; /** @@ -48,81 +53,40 @@ public class UrlLink */ public UrlLink(String link) { - int sep = link.indexOf("|"), psqid = link.indexOf("$SEQUENCE_ID"); + int sep = link.indexOf("|"); + int psqid = link.indexOf("$" + SEQUENCE_ID); + int nsqid = link.indexOf("$" + SEQUENCE_NAME); if (psqid > -1) { dynamic = true; - int p = sep; - do - { - sep = p; - p = link.indexOf("|", sep + 1); - } while (p > sep && p < psqid); - // Assuming that the URL itself does not contain any '|' symbols - // sep now contains last pipe symbol position prior to any regex symbols - label = link.substring(0, sep); - if (label.indexOf("|") > -1) - { - // | terminated database name / www target at start of Label - target = label.substring(0, label.indexOf("|")); - } - else if (label.indexOf(" ") > 2) - { - // space separated Label - matches database name - target = label.substring(0, label.indexOf(" ")); - } - else - { - target = label; - } - // Parse URL : Whole URL string first - url_prefix = link.substring(sep + 1, psqid); - if (link.indexOf("$SEQUENCE_ID=/") == psqid - && (p = link.indexOf("/=$", psqid + 14)) > psqid + 14) - { - // Extract Regex and suffix - url_suffix = link.substring(p + 3); - regexReplace = link.substring(psqid + 14, p); - try - { - com.stevesoft.pat.Regex rg = com.stevesoft.pat.Regex.perlCode("/" - + regexReplace + "/"); - if (rg == null) - { - invalidMessage = "Invalid Regular Expression : '" - + regexReplace + "'\n"; - } - } catch (Exception e) - { - invalidMessage = "Invalid Regular Expression : '" + regexReplace - + "'\n"; - } - } - else - { - regexReplace = null; - // verify format is really correct. - if (link.indexOf("$SEQUENCE_ID$") == psqid) - { - url_suffix = link.substring(psqid + 13); - regexReplace = null; - } - else - { - invalidMessage = "Warning: invalid regex structure for URL link : " - + link; - } - } + uses_seq_id = true; + + sep = parseTargetAndLabel(sep, psqid, link); + + parseUrl(link, SEQUENCE_ID, psqid, sep); + } + else if (nsqid > -1) + { + dynamic = true; + sep = parseTargetAndLabel(sep, nsqid, link); + + parseUrl(link, SEQUENCE_NAME, nsqid, sep); } else { target = link.substring(0, sep); - label = link.substring(0, sep = link.lastIndexOf("|")); + sep = link.lastIndexOf("|"); + label = link.substring(0, sep); url_prefix = link.substring(sep + 1); regexReplace = null; // implies we trim any prefix if necessary // // regexReplace=".*\\|?(.*)"; url_suffix = null; } + + label = label.trim(); + target = target.trim(); + target = target.toUpperCase(); // DBRefEntry uppercases DB names + // NB getCanonicalName might be better but does not currently change case } /** @@ -295,15 +259,122 @@ public class UrlLink } } + @Override public String toString() { + String var = (uses_seq_id ? SEQUENCE_ID : SEQUENCE_NAME); + return label + "|" + url_prefix - + (dynamic ? ("$SEQUENCE_ID" + ((regexReplace != null) ? "=" + + (dynamic ? ("$" + var + ((regexReplace != null) ? "=" + regexReplace + "=$" : "$")) : "") + ((url_suffix == null) ? "" : url_suffix); + } + + /** + * + * @param firstSep + * Location of first occurrence of separator in link string + * @param psqid + * Position of sequence id or name in link string + * @param link + * Link string containing database name and url + * @return Position of last separator symbol prior to any regex symbols + */ + protected int parseTargetAndLabel(int firstSep, int psqid, String link) + { + int p = firstSep; + int sep = firstSep; + do + { + sep = p; + p = link.indexOf("|", sep + 1); + } while (p > sep && p < psqid); + // Assuming that the URL itself does not contain any '|' symbols + // sep now contains last pipe symbol position prior to any regex symbols + label = link.substring(0, sep); + if (label.indexOf("|") > -1) + { + // | terminated database name / www target at start of Label + target = label.substring(0, label.indexOf("|")); + } + else if (label.indexOf(" ") > 2) + { + // space separated Label - matches database name + target = label.substring(0, label.indexOf(" ")); + } + else + { + target = label; + } + return sep; + } + + /** + * Parse the URL part of the link string + * + * @param link + * Link string containing database name and url + * @param varName + * Name of variable in url string (e.g. SEQUENCE_ID, SEQUENCE_NAME) + * @param sqidPos + * Position of id or name in link string + * @param sep + * Position of separator in link string + */ + protected void parseUrl(String link, String varName, int sqidPos, int sep) + { + url_prefix = link.substring(sep + 1, sqidPos); + + // delimiter at start of regex: e.g. $SEQUENCE_ID=/ + String startDelimiter = "$" + varName + "=/"; + // delimiter at end of regex: /=$ + String endDelimiter = "/=$"; + + int startLength = startDelimiter.length(); + + // Parse URL : Whole URL string first + int p = link.indexOf(endDelimiter, sqidPos + startLength); + + if (link.indexOf(startDelimiter) == sqidPos + && (p > sqidPos + startLength)) + { + // Extract Regex and suffix + url_suffix = link.substring(p + endDelimiter.length()); + regexReplace = link.substring(sqidPos + startLength, p); + try + { + com.stevesoft.pat.Regex rg = com.stevesoft.pat.Regex.perlCode("/" + + regexReplace + "/"); + if (rg == null) + { + invalidMessage = "Invalid Regular Expression : '" + regexReplace + + "'\n"; + } + } catch (Exception e) + { + invalidMessage = "Invalid Regular Expression : '" + regexReplace + + "'\n"; + } + } + else + { + // no regex + regexReplace = null; + // verify format is really correct. + if (link.indexOf("$" + varName + "$") == sqidPos) + { + url_suffix = link.substring(sqidPos + startLength - 1); + regexReplace = null; + } + else + { + invalidMessage = "Warning: invalid regex structure for URL link : " + + link; + } + } } private static void testUrls(UrlLink ul, String idstring, String[] urls) @@ -341,7 +412,8 @@ public class UrlLink * "PF3|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/PFAM:(.+)/=$" * , "NOTFER|http://notfer.org/$SEQUENCE_ID=/(?