1 |
/* |
/* |
2 |
* jlscp - a java LinuxSampler control protocol API |
* jlscp - a java LinuxSampler control protocol API |
3 |
* |
* |
4 |
* Copyright (C) 2005 Grigor Kirilov Iliev |
* Copyright (C) 2005-2006 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
* |
6 |
* This file is part of jlscp. |
* This file is part of jlscp. |
7 |
* |
* |
32 |
* @author Grigor Iliev |
* @author Grigor Iliev |
33 |
*/ |
*/ |
34 |
final class Parser { |
final class Parser { |
35 |
|
/** Forbits the instantiatrion of this class */ |
36 |
|
private Parser() { } |
37 |
|
|
38 |
/** |
/** |
39 |
* Parses an integer value. |
* Parses an integer value. |
40 |
* @param s The integer value to be parsed. |
* @param s The integer value to be parsed. |
68 |
* @return A <code>String</code> array containing all items in the list. |
* @return A <code>String</code> array containing all items in the list. |
69 |
*/ |
*/ |
70 |
protected static String[] |
protected static String[] |
71 |
parseList(String list) { |
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]; |
if(list == null || list.length() == 0) return new String[0]; |
82 |
int pos = 0; |
int pos = 0; |
83 |
int idx; |
int idx; |
84 |
Vector<String> v = new Vector<String>(); |
Vector<String> v = new Vector<String>(); |
85 |
while((idx = list.indexOf(',', pos)) > 0) { |
while((idx = list.indexOf(separator, pos)) > 0) { |
86 |
v.add(list.substring(pos, idx)); |
v.add(list.substring(pos, idx)); |
87 |
pos = idx + 1; |
pos = idx + 1; |
88 |
} |
} |
118 |
* @throws LscpException if the list contains value(s) from different type. |
* @throws LscpException if the list contains value(s) from different type. |
119 |
*/ |
*/ |
120 |
protected static Integer[] |
protected static Integer[] |
121 |
parseIntList(String list) throws LscpException { |
parseIntList(String list) throws LscpException { return parseIntList(list, ','); } |
122 |
String[] ar = parseList(list); |
|
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]; |
Integer[] iar = new Integer[ar.length]; |
137 |
for(int i = 0; i < ar.length; i++) iar[i] = parseInt(ar[i]); |
for(int i = 0; i < ar.length; i++) iar[i] = parseInt(ar[i]); |
156 |
|
|
157 |
return far; |
return far; |
158 |
} |
} |
|
|
|
159 |
/** |
/** |
160 |
* Parses a comma separated list whose items are encapsulated into apostrophes. |
* Parses a comma separated list whose items are encapsulated into apostrophes. |
|
* |
|
161 |
* @param list The comma separated list. |
* @param list The comma separated list. |
162 |
* @return A <code>String</code> array containing all items in the list. |
* @return A <code>String</code> array containing all items in the list. |
|
* |
|
163 |
* @throws LscpException if the list is broken. |
* @throws LscpException if the list is broken. |
164 |
*/ |
*/ |
165 |
protected static String[] |
protected static String[] |
166 |
parseStringList(String list) throws LscpException { |
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]; |
if(list == null || list.length() == 0) return new String[0]; |
180 |
int q1 = 0, q2 = 0; |
int q1 = 0, q2 = 0; |
181 |
Vector<String> v = new Vector<String>(); |
Vector<String> v = new Vector<String>(); |
189 |
|
|
190 |
if(q2 + 1 >= list.length()) break; |
if(q2 + 1 >= list.length()) break; |
191 |
|
|
192 |
if(list.charAt(q2 + 1) != ',') |
if(list.charAt(q2 + 1) != separator) |
193 |
throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!")); |
throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!")); |
194 |
q1 = q2 + 2; |
q1 = q2 + 2; |
195 |
if(q1 >= list.length()) |
if(q1 >= list.length()) |
228 |
return s2S; |
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>. */ |
/** Helper function used by <code>parseListOfStringLists</code>. */ |
265 |
private static int |
private static int |
266 |
getEndListIndex(int start, String list) throws LscpException { |
getEndListIndex(int start, String list) throws LscpException { |
463 |
|
|
464 |
return s; |
return s; |
465 |
} |
} |
466 |
|
|
467 |
|
/** |
468 |
|
* Returns the provided string with added escape sequences where necessary. |
469 |
|
*/ |
470 |
|
protected static String |
471 |
|
getEscapedString(String s) { |
472 |
|
StringBuffer sb = new StringBuffer(); |
473 |
|
for(int i = 0; i < s.length(); i++) { |
474 |
|
switch(s.charAt(i)) { |
475 |
|
case '\n': sb.append("\\n"); break; |
476 |
|
case '\r': sb.append("\\r"); break; |
477 |
|
case '\f': sb.append("\\f"); break; |
478 |
|
case '\t': sb.append("\\t"); break; |
479 |
|
case 0x0B: sb.append("\\v"); break; |
480 |
|
case '\'': sb.append("\\'"); break; |
481 |
|
case '\"': sb.append("\\\""); break; |
482 |
|
case '\\': sb.append("\\\\"); break; |
483 |
|
default : sb.append(s.charAt(i)); |
484 |
|
} |
485 |
|
} |
486 |
|
|
487 |
|
return sb.toString(); |
488 |
|
} |
489 |
} |
} |