From: Ben Soares Date: Thu, 2 Nov 2023 23:06:49 +0000 (+0000) Subject: JAL-4059 New namespaced query string parameters. X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=3d64f036a9efc527f8f45ef43ead4c3bb64ae448;p=jalview.git JAL-4059 New namespaced query string parameters. --- diff --git a/src/jalview/bin/argparser/ArgParser.java b/src/jalview/bin/argparser/ArgParser.java index 155f69e..e5b3a9d 100644 --- a/src/jalview/bin/argparser/ArgParser.java +++ b/src/jalview/bin/argparser/ArgParser.java @@ -194,6 +194,15 @@ public class ArgParser */ private boolean allStructures = false; + /** + * flag to say whether to ignore or reject non-string values args with a value + * e.g., --wrap=hello + * + * Default is false (i.e. reject non-string args that have a value. It is set + * to true for JalviewJS in Platform.getURLCommandArguments(). + */ + private static boolean ignoreNonStringValues = false; + protected static final Map argMap; protected Map linkedArgs = new HashMap<>(); @@ -469,11 +478,20 @@ public class ArgParser } if (!a.hasOption(Opt.STRING) && equalPos > -1) { - // set --argname=value when arg does not accept values - Console.error("Argument '" + a.argString() - + "' does not expect a value (given as '" + arg - + "'). Ignoring."); - continue; + if (!getIgnoreNonStringValues()) + { + // delete equals sign and value + val = null; + arg = arg.substring(0, equalPos); + } + else + { + // set --argname=value when arg does not accept values + Console.error("Argument '" + a.argString() + + "' does not expect a value (given as '" + arg + + "'). Ignoring."); + continue; + } } if (!a.hasOption(Opt.LINKED) && linkedId != null) { @@ -1290,4 +1308,13 @@ public class ArgParser this.currentStructureFilename = s; } + public static boolean getIgnoreNonStringValues() + { + return ignoreNonStringValues; + } + + public static void setIgnoreNonStringValues(boolean b) + { + ignoreNonStringValues = b; + } } \ No newline at end of file diff --git a/src/jalview/util/Platform.java b/src/jalview/util/Platform.java index 9c2825f..0d59910 100644 --- a/src/jalview/util/Platform.java +++ b/src/jalview/util/Platform.java @@ -38,6 +38,7 @@ 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; /** @@ -636,25 +637,112 @@ public class Platform public static void getURLCommandArguments() { + + // 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. * - * @j2sNative var a = + * 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(" ")); + * + "?").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); + */ } } diff --git a/utils/jalviewjs/template.html b/utils/jalviewjs/template.html index 7c20d44..b1bcf95 100644 --- a/utils/jalviewjs/template.html +++ b/utils/jalviewjs/template.html @@ -15,7 +15,8 @@ Info = { serverURL: 'https://chemapps.stolaf.edu/jmol/jsmol/php/jsmol.php', j2sPath: 'swingjs/j2s', console:'sysoutdiv', - allowjavascript: true + allowjavascript: true, + querystringnamespace: "" }