JAL-3253-applet adds NOMENUBAR, NOSTATUS, NOCALCULATION, SHOWOVERVIEW
[jalview.git] / src / jalview / bin / ArgsParser.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.bin;
22
23 import java.net.URLDecoder;
24 import java.util.Vector;
25
26 /**
27  * Notes: this argParser does not distinguish between parameter switches,
28  * parameter values and argument text. If an argument happens to be identical to
29  * a parameter, it will be taken as such (even though it didn't have a '-'
30  * prefixing it).
31  * 
32  * @author Andrew Waterhouse and JBP documented.
33  * 
34  */
35 public class ArgsParser
36 {
37
38   // BH 2019 - new
39
40   public static final String NOCALCULATION = "nocalculation";
41
42   public static final String NOMENUBAR = "nomenubar";
43
44   public static final String NOSTATUS = "nostatus";
45
46   public static final String SHOWOVERVIEW = "showoverview";
47
48   //
49   public static final String ANNOTATIONS = "annotations";
50
51   public static final String COLOUR = "colour";
52
53   public static final String FEATURES = "features";
54
55   public static final String GROOVY = "groovy";
56
57   public static final String GROUPS = "groups";
58
59   public static final String HEADLESS = "headless";
60
61   public static final String JABAWS = "jabaws";
62
63   public static final String NOANNOTATION = "no-annotation";
64
65   public static final String NOANNOTATION2 = "noannotation"; // BH 2019.05.07
66
67   public static final String NODISPLAY = "nodisplay";
68
69   public static final String NOGUI = "nogui";
70
71   public static final String NONEWS = "nonews";
72
73   public static final String NOQUESTIONNAIRE = "noquestionnaire";
74
75   public static final String NOSORTBYTREE = "nosortbytree";
76
77   public static final String NOUSAGESTATS = "nousagestats";
78
79   public static final String OPEN = "open";
80
81   public static final String OPEN2 = "open2"; // BH added -- for applet
82                                               // compatibility; not fully
83                                               // implemented
84
85   public static final String PROPS = "props";
86
87   public static final String QUESTIONNAIRE = "questionnaire";
88
89   public static final String SETPROP = "setprop";
90
91   public static final String SORTBYTREE = "sortbytree";
92
93   public static final String TREE = "tree";
94
95   public static final String VDOC = "vdoc";
96
97   public static final String VSESS = "vsess";
98
99   private Vector<String> vargs = null;
100
101   private boolean isApplet;
102
103   private AppletParams appletParams;
104
105   public boolean isApplet()
106   {
107     return isApplet;
108   }
109
110   public ArgsParser(String[] args)
111   {
112     vargs = new Vector<>();
113     isApplet = (args.length > 0 && args[0].startsWith("<applet"));
114     if (isApplet)
115     {
116       appletParams = AppletParams.getAppletParams(args, vargs);
117     }
118     else
119     {
120       for (int i = 0; i < args.length; i++)
121       {
122         String arg = args[i].trim();
123         if (arg.charAt(0) == '-')
124         {
125           arg = arg.substring(1);
126         }
127         vargs.addElement(arg);
128       }
129     }
130   }
131
132   /**
133    * check for and remove first occurence of arg+parameter in arglist.
134    * 
135    * @param arg
136    * @return return the argument following the given arg if arg was in list.
137    */
138   public String getValue(String arg)
139   {
140     return getValue(arg, false);
141   }
142
143   public String getValue(String arg, boolean utf8decode)
144   {
145     int index = vargs.indexOf(arg);
146     String dc = null, ret = null;
147     if (index != -1)
148     {
149       ret = vargs.elementAt(index + 1).toString();
150       vargs.removeElementAt(index);
151       vargs.removeElementAt(index);
152       if (utf8decode && ret != null)
153       {
154         try
155         {
156           dc = URLDecoder.decode(ret, "UTF-8");
157           ret = dc;
158         } catch (Exception e)
159         {
160           // TODO: log failure to decode
161         }
162       }
163     }
164     return ret;
165   }
166
167   /**
168    * check for and remove first occurence of arg in arglist.
169    * 
170    * @param arg
171    * @return true if arg was present in argslist.
172    */
173   public boolean contains(String arg)
174   {
175     if (vargs.contains(arg))
176     {
177       vargs.removeElement(arg);
178       return true;
179     }
180     else
181     {
182       return false;
183     }
184   }
185
186   public String nextValue()
187   {
188     return vargs.remove(0);
189   }
190
191   public int getSize()
192   {
193     return vargs.size();
194   }
195
196   public String getAppletValue(String key, String def)
197   {
198     String value;
199     return (appletParams == null ? null
200             : (value = appletParams.get(key.toLowerCase())) != null ? value
201                     : def);
202   }
203
204 }