JAL-4059 New namespaced query string parameters.
[jalview.git] / src / jalview / util / Platform.java
index 71d798d..0d59910 100644 (file)
@@ -20,8 +20,6 @@
  */
 package jalview.util;
 
-import jalview.javascript.json.JSON;
-
 import java.awt.Toolkit;
 import java.awt.event.MouseEvent;
 import java.io.BufferedReader;
@@ -40,6 +38,9 @@ import javax.swing.SwingUtilities;
 import org.json.simple.parser.JSONParser;
 import org.json.simple.parser.ParseException;
 
+import jalview.bin.argparser.ArgParser;
+import jalview.javascript.json.JSON;
+
 /**
  * System platform information used by Applet and Application
  * 
@@ -56,6 +57,27 @@ public class Platform
 
   private static Boolean isHeadless = null;
 
+  // If launched from CLI with launcher script then -DCOLUMNWIDTH is set
+  private static final int CONSOLEWIDTH;
+
+  private static final String CONSOLEWIDTHPROPERTY = "CONSOLEWIDTH";
+
+  static
+  {
+    int cw = 80;
+    if (System.getProperty(CONSOLEWIDTHPROPERTY) != null
+            && System.getProperty(CONSOLEWIDTHPROPERTY).length() > 0)
+    {
+      try
+      {
+        cw = Integer.parseInt(System.getProperty(CONSOLEWIDTHPROPERTY));
+      } catch (NumberFormatException e)
+      {
+      }
+    }
+    CONSOLEWIDTH = cw;
+  }
+
   /**
    * added to group mouse events into Windows and nonWindows (mac, unix, linux)
    * 
@@ -88,7 +110,8 @@ public class Platform
   public static boolean isLinux()
   {
     return (isLinux == null
-            ? (isLinux = (System.getProperty("os.name").indexOf("Linux") >= 0))
+            ? (isLinux = (System.getProperty("os.name")
+                    .indexOf("Linux") >= 0))
             : isLinux);
   }
 
@@ -287,7 +310,7 @@ public class Platform
       time = mark = t;
       if (msg != null)
       {
-        System.err.println("Platform: timer reset\t\t\t" + msg);
+        jalview.bin.Console.errPrintln("Platform: timer reset\t\t\t" + msg);
       }
       break;
     case TIME_MARK:
@@ -303,8 +326,9 @@ public class Platform
         }
         if (msg != null)
         {
-          System.err.println("Platform: timer mark\t" + ((t - time) / 1000f)
-                  + "\t" + ((t - mark) / 1000f) + "\t" + msg);
+          jalview.bin.Console.errPrintln(
+                  "Platform: timer mark\t" + ((t - time) / 1000f) + "\t"
+                          + ((t - mark) / 1000f) + "\t" + msg);
         }
         mark = t;
       }
@@ -315,8 +339,9 @@ public class Platform
     case TIME_GET:
       if (msg != null)
       {
-        System.err.println("Platform: timer dur\t" + ((t - time) / 1000f)
-                + "\t" + ((duration) / 1000f) + "\t" + msg);
+        jalview.bin.Console
+                .errPrintln("Platform: timer dur\t" + ((t - time) / 1000f)
+                        + "\t" + ((duration) / 1000f) + "\t" + msg);
       }
       set = 0;
       break;
@@ -349,7 +374,7 @@ public class Platform
 
   public static byte[] getFileBytes(File f)
   {
-    return /** @j2sNative f && swingjs.JSUtil.getFileBytes$java_io_File(f) || */
+    return /** @j2sNative f && swingjs.JSUtil.getFileAsBytes$O(f) || */
     null;
   }
 
@@ -472,7 +497,7 @@ public class Platform
      *            info[key];
      */
 
-    System.out.println(
+    jalview.bin.Console.outPrintln(
             "Platform id=" + id + " reading Info." + key + " = " + value);
     p.put(id + "_" + key, value);
 
@@ -522,8 +547,7 @@ public class Platform
 
   public static Object parseJSON(String json) throws ParseException
   {
-    return (isJS() ? JSON.parse(json)
-            : new JSONParser().parse(json));
+    return (isJS() ? JSON.parse(json) : new JSONParser().parse(json));
   }
 
   public static Object parseJSON(Reader r)
@@ -555,8 +579,8 @@ public class Platform
    * @param is
    * @param outFile
    * @throws IOException
-   *                       if the file cannot be created or there is a problem
-   *                       reading the input stream.
+   *           if the file cannot be created or there is a problem reading the
+   *           input stream.
    */
   public static void streamToFile(InputStream is, File outFile)
           throws IOException
@@ -599,9 +623,9 @@ public class Platform
 
     if (isJS())
     {
-      System.out.println(
-            "Platform adding known access-control-allow-origin * for domain "
-                    + domain);
+      jalview.bin.Console.outPrintln(
+              "Platform adding known access-control-allow-origin * for domain "
+                      + domain);
       /**
        * @j2sNative
        * 
@@ -614,22 +638,117 @@ public class Platform
   public static void getURLCommandArguments()
   {
 
-    /**
-     * Retrieve the first query field as command arguments to Jalview. Include
-     * only if prior to "?j2s" or "&j2s" or "#". Assign the applet's __Info.args
-     * element to this value.
-     * 
-     * @j2sNative var a =
-     *            decodeURI((document.location.href.replace("&","?").split("?j2s")[0]
-     *            + "?").split("?")[1].split("#")[0]); a &&
-     *            (J2S.thisApplet.__Info.args = a.split(" "));
-     */
+    // setting ArgParser.ignoreNonStringValues allows non-string args to be
+    // set with, e.g., --wrap=hello
+    // which might be necessary for a querystring, plus we don't have access
+    // to Arg.hasOption(Opt.STRING)
+    if (Platform.isJS())
+    {
+      ArgParser.setIgnoreNonStringValues(true);
+    }
+    try
+    {
+      // extra spaces between lines of javascript to avoid eclipse comment
+      // munging into one line
 
+      /**
+       * Retrieve the first query field as command arguments to Jalview. Include
+       * only if prior to "?j2s" or "&j2s" or "#". Assign the applet's
+       * __Info.args element to this value.
+       * 
+       * if a querystringnamespace has been given in Info={...}. Use this
+       * namespace to find arguments and values in the querystring parameters.
+       * Arguments that do not take a value do not need to have a value in the
+       * querystring. If they do they will be ignored. Note, this means you
+       * cannot do 'debug=false' instead of 'nodebug'. If querystringnamepsace
+       * is an empty string ("") then no colon (":") will be expected.
+       * 
+       * if querystringnamespace is not defined then use the old style single
+       * first parameter for arguments
+       *
+       * @j2sNative var querystringnamespace =
+       *            J2S.thisApplet.__Info.querystringnamespace;
+       * 
+       *            if (querystringnamespace === undefined)
+       * 
+       *            {
+       * 
+       *            System.out.println("No querystringnamespace given");
+       * 
+       *            var a =
+       *            decodeURI((document.location.href.replace("&","?").split("?j2s")[0]
+       *            + "?").split("?")[1].split("#")[0]);
+       * 
+       *            a && (System.out.println("URL arguments detected were "+a))
+       *            && (J2S.thisApplet.__Info.urlargs = a.split(" "));
+       * 
+       *            (!J2S.thisApplet.__Info.args || J2S.thisApplet.__Info.args
+       *            == "" || J2S.thisApplet.__Info.args == "??") &&
+       *            (J2S.thisApplet.__Info.args = a) && (System.out.println("URL
+       *            arguments were passed to J2S main."));
+       * 
+       *            }
+       * 
+       *            else // querystringnamespace is defined
+       * 
+       *            {
+       * 
+       *            var qsnsc = "";
+       * 
+       *            if (J2S.thisApplet.__Info.querystringnamespace) {
+       * 
+       *            qsnsc = J2S.thisApplet.__Info.querystringnamespace + ":";
+       * 
+       *            }
+       * 
+       *            System.out.println("querystringnamespace is '"+qsnsc+"'");
+       * 
+       *            var qsParams = new URLSearchParams(window.location.search);
+       * 
+       *            var qsargs = [];
+       * 
+       *            for (var param of qsParams) {
+       * 
+       *            var key = param[0];
+       * 
+       *            var val = param[1];
+       * 
+       *            if (key.startsWith(qsnsc)) {
+       * 
+       *            var arg = key.substring(qsnsc.length);
+       * 
+       *            qsargs.push("--" + arg + "=" + val);
+       * 
+       *            System.out.println("Setting arg '"+arg+"' to '"+val+"'");
+       * 
+       *            }
+       * 
+       *            }
+       * 
+       *            qsargs && (System.out.println("URL parameters detected were
+       *            "+qsargs.join(" "))) && (J2S.thisApplet.__Info.urlargs =
+       *            qsargs);
+       * 
+       *            (!J2S.thisApplet.__Info.args || J2S.thisApplet.__Info.args
+       *            == "" || J2S.thisApplet.__Info.args == "??") &&
+       *            (J2S.thisApplet.__Info.args = qsargs.join(" ")) &&
+       *            (System.out.println("URL parameters were passed to J2S
+       *            main."));
+       * 
+       *            }
+       */
+    } catch (Throwable t)
+    {
+      /**
+       * @j2sNative console.log("Problem looking for arguments");
+       *            console.log(t);
+       */
+    }
   }
 
   /**
-   * A (case sensitive) file path comparator that ignores the difference between /
-   * and \
+   * A (case sensitive) file path comparator that ignores the difference between
+   * / and \
    * 
    * @param path1
    * @param path2
@@ -649,4 +768,12 @@ public class Platform
     String p2 = path2.replace('\\', '/');
     return p1.equals(p2);
   }
+
+  /**
+   * If started on command line using launch script, return the console width
+   */
+  public static int consoleWidth()
+  {
+    return CONSOLEWIDTH;
+  }
 }