applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / test / simpleclient / CommandProcessor.java
1 /*\r
2  * This file is part of the Vamsas Client version 0.1. \r
3  * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
4  *  Andrew Waterhouse and Dominik Lindner.\r
5  * \r
6  * Earlier versions have also been incorporated into Jalview version 2.4 \r
7  * since 2008, and TOPALi version 2 since 2007.\r
8  * \r
9  * The Vamsas Client is free software: you can redistribute it and/or modify\r
10  * it under the terms of the GNU Lesser General Public License as published by\r
11  * the Free Software Foundation, either version 3 of the License, or\r
12  * (at your option) any later version.\r
13  *  \r
14  * The Vamsas Client is distributed in the hope that it will be useful,\r
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
17  * GNU Lesser General Public License for more details.\r
18  * \r
19  * You should have received a copy of the GNU Lesser General Public License\r
20  * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.\r
21  */\r
22 package uk.ac.vamsas.test.simpleclient;\r
23 \r
24 import java.util.Iterator;\r
25 import java.util.Vector;\r
26 \r
27 public class CommandProcessor {\r
28   /**\r
29    * this is not getOPT!!!! - processes a *series* of space separated commands -\r
30    * some of which take arguments.\r
31    */\r
32   private Vector commands;\r
33 \r
34   /*\r
35    * static { ClientsFileTest.commands=new Vector();\r
36    * ClientsFileTest.commands.add(new String("add"));\r
37    * ClientsFileTest.commands.add(new String("remove"));\r
38    * ClientsFileTest.commands.add(new String("list"));\r
39    * ClientsFileTest.commands.add(new String("clear"));\r
40    * ClientsFileTest.commands.add(new String("watch"));\r
41    * ClientsFileTest.commands.add(new String("monitor")); }\r
42    */\r
43 \r
44   public int addCommand(String cmd, int argneed, String complainString) {\r
45     int cnum = 0;\r
46     if (commands == null)\r
47       commands = new Vector();\r
48     else\r
49       cnum = commands.size();\r
50     Vector cv = new Vector();\r
51     cv.add(new String(cmd));\r
52     cv.add(new Integer(argneed));\r
53     cv.add(new String(complainString));\r
54     commands.add(cv);\r
55     return cnum;\r
56   }\r
57 \r
58   /**\r
59    * Integer argl, Integer argpos, String cmd, Integer argneed, String msg in\r
60    * vector\r
61    */\r
62   public void complainArgs(int argl, int argpos, Vector ca) {\r
63     int argneed = ((Integer) ca.get(1)).intValue();\r
64     if (argl - argpos < argneed)\r
65       throw new Error(((String) ca.get(0)) + " at position " + argpos\r
66           + " needs " + argneed + " arguments : " + (String) ca.get(2));\r
67   }\r
68 \r
69   /**\r
70    * find and verify a command\r
71    * \r
72    * @param args\r
73    *          argstring\r
74    * @param argpos\r
75    *          position to check for command\r
76    * @return matching command or -1\r
77    */\r
78   public int getCommand(String[] args, int argpos) {\r
79     Iterator coms = commands.iterator();\r
80     int com = -1, argc;\r
81     argc = argpos;\r
82     while ((coms != null) && coms.hasNext()) {\r
83       com++;\r
84       Vector comnext = (Vector) coms.next();\r
85       if (args[argc].toLowerCase().equals((String) comnext.get(0))) {\r
86         if (comnext.size() > 2)\r
87           complainArgs(args.length, argc + 1, comnext);\r
88         return com;\r
89       }\r
90     }\r
91     return -1;\r
92 \r
93   }\r
94 }\r