Merge branch 'Jalview-JS/JAL-3253-applet' of https://source.jalview.org/git/jalview...
[jalview.git] / src / jalview / bin / AppletParams.java
1 package jalview.bin;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.Vector;
6
7 /**
8  * Collection of all known applet tags from JalviewLite
9  * 
10  * @author hansonr
11  *
12  */
13 @SuppressWarnings("serial")
14 public class AppletParams extends HashMap<String, String>
15 {
16
17   private final static String[] params = { "alignpdbfiles",
18       "ANNOTATIONCOLOUR_MAX", "ANNOTATIONCOLOUR_MIN", "annotations",
19       "APPLICATION_URL", "automaticScrolling", "centrecolumnlabels",
20       "debug", "defaultColour", "defaultColourNuc", "defaultColourProt",
21       "embedded", "enableSplitFrame", "externalstructureviewer", "features",
22       "file", "file2", "format", "heightScale", "hidefeaturegroups",
23       "jalviewhelpurl", "jnetfile", "jpredfile", "label", "linkLabel_",
24       "linkLabel_1", "linkURL_", "nojmol", "normaliseLogo",
25       "normaliseSequenceLogo", "oninit", "PDBFILE", "PDBSEQ",
26       "relaxedidmatch", "resolvetocodebase", "RGB", "scaleProteinAsCdna",
27       "scoreFile", "separator", "sequence", "showAnnotation", "showbutton",
28       "showConsensus", "showConsensusHistogram", "showConservation",
29       "showfeaturegroups", "showFeatureSettings", "showFullId",
30       "showGroupConsensus", "showGroupConservation", "showOccupancy",
31       "showQuality", "showSequenceLogo", "showTreeBootstraps",
32       "showTreeDistances", "showUnconserved", "showUnlinkedTreeNodes",
33       "sortBy", "sortByTree", "tree", "treeFile", "upperCase",
34       "userDefinedColour", "widthScale", "windowHeight", "windowWidth",
35       "wrap", };
36
37   public AppletParams(Map<String, String> info)
38   {
39     for (int i = params.length; --i >= 0;)
40     {
41       put(params[i], info.get(params[i]));
42     }
43   }
44
45   public String getParam(String param, String def)
46   {
47     String val = get(param);
48     return (val != null ? val : def);
49   }
50
51   // <applet
52   // code="jalview.bin.JalviewLite" width="140" height="35"
53   // archive="jalviewApplet.jar,JmolApplet-14.6.4_2016.10.26.jar,java-json.jar,json_simple-1.1.jar">
54   // <param name="permissions" value="sandbox"/>
55   // <param name="file" value="uniref50.fa"/>
56   // <param name="treeFile" value="ferredoxin.nw"/>
57   // <param name="userDefinedColour" value="C=yellow; R,K,H=FF5555;
58   // D,E=5555FF"/>
59   // <param name="sortByTree" value="True"/>
60   // <param name="showSequenceLogo" value="true"/>
61   // <param name="showGroupConsensus" value="true"/>
62   // <param name="showFullId" value="false"/>
63   // <param name="linkLabel_1" value="Uniprot"/>
64   // <param name="linkUrl_1"
65   // value="http://www.uniprot.org/uniprot/$SEQUENCE_ID$"/>
66   // <param name="linkLabel_2" value="EMBL-EBI Search"/>
67   // <param name="linkUrl_2"
68   // value="http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$"/>
69   // <param name="APPLICATION_URL"
70   // value="http://www.jalview.org/services/launchApp"/>
71   // </applet>
72   //
73   public AppletParams(String outerHTML)
74   {
75     String[] tokens = outerHTML.split("<param");
76     outerHTML = tokens[0];
77     String code = getAttr(outerHTML, "code");
78     if (!code.equals("jalview.bin.JalviewLite"))
79     {
80       return;
81     }
82     for (int i = tokens.length; --i > 0;)
83     {
84       String param = tokens[i];
85       String key = getAttr(param, "name");
86       if (key != null)
87       {
88         String value = getAttr(param, "value");
89         System.out.println("AppletParams " + key + " = \"" + value + "\"");
90         put(key, value);
91       }
92     }
93     put("_width", getAttr(outerHTML, "width"));
94     put("_height", getAttr(outerHTML, "height"));
95     put("_id", getAttr(outerHTML, "id"));
96     put("_name", getAttr(outerHTML, "name"));
97     put("_archive", getAttr(outerHTML, "archive"));
98     put("_code", code);
99   }
100
101   public AppletParams()
102   {
103     // TODO Auto-generated constructor stub
104   }
105
106   public static AppletParams getAppletParams(String[] args,
107           Vector<String> vargs)
108   {
109     AppletParams appletParams = new AppletParams();
110     String resourcePath = null;
111     for (int i = args.length; --i > 0;) // > 0 is correct, not >=0
112     {
113       if (args[i].startsWith("name=\"Info.resourcePath\""))
114       {
115         resourcePath = getAttr(args[i], "value");
116         if (resourcePath.length() > 0 && !resourcePath.endsWith("/"))
117         {
118           resourcePath += "/";
119         }
120         break;
121       }
122     }
123     for (int i = 1; i < args.length; i++)
124     {
125       String arg = args[i].trim();
126       if (arg.startsWith("name="))
127       {
128         String appName = getAttr(arg, "name");
129         String value = getAttr(arg, "value");
130         String appletName = appName.toLowerCase();
131
132         // note that Application arguments ARE case-sensitive, but
133         // Applet.getParameter() is not.
134
135         switch (appletName)
136         {
137         // tested, working:
138         case "jnetfile":
139           value = resourcePath + value;
140           appName = null;
141           break;
142         case "jpredfile":
143           value = resourcePath + value;
144           appName = null;
145           break;
146         case "features":
147           value = resourcePath + value;
148           appName = null;
149           break;
150         case "pdbfile":
151           appName = "PDBFILE";
152           value = resourcePath + value;
153           appName = null;
154           break;
155         case "tree":
156         case "treefile":
157           appName = "tree";
158           value = resourcePath + value;
159           appletName = null; // taken care of by Jalview
160           break;
161         // implemented; not tested:
162         case "annotations":
163           value = resourcePath + value;
164           appName = null;
165           break;
166         case "defaultcolour":
167           appName = "DEFAULT_COLOUR"; // defaultColour
168           break;
169         case "defaultcolournuc":
170           appName = "DEFAULT_COLOUR_NUC";// defaultColourNuc
171           break;
172         case "defaultcolourprot":
173           appName = "DEFAULT_COLOUR_PROT";// defaultColourProt";
174           break;
175         case "annotationcolour_max":
176           appName = "ANNOTATIONCOLOUR_MAX";
177           break;
178         case "annotationcolour_min":
179           appName = "ANNOTATIONCOLOUR_MIN";
180           break;
181         case "centrecolumnlabels":
182           appName = null;
183           break;
184         case "enablesplitframe":
185           appName = "enableSplitFrame";
186           break;
187         case "externalstructureviewer":
188           break;
189         case "alignpdbfiles":
190           break;
191         case "application_url":
192           appName = "APPLICATION_URL";
193           break;
194         case "automaticscrolling":
195           appName = "automaticScrolling";
196           break;
197         case "heightscale":
198           appName = "heightScale";
199           break;
200         case "hidefeaturegroups":
201           break;
202         case "jalviewhelpurl":
203           break;
204         case "label":
205           break;
206         case "linklabel_":
207           appName = "linkLabel_";
208           break;
209         case "linklabel_1":
210           appName = "linkLabel_1";
211           break;
212         case "linkurl_":
213           appName = "linkURL_";
214           break;
215         case "nojmol":
216           break;
217         case "normaliselogo":
218           appName = "normaliseLogo";
219           break;
220         case "normalisesequencelogo":
221           appName = "normaliseSequenceLogo";
222           break;
223         case "oninit":
224           break;
225         case "pdbseq":
226           appName = "PDBSEQ";
227           break;
228         case "relaxedidmatch":
229           break;
230         case "resolvetocodebase":
231           break;
232         case "rgb":
233           appName = "RGB";
234           break;
235         case "scaleproteinascdna":
236           appName = "scaleProteinAsCdna";
237           break;
238         case "separator":
239           break;
240         case "sequence":
241           break;
242         case "sortby":
243           break;
244         case "sortbytree":
245           value = checkTF(value);
246           appletName = null; // taken care of by Jalview
247           break;
248         case "format":
249           break;
250         case "scorefile":
251           appName = "scoreFile";
252           value = resourcePath + value;
253           break;
254         case "uppercase":
255           appName = "upperCase";
256           break;
257         case "userdefinedcolour":
258           appName = "colour";
259           break;
260         case "widthscale":
261           appName = "widthScale";
262           break;
263         case "windowheight":
264           appName = "windowHeight";
265           break;
266         case "windowwidth":
267           appName = "windowWidth";
268           break;
269         case "wrap":
270           break;
271
272         // cases not handled in doc/parameters.xlsx
273         case "debug":
274           value = checkTF(value);
275           break;
276         case "file":
277           appName = "open";
278           value = resourcePath + value;
279           break;
280         case "file2":
281           value = resourcePath + value;
282           break;
283         case "embedded":
284           value = checkTF(value);
285           break;
286         case "showannotation":
287           appName = "SHOW_ANNOTATIONS";
288           value = checkTF(value);
289           break;
290         case "showbutton":
291           value = checkTF(value);
292           break;
293         case "showconsensus":
294           appName = "SHOW_CONSENSUS_LOGO";
295           value = checkTF(value);
296           break;
297         case "showconsensushistogram":
298           appName = "SHOW_CONSENSUS_HISTOGRAM";
299           value = checkTF(value);
300           break;
301         case "showconservation":
302           appName = "SHOW_CONSERVATION";
303           value = checkTF(value);
304           break;
305         case "showfeaturegroups":
306           value = checkTF(value);
307           break;
308         case "showfeaturesettings":
309           appName = "showFeatureSettings";
310           value = checkTF(value);
311           break;
312         case "showfullid":
313           appName = "showFullId";
314           value = checkTF(value);
315           break;
316         case "showgroupconsensus":
317           appName = "SHOW_GROUP_CONSENSUS";
318           value = checkTF(value);
319           break;
320         case "showgroupconservation":
321           appName = "SHOW_GROUP_CONSERVATION";
322           value = checkTF(value);
323           break;
324         case "showoccupancy":
325           appName = "SHOW_OCCUPANCY";
326           value = checkTF(value);
327           break;
328         case "showquality":
329           appName = "SHOW_QUALITY";
330           value = checkTF(value);
331           break;
332         case "showsequencelogo":
333           appName = "showSequenceLogo";
334           value = checkTF(value);
335           break;
336         case "showtreebootstraps":
337           appName = "showTreeBootstraps";
338           value = checkTF(value);
339           break;
340         case "showtreedistances":
341           appName = "showTreeDistances";
342           value = checkTF(value);
343           break;
344         case "showunconserved":
345           appName = "showUnconserved";
346           value = checkTF(value);
347           break;
348         case "showunlinkedtreenodes":
349           appName = "showUnlinkedTreeNodes";
350           value = checkTF(value);
351           break;
352         default:
353           break;
354         }
355         if (value != null)
356         {
357           vargs.add(appName);
358           if (value != "true")
359           {
360             vargs.add(value);
361           }
362         }
363         if (value == null)
364         {
365           value = "false";
366         }
367         System.out.println("AppletParams appName=" + appName + "appletName="
368                 + appletName + " value=" + value);
369         Cache.setPropertyNoSave(appName, value);
370         if (appletName != null)
371         {
372           appletParams.put(appletName, value);
373         }
374       }
375     }
376     return appletParams;
377   }
378
379   /**
380    * Check for a single-argument option.
381    * 
382    * @param value
383    * @return "true" or null
384    */
385   private static String checkTF(String value)
386   {
387     return (value.toLowerCase() == "true" ? "true" : null);
388   }
389
390   /**
391    * Crude applet innerHTML parser
392    * 
393    * @param tag
394    * @param attr
395    * @return
396    */
397   private static String getAttr(String tag, String attr)
398   {
399     int pt = tag.indexOf(attr + "=\"");
400     if (pt < 0)
401     {
402       System.out
403               .println("AppletParams did not read " + attr + " in " + tag);
404       return null;
405     }
406     // <param name="sortByTree" value="True"/>
407     int pt1 = pt + attr.length() + 2;
408     int pt2 = tag.indexOf("\"", pt1);
409     return (pt < 0 ? null : tag.substring(pt1, pt2));
410   }
411
412   public static void main(String[] args)
413   {
414     new AppletParams("<applet\r\n"
415             + "    code=\"jalview.bin.JalviewLite\" width=\"140\" height=\"35\"\r\n"
416             + "    archive=\"jalviewApplet.jar,JmolApplet-14.6.4_2016.10.26.jar,java-json.jar,json_simple-1.1.jar\">  \r\n"
417             + "  <param name=\"permissions\" value=\"sandbox\"/>\r\n"
418             + "  <param name=\"file\" value=\"uniref50.fa\"/>\r\n"
419             + "  <param name=\"treeFile\" value=\"ferredoxin.nw\"/>\r\n"
420             + "  <param name=\"userDefinedColour\" value=\"C=yellow; R,K,H=FF5555; D,E=5555FF\"/>\r\n"
421             + "  <param name=\"sortByTree\" value=\"True\"/>\r\n"
422             + "  <param name=\"showSequenceLogo\" value=\"true\"/>\r\n"
423             + "  <param name=\"showGroupConsensus\" value=\"true\"/>\r\n"
424             + "  <param name=\"showFullId\" value=\"false\"/>\r\n"
425             + "    <param name=\"linkLabel_1\" value=\"Uniprot\"/>\r\n"
426             + "    <param name=\"linkUrl_1\" value=\"http://www.uniprot.org/uniprot/$SEQUENCE_ID$\"/>\r\n"
427             + "    <param name=\"linkLabel_2\" value=\"EMBL-EBI Search\"/>\r\n"
428             + "    <param name=\"linkUrl_2\" value=\"http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$\"/>\r\n"
429             + "    <param name=\"APPLICATION_URL\" value=\"http://www.jalview.org/services/launchApp\"/>\r\n"
430             + "     </applet>");
431   }
432
433 }