/[svn]/jlscp/trunk/src/org/linuxsampler/lscp/Parser.java
ViewVC logotype

Contents of /jlscp/trunk/src/org/linuxsampler/lscp/Parser.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1202 - (show annotations) (download)
Thu May 24 20:17:25 2007 UTC (16 years, 10 months ago) by iliev
File size: 14928 byte(s)
* updated to version 0.5a

1 /*
2 * jlscp - a java LinuxSampler control protocol API
3 *
4 * Copyright (C) 2005-2006 Grigor Iliev <grigor@grigoriliev.com>
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 /** Forbits the instantiatrion of this class */
36 private Parser() { }
37
38 /**
39 * Parses an integer value.
40 * @param s The integer value to be parsed.
41 * @throws LscpException If the string does not contain valid integer value.
42 */
43 protected static int
44 parseInt(String s) throws LscpException {
45 try { return Integer.parseInt(s); }
46 catch(NumberFormatException x) {
47 throw new LscpException(LscpI18n.getLogMsg("Parser.notInt!", s), x);
48 }
49 }
50
51 /**
52 * Parses a float value.
53 * @param s The float value to be parsed.
54 * @throws LscpException If the string does not contain valid float value.
55 */
56 protected static float
57 parseFloat(String s) throws LscpException {
58 try { return Float.parseFloat(s); }
59 catch(NumberFormatException x) {
60 throw new LscpException(LscpI18n.getLogMsg("Parser.notFloat!", s));
61 }
62 }
63
64 /**
65 * Parses a comma separated list.
66 *
67 * @param list The comma separated list.
68 * @return A <code>String</code> array containing all items in the list.
69 */
70 protected static String[]
71 parseList(String list) { return parseList(list, ','); }
72
73 /**
74 * Parses a list.
75 * @param list The list to parse.
76 * @param separator Provides the character used as separator.
77 * @return A <code>String</code> array containing all items in the list.
78 */
79 protected static String[]
80 parseList(String list, char separator) {
81 if(list == null || list.length() == 0) return new String[0];
82 int pos = 0;
83 int idx;
84 Vector<String> v = new Vector<String>();
85 while((idx = list.indexOf(separator, pos)) > 0) {
86 v.add(list.substring(pos, idx));
87 pos = idx + 1;
88 }
89
90 if(pos < list.length()) v.add(list.substring(pos));
91 return v.toArray(new String[v.size()]);
92 }
93
94 /**
95 * Parses a comma separated list with boolean values.
96 *
97 * @param list The comma separated list with boolean values.
98 * @return A <code>Boolean</code> array containing all items in the list.
99 */
100 protected static Boolean[]
101 parseBoolList(String list) {
102 String[] ar = parseList(list);
103
104 Boolean[] bar = new Boolean[ar.length];
105 for(int i = 0; i < ar.length; i++) {
106 bar[i] = Boolean.parseBoolean(ar[i]);
107 }
108
109 return bar;
110 }
111
112 /**
113 * Parses a comma separated list with integer values.
114 *
115 * @param list The comma separated list with integer values.
116 * @return A <code>Integer</code> array containing all items in the list.
117 *
118 * @throws LscpException if the list contains value(s) from different type.
119 */
120 protected static Integer[]
121 parseIntList(String list) throws LscpException { return parseIntList(list, ','); }
122
123 /**
124 * Parses a list of integer values.
125 *
126 * @param list The list of integer values.
127 * @param separator Provides the character used as separator.
128 * @return A <code>Integer</code> array containing all items in the list.
129 *
130 * @throws LscpException if the list contains value(s) from different type.
131 */
132 protected static Integer[]
133 parseIntList(String list, char separator) throws LscpException {
134 String[] ar = parseList(list, separator);
135
136 Integer[] iar = new Integer[ar.length];
137 for(int i = 0; i < ar.length; i++) iar[i] = parseInt(ar[i]);
138
139 return iar;
140 }
141
142 /**
143 * Parses a comma separated list with float values.
144 *
145 * @param list The comma separated list with float values.
146 * @return A <code>Float</code> array containing all items in the list.
147 *
148 * @throws LscpException if the list contains value(s) from different type.
149 */
150 protected static Float[]
151 parseFloatList(String list) throws LscpException {
152 String[] ar = parseList(list);
153
154 Float[] far = new Float[ar.length];
155 for(int i = 0; i < ar.length; i++) far[i] = parseFloat(ar[i]);
156
157 return far;
158 }
159 /**
160 * Parses a comma separated list whose items are encapsulated into apostrophes.
161 * @param list The comma separated list.
162 * @return A <code>String</code> array containing all items in the list.
163 * @throws LscpException if the list is broken.
164 */
165 protected static String[]
166 parseStringList(String list) throws LscpException {
167 return parseStringList(list, ',');
168 }
169
170 /**
171 * Parses a list whose items are encapsulated into apostrophes.
172 * @param list The list of strings.
173 * @param separator Provides the character used as separator.
174 * @return A <code>String</code> array containing all items in the list.
175 * @throws LscpException if the list is broken.
176 */
177 protected static String[]
178 parseStringList(String list, char separator) throws LscpException {
179 if(list == null || list.length() == 0) return new String[0];
180 int q1 = 0, q2 = 0;
181 Vector<String> v = new Vector<String>();
182
183 for(;;) {
184 if(list.charAt(q1) != '\'')
185 throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!"));
186 q2 = list.indexOf('\'', q1 + 1);
187 if(q2 == -1) throw new LscpException(LscpI18n.getLogMsg("Parser.EOL!"));
188 v.add(list.substring(q1 + 1, q2));
189
190 if(q2 + 1 >= list.length()) break;
191
192 if(list.charAt(q2 + 1) != separator)
193 throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!"));
194 q1 = q2 + 2;
195 if(q1 >= list.length())
196 throw new LscpException(LscpI18n.getLogMsg("Parser.EOL!"));
197 }
198
199 return v.toArray(new String[v.size()]);
200 }
201
202 protected static String[][]
203 parseListOfStringLists(String list) throws LscpException {
204 if(list.length() == 0) return new String[0][0];
205
206 String[][] s2S;
207 if(!list.startsWith("''") && !list.startsWith("\"\"")) {
208 s2S = new String[1][];
209 s2S[0] = parseStringList(list);
210 return s2S;
211 }
212
213 int i = 0, i2 = 0;
214 Vector<String> v = new Vector<String>();
215
216 for(;;) {
217 i2 = getEndListIndex(i, list);
218 v.add(list.substring(i + 1, i2));
219 if(i2 == list.length() - 1) break;
220 if(list.charAt(i2 + 1) != ',')
221 throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!"));
222 i = i2 + 2;
223 }
224
225 s2S = new String[v.size()][];
226 for(i = 0; i < v.size(); i++) s2S[i] = Parser.parseStringList(v.get(i));
227
228 return s2S;
229 }
230
231 /**
232 * Parses a comma separated list whose items are encapsulated into curly braces.
233 *
234 * @param list The comma separated list.
235 * @return A <code>String</code> array containing all items in the list.
236 *
237 * @throws LscpException if the list is broken.
238 */
239 protected static String[]
240 parseArray(String list) throws LscpException {
241 if(list == null || list.length() == 0) return new String[0];
242 int q1 = 0, q2 = 0;
243 Vector<String> v = new Vector<String>();
244
245 for(;;) {
246 if(list.charAt(q1) != '{')
247 throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!"));
248 q2 = list.indexOf('}', q1 + 1);
249 if(q2 == -1) throw new LscpException(LscpI18n.getLogMsg("Parser.EOL!"));
250 v.add(list.substring(q1 + 1, q2));
251
252 if(q2 + 1 >= list.length()) break;
253
254 if(list.charAt(q2 + 1) != ',')
255 throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!"));
256 q1 = q2 + 2;
257 if(q1 >= list.length())
258 throw new LscpException(LscpI18n.getLogMsg("Parser.EOL!"));
259 }
260
261 return v.toArray(new String[v.size()]);
262 }
263
264 /** Helper function used by <code>parseListOfStringLists</code>. */
265 private static int
266 getEndListIndex(int start, String list) throws LscpException {
267 int i = start + 1;
268 char q = list.charAt(0); // quote symbol
269 if(list.charAt(start) != q)
270 throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!"));
271
272 if(list.charAt(i) == '\'') { // Check for empty list
273 if(i == list.length() - 1 || list.charAt(i + 1) == ',') return i;
274 }
275
276 for(;;) {
277 if(list.charAt(i) != q)
278 throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!"));
279 i = list.indexOf(q, i + 1);
280 if(i == -1 || i == list.length() - 1)
281 throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!"));
282
283 if(list.charAt(i + 1) == q) return i + 1;
284
285 if(list.charAt(i + 1) != ',')
286 throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!"));
287 i += 2;
288 }
289 }
290
291
292 /**
293 * Gets the type of the parameter represented by the specified result set.
294 * @param resultSet A <code>String</code> array containing the information categories
295 * of a multi-line result set.
296 * @return The type of the parameter represented by the specified result set or
297 * <code>null</code> if the specified result set does not contain
298 * <code>TYPE</code> category.
299 */
300 protected static ParameterType
301 parseType(String[] resultSet) {
302 if(resultSet == null || resultSet.length == 0) return null;
303 for(String s : resultSet) {
304 if(s.startsWith("TYPE: ")) {
305 String type = s.substring("TYPE: ".length(), s.length());
306 if(type.equals("BOOL")) return ParameterType.BOOL;
307 if(type.equals("INT")) return ParameterType.INT;
308 if(type.equals("FOAT")) return ParameterType.FLOAT;
309 if(type.equals("STRING")) return ParameterType.STRING;
310 }
311 }
312 return null;
313 }
314
315 /**
316 * Determines whether the parameter represented by the specified result set allows
317 * only one value or a list of values.
318 * @param resultSet A <code>String</code> array containing the information categories
319 * of a multi-line result set.
320 * @return <code>false</code> if the parameter represented by the specified result set
321 * allows only one value and <code>true</code> if allows a list of values.
322 */
323 protected static Boolean
324 parseMultiplicity(String[] resultSet) {
325 if(resultSet == null || resultSet.length == 0) return null;
326
327 for(String s : resultSet) {
328 if(s.startsWith("MULTIPLICITY: ")) return Boolean.parseBoolean (
329 s.substring("MULTIPLICITY: ".length(), s.length())
330 );
331 }
332
333 return null;
334 }
335
336 /**
337 * Parses an empty result set and returns an appropriate <code>ResultSet</code> object.
338 * Notice that the result set may be of type warning or error.
339 * @param ln A <code>String</code> representing the single line result set to be parsed.
340 * @return A <code>ResultSet</code> object.
341 * @throws LscpException If LSCP protocol error occurs.
342 * @throws LSException If the LinuxSampler instance returns error message.
343 */
344 protected static ResultSet
345 parseEmptyResultSet(String ln) throws LscpException, LSException {
346 ResultSet rs = new ResultSet();
347
348 if(ln.equals("OK")) {
349 return rs;
350 } else if(ln.startsWith("OK[") && ln.endsWith("]")) {
351 ln = ln.substring("OK[".length(), ln.length() - 1);
352 try {
353 rs.setIndex(Integer.parseInt(ln));
354 return rs;
355 } catch(NumberFormatException x) {
356 throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"), x);
357 }
358 } else if(ln.startsWith("WRN")) {
359 parseWarning(ln, rs);
360 Client.getLogger().warning(rs.getMessage());
361 return rs;
362 } else if(ln.startsWith("ERR:")) {
363 parseError(ln, rs);
364 throw new LSException(rs.getCode(), rs.getMessage());
365 }
366
367 throw new LscpException(LscpI18n.getLogMsg("CommandFailed!") );
368 }
369
370 /**
371 * Parses warning message.
372 * @param ln The warning message to be parsed.
373 * @param rs A <code>ResultSet</code> instance where the warning must be stored.
374 * @throws LscpException If LSCP protocol corruption occurs.
375 */
376 protected static void
377 parseWarning(String ln, ResultSet rs) throws LscpException {
378 if(!ln.startsWith("WRN"))
379 throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"));
380
381 int i, j;
382 rs.setWarning(true);
383
384 if(ln.charAt(3) == '[') {
385 i = ln.indexOf(']');
386 if(i == -1) throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"));
387
388 try {
389 j = Integer.parseInt(ln.substring("WRN[".length(), i));
390 rs.setIndex(j);
391 } catch(NumberFormatException x) {
392 throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"), x);
393 }
394
395 if(ln.charAt(i + 1) != ':')
396 throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"));
397 }
398
399 i = ln.indexOf(':');
400 if(i == -1) throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"));
401 j = ln.indexOf(':', i + 1);
402 if(j == -1) throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"));
403
404 try { rs.setCode(Integer.parseInt(ln.substring(i + 1, j))); }
405 catch(NumberFormatException x) {
406 throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"), x);
407 }
408
409 rs.setMessage(ln.substring(j + 1));
410 }
411
412 /**
413 * Parses error message.
414 * @param ln The error message to be parsed.
415 * @param rs A <code>ResultSet</code> instance where the error must be stored.
416 * @throws LscpException If LSCP protocol corruption occurs.
417 */
418 protected static void
419 parseError(String ln, ResultSet rs) throws LscpException {
420 if(!ln.startsWith("ERR:"))
421 throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"));
422
423 int i = ln.indexOf(':', "ERR:".length());
424 if(i == -1) throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"));
425
426 try { rs.setCode(Integer.parseInt(ln.substring("ERR:".length(), i))); }
427 catch(NumberFormatException x) {
428 throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"), x);
429 }
430
431 rs.setMessage(ln.substring(i + 1));
432 }
433
434 /**
435 * Gets the info character string to the specified information category.
436 * @param resultSet A <code>String</code> array containing the information categories
437 * of a multi-line result set.
438 * @param category Specifies the category whose info character string to be returned.
439 * @return The info character string to the specified information category or
440 * <code>null</code> if the specified result set does not contain that category.
441 */
442 protected static String
443 getCategoryInfo(String[] resultSet, String category) {
444 String c = category + ": ";
445 for(String s : resultSet)
446 if(s.startsWith(c)) return s.substring(c.length(), s.length());
447
448 return null;
449 }
450
451 /**
452 * Eliminates the quotation marks if the string is quoted.
453 * @return New string without quotation marks if the string is quoted; else
454 * the same string is returned.
455 */
456 protected static String
457 removeQuotation(String s) {
458 if(s == null || s.length() < 2) return s;
459 char q = s.charAt(0);
460 char q2 = s.charAt(s.length() - 1);
461 if((q == '\'' && q2 == '\'') || (q == '"' && q2 == '"'))
462 return s.substring(1, s.length() - 1);
463
464 return s;
465 }
466 }

  ViewVC Help
Powered by ViewVC