X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2FUrlLink.java;fp=src%2Fjalview%2Futil%2FUrlLink.java;h=f9dda09c5ab2f96e69cf09ecc4dc8387ef12ecee;hb=e2e909ef04c5ec3d5b87cd6cc6fe5d6cc4cb3ac6;hp=a8c0b324df11e44136ade1acaa6c18dc0efb3bf2;hpb=b8961bc1a1c973c507489abb91572c73edcda1e2;p=jalview.git diff --git a/src/jalview/util/UrlLink.java b/src/jalview/util/UrlLink.java index a8c0b32..f9dda09 100644 --- a/src/jalview/util/UrlLink.java +++ b/src/jalview/util/UrlLink.java @@ -63,99 +63,29 @@ public class UrlLink sep = parseTargetAndLabel(sep, psqid, link); - // Parse URL : Whole URL string first - int p; - 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; - } - } + parseUrl(link, SEQUENCE_ID, psqid, sep); } else if (nsqid > -1) { dynamic = true; sep = parseTargetAndLabel(sep, nsqid, link); - int p; - url_prefix = link.substring(sep + 1, nsqid); - if (link.indexOf("$" + SEQUENCE_NAME + "=/") == nsqid - && (p = link.indexOf("/=$", nsqid + 16)) > nsqid + 16) - { - // Extract Regex and suffix - url_suffix = link.substring(p + 3); - regexReplace = link.substring(nsqid + 16, 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_NAME + "$") == nsqid) - { - url_suffix = link.substring(nsqid + 15); - regexReplace = null; - } - else - { - invalidMessage = "Warning: invalid regex structure for URL link : " - + 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 } /** @@ -340,9 +270,20 @@ public class UrlLink } - private int parseTargetAndLabel(int sep, int psqid, String link) + /** + * + * @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 = sep; + int p = firstSep; + int sep = firstSep; do { sep = p; @@ -368,6 +309,72 @@ public class UrlLink 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) {