Merge branch 'Jalview-JS/develop' into merge_js_develop
[jalview.git] / src / jalview / bin / AppletParams.java
diff --git a/src/jalview/bin/AppletParams.java b/src/jalview/bin/AppletParams.java
new file mode 100644 (file)
index 0000000..726734d
--- /dev/null
@@ -0,0 +1,479 @@
+
+package jalview.bin;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import jalview.gui.Preferences;
+
+/**
+ * Collection of all known applet tags from JalviewLite.
+ * Three cases; can be one or more of these:
+ * 
+ * CASE I. args[] name and value for ArgsParser
+ * 
+ * CASE II. applet parameter for JalviewJSApp
+ * 
+ * CASE III. mapped to a Preference
+ * 
+ * 
+ * @author hansonr
+ *
+ */
+@SuppressWarnings("serial")
+public class AppletParams extends HashMap<String, Object>
+{
+
+  private final static String[] params = { 
+      "alignpdbfiles",
+      "ANNOTATIONCOLOUR_MAX", "ANNOTATIONCOLOUR_MIN",
+      "annotations",
+      "APPLICATION_URL", "automaticScrolling", "centrecolumnlabels",
+      "debug", "defaultColour", "defaultColourNuc", "defaultColourProt",
+      "embedded", "enableSplitFrame", "externalstructureviewer", "features",
+      "file", "file2", "format", "heightScale", "hidefeaturegroups",
+      "jalviewhelpurl", "jnetfile", "jpredfile", "label", "linkLabel_",
+      "linkLabel_1", "linkURL_", "nojmol", "normaliseLogo",
+      "normaliseSequenceLogo", "oninit", "PDBFILE", "PDBSEQ",
+      "relaxedidmatch", "resolvetocodebase", "RGB", "scaleProteinAsCdna",
+      "scoreFile", "separator", "sequence", "showAnnotation", "showbutton",
+      "showConsensus", "showConsensusHistogram", "showConservation",
+      "showfeaturegroups", "showFeatureSettings", "showFullId",
+      "showGroupConsensus", "showGroupConservation", "showOccupancy",
+      "showQuality", "showSequenceLogo", "showTreeBootstraps",
+      "showTreeDistances", "showUnconserved", "showUnlinkedTreeNodes",
+      "sortBy", "sortByTree", "tree", "treeFile", "upperCase",
+      "userDefinedColour", "widthScale", "windowHeight", "windowWidth",
+      "wrap", };
+
+  public String getParam(String param, String def)
+  {
+    Object val = get(param);
+    return (val != null ? val.toString() : def);
+  }
+
+  // <applet
+  // code="jalview.bin.JalviewLite" width="140" height="35"
+  // archive="jalviewApplet.jar,JmolApplet-14.6.4_2016.10.26.jar,java-json.jar,json_simple-1.1.jar">
+  // <param name="permissions" value="sandbox"/>
+  // <param name="file" value="uniref50.fa"/>
+  // <param name="treeFile" value="ferredoxin.nw"/>
+  // <param name="userDefinedColour" value="C=yellow; R,K,H=FF5555;
+  // D,E=5555FF"/>
+  // <param name="sortByTree" value="True"/>
+  // <param name="showSequenceLogo" value="true"/>
+  // <param name="showGroupConsensus" value="true"/>
+  // <param name="showFullId" value="false"/>
+  // <param name="linkLabel_1" value="Uniprot"/>
+  // <param name="linkUrl_1"
+  // value="http://www.uniprot.org/uniprot/$SEQUENCE_ID$"/>
+  // <param name="linkLabel_2" value="EMBL-EBI Search"/>
+  // <param name="linkUrl_2"
+  // value="http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$"/>
+  // <param name="APPLICATION_URL"
+  // value="http://www.jalview.org/services/launchApp"/>
+  // </applet>
+  //
+  public AppletParams(String outerHTML)
+  {
+    String[] tokens = outerHTML.split("<param");
+    outerHTML = tokens[0];
+    String code = getAttr(outerHTML, "code");
+    if (!code.equals("jalview.bin.JalviewLite"))
+    {
+      return;
+    }
+    for (int i = tokens.length; --i > 0;)
+    {
+      String param = tokens[i];
+      String key = getAttr(param, "name");
+      if (key != null)
+      {
+        String value = getAttr(param, "value");
+        System.out.println("AppletParams " + key + " = \"" + value + "\"");
+        put(key, value);
+      }
+    }
+    put("_width", getAttr(outerHTML, "width"));
+    put("_height", getAttr(outerHTML, "height"));
+    put("_id", getAttr(outerHTML, "id"));
+    put("_name", getAttr(outerHTML, "name"));
+    put("_archive", getAttr(outerHTML, "archive"));
+    put("_code", code);
+  }
+
+  public AppletParams()
+  {
+  }
+
+  public static AppletParams getAppletParams(Map<String, Object> map,
+          List<String> vargs)
+  {
+    AppletParams appletParams = new AppletParams();
+    String resourcePath = getString(map, "resourcePath");
+    if (resourcePath == null)
+      resourcePath = "";
+    if (resourcePath.length() > 0 && !resourcePath.endsWith("/"))
+    {
+      resourcePath += "/";
+    }
+    for (int i = params.length; --i >= 0;)
+    {
+      String prefName = params[i];
+      Object value = map.get(prefName);
+      if (value != null)
+        addParam(vargs, prefName, value, appletParams, resourcePath);
+    }
+    return appletParams;
+  }
+
+  private static String getString(Map<String, Object> map, String key)
+  {
+    Object o = map.get(key);  
+    return (o == null ? null : o.toString());
+  }
+
+  public static AppletParams getAppletParams(String[] args,
+          List<String> vargs)
+  {
+    AppletParams appletParams = new AppletParams();
+    String resourcePath = "";
+    for (int i = args.length; --i > 0;) // > 0 is correct, not >=0
+    {
+      if (args[i].startsWith("name=\"Info.resourcePath\""))
+      {
+        resourcePath = getAttr(args[i], "value");
+        if (resourcePath.length() > 0 && !resourcePath.endsWith("/"))
+        {
+          resourcePath += "/";
+        }
+        break;
+      }
+    }
+    for (int i = 1; i < args.length; i++)
+    {
+      String arg = args[i].trim();
+      if (arg.startsWith("name="))
+      {
+        String prefName = getAttr(arg, "name");
+        String value = getAttr(arg, "value");
+        addParam(vargs, prefName, value, appletParams, resourcePath);
+      }
+    }
+    return appletParams;
+  }
+
+  private static void addParam(List<String> vargs, String prefName,
+          Object value, AppletParams appletParams, String resourcePath)
+  {
+
+    // note that Application arguments ARE case-sensitive, but
+    // Applet.getParameter() is not.
+
+    // prefName // CASE III
+
+    String argName = null;  // CASE I                
+
+    String appletName = prefName.toLowerCase();   // CASE II
+
+    // by nulling one or more of these names, that route will not be used.
+    
+    switch (appletName)
+    {
+
+    case "file":
+      argName = "open";
+      prefName = null;
+      value = resourcePath + value;
+      break;
+    case "file2":
+      argName = "open2";
+      prefName = null;
+      value = resourcePath + value;
+      break;
+    case "oninit":
+    case "hidefeaturegroups":
+      // applet parameter only
+      // setting argName to null indicates that we want
+      // JalviewJSApp to take care of this using getParameter or getParameterAsObject      
+     prefName = argName = null;
+      break;
+    case "tree":
+    case "treefile":
+      // setting appletName to null indicates that we want
+      // Jalview.doMain to taken care of this as Jalview args
+      argName = "tree";
+      prefName = null;
+      value = resourcePath + value;
+      break;
+
+    case "features":
+    case "jnetfile":
+    case "jpredfile":
+    case "pdbfile":
+    case "scorefile":
+    case "sequence":
+    case "annotations":
+      prefName = argName = null;
+      value = resourcePath + value;
+      break;
+
+      // non-loading preferences
+
+    case "defaultcolour":
+      prefName = Preferences.DEFAULT_COLOUR;
+      break;
+    case "defaultcolournuc":
+      prefName = Preferences.DEFAULT_COLOUR_NUC;
+      break;
+    case "defaultcolourprot":
+      prefName = Preferences.DEFAULT_COLOUR_PROT;
+      break;
+    case "annotationcolour_max":
+      prefName = Preferences.ANNOTATIONCOLOUR_MAX;
+      break;
+    case "annotationcolour_min":
+      prefName = Preferences.ANNOTATIONCOLOUR_MIN;
+      break;
+    case "enablesplitframe":
+      prefName = Preferences.ENABLE_SPLIT_FRAME;
+      break;
+    case "centrecolumnlabels":
+      prefName = Preferences.CENTRE_COLUMN_LABELS;
+      break;
+    case "sortby":
+      prefName = Preferences.SORT_ALIGNMENT; // id, etc.
+      break;
+    case "normalisesequencelogo":
+      prefName = Preferences.NORMALISE_CONSENSUS_LOGO;
+      break;
+    case "relaxedidmatch":
+      prefName = Preferences.RELAXEDSEQIDMATCHING;
+      break;
+    case "scaleproteinascdna":
+      prefName = Preferences.SCALE_PROTEIN_TO_CDNA;
+      break;
+    case "userdefinedcolour":
+      argName = "colour";
+      prefName = Preferences.USER_DEFINED_COLOURS;
+      break;
+    case "wrap":
+      prefName = Preferences.WRAP_ALIGNMENT;
+      break;
+    case "sortbytree":
+      argName = prefName;
+      prefName = Preferences.SORT_BY_TREE;
+      value = checkTF(value);
+      break;
+
+    // implemented; not tested:
+
+    case "pdbseq":
+    case "alignpdbfiles":
+      prefName = null;
+      break;
+    case "format":
+      argName = prefName;
+      break;
+    case "separator":
+      argName = prefName;
+      break;
+
+    // TODO: probably not relevant?
+
+    case "rgb":
+      prefName = null; // TODO no background for application?
+      break;
+    case "externalstructureviewer":
+      break;
+    case "application_url":
+      break;
+    case "automaticscrolling":
+      break;
+    case "heightscale":
+      break;
+    case "jalviewhelpurl":
+      break;
+    case "label":
+      break;
+    case "linklabel_":
+      prefName = "linkLabel_";
+      break;
+    case "linklabel_1":
+      prefName = "linkLabel_1";
+      break;
+    case "linkurl_":
+      prefName = "linkURL_";
+      break;
+
+    // unknown:
+
+    case "nojmol":
+    case "normaliselogo":
+    case "resolvetocodebase":
+    case "uppercase":
+    case "widthscale":
+    case "windowheight":
+    case "windowwidth":
+      prefName = null;
+      break;
+
+    // TRUE/FALSE
+
+    case "debug":
+    case "embedded":
+    case "showbutton":
+      value = checkTF(value);
+      break;
+    case "showannotation":
+      prefName = Preferences.SHOW_ANNOTATIONS;
+      value = checkTF(value);
+      break;
+    case "showconsensus":
+      prefName = Preferences.SHOW_CONSENSUS_LOGO;
+      value = checkTF(value);
+      break;
+    case "showconsensushistogram":
+      prefName = Preferences.SHOW_CONSENSUS_HISTOGRAM;
+      value = checkTF(value);
+      break;
+    case "showconservation":
+      prefName = Preferences.SHOW_CONSERVATION;
+      value = checkTF(value);
+      break;
+    case "showgroupconsensus":
+      prefName = Preferences.SHOW_GROUP_CONSENSUS;
+      value = checkTF(value);
+      break;
+    case "showgroupconservation":
+      prefName = Preferences.SHOW_GROUP_CONSERVATION;
+      value = checkTF(value);
+      break;
+    case "showoccupancy":
+      prefName = Preferences.SHOW_OCCUPANCY;
+      value = checkTF(value);
+      break;
+    case "showquality":
+      prefName = Preferences.SHOW_QUALITY;
+      value = checkTF(value);
+      break;
+    case "showsequencelogo":
+      prefName = Preferences.SHOW_CONSENSUS_LOGO;
+      value = checkTF(value);
+      break;
+    case "showunconserved":
+      prefName = Preferences.SHOW_UNCONSERVED;
+      value = checkTF(value);
+      break;
+    case "showfeaturegroups":
+    case "showfeaturesettings":
+    case "showfullid":
+    case "showtreebootstraps":
+    case "showtreedistances":
+    case "showunlinkedtreenodes":
+      value = checkTF(value);
+      break;
+    default:
+      if (appletName.startsWith("pdbfile")
+              || appletName.startsWith("sequence") && Character
+                      .isDigit(appletName.charAt(appletName.length() - 1)))
+      {
+        // could be pdbFile2, for example
+        prefName = argName = null;
+        value = resourcePath + value;
+        break;
+      }
+      // or one of the app preference names
+      break;
+    }
+
+    // CASE I.  args[] name and value for ArgsParser
+    //
+    // If given an argument name, 
+    // put name and value into application args
+    if (value != null && argName != null)
+    {
+      vargs.add(argName);
+      if (value != "true")
+      {
+        vargs.add(value.toString());
+      }
+    }
+    
+    // CASE II.   applet parameter for JalviewJSApp
+    
+    if (value == null)
+    {
+      value = "false";
+    }
+    System.out.println("AppletParams propName=" + prefName + " argName="
+            + argName + " appletName=" + appletName + " value=" + value);    
+    if (appletName != null)
+    {
+      appletParams.put(appletName, value);
+    }
+    
+    // CASE III.  mapped to a Preference
+    
+    if (prefName != null)
+    {
+      Cache.setPropertyNoSave(prefName, value.toString());
+    }
+  }
+
+  /**
+   * Check for a single-argument option.
+   * 
+   * @param value
+   * @return "true" or null
+   */
+  private static String checkTF(Object value)
+  {
+    return (("" + value).toLowerCase() == "true" ? "true" : null);
+  }
+
+  /**
+   * Crude applet innerHTML parser
+   * 
+   * @param tag
+   * @param attr
+   * @return
+   */
+  private static String getAttr(String tag, String attr)
+  {
+    int pt = tag.indexOf(attr + "=\"");
+    if (pt < 0)
+    {
+      System.out
+              .println("AppletParams did not read " + attr + " in " + tag);
+      return null;
+    }
+    // <param name="sortByTree" value="True"/>
+    int pt1 = pt + attr.length() + 2;
+    int pt2 = tag.indexOf("\"", pt1);
+    return (pt < 0 ? null : tag.substring(pt1, pt2));
+  }
+
+  public static void main(String[] args)
+  {
+    new AppletParams("<applet\r\n"
+            + "    code=\"jalview.bin.JalviewLite\" width=\"140\" height=\"35\"\r\n"
+            + "    archive=\"jalviewApplet.jar,JmolApplet-14.6.4_2016.10.26.jar,java-json.jar,json_simple-1.1.jar\">  \r\n"
+            + "  <param name=\"permissions\" value=\"sandbox\"/>\r\n"
+            + "  <param name=\"file\" value=\"uniref50.fa\"/>\r\n"
+            + "  <param name=\"treeFile\" value=\"ferredoxin.nw\"/>\r\n"
+            + "  <param name=\"userDefinedColour\" value=\"C=yellow; R,K,H=FF5555; D,E=5555FF\"/>\r\n"
+            + "  <param name=\"sortByTree\" value=\"True\"/>\r\n"
+            + "  <param name=\"showSequenceLogo\" value=\"true\"/>\r\n"
+            + "  <param name=\"showGroupConsensus\" value=\"true\"/>\r\n"
+            + "  <param name=\"showFullId\" value=\"false\"/>\r\n"
+            + "    <param name=\"linkLabel_1\" value=\"Uniprot\"/>\r\n"
+            + "    <param name=\"linkUrl_1\" value=\"http://www.uniprot.org/uniprot/$SEQUENCE_ID$\"/>\r\n"
+            + "    <param name=\"linkLabel_2\" value=\"EMBL-EBI Search\"/>\r\n"
+            + "    <param name=\"linkUrl_2\" value=\"http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$\"/>\r\n"
+            + "    <param name=\"APPLICATION_URL\" value=\"http://www.jalview.org/services/launchApp\"/>\r\n"
+            + "     </applet>");
+  }
+
+}
\ No newline at end of file