From: kiramt Date: Tue, 10 Jan 2017 13:40:32 +0000 (+0000) Subject: JAL-2316 Separate label/db name in UrlProviders. Unit test updates. X-Git-Tag: Release_2_10_3b1~346^2~9 X-Git-Url: http://source.jalview.org/gitweb/?p=jalview.git;a=commitdiff_plain;h=64f19a3315b24ae0084cdc5ca8d47c9485040588 JAL-2316 Separate label/db name in UrlProviders. Unit test updates. --- diff --git a/src/jalview/urls/CustomUrlProvider.java b/src/jalview/urls/CustomUrlProvider.java index b10d816..07f4068 100644 --- a/src/jalview/urls/CustomUrlProvider.java +++ b/src/jalview/urls/CustomUrlProvider.java @@ -37,7 +37,6 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.StringTokenizer; -import java.util.Vector; /** * @@ -167,15 +166,15 @@ public class CustomUrlProvider extends UrlProviderImpl { urls.remove(SRS_LABEL); UrlLink link = new UrlLink(UrlConstants.DEFAULT_STRING); - link.setDescription(UrlConstants.DEFAULT_LABEL); + link.setLabel(UrlConstants.DEFAULT_LABEL); urls.put(UrlConstants.DEFAULT_LABEL, link); } } @Override - public Vector getLinksForMenu() + public List getLinksForMenu() { - Vector links = new Vector(); + List links = new ArrayList(); Iterator> it = selectedUrls.entrySet() .iterator(); while (it.hasNext()) @@ -298,14 +297,14 @@ public class CustomUrlProvider extends UrlProviderImpl if (link.getIsSelected()) { selurls.put(link.getId(), - new UrlLink(link.getName(), link.getUrl(), link.getName())); + new UrlLink(link.getDescription(), link.getUrl(), link.getDescription())); } else { unselurls .put(link.getId(), - new UrlLink(link.getName(), link.getUrl(), link - .getName())); + new UrlLink(link.getDescription(), link.getUrl(), link + .getDescription())); } // sort out primary and selected ids if (link.getIsPrimary()) @@ -327,7 +326,7 @@ public class CustomUrlProvider extends UrlProviderImpl && (!selectedUrls.containsKey(UrlConstants.DEFAULT_LABEL))) { UrlLink link = new UrlLink(UrlConstants.DEFAULT_STRING); - link.setDescription(UrlConstants.DEFAULT_LABEL); + link.setLabel(UrlConstants.DEFAULT_LABEL); selectedUrls.put(UrlConstants.DEFAULT_LABEL, link); } primaryUrl = UrlConstants.DEFAULT_LABEL; diff --git a/src/jalview/urls/IdentifiersUrlProvider.java b/src/jalview/urls/IdentifiersUrlProvider.java index 0219b54..a966b07 100644 --- a/src/jalview/urls/IdentifiersUrlProvider.java +++ b/src/jalview/urls/IdentifiersUrlProvider.java @@ -35,7 +35,6 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.StringTokenizer; -import java.util.Vector; import org.json.simple.JSONArray; import org.json.simple.JSONObject; @@ -170,12 +169,12 @@ public class IdentifiersUrlProvider extends UrlProviderImpl } @Override - public Vector getLinksForMenu() + public List getLinksForMenu() { - Vector links = new Vector(); + List links = new ArrayList(); for (String key : selectedUrls) { - links.add(urls.get(key).toString()); + links.add(urls.get(key).toStringWithTarget()); } return links; } diff --git a/src/jalview/urls/UrlLinkDisplay.java b/src/jalview/urls/UrlLinkDisplay.java index 814370e..0eabff7 100644 --- a/src/jalview/urls/UrlLinkDisplay.java +++ b/src/jalview/urls/UrlLinkDisplay.java @@ -87,14 +87,14 @@ public class UrlLinkDisplay return id; } - public String getName() + public String getDescription() { return link.getLabel(); } public String getDBName() { - return link.getDBName(); + return link.getTarget(); } public String getUrl() @@ -112,19 +112,19 @@ public class UrlLinkDisplay return isSelected; } - public void setName(String name) + public void setDBName(String name) { - link.setLabel(name); + link.setTarget(name); } public void setUrl(String rowUrl) { - link = new UrlLink(getName(), rowUrl, getName()); + link = new UrlLink(getDescription(), rowUrl, getDBName()); } public void setDescription(String desc) { - link.setDescription(desc); + link.setLabel(desc); } public void setIsDefault(boolean rowDefault) @@ -150,7 +150,7 @@ public class UrlLinkDisplay case SELECTED: return isSelected; case NAME: - return getName(); + return getDescription(); case DATABASE: return getDBName(); default: @@ -177,7 +177,7 @@ public class UrlLinkDisplay case NAME: setDescription((String) value); case DATABASE: - setName((String) value); + setDBName((String) value); break; default: // do nothing diff --git a/src/jalview/util/UrlLink.java b/src/jalview/util/UrlLink.java index d95edb2..0529c73 100644 --- a/src/jalview/util/UrlLink.java +++ b/src/jalview/util/UrlLink.java @@ -45,6 +45,10 @@ public class UrlLink * documentation todo. */ + private static final String EQUALS = "="; + + private static final String SPACE = " "; + private String urlSuffix; private String urlPrefix; @@ -80,23 +84,38 @@ public class UrlLink dynamic = true; usesDBaccession = true; - sep = parseTargetAndLabel(sep, psqid, link); + sep = parseLabel(sep, psqid, link); - parseUrl(link, DB_ACCESSION, psqid, sep); + int endOfRegex = parseUrl(link, DB_ACCESSION, psqid, sep); + parseTarget(link, sep, endOfRegex); } else if (nsqid > -1) { dynamic = true; - sep = parseTargetAndLabel(sep, nsqid, link); + sep = parseLabel(sep, nsqid, link); + + int endOfRegex = parseUrl(link, SEQUENCE_ID, nsqid, sep); - parseUrl(link, SEQUENCE_ID, nsqid, sep); + parseTarget(link, sep, endOfRegex); } else { - target = link.substring(0, sep); - sep = link.lastIndexOf(SEP); - label = link.substring(0, sep); - urlPrefix = link.substring(sep + 1).trim(); + label = link.substring(0, sep).trim(); + + // if there's a third element in the url link string + // it is the target name, otherwise target=label + int lastsep = link.lastIndexOf(SEP); + if (lastsep != sep) + { + urlPrefix = link.substring(sep + 1, lastsep).trim(); + target = link.substring(lastsep + 1).trim(); + } + else + { + urlPrefix = link.substring(sep + 1).trim(); + target = label; + } + regexReplace = null; // implies we trim any prefix if necessary // urlSuffix = null; } @@ -117,14 +136,13 @@ public class UrlLink */ public UrlLink(String name, String url, String desc) { - this(name + SEP + url); - dbname = desc; + this(name + SEP + url + SEP + desc); } /** * @return the url_suffix */ - public String getUrl_suffix() + public String getUrlSuffix() { return urlSuffix; } @@ -132,7 +150,7 @@ public class UrlLink /** * @return the url_prefix */ - public String getUrl_prefix() + public String getUrlPrefix() { return urlPrefix; } @@ -153,21 +171,13 @@ public class UrlLink return label; } - /** - * @return the name of this link's associated database - */ - public String getDBName() - { - return dbname; - } - public String getUrlWithToken() { String var = (usesDBaccession ? DB_ACCESSION : SEQUENCE_ID); return urlPrefix - + (dynamic ? (DELIM + var + ((regexReplace != null) ? "=" - + regexReplace + "=" + DELIM : DELIM)) : "") + + (dynamic ? (DELIM + var + ((regexReplace != null) ? EQUALS + + regexReplace + EQUALS + DELIM : DELIM)) : "") + ((urlSuffix == null) ? "" : urlSuffix); } @@ -227,13 +237,13 @@ public class UrlLink } /** - * Set the description + * Set the target * * @param desc */ - public void setDescription(String desc) + public void setTarget(String desc) { - this.dbname = desc; + target = desc; } /** @@ -277,7 +287,7 @@ public class UrlLink + rg.stringMatched(s) + "'"); } // try to collate subgroup matches - Vector subs = new Vector(); + Vector subs = new Vector(); // have to loop through submatches, collating them at top level // match int s = 0; // 1; @@ -321,7 +331,7 @@ public class UrlLink String[] res = new String[subs.size()]; for (int r = 0, rs = subs.size(); r < rs; r++) { - res[r] = (String) subs.elementAt(r); + res[r] = subs.elementAt(r); } subs.removeAllElements(); return res; @@ -354,6 +364,15 @@ public class UrlLink } /** + * @return delimited string containing label, url and target + */ + public String toStringWithTarget() + { + return label + SEP + getUrlWithToken() + SEP + target; + } + + /** + * Parse the label from the link string * * @param firstSep * Location of first occurrence of separator in link string @@ -363,7 +382,7 @@ public class UrlLink * 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) + protected int parseLabel(int firstSep, int psqid, String link) { int p = firstSep; int sep = firstSep; @@ -375,21 +394,44 @@ public class UrlLink // Assuming that the URL itself does not contain any SEP symbols // sep now contains last pipe symbol position prior to any regex symbols label = link.substring(0, sep); - if (label.indexOf(SEP) > -1) - { - // SEP terminated database name / www target at start of Label - target = label.substring(0, label.indexOf(SEP)); - } - else if (label.indexOf(" ") > 2) + + return sep; + } + + /** + * Parse the target from the link string + * + * @param link + * Link string containing database name and url + * @param sep + * Location of first separator symbol + * @param endOfRegex + * Location of end of any regular expression in link string + */ + protected void parseTarget(String link, int sep, int endOfRegex) + { + int lastsep = link.lastIndexOf(SEP); + + if ((lastsep != sep) && (lastsep > endOfRegex)) { - // space separated Label - matches database name - target = label.substring(0, label.indexOf(" ")); + // final element in link string is the target + target = link.substring(lastsep + 1).trim(); } else { target = label; } - return sep; + + if (target.indexOf(SEP) > -1) + { + // SEP terminated database name / www target at start of Label + target = target.substring(0, target.indexOf(SEP)); + } + else if (target.indexOf(SPACE) > 2) + { + // space separated label - first word matches database name + target = target.substring(0, target.indexOf(SPACE)); + } } /** @@ -403,8 +445,9 @@ public class UrlLink * Position of id or name in link string * @param sep * Position of separator in link string + * @return Location of end of any regex in link string */ - protected void parseUrl(String link, String varName, int sqidPos, int sep) + protected int parseUrl(String link, String varName, int sqidPos, int sep) { urlPrefix = link.substring(sep + 1, sqidPos).trim(); @@ -447,7 +490,14 @@ public class UrlLink // verify format is really correct. if (link.indexOf(DELIM + varName + DELIM) == sqidPos) { - urlSuffix = link.substring(sqidPos + startLength - 1); + int lastsep = link.lastIndexOf(SEP); + if (lastsep < sqidPos + startLength - 1) + { + // the last SEP character was before the regex, ignore + lastsep = link.length(); + } + urlSuffix = link.substring(sqidPos + startLength - 1, lastsep) + .trim(); regexReplace = null; } else @@ -456,6 +506,8 @@ public class UrlLink + link; } } + + return p; } /** @@ -489,11 +541,11 @@ public class UrlLink */ protected void createStaticLink(Map> linkset) { - if (!linkset.containsKey(label + SEP + getUrl_prefix())) + if (!linkset.containsKey(label + SEP + getUrlPrefix())) { // Add a non-dynamic link - linkset.put(label + SEP + getUrl_prefix(), - Arrays.asList(target, label, null, getUrl_prefix())); + linkset.put(label + SEP + getUrlPrefix(), + Arrays.asList(target, label, null, getUrlPrefix())); } } @@ -575,82 +627,4 @@ public class UrlLink } } } - - private static void testUrls(UrlLink ul, String idstring, String[] urls) - { - - if (urls == null) - { - System.out.println("Created NO urls."); - } - else - { - System.out.println("Created " + (urls.length / 2) + " Urls."); - for (int uls = 0; uls < urls.length; uls += 2) - { - System.out.println("URL Replacement text : " + urls[uls] - + " : URL : " + urls[uls + 1]); - } - } - } - - public static void main(String argv[]) - { - String[] links = new String[] { - /* - * "AlinkT|Target|http://foo.foo.soo/", - * "myUrl1|http://$SEQUENCE_ID=/[0-9]+/=$.someserver.org/foo", - * "myUrl2|http://$SEQUENCE_ID=/(([0-9]+).+([A-Za-z]+))/=$.someserver.org/foo" - * , - * "myUrl3|http://$SEQUENCE_ID=/([0-9]+).+([A-Za-z]+)/=$.someserver.org/foo" - * , "myUrl4|target|http://$SEQUENCE_ID$.someserver.org/foo|too", - * "PF1|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/(?:PFAM:)?(.+)/=$" - * , - * "PF2|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/(PFAM:)?(.+)/=$" - * , - * "PF3|http://us.expasy.org/cgi-bin/niceprot.pl?$SEQUENCE_ID=/PFAM:(.+)/=$" - * , "NOTFER|http://notfer.org/$SEQUENCE_ID=/(?