JAL-629 Fixed appending URLs
[jalview.git] / src / jalview / bin / argparser / ArgParser.java
index 0ff1845..e08ae03 100644 (file)
@@ -29,12 +29,15 @@ import java.util.Arrays;
 import java.util.EnumSet;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 
+import jalview.bin.Cache;
 import jalview.bin.Console;
 import jalview.bin.Jalview;
 import jalview.bin.argparser.Arg.Opt;
 import jalview.util.FileUtils;
+import jalview.util.HttpUtils;
 
 public class ArgParser
 {
@@ -53,11 +56,12 @@ public class ArgParser
   // the substitution string used to use the defaultLinkedIdCounter
   private static final String DEFAULTLINKEDIDCOUNTER = "{}";
 
-  // the counter added to the default linked id prefix
-  private int opennewLinkedIdCounter = 0;
+  // the counter added to the default linked id prefix. NOW using
+  // linkedIdAutoCounter
+  // private int openLinkedIdCounter = 0;
 
-  // the linked id prefix used for --opennew files
-  protected static final String OPENNEWLINKEDIDPREFIX = "OPENNEW:";
+  // the linked id prefix used for --open files. NOW the same as DEFAULT
+  protected static final String OPENLINKEDIDPREFIX = DEFAULTLINKEDIDPREFIX;
 
   // the counter used for {n} substitutions
   private int linkedIdAutoCounter = 0;
@@ -69,12 +73,12 @@ public class ArgParser
   // the linked id substitution string used to use the idCounter
   private static final String LINKEDIDAUTOCOUNTER = "{n}";
 
-  // the linked id substitution string used to use the base filename of --open
-  // or --opennew
+  // the linked id substitution string used to use the base filename of --append
+  // or --open
   private static final String LINKEDIDBASENAME = "{basename}";
 
-  // the linked id substitution string used to use the dir path of --open
-  // or --opennew
+  // the linked id substitution string used to use the dir path of --append
+  // or --open
   private static final String LINKEDIDDIRNAME = "{dirname}";
 
   // the current argfile
@@ -139,7 +143,7 @@ public class ArgParser
     // Make a mutable new ArrayList so that shell globbing parser works.
     // (When shell file globbing is used, there are a sequence of non-Arg
     // arguments (which are the expanded globbed filenames) that need to be
-    // consumed by the --open/--argfile/etc Arg which is most easily done by
+    // consumed by the --append/--argfile/etc Arg which is most easily done by
     // removing these filenames from the list one at a time. This can't be done
     // with an ArrayList made with only Arrays.asList(String[] args). )
     this(new ArrayList<>(Arrays.asList(args)), initsubstitutions);
@@ -153,7 +157,8 @@ public class ArgParser
   public ArgParser(List<String> args, boolean initsubstitutions,
           boolean allowPrivate)
   {
-    // do nothing if there are no "--" args and some "-" args
+    // do nothing if there are no "--" args and (some "-" args || >0 arg is
+    // "open")
     boolean d = false;
     boolean dd = false;
     for (String arg : args)
@@ -163,7 +168,7 @@ public class ArgParser
         dd = true;
         break;
       }
-      else if (arg.startsWith("-"))
+      else if (arg.startsWith("-") || arg.equals("open"))
       {
         d = true;
       }
@@ -193,9 +198,10 @@ public class ArgParser
       // single arg[i] and increment the defaultLinkedIdCounter so that each of
       // these files is opened separately.
       if (openEachInitialFilenames && !arg.startsWith(DOUBLEDASH)
-              && !arg.startsWith("-") && new File(arg).exists())
+              && !arg.startsWith("-") && (new File(arg).exists()
+                      || HttpUtils.startsWithHttpOrHttps(arg)))
       {
-        arg = Arg.OPENNEW.argString();
+        arg = Arg.OPEN.argString();
       }
       else
       {
@@ -327,7 +333,7 @@ public class ArgParser
 
         // make NOACTION adjustments
         // default and auto counter increments
-        if (a == Arg.INCREMENT)
+        if (a == Arg.NEWFRAME)
         {
           defaultLinkedIdCounter++;
         }
@@ -358,13 +364,14 @@ public class ArgParser
         {
           if (linkedId == null)
           {
-            if (a == Arg.OPENNEW)
+            if (a == Arg.OPEN)
             {
-              // use the next default prefixed OPENNEWLINKEDID
-              linkedId = new StringBuilder(OPENNEWLINKEDIDPREFIX)
-                      .append(Integer.toString(opennewLinkedIdCounter))
+              // use the next default prefixed OPENLINKEDID
+              // NOW using the linkedIdAutoCounter
+              defaultLinkedIdCounter++;
+              linkedId = new StringBuilder(OPENLINKEDIDPREFIX)
+                      .append(Integer.toString(defaultLinkedIdCounter))
                       .toString();
-              opennewLinkedIdCounter++;
             }
             else
             {
@@ -707,4 +714,63 @@ public class ArgParser
     return args;
   }
 
+  public static enum Position
+  {
+    FIRST, BEFORE, AFTER
+  }
+
+  public static String getValueFromSubValOrArg(ArgValuesMap avm, Arg a,
+          SubVals sv)
+  {
+    return getFromSubValArgOrPref(avm, a, sv, null, null, null);
+  }
+
+  public static String getFromSubValArgOrPref(ArgValuesMap avm, Arg a,
+          SubVals sv, String key, String pref, String def)
+  {
+    return getFromSubValArgOrPref(avm, a, Position.FIRST, null, sv, key,
+            pref, def);
+  }
+
+  public static String getFromSubValArgOrPref(ArgValuesMap avm, Arg a,
+          Position pos, ArgValue av, SubVals sv, String key, String pref,
+          String def)
+  {
+    if (key == null)
+      key = a.getName();
+    if (sv != null && sv.has(key) && sv.get(key) != null)
+      return sv.get(key);
+    if (avm != null && avm.containsArg(a))
+    {
+      String val = null;
+      if (pos == Position.FIRST && avm.getValue(a) != null)
+        return avm.getValue(a);
+      else if (pos == Position.BEFORE
+              && avm.getClosestPreviousArgValueOfArg(av, a) != null)
+        return avm.getClosestPreviousArgValueOfArg(av, a).getValue();
+      else if (pos == Position.AFTER
+              && avm.getClosestNextArgValueOfArg(av, a) != null)
+        return avm.getClosestNextArgValueOfArg(av, a).getValue();
+    }
+    return pref != null ? Cache.getDefault(pref, def) : def;
+  }
+
+  public static boolean getBoolFromSubValOrArg(ArgValuesMap avm, Arg a,
+          SubVals sv)
+  {
+    return getFromSubValArgOrPref(avm, a, sv, null, null, false);
+  }
+
+  public static boolean getFromSubValArgOrPref(ArgValuesMap avm, Arg a,
+          SubVals sv, String key, String pref, boolean def)
+  {
+    if (key == null)
+      key = a.getName();
+    if (sv != null && sv.has(key) && sv.get(key) != null)
+      return sv.get(key).toLowerCase(Locale.ROOT).equals("true");
+    if (avm != null && avm.containsArg(a))
+      return avm.getBoolean(a);
+    return pref != null ? Cache.getDefault(pref, def) : def;
+  }
+
 }
\ No newline at end of file