JAL-1859 refactor addProtocol, setProtocolState as resolveFileProtocol
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Wed, 16 Sep 2015 09:25:43 +0000 (10:25 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Wed, 16 Sep 2015 09:25:43 +0000 (10:25 +0100)
src/jalview/bin/JalviewLite.java

index 7cad007..1be926e 100644 (file)
@@ -66,6 +66,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.URL;
+import java.util.ArrayList;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.StringTokenizer;
@@ -1791,27 +1792,70 @@ public class JalviewLite extends Applet implements
      * update the protocol state variable for accessing the datasource located
      * by file.
      * 
-     * @param file
+     * @param path
      * @return possibly updated datasource string
      */
-    public String setProtocolState(String file)
+    public String resolveFileProtocol(String path)
     {
-      if (file.startsWith("PASTE"))
+      /*
+       * paste data
+       */
+      if (path.startsWith("PASTE"))
       {
-        file = file.substring(5);
         protocol = AppletFormatAdapter.PASTE;
+        return path.substring(5);
       }
-      else if (inArchive(file))
+
+      /*
+       * a URL
+       */
+      if (path.indexOf("://") != -1)
       {
-        protocol = AppletFormatAdapter.CLASSLOADER;
+        protocol = AppletFormatAdapter.URL;
+        return path;
       }
-      else
+
+      /*
+       * relative to document root
+       */
+      URL documentBase = getDocumentBase();
+      String url = resolveUrlForLocalOrAbsolute(path, documentBase);
+      if (urlExists(url))
       {
-        file = addProtocol(file);
+        if (debug)
+        {
+          System.err.println("Prepended document base '" + documentBase
+                  + "' to make: '" + url + "'");
+        }
         protocol = AppletFormatAdapter.URL;
+        return url;
       }
-      dbgMsg("Protocol identified as '" + protocol + "'");
-      return file;
+
+      /*
+       * relative to codebase
+       */
+      URL codeBase = getCodeBase();
+      url = applet.resolveUrlForLocalOrAbsolute(path, codeBase);
+      if (urlExists(url))
+      {
+        protocol = AppletFormatAdapter.URL;
+        if (debug)
+        {
+          System.err.println("Prepended codebase '" + codeBase
+                  + "' to make: '" + url + "'");
+        }
+        return url;
+      }
+
+      /*
+       * locatable by classloader; test for this last as files in the document
+       * root may also be found by the classloader
+       */
+      if (inArchive(path))
+      {
+        protocol = AppletFormatAdapter.CLASSLOADER;
+      }
+      return path;
     }
 
     public LoadingThread(String file, String file2, JalviewLite _applet)
@@ -1921,7 +1965,7 @@ public class JalviewLite extends Applet implements
       {
         return null;
       }
-      String resolvedFile = setProtocolState(fileParam);
+      String resolvedFile = resolveFileProtocol(fileParam);
       String format = new IdentifyFile().Identify(resolvedFile, protocol);
       dbgMsg("File identified as '" + format + "'");
       AlignmentI al = null;
@@ -2056,8 +2100,8 @@ public class JalviewLite extends Applet implements
           else
           {
             param = st.nextToken();
-            Vector tmp = new Vector();
-            Vector tmp2 = new Vector();
+            List<SequenceI> tmp = new ArrayList<SequenceI>();
+            List<String> tmp2 = new ArrayList<String>();
 
             while (st.hasMoreTokens())
             {
@@ -2066,39 +2110,22 @@ public class JalviewLite extends Applet implements
               if (st2.countTokens() > 1)
               {
                 // This is the chain
-                tmp2.addElement(st2.nextToken());
+                tmp2.add(st2.nextToken());
                 seqstring = st2.nextToken();
               }
-              tmp.addElement(matcher == null ? (Sequence) alignFrame
+              tmp.add(matcher == null ? (Sequence) alignFrame
                       .getAlignViewport().getAlignment()
                       .findName(seqstring) : matcher.findIdMatch(seqstring));
             }
 
-            seqs = new SequenceI[tmp.size()];
-            tmp.copyInto(seqs);
+            seqs = tmp.toArray(new SequenceI[tmp.size()]);
             if (tmp2.size() == tmp.size())
             {
-              chains = new String[tmp2.size()];
-              tmp2.copyInto(chains);
+              chains = tmp2.toArray(new String[tmp2.size()]);
             }
           }
-          param = setProtocolState(param);
-
-          if (// !jmolAvailable
-          // &&
-          protocol == AppletFormatAdapter.CLASSLOADER && !useXtrnalSviewer)
-          {
-            // Re: JAL-357 : the bug isn't a problem if we are using an
-            // external viewer!
-            // TODO: verify this Re:
-            // https://mantis.lifesci.dundee.ac.uk/view.php?id=36605
-            // This exception preserves the current behaviour where, even if
-            // the local pdb file was identified in the class loader
-            protocol = AppletFormatAdapter.URL; // this is probably NOT
-            // CORRECT!
-            param = addProtocol(param); //
-          }
-
+          param = resolveFileProtocol(param);
+          // TODO check JAL-357 for files in a jar (CLASSLOADER)
           pdb.setFile(param);
 
           if (seqs != null)
@@ -2175,7 +2202,7 @@ public class JalviewLite extends Applet implements
       {
         try
         {
-          param = setProtocolState(param);
+          param = resolveFileProtocol(param);
           JPredFile predictions = new JPredFile(param, protocol);
           JnetAnnotationMaker.add_annotation(predictions,
                   alignFrame.viewport.getAlignment(), 0, false);
@@ -2210,7 +2237,7 @@ public class JalviewLite extends Applet implements
       String param = applet.getParameter("annotations");
       if (param != null)
       {
-        param = setProtocolState(param);
+        param = resolveFileProtocol(param);
 
         if (new AnnotationFile().annotateAlignmentView(alignFrame.viewport,
                 param, protocol))
@@ -2263,7 +2290,7 @@ public class JalviewLite extends Applet implements
       param = applet.getParameter("features");
       if (param != null)
       {
-        param = setProtocolState(param);
+        param = resolveFileProtocol(param);
 
         result = alignFrame.parseFeaturesFile(param, protocol);
       }
@@ -2332,7 +2359,7 @@ public class JalviewLite extends Applet implements
       {
         try
         {
-          treeFile = setProtocolState(treeFile);
+          treeFile = resolveFileProtocol(treeFile);
           NewickFile fin = new NewickFile(treeFile, protocol);
           fin.parse();