Change header template for a new version
[jabaws.git] / webservices / compbio / ws / client / CmdHelper.java
1 /* Copyright (c) 2011 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 package compbio.ws.client;\r
19 import static compbio.ws.client.Constraints.hostkey;\r
20 import static compbio.ws.client.Constraints.limitList;\r
21 import static compbio.ws.client.Constraints.listServices;\r
22 import static compbio.ws.client.Constraints.paramList;\r
23 import static compbio.ws.client.Constraints.presetList;\r
24 import static compbio.ws.client.Constraints.presetkey;\r
25 import static compbio.ws.client.Constraints.pseparator;\r
26 import static compbio.ws.client.Constraints.servicekey;\r
27 import static compbio.ws.client.Constraints.testKey;\r
28 \r
29 class CmdHelper {\r
30 \r
31         /**\r
32          * Check whether presetList is set in the command line\r
33          * \r
34          * @param cmd\r
35          *            command line options\r
36          * @return true if presetList is found, false otherwise\r
37          */\r
38         static boolean listPresets(String[] cmd) {\r
39                 return keyFound(cmd, presetList);\r
40         }\r
41 \r
42         /**\r
43          * Checks whether limitList parameter is in the command line\r
44          * \r
45          * @param cmd\r
46          *            - command line options\r
47          * @return true if it is, false otherwise\r
48          */\r
49         static boolean listLimits(String[] cmd) {\r
50                 return keyFound(cmd, limitList);\r
51         }\r
52 \r
53         /**\r
54          * list available services\r
55          * \r
56          * @param cmd\r
57          * @return\r
58          */\r
59         static boolean listServices(String[] cmd) {\r
60                 return keyFound(cmd, listServices);\r
61         }\r
62 \r
63         /**\r
64          * tests service\r
65          * \r
66          * @param cmd\r
67          * @return\r
68          */\r
69         static boolean testService(String[] cmd) {\r
70                 return keyFound(cmd, testKey);\r
71         }\r
72 \r
73         /**\r
74          * Checks whether the key is in the command line\r
75          * \r
76          * @param cmd\r
77          * @param key\r
78          * @return true if it is, false otherwise\r
79          */\r
80         static boolean keyFound(String[] cmd, String key) {\r
81                 assert cmd != null && cmd.length > 0;\r
82                 assert key != null;\r
83                 for (int i = 0; i < cmd.length; i++) {\r
84                         String listPresets = cmd[i];\r
85                         if (listPresets.trim().equalsIgnoreCase(key)) {\r
86                                 return true;\r
87                         }\r
88                 }\r
89                 return false;\r
90         }\r
91 \r
92         /**\r
93          * Extracts preset name from the command line is any\r
94          * \r
95          * @param cmd\r
96          *            command line options\r
97          * @return presetName or null if no presets is defined\r
98          */\r
99         static String getPresetName(String[] cmd) {\r
100                 String preset = null;\r
101                 for (int i = 0; i < cmd.length; i++) {\r
102                         String presetPrm = cmd[i];\r
103                         if (presetPrm.trim().toLowerCase()\r
104                                         .startsWith(presetkey + pseparator)) {\r
105                                 preset = presetPrm.substring(presetPrm.indexOf(pseparator) + 1);\r
106                                 break;\r
107                         }\r
108                 }\r
109                 return preset;\r
110         }\r
111 \r
112         /**\r
113          * Extracts service name from the command line\r
114          * \r
115          * @param cmd\r
116          *            command line options\r
117          * @return service name or null if it is not defined\r
118          */\r
119         public static String getServiceName(String[] cmd) {\r
120                 for (int i = 0; i < cmd.length; i++) {\r
121                         String serv = cmd[i];\r
122                         if (serv.trim().toLowerCase().startsWith(servicekey + pseparator)) {\r
123                                 return serv.substring(serv.indexOf(pseparator) + 1);\r
124                         }\r
125                 }\r
126                 return null;\r
127         }\r
128 \r
129         /**\r
130          * Extracts host name from the command line\r
131          * \r
132          * @param cmd\r
133          *            command line options\r
134          * @return host name or null if it is not defined\r
135          */\r
136         public static String getHost(String[] cmd) {\r
137                 for (int i = 0; i < cmd.length; i++) {\r
138                         String host = cmd[i];\r
139                         if (host.trim().toLowerCase().startsWith(hostkey + pseparator)) {\r
140                                 return host.substring(host.indexOf(pseparator) + 1);\r
141                         }\r
142                 }\r
143                 return null;\r
144         }\r
145 \r
146         /**\r
147          * Searches the command line keys in the array of parameters\r
148          * \r
149          * @param cmd\r
150          *            command line options\r
151          * @return true is the list of Parameters is requested, false otherwise\r
152          */\r
153         static boolean listParameters(String[] cmd) {\r
154                 return keyFound(cmd, paramList);\r
155         }\r
156 \r
157 }\r