Working links menu
[jalview.git] / src / jalview / util / UrlLink.java
index e196c8c..a8c0b32 100644 (file)
@@ -21,6 +21,7 @@
 package jalview.util;
 
 import static jalview.util.UrlConstants.SEQUENCE_ID;
+import static jalview.util.UrlConstants.SEQUENCE_NAME;
 
 import java.util.Vector;
 
@@ -39,6 +40,8 @@ public class UrlLink
 
   private boolean dynamic = false;
 
+  private boolean uses_seq_id = false;
+
   private String invalidMessage = null;
 
   /**
@@ -50,34 +53,18 @@ 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;
-      }
+      uses_seq_id = true;
+
+      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)
@@ -116,6 +103,50 @@ public class UrlLink
         }
       }
     }
+    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;
+        }
+      }
+    }
     else
     {
       target = link.substring(0, sep);
@@ -309,6 +340,34 @@ public class UrlLink
 
   }
 
+  private int parseTargetAndLabel(int sep, int psqid, String link)
+  {
+    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;
+    }
+    return sep;
+  }
+
   private static void testUrls(UrlLink ul, String idstring, String[] urls)
   {
 
@@ -393,6 +452,11 @@ public class UrlLink
     return dynamic;
   }
 
+  public boolean usesSeqId()
+  {
+    return uses_seq_id;
+  }
+
   public void setLabel(String newlabel)
   {
     this.label = newlabel;