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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1139 - (hide annotations) (download)
Mon Apr 2 20:43:58 2007 UTC (17 years, 1 month ago) by iliev
File size: 14496 byte(s)
* upgraded to version 0.4a

1 iliev 596 /*
2     * jlscp - a java LinuxSampler control protocol API
3     *
4 iliev 1139 * Copyright (C) 2005-2006 Grigor Iliev <grigor@grigoriliev.com>
5 iliev 596 *
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 iliev 1139 /** Forbits the instantiatrion of this class */
36     private Parser() { }
37    
38 iliev 596 /**
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 iliev 1139 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 iliev 596 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 iliev 1139 while((idx = list.indexOf(separator, pos)) > 0) {
86 iliev 596 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 iliev 1139 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 iliev 596
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     /**
161     * Parses a comma separated list whose items are encapsulated into apostrophes.
162     *
163     * @param list The comma separated list.
164     * @return A <code>String</code> array containing all items in the list.
165     *
166     * @throws LscpException if the list is broken.
167     */
168     protected static String[]
169     parseStringList(String list) throws LscpException {
170     if(list == null || list.length() == 0) return new String[0];
171     int q1 = 0, q2 = 0;
172     Vector<String> v = new Vector<String>();
173    
174     for(;;) {
175     if(list.charAt(q1) != '\'')
176     throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!"));
177     q2 = list.indexOf('\'', q1 + 1);
178     if(q2 == -1) throw new LscpException(LscpI18n.getLogMsg("Parser.EOL!"));
179     v.add(list.substring(q1 + 1, q2));
180    
181     if(q2 + 1 >= list.length()) break;
182    
183     if(list.charAt(q2 + 1) != ',')
184     throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!"));
185     q1 = q2 + 2;
186     if(q1 >= list.length())
187     throw new LscpException(LscpI18n.getLogMsg("Parser.EOL!"));
188     }
189    
190     return v.toArray(new String[v.size()]);
191     }
192    
193     protected static String[][]
194     parseListOfStringLists(String list) throws LscpException {
195     if(list.length() == 0) return new String[0][0];
196    
197     String[][] s2S;
198     if(!list.startsWith("''") && !list.startsWith("\"\"")) {
199     s2S = new String[1][];
200     s2S[0] = parseStringList(list);
201     return s2S;
202     }
203    
204     int i = 0, i2 = 0;
205     Vector<String> v = new Vector<String>();
206    
207     for(;;) {
208     i2 = getEndListIndex(i, list);
209     v.add(list.substring(i + 1, i2));
210     if(i2 == list.length() - 1) break;
211     if(list.charAt(i2 + 1) != ',')
212     throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!"));
213     i = i2 + 2;
214     }
215    
216     s2S = new String[v.size()][];
217     for(i = 0; i < v.size(); i++) s2S[i] = Parser.parseStringList(v.get(i));
218    
219     return s2S;
220     }
221    
222 iliev 1139 /**
223     * Parses a comma separated list whose items are encapsulated into curly braces.
224     *
225     * @param list The comma separated list.
226     * @return A <code>String</code> array containing all items in the list.
227     *
228     * @throws LscpException if the list is broken.
229     */
230     protected static String[]
231     parseArray(String list) throws LscpException {
232     if(list == null || list.length() == 0) return new String[0];
233     int q1 = 0, q2 = 0;
234     Vector<String> v = new Vector<String>();
235    
236     for(;;) {
237     if(list.charAt(q1) != '{')
238     throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!"));
239     q2 = list.indexOf('}', q1 + 1);
240     if(q2 == -1) throw new LscpException(LscpI18n.getLogMsg("Parser.EOL!"));
241     v.add(list.substring(q1 + 1, q2));
242    
243     if(q2 + 1 >= list.length()) break;
244    
245     if(list.charAt(q2 + 1) != ',')
246     throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!"));
247     q1 = q2 + 2;
248     if(q1 >= list.length())
249     throw new LscpException(LscpI18n.getLogMsg("Parser.EOL!"));
250     }
251    
252     return v.toArray(new String[v.size()]);
253     }
254    
255 iliev 596 /** Helper function used by <code>parseListOfStringLists</code>. */
256     private static int
257     getEndListIndex(int start, String list) throws LscpException {
258     int i = start + 1;
259     char q = list.charAt(0); // quote symbol
260     if(list.charAt(start) != q)
261     throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!"));
262    
263     if(list.charAt(i) == '\'') { // Check for empty list
264     if(i == list.length() - 1 || list.charAt(i + 1) == ',') return i;
265     }
266    
267     for(;;) {
268     if(list.charAt(i) != q)
269     throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!"));
270     i = list.indexOf(q, i + 1);
271     if(i == -1 || i == list.length() - 1)
272     throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!"));
273    
274     if(list.charAt(i + 1) == q) return i + 1;
275    
276     if(list.charAt(i + 1) != ',')
277     throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!"));
278     i += 2;
279     }
280     }
281    
282    
283     /**
284     * Gets the type of the parameter represented by the specified result set.
285     * @param resultSet A <code>String</code> array containing the information categories
286     * of a multi-line result set.
287     * @return The type of the parameter represented by the specified result set or
288     * <code>null</code> if the specified result set does not contain
289     * <code>TYPE</code> category.
290     */
291     protected static ParameterType
292     parseType(String[] resultSet) {
293     if(resultSet == null || resultSet.length == 0) return null;
294     for(String s : resultSet) {
295     if(s.startsWith("TYPE: ")) {
296     String type = s.substring("TYPE: ".length(), s.length());
297     if(type.equals("BOOL")) return ParameterType.BOOL;
298     if(type.equals("INT")) return ParameterType.INT;
299     if(type.equals("FOAT")) return ParameterType.FLOAT;
300     if(type.equals("STRING")) return ParameterType.STRING;
301     }
302     }
303     return null;
304     }
305    
306     /**
307     * Determines whether the parameter represented by the specified result set allows
308     * only one value or a list of values.
309     * @param resultSet A <code>String</code> array containing the information categories
310     * of a multi-line result set.
311     * @return <code>false</code> if the parameter represented by the specified result set
312     * allows only one value and <code>true</code> if allows a list of values.
313     */
314     protected static Boolean
315     parseMultiplicity(String[] resultSet) {
316     if(resultSet == null || resultSet.length == 0) return null;
317    
318     for(String s : resultSet) {
319     if(s.startsWith("MULTIPLICITY: ")) return Boolean.parseBoolean (
320     s.substring("MULTIPLICITY: ".length(), s.length())
321     );
322     }
323    
324     return null;
325     }
326    
327     /**
328     * Parses an empty result set and returns an appropriate <code>ResultSet</code> object.
329     * Notice that the result set may be of type warning or error.
330     * @param ln A <code>String</code> representing the single line result set to be parsed.
331     * @return A <code>ResultSet</code> object.
332     * @throws LscpException If LSCP protocol error occurs.
333     * @throws LSException If the LinuxSampler instance returns error message.
334     */
335     protected static ResultSet
336     parseEmptyResultSet(String ln) throws LscpException, LSException {
337     ResultSet rs = new ResultSet();
338    
339     if(ln.equals("OK")) {
340     return rs;
341     } else if(ln.startsWith("OK[") && ln.endsWith("]")) {
342     ln = ln.substring("OK[".length(), ln.length() - 1);
343     try {
344     rs.setIndex(Integer.parseInt(ln));
345     return rs;
346     } catch(NumberFormatException x) {
347     throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"), x);
348     }
349     } else if(ln.startsWith("WRN")) {
350     parseWarning(ln, rs);
351     Client.getLogger().warning(rs.getMessage());
352     return rs;
353     } else if(ln.startsWith("ERR:")) {
354     parseError(ln, rs);
355     throw new LSException(rs.getCode(), rs.getMessage());
356     }
357    
358     throw new LscpException(LscpI18n.getLogMsg("CommandFailed!") );
359     }
360    
361     /**
362     * Parses warning message.
363     * @param ln The warning message to be parsed.
364     * @param rs A <code>ResultSet</code> instance where the warning must be stored.
365     * @throws LscpException If LSCP protocol corruption occurs.
366     */
367     protected static void
368     parseWarning(String ln, ResultSet rs) throws LscpException {
369     if(!ln.startsWith("WRN"))
370     throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"));
371    
372     int i, j;
373     rs.setWarning(true);
374    
375     if(ln.charAt(3) == '[') {
376     i = ln.indexOf(']');
377     if(i == -1) throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"));
378    
379     try {
380     j = Integer.parseInt(ln.substring("WRN[".length(), i));
381     rs.setIndex(j);
382     } catch(NumberFormatException x) {
383     throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"), x);
384     }
385    
386     if(ln.charAt(i + 1) != ':')
387     throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"));
388     }
389    
390     i = ln.indexOf(':');
391     if(i == -1) throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"));
392     j = ln.indexOf(':', i + 1);
393     if(j == -1) throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"));
394    
395     try { rs.setCode(Integer.parseInt(ln.substring(i + 1, j))); }
396     catch(NumberFormatException x) {
397     throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"), x);
398     }
399    
400     rs.setMessage(ln.substring(j + 1));
401     }
402    
403     /**
404     * Parses error message.
405     * @param ln The error message to be parsed.
406     * @param rs A <code>ResultSet</code> instance where the error must be stored.
407     * @throws LscpException If LSCP protocol corruption occurs.
408     */
409     protected static void
410     parseError(String ln, ResultSet rs) throws LscpException {
411     if(!ln.startsWith("ERR:"))
412     throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"));
413    
414     int i = ln.indexOf(':', "ERR:".length());
415     if(i == -1) throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"));
416    
417     try { rs.setCode(Integer.parseInt(ln.substring("ERR:".length(), i))); }
418     catch(NumberFormatException x) {
419     throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"), x);
420     }
421    
422     rs.setMessage(ln.substring(i + 1));
423     }
424    
425     /**
426     * Gets the info character string to the specified information category.
427     * @param resultSet A <code>String</code> array containing the information categories
428     * of a multi-line result set.
429     * @param category Specifies the category whose info character string to be returned.
430     * @return The info character string to the specified information category or
431     * <code>null</code> if the specified result set does not contain that category.
432     */
433     protected static String
434     getCategoryInfo(String[] resultSet, String category) {
435     String c = category + ": ";
436     for(String s : resultSet)
437     if(s.startsWith(c)) return s.substring(c.length(), s.length());
438    
439     return null;
440     }
441 iliev 784
442     /**
443     * Eliminates the quotation marks if the string is quoted.
444     * @return New string without quotation marks if the string is quoted; else
445     * the same string is returned.
446     */
447     protected static String
448     removeQuotation(String s) {
449     if(s == null || s.length() < 2) return s;
450     char q = s.charAt(0);
451     char q2 = s.charAt(s.length() - 1);
452     if((q == '\'' && q2 == '\'') || (q == '"' && q2 == '"'))
453     return s.substring(1, s.length() - 1);
454    
455     return s;
456     }
457 iliev 596 }

  ViewVC Help
Powered by ViewVC