JAL-4059 Made jalview.bin.JalviewJS to use new command-line args in querystring....
[jalview.git] / src / jalview / util / JalviewJSUtil.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.util;
22
23 import java.util.Arrays;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import jalview.bin.Console;
28 import jalview.bin.argparser.ArgParser;
29
30 public class JalviewJSUtil
31 {
32   /**
33    * To be populated by Info={} hash set by instantiating JalviewJS
34    */
35   private static Map<String, String> j2sInfo = null;
36
37   /**
38    * The non-j2s_-prefixed parameters to store
39    */
40   private static final String[] j2sSavedKeys = { "main", "core", "width",
41       "height", "serverURL", "j2sPath", "console" };
42
43   /**
44    * The prefix for non-named keys in the Info={} block
45    */
46   private static final String j2sParameterPrefix = "j2s_";
47
48   /**
49    * The Info key name for namespacing (query string parameters, class and id
50    * values)
51    */
52   private static final String j2sNamespaceKey = j2sParameterPrefix
53           + "namespace";
54
55   /**
56    * Get a value passed in from the Info={} hash. key should NOT use the
57    * namespace prefix.
58    * 
59    * @param key
60    *          key name without the j2s_ namespace in the Info={} hash.
61    * @return value value from the Info={} hash.
62    */
63   public static String getJ2sInfoValue(String key)
64   {
65     if (j2sInfo == null || key == null)
66     {
67       return null;
68     }
69     String j2sKey = Arrays.asList(j2sSavedKeys).contains(key) ? key
70             : j2sParameterPrefix + key;
71     return j2sInfo.get(j2sKey);
72   }
73
74   /**
75    * Get and save parameters and values from the Info={} hash.
76    */
77   public static void setJ2sInfo()
78   {
79     if (!Platform.isJS())
80     {
81       return;
82     }
83     j2sInfo = new HashMap<>();
84     String key = null;
85     String val = null;
86     /**
87      * @j2sNative if (J2S.thisApplet.__Info !== undefined) {
88      * 
89      *            Object.entries(J2S.thisApplet.__Info).forEach( entry => {
90      * 
91      *            key = entry[0];
92      * 
93      *            val = entry[1];
94      */
95     if (key != null && (key.startsWith(j2sParameterPrefix)
96             || Arrays.asList(j2sSavedKeys).contains(key)))
97     {
98       j2sInfo.put(key, val);
99
100       if (key.equals(j2sNamespaceKey))
101       {
102         setJ2sNamespace(val);
103       }
104     }
105     /**
106      * @j2sNative }
107      * 
108      *            );
109      * 
110      *            }
111      */
112   }
113
114   /**
115    * Namespace (if set) by JalviewJS
116    */
117   private static String j2sNamespace = null;
118
119   private static void setJ2sNamespace(String ns)
120   {
121     if (!Platform.isJS())
122     {
123       return;
124     }
125     Console.outPrintln("j2sNamespace set to '" + ns + "'");
126     j2sNamespace = ns;
127   }
128
129   public static String getJ2sNamespace()
130   {
131     return j2sNamespace;
132   }
133
134   public static void getURLCommandArguments()
135   {
136
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)
141     if (Platform.isJS())
142     {
143       ArgParser.setIgnoreNonStringValues(true);
144       try
145       {
146
147         String ns = null;
148
149         // extra spaces between lines of javascript to avoid eclipse comment
150         // munging into one line
151
152         /**
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.
156          * 
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.
163          * 
164          * if namespace is not defined then use the old style single first
165          * parameter for arguments
166          *
167          * @j2sNative var namespace = J2S.thisApplet.__Info.j2s_namespace;
168          * 
169          *            if (namespace === undefined)
170          * 
171          *            {
172          * 
173          *            System.out.println("No namespace given");
174          * 
175          *            var a =
176          *            decodeURI((document.location.href.replace("&","?").split("?j2s")[0]
177          *            + "?").split("?")[1].split("#")[0]);
178          * 
179          *            a && (System.out.println("URL arguments detected were
180          *            "+a)) && (J2S.thisApplet.__Info.urlargs = a.split(" "));
181          * 
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
186          *            main."));
187          * 
188          *            }
189          * 
190          *            else // namespace is defined
191          * 
192          *            {
193          * 
194          *            ns = "";
195          * 
196          *            var nsc = "";
197          * 
198          *            if (namespace) {
199          * 
200          *            ns = namespace;
201          * 
202          *            nsc = ns + ":";
203          * 
204          *            }
205          * 
206          *            System.out.println("Querystring namespace is '" + nsc +
207          *            "'");
208          * 
209          *            var qsParams = new
210          *            URLSearchParams(window.location.search);
211          * 
212          *            var qsargs = [];
213          * 
214          *            for (var param of qsParams) {
215          * 
216          *            var key = param[0];
217          * 
218          *            var val = param[1];
219          * 
220          *            if (key.startsWith(nsc)) {
221          * 
222          *            var arg = key.substring(nsc.length);
223          * 
224          *            qsargs.push("--" + arg + "=" + val);
225          * 
226          *            System.out.println("Setting arg '"+arg+"' to '"+val+"'");
227          * 
228          *            }
229          * 
230          *            }
231          * 
232          *            qsargs && (System.out.println("URL parameters detected
233          *            were "+qsargs.join(" "))) &&
234          *            (J2S.thisApplet.__Info.urlargs = qsargs);
235          * 
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
240          *            main."));
241          * 
242          *            }
243          */
244       } catch (Throwable t)
245       {
246         /**
247          * @j2sNative System.err.println("Problem looking for arguments");
248          *            console.log("Problem looking for arguments");
249          *            console.log(t);
250          */
251       }
252     }
253   }
254
255 }