5b33aba439e4e44382889d69a5ffa25891d194ec
[jabaws.git] / webservices / compbio / ws / client / CmdHelper.java
1 package compbio.ws.client;\r
2 import static compbio.ws.client.Constraints.hostkey;\r
3 import static compbio.ws.client.Constraints.limitList;\r
4 import static compbio.ws.client.Constraints.listServices;\r
5 import static compbio.ws.client.Constraints.paramList;\r
6 import static compbio.ws.client.Constraints.presetList;\r
7 import static compbio.ws.client.Constraints.presetkey;\r
8 import static compbio.ws.client.Constraints.pseparator;\r
9 import static compbio.ws.client.Constraints.servicekey;\r
10 import static compbio.ws.client.Constraints.testKey;\r
11 \r
12 class CmdHelper {\r
13 \r
14         /**\r
15          * Check whether presetList is set in the command line\r
16          * \r
17          * @param cmd\r
18          *            command line options\r
19          * @return true if presetList is found, false otherwise\r
20          */\r
21         static boolean listPresets(String[] cmd) {\r
22                 return keyFound(cmd, presetList);\r
23         }\r
24 \r
25         /**\r
26          * Checks whether limitList parameter is in the command line\r
27          * \r
28          * @param cmd\r
29          *            - command line options\r
30          * @return true if it is, false otherwise\r
31          */\r
32         static boolean listLimits(String[] cmd) {\r
33                 return keyFound(cmd, limitList);\r
34         }\r
35 \r
36         /**\r
37          * list available services\r
38          * \r
39          * @param cmd\r
40          * @return\r
41          */\r
42         static boolean listServices(String[] cmd) {\r
43                 return keyFound(cmd, listServices);\r
44         }\r
45 \r
46         /**\r
47          * tests service\r
48          * \r
49          * @param cmd\r
50          * @return\r
51          */\r
52         static boolean testService(String[] cmd) {\r
53                 return keyFound(cmd, testKey);\r
54         }\r
55 \r
56         /**\r
57          * Checks whether the key is in the command line\r
58          * \r
59          * @param cmd\r
60          * @param key\r
61          * @return true if it is, false otherwise\r
62          */\r
63         static boolean keyFound(String[] cmd, String key) {\r
64                 assert cmd != null && cmd.length > 0;\r
65                 assert key != null;\r
66                 for (int i = 0; i < cmd.length; i++) {\r
67                         String listPresets = cmd[i];\r
68                         if (listPresets.trim().equalsIgnoreCase(key)) {\r
69                                 return true;\r
70                         }\r
71                 }\r
72                 return false;\r
73         }\r
74 \r
75         /**\r
76          * Extracts preset name from the command line is any\r
77          * \r
78          * @param cmd\r
79          *            command line options\r
80          * @return presetName or null if no presets is defined\r
81          */\r
82         static String getPresetName(String[] cmd) {\r
83                 String preset = null;\r
84                 for (int i = 0; i < cmd.length; i++) {\r
85                         String presetPrm = cmd[i];\r
86                         if (presetPrm.trim().toLowerCase()\r
87                                         .startsWith(presetkey + pseparator)) {\r
88                                 preset = presetPrm.substring(presetPrm.indexOf(pseparator) + 1);\r
89                                 break;\r
90                         }\r
91                 }\r
92                 return preset;\r
93         }\r
94 \r
95         /**\r
96          * Extracts service name from the command line\r
97          * \r
98          * @param cmd\r
99          *            command line options\r
100          * @return service name or null if it is not defined\r
101          */\r
102         public static String getServiceName(String[] cmd) {\r
103                 for (int i = 0; i < cmd.length; i++) {\r
104                         String serv = cmd[i];\r
105                         if (serv.trim().toLowerCase().startsWith(servicekey + pseparator)) {\r
106                                 return serv.substring(serv.indexOf(pseparator) + 1);\r
107                         }\r
108                 }\r
109                 return null;\r
110         }\r
111 \r
112         /**\r
113          * Extracts host name from the command line\r
114          * \r
115          * @param cmd\r
116          *            command line options\r
117          * @return host name or null if it is not defined\r
118          */\r
119         public static String getHost(String[] cmd) {\r
120                 for (int i = 0; i < cmd.length; i++) {\r
121                         String host = cmd[i];\r
122                         if (host.trim().toLowerCase().startsWith(hostkey + pseparator)) {\r
123                                 return host.substring(host.indexOf(pseparator) + 1);\r
124                         }\r
125                 }\r
126                 return null;\r
127         }\r
128 \r
129         /**\r
130          * Searches the command line keys in the array of parameters\r
131          * \r
132          * @param cmd\r
133          *            command line options\r
134          * @return true is the list of Parameters is requested, false otherwise\r
135          */\r
136         static boolean listParameters(String[] cmd) {\r
137                 return keyFound(cmd, paramList);\r
138         }\r
139 \r
140 }\r