1 |
/* |
2 |
* jlscp - a java LinuxSampler control protocol API |
3 |
* |
4 |
* Copyright (C) 2005 Grigor Kirilov Iliev |
5 |
* |
6 |
* This file is part of jlscp. |
7 |
* |
8 |
* jlscp is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License version 2 |
10 |
* as published by the Free Software Foundation. |
11 |
* |
12 |
* jlscp is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with jlscp; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
* MA 02111-1307 USA |
21 |
*/ |
22 |
|
23 |
package org.linuxsampler.lscp; |
24 |
|
25 |
import java.io.IOException; |
26 |
|
27 |
import java.util.Vector; |
28 |
|
29 |
|
30 |
/** |
31 |
* This class contains only helper functions that are used from the other classes in this library. |
32 |
* @author Grigor Iliev |
33 |
*/ |
34 |
final class Parser { |
35 |
/** |
36 |
* Parses an integer value. |
37 |
* @param s The integer value to be parsed. |
38 |
* @throws LscpException If the string does not contain valid integer value. |
39 |
*/ |
40 |
protected static int |
41 |
parseInt(String s) throws LscpException { |
42 |
try { return Integer.parseInt(s); } |
43 |
catch(NumberFormatException x) { |
44 |
throw new LscpException(LscpI18n.getLogMsg("Parser.notInt!", s), x); |
45 |
} |
46 |
} |
47 |
|
48 |
/** |
49 |
* Parses a float value. |
50 |
* @param s The float value to be parsed. |
51 |
* @throws LscpException If the string does not contain valid float value. |
52 |
*/ |
53 |
protected static float |
54 |
parseFloat(String s) throws LscpException { |
55 |
try { return Float.parseFloat(s); } |
56 |
catch(NumberFormatException x) { |
57 |
throw new LscpException(LscpI18n.getLogMsg("Parser.notFloat!", s)); |
58 |
} |
59 |
} |
60 |
|
61 |
/** |
62 |
* Parses a comma separated list. |
63 |
* |
64 |
* @param list The comma separated list. |
65 |
* @return A <code>String</code> array containing all items in the list. |
66 |
*/ |
67 |
protected static String[] |
68 |
parseList(String list) { |
69 |
if(list == null || list.length() == 0) return new String[0]; |
70 |
int pos = 0; |
71 |
int idx; |
72 |
Vector<String> v = new Vector<String>(); |
73 |
while((idx = list.indexOf(',', pos)) > 0) { |
74 |
v.add(list.substring(pos, idx)); |
75 |
pos = idx + 1; |
76 |
} |
77 |
|
78 |
if(pos < list.length()) v.add(list.substring(pos)); |
79 |
return v.toArray(new String[v.size()]); |
80 |
} |
81 |
|
82 |
/** |
83 |
* Parses a comma separated list with boolean values. |
84 |
* |
85 |
* @param list The comma separated list with boolean values. |
86 |
* @return A <code>Boolean</code> array containing all items in the list. |
87 |
*/ |
88 |
protected static Boolean[] |
89 |
parseBoolList(String list) { |
90 |
String[] ar = parseList(list); |
91 |
|
92 |
Boolean[] bar = new Boolean[ar.length]; |
93 |
for(int i = 0; i < ar.length; i++) { |
94 |
bar[i] = Boolean.parseBoolean(ar[i]); |
95 |
} |
96 |
|
97 |
return bar; |
98 |
} |
99 |
|
100 |
/** |
101 |
* Parses a comma separated list with integer values. |
102 |
* |
103 |
* @param list The comma separated list with integer values. |
104 |
* @return A <code>Integer</code> array containing all items in the list. |
105 |
* |
106 |
* @throws LscpException if the list contains value(s) from different type. |
107 |
*/ |
108 |
protected static Integer[] |
109 |
parseIntList(String list) throws LscpException { |
110 |
String[] ar = parseList(list); |
111 |
|
112 |
Integer[] iar = new Integer[ar.length]; |
113 |
for(int i = 0; i < ar.length; i++) iar[i] = parseInt(ar[i]); |
114 |
|
115 |
return iar; |
116 |
} |
117 |
|
118 |
/** |
119 |
* Parses a comma separated list with float values. |
120 |
* |
121 |
* @param list The comma separated list with float values. |
122 |
* @return A <code>Float</code> array containing all items in the list. |
123 |
* |
124 |
* @throws LscpException if the list contains value(s) from different type. |
125 |
*/ |
126 |
protected static Float[] |
127 |
parseFloatList(String list) throws LscpException { |
128 |
String[] ar = parseList(list); |
129 |
|
130 |
Float[] far = new Float[ar.length]; |
131 |
for(int i = 0; i < ar.length; i++) far[i] = parseFloat(ar[i]); |
132 |
|
133 |
return far; |
134 |
} |
135 |
|
136 |
/** |
137 |
* Parses a comma separated list whose items are encapsulated into apostrophes. |
138 |
* |
139 |
* @param list The comma separated list. |
140 |
* @return A <code>String</code> array containing all items in the list. |
141 |
* |
142 |
* @throws LscpException if the list is broken. |
143 |
*/ |
144 |
protected static String[] |
145 |
parseStringList(String list) throws LscpException { |
146 |
if(list == null || list.length() == 0) return new String[0]; |
147 |
int q1 = 0, q2 = 0; |
148 |
Vector<String> v = new Vector<String>(); |
149 |
|
150 |
for(;;) { |
151 |
if(list.charAt(q1) != '\'') |
152 |
throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!")); |
153 |
q2 = list.indexOf('\'', q1 + 1); |
154 |
if(q2 == -1) throw new LscpException(LscpI18n.getLogMsg("Parser.EOL!")); |
155 |
v.add(list.substring(q1 + 1, q2)); |
156 |
|
157 |
if(q2 + 1 >= list.length()) break; |
158 |
|
159 |
if(list.charAt(q2 + 1) != ',') |
160 |
throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!")); |
161 |
q1 = q2 + 2; |
162 |
if(q1 >= list.length()) |
163 |
throw new LscpException(LscpI18n.getLogMsg("Parser.EOL!")); |
164 |
} |
165 |
|
166 |
return v.toArray(new String[v.size()]); |
167 |
} |
168 |
|
169 |
protected static String[][] |
170 |
parseListOfStringLists(String list) throws LscpException { |
171 |
if(list.length() == 0) return new String[0][0]; |
172 |
|
173 |
String[][] s2S; |
174 |
if(!list.startsWith("''") && !list.startsWith("\"\"")) { |
175 |
s2S = new String[1][]; |
176 |
s2S[0] = parseStringList(list); |
177 |
return s2S; |
178 |
} |
179 |
|
180 |
int i = 0, i2 = 0; |
181 |
Vector<String> v = new Vector<String>(); |
182 |
|
183 |
for(;;) { |
184 |
i2 = getEndListIndex(i, list); |
185 |
v.add(list.substring(i + 1, i2)); |
186 |
if(i2 == list.length() - 1) break; |
187 |
if(list.charAt(i2 + 1) != ',') |
188 |
throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!")); |
189 |
i = i2 + 2; |
190 |
} |
191 |
|
192 |
s2S = new String[v.size()][]; |
193 |
for(i = 0; i < v.size(); i++) s2S[i] = Parser.parseStringList(v.get(i)); |
194 |
|
195 |
return s2S; |
196 |
} |
197 |
|
198 |
/** Helper function used by <code>parseListOfStringLists</code>. */ |
199 |
private static int |
200 |
getEndListIndex(int start, String list) throws LscpException { |
201 |
int i = start + 1; |
202 |
char q = list.charAt(0); // quote symbol |
203 |
if(list.charAt(start) != q) |
204 |
throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!")); |
205 |
|
206 |
if(list.charAt(i) == '\'') { // Check for empty list |
207 |
if(i == list.length() - 1 || list.charAt(i + 1) == ',') return i; |
208 |
} |
209 |
|
210 |
for(;;) { |
211 |
if(list.charAt(i) != q) |
212 |
throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!")); |
213 |
i = list.indexOf(q, i + 1); |
214 |
if(i == -1 || i == list.length() - 1) |
215 |
throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!")); |
216 |
|
217 |
if(list.charAt(i + 1) == q) return i + 1; |
218 |
|
219 |
if(list.charAt(i + 1) != ',') |
220 |
throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!")); |
221 |
i += 2; |
222 |
} |
223 |
} |
224 |
|
225 |
|
226 |
/** |
227 |
* Gets the type of the parameter represented by the specified result set. |
228 |
* @param resultSet A <code>String</code> array containing the information categories |
229 |
* of a multi-line result set. |
230 |
* @return The type of the parameter represented by the specified result set or |
231 |
* <code>null</code> if the specified result set does not contain |
232 |
* <code>TYPE</code> category. |
233 |
*/ |
234 |
protected static ParameterType |
235 |
parseType(String[] resultSet) { |
236 |
if(resultSet == null || resultSet.length == 0) return null; |
237 |
for(String s : resultSet) { |
238 |
if(s.startsWith("TYPE: ")) { |
239 |
String type = s.substring("TYPE: ".length(), s.length()); |
240 |
if(type.equals("BOOL")) return ParameterType.BOOL; |
241 |
if(type.equals("INT")) return ParameterType.INT; |
242 |
if(type.equals("FOAT")) return ParameterType.FLOAT; |
243 |
if(type.equals("STRING")) return ParameterType.STRING; |
244 |
} |
245 |
} |
246 |
return null; |
247 |
} |
248 |
|
249 |
/** |
250 |
* Determines whether the parameter represented by the specified result set allows |
251 |
* only one value or a list of values. |
252 |
* @param resultSet A <code>String</code> array containing the information categories |
253 |
* of a multi-line result set. |
254 |
* @return <code>false</code> if the parameter represented by the specified result set |
255 |
* allows only one value and <code>true</code> if allows a list of values. |
256 |
*/ |
257 |
protected static Boolean |
258 |
parseMultiplicity(String[] resultSet) { |
259 |
if(resultSet == null || resultSet.length == 0) return null; |
260 |
|
261 |
for(String s : resultSet) { |
262 |
if(s.startsWith("MULTIPLICITY: ")) return Boolean.parseBoolean ( |
263 |
s.substring("MULTIPLICITY: ".length(), s.length()) |
264 |
); |
265 |
} |
266 |
|
267 |
return null; |
268 |
} |
269 |
|
270 |
/** |
271 |
* Parses an empty result set and returns an appropriate <code>ResultSet</code> object. |
272 |
* Notice that the result set may be of type warning or error. |
273 |
* @param ln A <code>String</code> representing the single line result set to be parsed. |
274 |
* @return A <code>ResultSet</code> object. |
275 |
* @throws LscpException If LSCP protocol error occurs. |
276 |
* @throws LSException If the LinuxSampler instance returns error message. |
277 |
*/ |
278 |
protected static ResultSet |
279 |
parseEmptyResultSet(String ln) throws LscpException, LSException { |
280 |
ResultSet rs = new ResultSet(); |
281 |
|
282 |
if(ln.equals("OK")) { |
283 |
return rs; |
284 |
} else if(ln.startsWith("OK[") && ln.endsWith("]")) { |
285 |
ln = ln.substring("OK[".length(), ln.length() - 1); |
286 |
try { |
287 |
rs.setIndex(Integer.parseInt(ln)); |
288 |
return rs; |
289 |
} catch(NumberFormatException x) { |
290 |
throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"), x); |
291 |
} |
292 |
} else if(ln.startsWith("WRN")) { |
293 |
parseWarning(ln, rs); |
294 |
Client.getLogger().warning(rs.getMessage()); |
295 |
return rs; |
296 |
} else if(ln.startsWith("ERR:")) { |
297 |
parseError(ln, rs); |
298 |
throw new LSException(rs.getCode(), rs.getMessage()); |
299 |
} |
300 |
|
301 |
throw new LscpException(LscpI18n.getLogMsg("CommandFailed!") ); |
302 |
} |
303 |
|
304 |
/** |
305 |
* Parses warning message. |
306 |
* @param ln The warning message to be parsed. |
307 |
* @param rs A <code>ResultSet</code> instance where the warning must be stored. |
308 |
* @throws LscpException If LSCP protocol corruption occurs. |
309 |
*/ |
310 |
protected static void |
311 |
parseWarning(String ln, ResultSet rs) throws LscpException { |
312 |
if(!ln.startsWith("WRN")) |
313 |
throw new LscpException(LscpI18n.getLogMsg("CommandFailed!")); |
314 |
|
315 |
int i, j; |
316 |
rs.setWarning(true); |
317 |
|
318 |
if(ln.charAt(3) == '[') { |
319 |
i = ln.indexOf(']'); |
320 |
if(i == -1) throw new LscpException(LscpI18n.getLogMsg("CommandFailed!")); |
321 |
|
322 |
try { |
323 |
j = Integer.parseInt(ln.substring("WRN[".length(), i)); |
324 |
rs.setIndex(j); |
325 |
} catch(NumberFormatException x) { |
326 |
throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"), x); |
327 |
} |
328 |
|
329 |
if(ln.charAt(i + 1) != ':') |
330 |
throw new LscpException(LscpI18n.getLogMsg("CommandFailed!")); |
331 |
} |
332 |
|
333 |
i = ln.indexOf(':'); |
334 |
if(i == -1) throw new LscpException(LscpI18n.getLogMsg("CommandFailed!")); |
335 |
j = ln.indexOf(':', i + 1); |
336 |
if(j == -1) throw new LscpException(LscpI18n.getLogMsg("CommandFailed!")); |
337 |
|
338 |
try { rs.setCode(Integer.parseInt(ln.substring(i + 1, j))); } |
339 |
catch(NumberFormatException x) { |
340 |
throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"), x); |
341 |
} |
342 |
|
343 |
rs.setMessage(ln.substring(j + 1)); |
344 |
} |
345 |
|
346 |
/** |
347 |
* Parses error message. |
348 |
* @param ln The error message to be parsed. |
349 |
* @param rs A <code>ResultSet</code> instance where the error must be stored. |
350 |
* @throws LscpException If LSCP protocol corruption occurs. |
351 |
*/ |
352 |
protected static void |
353 |
parseError(String ln, ResultSet rs) throws LscpException { |
354 |
if(!ln.startsWith("ERR:")) |
355 |
throw new LscpException(LscpI18n.getLogMsg("CommandFailed!")); |
356 |
|
357 |
int i = ln.indexOf(':', "ERR:".length()); |
358 |
if(i == -1) throw new LscpException(LscpI18n.getLogMsg("CommandFailed!")); |
359 |
|
360 |
try { rs.setCode(Integer.parseInt(ln.substring("ERR:".length(), i))); } |
361 |
catch(NumberFormatException x) { |
362 |
throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"), x); |
363 |
} |
364 |
|
365 |
rs.setMessage(ln.substring(i + 1)); |
366 |
} |
367 |
|
368 |
/** |
369 |
* Gets the info character string to the specified information category. |
370 |
* @param resultSet A <code>String</code> array containing the information categories |
371 |
* of a multi-line result set. |
372 |
* @param category Specifies the category whose info character string to be returned. |
373 |
* @return The info character string to the specified information category or |
374 |
* <code>null</code> if the specified result set does not contain that category. |
375 |
*/ |
376 |
protected static String |
377 |
getCategoryInfo(String[] resultSet, String category) { |
378 |
String c = category + ": "; |
379 |
for(String s : resultSet) |
380 |
if(s.startsWith(c)) return s.substring(c.length(), s.length()); |
381 |
|
382 |
return null; |
383 |
} |
384 |
} |