2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import java.util.Arrays;
24 import java.util.HashMap;
27 import jalview.bin.Console;
28 import jalview.bin.argparser.ArgParser;
30 public class JalviewJSUtil
33 * To be populated by Info={} hash set by instantiating JalviewJS
35 private static Map<String, String> j2sInfo = null;
38 * The non-j2s_-prefixed parameters to store
40 private static final String[] j2sSavedKeys = { "main", "core", "width",
41 "height", "serverURL", "j2sPath", "console" };
44 * The prefix for non-named keys in the Info={} block
46 private static final String j2sParameterPrefix = "j2s_";
49 * The Info key name for namespacing (query string parameters, class and id
52 private static final String j2sNamespaceKey = j2sParameterPrefix
56 * Get a value passed in from the Info={} hash. key should NOT use the
60 * key name without the j2s_ namespace in the Info={} hash.
61 * @return value value from the Info={} hash.
63 public static String getJ2sInfoValue(String key)
65 if (j2sInfo == null || key == null)
69 String j2sKey = Arrays.asList(j2sSavedKeys).contains(key) ? key
70 : j2sParameterPrefix + key;
71 return j2sInfo.get(j2sKey);
75 * Get and save parameters and values from the Info={} hash.
77 public static void setJ2sInfo()
83 j2sInfo = new HashMap<>();
87 * @j2sNative if (J2S.thisApplet.__Info !== undefined) {
89 * Object.entries(J2S.thisApplet.__Info).forEach( entry => {
95 if (key != null && (key.startsWith(j2sParameterPrefix)
96 || Arrays.asList(j2sSavedKeys).contains(key)))
98 j2sInfo.put(key, val);
100 if (key.equals(j2sNamespaceKey))
102 setJ2sNamespace(val);
115 * Namespace (if set) by JalviewJS
117 private static String j2sNamespace = null;
119 private static void setJ2sNamespace(String ns)
121 if (!Platform.isJS())
125 Console.outPrintln("j2sNamespace set to '" + ns + "'");
129 public static String getJ2sNamespace()
134 public static void getURLCommandArguments()
137 // setting ArgParser.ignoreNonStringValues allows non-string args to be
138 // set with, e.g., --wrap=hello
139 // which might be necessary for a querystring, plus we don't have access
140 // to Arg.hasOption(Opt.STRING)
143 ArgParser.setIgnoreNonStringValues(true);
149 // extra spaces between lines of javascript to avoid eclipse comment
150 // munging into one line
153 * Retrieve the first query field as command arguments to Jalview.
154 * Include only if prior to "?j2s" or "&j2s" or "#". Assign the applet's
155 * __Info.args element to this value.
157 * if a namespace has been given in Info={...}. Use this namespace to
158 * find arguments and values in the querystring parameters. Arguments
159 * that do not take a value do not need to have a value in the
160 * querystring. If they do they will be ignored. Note, this means you
161 * cannot do 'debug=false' instead of 'nodebug'. If querystringnamepsace
162 * is an empty string ("") then no colon (":") will be expected.
164 * if namespace is not defined then use the old style single first
165 * parameter for arguments
167 * @j2sNative var namespace = J2S.thisApplet.__Info.j2s_namespace;
169 * if (namespace === undefined)
173 * System.out.println("No namespace given");
176 * decodeURI((document.location.href.replace("&","?").split("?j2s")[0]
177 * + "?").split("?")[1].split("#")[0]);
179 * a && (System.out.println("URL arguments detected were
180 * "+a)) && (J2S.thisApplet.__Info.urlargs = a.split(" "));
182 * (!J2S.thisApplet.__Info.args || J2S.thisApplet.__Info.args
183 * == "" || J2S.thisApplet.__Info.args == "??") &&
184 * (J2S.thisApplet.__Info.args = a) &&
185 * (System.out.println("URL arguments were passed to J2S
190 * else // namespace is defined
206 * System.out.println("Querystring namespace is '" + nsc +
210 * URLSearchParams(window.location.search);
214 * for (var param of qsParams) {
216 * var key = param[0];
218 * var val = param[1];
220 * if (key.startsWith(nsc)) {
222 * var arg = key.substring(nsc.length);
224 * qsargs.push("--" + arg + "=" + val);
226 * System.out.println("Setting arg '"+arg+"' to '"+val+"'");
232 * qsargs && (System.out.println("URL parameters detected
233 * were "+qsargs.join(" "))) &&
234 * (J2S.thisApplet.__Info.urlargs = qsargs);
236 * (!J2S.thisApplet.__Info.args || J2S.thisApplet.__Info.args
237 * == "" || J2S.thisApplet.__Info.args == "??") &&
238 * (J2S.thisApplet.__Info.args = qsargs.join(" ")) &&
239 * (System.out.println("URL parameters were passed to J2S
244 } catch (Throwable t)
247 * @j2sNative System.err.println("Problem looking for arguments");
248 * console.log("Problem looking for arguments");