JAL-629 Fixed appending URLs
[jalview.git] / src / jalview / bin / argparser / ArgParser.java
index 7d4f187..e08ae03 100644 (file)
@@ -32,10 +32,12 @@ 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
 {
@@ -51,25 +53,32 @@ public class ArgParser
   // the counter added to the default linked id prefix
   private int defaultLinkedIdCounter = 0;
 
-  // the linked id prefix used for --opennew files
-  protected static final String OPENNEWLINKEDIDPREFIX = "OPENNEW:";
+  // 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 --open files. NOW the same as DEFAULT
+  protected static final String OPENLINKEDIDPREFIX = DEFAULTLINKEDIDPREFIX;
+
+  // the counter used for {n} substitutions
+  private int linkedIdAutoCounter = 0;
 
   // the linked id substitution string used to increment the idCounter (and use
   // the incremented value)
-  private static final String INCREMENTAUTOCOUNTERLINKEDID = "{++n}";
+  private static final String INCREMENTLINKEDIDAUTOCOUNTER = "{++n}";
 
   // the linked id substitution string used to use the idCounter
-  private static final String AUTOCOUNTERLINKEDID = "{n}";
+  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
@@ -83,8 +92,6 @@ public class ArgParser
   // --argfile name
   private static final String ARGFILEDIRNAME = "{argfiledirname}";
 
-  private int linkedIdAutoCounter = 0;
-
   // flag to say whether {n} subtitutions in output filenames should be made.
   // Turn on and off with --subs and --nosubs
   private boolean substitutions = false;
@@ -97,6 +104,8 @@ public class ArgParser
 
   protected List<Arg> argList;
 
+  private static final char ARGFILECOMMENT = '#';
+
   static
   {
     argMap = new HashMap<>();
@@ -107,7 +116,8 @@ public class ArgParser
         if (argMap.containsKey(argName))
         {
           Console.warn("Trying to add argument name multiple times: '"
-                  + argName + "'"); // RESTORE THIS WHEN MERGED
+                  + argName + "'"); // RESTORE THIS WHEN
+          // MERGED
           if (argMap.get(argName) != a)
           {
             Console.error(
@@ -133,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);
@@ -147,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)
@@ -157,7 +168,7 @@ public class ArgParser
         dd = true;
         break;
       }
-      else if (arg.startsWith("-"))
+      else if (arg.startsWith("-") || arg.equals("open"))
       {
         d = true;
       }
@@ -187,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 = DOUBLEDASH + Arg.OPENNEW.getName();
+        arg = Arg.OPEN.argString();
       }
       else
       {
@@ -242,8 +254,8 @@ public class ArgParser
         }
         if (a.hasOption(Opt.PRIVATE) && !allowPrivate)
         {
-          Console.error("Argument '" + DOUBLEDASH + argName
-                  + "' is private. Ignoring.");
+          Console.error(
+                  "Argument '" + a.argString() + "' is private. Ignoring.");
           continue;
         }
         if (!a.hasOption(Opt.BOOLEAN) && negated)
@@ -256,7 +268,7 @@ public class ArgParser
         if (!a.hasOption(Opt.STRING) && equalPos > -1)
         {
           // set --argname=value when arg does not accept values
-          Console.error("Argument '" + DOUBLEDASH + argName
+          Console.error("Argument '" + a.argString()
                   + "' does not expect a value (given as '" + arg
                   + "').  Ignoring.");
           continue;
@@ -264,7 +276,7 @@ public class ArgParser
         if (!a.hasOption(Opt.LINKED) && linkedId != null)
         {
           // set --argname[linkedId] when arg does not use linkedIds
-          Console.error("Argument '" + DOUBLEDASH + argName
+          Console.error("Argument '" + a.argString()
                   + "' does not expect a linked id (given as '" + arg
                   + "'). Ignoring.");
           continue;
@@ -278,7 +290,7 @@ public class ArgParser
             if (a.hasOption(Opt.GLOB))
             {
               // strip off and save the SubVals to be added individually later
-              globSubVals = ArgParser.getSubVals(val);
+              globSubVals = new SubVals(val);
               // make substitutions before looking for files
               String fileGlob = makeSubstitutions(globSubVals.getContent(),
                       linkedId);
@@ -321,7 +333,7 @@ public class ArgParser
 
         // make NOACTION adjustments
         // default and auto counter increments
-        if (a == Arg.INCREMENT)
+        if (a == Arg.NEWFRAME)
         {
           defaultLinkedIdCounter++;
         }
@@ -352,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
             {
@@ -369,21 +382,21 @@ public class ArgParser
                       + arg);
             }
           }
-          else if (linkedId.contains(AUTOCOUNTERLINKEDID))
+          else if (linkedId.contains(LINKEDIDAUTOCOUNTER))
           {
             // turn {n} to the autoCounter
             autoCounterString = Integer.toString(linkedIdAutoCounter);
-            linkedId = linkedId.replace(AUTOCOUNTERLINKEDID,
+            linkedId = linkedId.replace(LINKEDIDAUTOCOUNTER,
                     autoCounterString);
             usingAutoCounterLinkedId = true;
             Console.debug(
                     "Changing linkedId to '" + linkedId + "' from " + arg);
           }
-          else if (linkedId.contains(INCREMENTAUTOCOUNTERLINKEDID))
+          else if (linkedId.contains(INCREMENTLINKEDIDAUTOCOUNTER))
           {
             // turn {++n} to the incremented autoCounter
             autoCounterString = Integer.toString(++linkedIdAutoCounter);
-            linkedId = linkedId.replace(INCREMENTAUTOCOUNTERLINKEDID,
+            linkedId = linkedId.replace(INCREMENTLINKEDIDAUTOCOUNTER,
                     autoCounterString);
             usingAutoCounterLinkedId = true;
             Console.debug(
@@ -403,18 +416,18 @@ public class ArgParser
         // not dealing with both NODUPLICATEVALUES and GLOB
         if (a.hasOption(Opt.NODUPLICATEVALUES) && avm.hasValue(a, val))
         {
-          Console.error("Argument '" + DOUBLEDASH + argName
+          Console.error("Argument '" + a.argString()
                   + "' cannot contain a duplicate value ('" + val
                   + "'). Ignoring this and subsequent occurrences.");
           continue;
         }
 
         // check for unique id
-        SubVals idsv = ArgParser.getSubVals(val);
+        SubVals idsv = new SubVals(val);
         String id = idsv.get(ArgValues.ID);
         if (id != null && avm.hasId(a, id))
         {
-          Console.error("Argument '" + DOUBLEDASH + argName
+          Console.error("Argument '" + a.argString()
                   + "' has a duplicate id ('" + id + "'). Ignoring.");
           continue;
         }
@@ -474,9 +487,9 @@ public class ArgParser
     }
   }
 
-  private String makeSubstitutions(String val, String linkedId)
+  public String makeSubstitutions(String val, String linkedId)
   {
-    if (!this.substitutions)
+    if (!this.substitutions || val == null)
       return val;
 
     String subvals;
@@ -494,14 +507,15 @@ public class ArgParser
       subvals = "";
       rest = val;
     }
-    if (rest.contains(AUTOCOUNTERLINKEDID))
-      rest = rest.replace(AUTOCOUNTERLINKEDID,
+    if (rest.contains(LINKEDIDAUTOCOUNTER))
+      rest = rest.replace(LINKEDIDAUTOCOUNTER,
               String.valueOf(linkedIdAutoCounter));
-    if (rest.contains(INCREMENTAUTOCOUNTERLINKEDID))
-      rest = rest.replace(INCREMENTAUTOCOUNTERLINKEDID,
+    if (rest.contains(INCREMENTLINKEDIDAUTOCOUNTER))
+      rest = rest.replace(INCREMENTLINKEDIDAUTOCOUNTER,
               String.valueOf(++linkedIdAutoCounter));
-    if (rest.contains("{}"))
-      rest = rest.replace("{}", String.valueOf(defaultLinkedIdCounter));
+    if (rest.contains(DEFAULTLINKEDIDCOUNTER))
+      rest = rest.replace(DEFAULTLINKEDIDCOUNTER,
+              String.valueOf(defaultLinkedIdCounter));
     ArgValuesMap avm = linkedArgs.get(linkedId);
     if (avm != null)
     {
@@ -534,12 +548,11 @@ public class ArgParser
   /*
    * A helper method to take a list of String args where we're expecting
    * {"--previousargs", "--arg", "file1", "file2", "file3", "--otheroptionsornot"}
-   * and the index of the globbed arg, here 1.  It returns a
-   * List<String> {"file1", "file2", "file3"}
-   * *and remove these from the original list object* so that processing
-   * can continue from where it has left off, e.g. args has become
-   * {"--previousargs", "--arg", "--otheroptionsornot"}
-   * so the next increment carries on from the next --arg if available.
+   * and the index of the globbed arg, here 1. It returns a List<String> {"file1",
+   * "file2", "file3"} *and remove these from the original list object* so that
+   * processing can continue from where it has left off, e.g. args has become
+   * {"--previousargs", "--arg", "--otheroptionsornot"} so the next increment
+   * carries on from the next --arg if available.
    */
   protected static List<String> getShellGlobbedFilenameValues(Arg a,
           List<String> args, int i)
@@ -631,11 +644,6 @@ public class ArgParser
     return sb.toString();
   }
 
-  public static SubVals getSubVals(String item)
-  {
-    return new SubVals(item);
-  }
-
   public static ArgParser parseArgFiles(List<String> argFilenameGlobs,
           boolean initsubstitutions)
   {
@@ -658,25 +666,22 @@ public class ArgParser
     {
       if (!argFile.exists())
       {
-        String message = DOUBLEDASH
-                + Arg.ARGFILE.name().toLowerCase(Locale.ROOT) + EQUALS
-                + "\"" + argFile.getPath() + "\": File does not exist.";
+        String message = Arg.ARGFILE.argString() + EQUALS + "\""
+                + argFile.getPath() + "\": File does not exist.";
         Jalview.exit(message, 2);
       }
       try
       {
-        String setargfile = new StringBuilder(ArgParser.DOUBLEDASH)
-                .append(Arg.SETARGFILE.getName()).append(EQUALS)
-                .append(argFile.getCanonicalPath()).toString();
+        String setargfile = new StringBuilder(Arg.SETARGFILE.argString())
+                .append(EQUALS).append(argFile.getCanonicalPath())
+                .toString();
         argsList.add(setargfile);
-        argsList.addAll(Files.readAllLines(Paths.get(argFile.getPath())));
-        argsList.add(new StringBuilder(ArgParser.DOUBLEDASH)
-                .append(Arg.UNSETARGFILE.getName()).toString());
+        argsList.addAll(readArgFile(argFile));
+        argsList.add(Arg.UNSETARGFILE.argString());
       } catch (IOException e)
       {
-        String message = DOUBLEDASH
-                + Arg.ARGFILE.name().toLowerCase(Locale.ROOT) + "=\""
-                + argFile.getPath() + "\": File could not be read.";
+        String message = Arg.ARGFILE.argString() + "=\"" + argFile.getPath()
+                + "\": File could not be read.";
         Jalview.exit(message, 3);
       }
     }
@@ -685,4 +690,87 @@ public class ArgParser
     return new ArgParser(argsList, initsubstitutions, true);
   }
 
+  protected static List<String> readArgFile(File argFile)
+  {
+    List<String> args = new ArrayList<>();
+    if (argFile != null && argFile.exists())
+    {
+      try
+      {
+        for (String line : Files.readAllLines(Paths.get(argFile.getPath())))
+        {
+          if (line != null && line.length() > 0
+                  && line.charAt(0) != ARGFILECOMMENT)
+            args.add(line);
+        }
+      } catch (IOException e)
+      {
+        String message = Arg.ARGFILE.argString() + "=\"" + argFile.getPath()
+                + "\": File could not be read.";
+        Console.debug(message, e);
+        Jalview.exit(message, 3);
+      }
+    }
+    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