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 { 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); } // // // // // // // // // // // // // // // // public AppletParams(String outerHTML) { String[] tokens = outerHTML.split(" 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 map, List 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 map, String key) { Object o = map.get(key); return (o == null ? null : o.toString()); } public static AppletParams getAppletParams(String[] args, List 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 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; } // 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(" \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " "); } }