JAL-2282 Refactored URL link creation into UrlLink
[jalview.git] / src / jalview / util / UrlLink.java
index 872f432..0968d36 100644 (file)
@@ -23,6 +23,10 @@ package jalview.util;
 import static jalview.util.UrlConstants.SEQUENCE_ID;
 import static jalview.util.UrlConstants.SEQUENCE_NAME;
 
+import jalview.datamodel.DBRefEntry;
+import jalview.datamodel.SequenceI;
+
+import java.util.Map;
 import java.util.Vector;
 
 public class UrlLink
@@ -377,6 +381,114 @@ public class UrlLink
     }
   }
 
+  /**
+   * 
+   * @param urlLink
+   * @param seq
+   * @param linkset
+   */
+  public void createLinksFromSeq(final SequenceI seq,
+          Map<String, String[]> linkset)
+  {
+    if (seq != null && dynamic)
+    {
+      createDynamicLinks(seq, linkset);
+    }
+    else
+    {
+      createStaticLink(linkset);
+    }
+  }
+
+  /**
+   * Create a static URL link
+   * 
+   * @param linkset
+   */
+  public void createStaticLink(Map<String, String[]> linkset)
+  {
+    if (!linkset.containsKey(label + "|" + getUrl_prefix()))
+    {
+      // Add a non-dynamic link
+      linkset.put(label + "|" + getUrl_prefix(), new String[] { "", label,
+          "", getUrl_prefix() });
+    }
+  }
+
+  /**
+   * Create a dynamic URL link
+   * 
+   * @param seq
+   * @param linkset
+   */
+  public void createDynamicLinks(final SequenceI seq,
+          Map<String, String[]> linkset)
+  {
+    // collect id string too
+    String id = seq.getName();
+    String descr = seq.getDescription();
+    if (descr != null && descr.length() < 1)
+    {
+      descr = null;
+    }
+
+    if (usesSeqId()) // link is ID
+    {
+      // collect matching db-refs
+      DBRefEntry[] dbr = DBRefUtils.selectRefs(seq.getDBRefs(),
+              new String[] { target });
+
+      // if there are any dbrefs which match up with the link
+      if (dbr != null)
+      {
+        for (int r = 0; r < dbr.length; r++)
+        {
+          // create Bare ID link for this URL
+          createBareURLLink(dbr[r].getAccessionId(), linkset, true);
+        }
+      }
+    }
+    else if (!usesSeqId() && id != null) // link is name
+    {
+      // create Bare ID link for this URL
+      createBareURLLink(id, linkset, false);
+    }
+
+    // Create urls from description but only for URL links which are regex
+    // links
+    if (descr != null && getRegexReplace() != null)
+    {
+      // create link for this URL from description where regex matches
+      createBareURLLink(descr, linkset, false);
+    }
+  }
+
+  /*
+   * Create a bare URL Link
+   */
+  protected void createBareURLLink(String id,
+          Map<String, String[]> linkset, Boolean combineLabel)
+  {
+    String[] urls = makeUrls(id, true);
+    if (urls != null)
+    {
+      for (int u = 0; u < urls.length; u += 2)
+      {
+        if (!linkset.containsKey(urls[u] + "|" + urls[u + 1]))
+        {
+          String thisLabel = label;
+          if (combineLabel)
+          {
+            thisLabel = label + "|" + urls[u];
+          }
+
+          linkset.put(urls[u] + "|" + urls[u + 1], new String[] { target,
+              thisLabel, urls[u], urls[u + 1] });
+        }
+      }
+    }
+  }
+
   private static void testUrls(UrlLink ul, String idstring, String[] urls)
   {
 
@@ -457,7 +569,6 @@ public class UrlLink
 
   public boolean isDynamic()
   {
-    // TODO Auto-generated method stub
     return dynamic;
   }