1 |
/* |
2 |
* jlscp - a java LinuxSampler control protocol API |
3 |
* |
4 |
* Copyright (C) 2005-2007 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 |
public 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 |
/** |
161 |
* Parses a comma separated list whose items are not encapsulated into apostrophes. |
162 |
* @param list The comma separated list. |
163 |
* @return A <code>String</code> array containing all items in the list. |
164 |
* @throws LscpException if the list is broken. |
165 |
*/ |
166 |
protected static String[] |
167 |
parseStringList(String list) throws LscpException { |
168 |
list = removeQuotation(list); |
169 |
return parseList(list); |
170 |
} |
171 |
|
172 |
/** |
173 |
* Parses a comma separated string list, which elements contains escaped sequences. |
174 |
* @param list The list to parse. |
175 |
* @return A <code>String</code> array containing all items in the list. |
176 |
*/ |
177 |
protected static String[] |
178 |
parseEscapedStringList(String list) throws LscpException { |
179 |
return parseEscapedStringList(list, ','); |
180 |
} |
181 |
|
182 |
/** |
183 |
* Parses a string list, which elements contains escaped sequences. |
184 |
* @param list The list to parse. |
185 |
* @param separator Provides the character used as separator. |
186 |
* @return A <code>String</code> array containing all items in the list. |
187 |
*/ |
188 |
protected static String[] |
189 |
parseEscapedStringList(String list, char separator) throws LscpException { |
190 |
if(list == null || list.length() == 0) return new String[0]; |
191 |
int q1 = 0, q2 = 0; |
192 |
Vector<String> v = new Vector<String>(); |
193 |
|
194 |
for(;;) { |
195 |
if(list.charAt(q1) != '\'') |
196 |
throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!")); |
197 |
q2 = findApostrophe(list, q1 + 1); |
198 |
if(q2 == -1) throw new LscpException(LscpI18n.getLogMsg("Parser.EOL!")); |
199 |
v.add(list.substring(q1 + 1, q2)); |
200 |
|
201 |
if(q2 + 1 >= list.length()) break; |
202 |
|
203 |
if(list.charAt(q2 + 1) != separator) |
204 |
throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!")); |
205 |
q1 = q2 + 2; |
206 |
if(q1 >= list.length()) |
207 |
throw new LscpException(LscpI18n.getLogMsg("Parser.EOL!")); |
208 |
} |
209 |
|
210 |
return v.toArray(new String[v.size()]); |
211 |
} |
212 |
|
213 |
/** |
214 |
* Returns the index of the first occurrence of a non-escaped apostrophe |
215 |
* in the specified string, starting at <b>index</b>, or -1 if nothing is found. |
216 |
*/ |
217 |
private static int |
218 |
findApostrophe(String s, int index) { |
219 |
return findNonEscapedChar(s, index, '\''); |
220 |
} |
221 |
|
222 |
/** |
223 |
* Parses a comma separated list whose items are encapsulated into apostrophes. |
224 |
* @param list The comma separated list. |
225 |
* @return A <code>String</code> array containing all items in the list. |
226 |
* @throws LscpException if the list is broken. |
227 |
*/ |
228 |
protected static String[] |
229 |
parseQuotedStringList(String list) throws LscpException { |
230 |
return parseQuotedStringList(list, ','); |
231 |
} |
232 |
|
233 |
/** |
234 |
* Parses a list whose items are encapsulated into apostrophes. |
235 |
* @param list The list of strings. |
236 |
* @param separator Provides the character used as separator. |
237 |
* @return A <code>String</code> array containing all items in the list. |
238 |
* @throws LscpException if the list is broken. |
239 |
*/ |
240 |
protected static String[] |
241 |
parseQuotedStringList(String list, char separator) throws LscpException { |
242 |
if(list == null || list.length() == 0) return new String[0]; |
243 |
int q1 = 0, q2 = 0; |
244 |
Vector<String> v = new Vector<String>(); |
245 |
|
246 |
for(;;) { |
247 |
if(list.charAt(q1) != '\'') |
248 |
throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!")); |
249 |
q2 = list.indexOf('\'', q1 + 1); |
250 |
if(q2 == -1) throw new LscpException(LscpI18n.getLogMsg("Parser.EOL!")); |
251 |
v.add(list.substring(q1 + 1, q2)); |
252 |
|
253 |
if(q2 + 1 >= list.length()) break; |
254 |
|
255 |
if(list.charAt(q2 + 1) != separator) |
256 |
throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!")); |
257 |
q1 = q2 + 2; |
258 |
if(q1 >= list.length()) |
259 |
throw new LscpException(LscpI18n.getLogMsg("Parser.EOL!")); |
260 |
} |
261 |
|
262 |
return v.toArray(new String[v.size()]); |
263 |
} |
264 |
|
265 |
protected static String[][] |
266 |
parseListOfStringLists(String list) throws LscpException { |
267 |
if(list.length() == 0) return new String[0][0]; |
268 |
|
269 |
String[][] s2S; |
270 |
if(!list.startsWith("''") && !list.startsWith("\"\"")) { |
271 |
s2S = new String[1][]; |
272 |
s2S[0] = parseQuotedStringList(list); |
273 |
return s2S; |
274 |
} |
275 |
|
276 |
int i = 0, i2 = 0; |
277 |
Vector<String> v = new Vector<String>(); |
278 |
|
279 |
for(;;) { |
280 |
i2 = getEndListIndex(i, list); |
281 |
v.add(list.substring(i + 1, i2)); |
282 |
if(i2 == list.length() - 1) break; |
283 |
if(list.charAt(i2 + 1) != ',') |
284 |
throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!")); |
285 |
i = i2 + 2; |
286 |
} |
287 |
|
288 |
s2S = new String[v.size()][]; |
289 |
for(i = 0; i < v.size(); i++) s2S[i] = parseQuotedStringList(v.get(i)); |
290 |
|
291 |
return s2S; |
292 |
} |
293 |
|
294 |
/** |
295 |
* Parses a comma separated list whose items are encapsulated into curly braces. |
296 |
* |
297 |
* @param list The comma separated list. |
298 |
* @return A <code>String</code> array containing all items in the list. |
299 |
* |
300 |
* @throws LscpException if the list is broken. |
301 |
*/ |
302 |
protected static String[] |
303 |
parseArray(String list) throws LscpException { |
304 |
if(list == null || list.length() == 0) return new String[0]; |
305 |
int q1 = 0, q2 = 0; |
306 |
Vector<String> v = new Vector<String>(); |
307 |
|
308 |
for(;;) { |
309 |
if(list.charAt(q1) != '{') |
310 |
throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!")); |
311 |
q2 = list.indexOf('}', q1 + 1); |
312 |
if(q2 == -1) throw new LscpException(LscpI18n.getLogMsg("Parser.EOL!")); |
313 |
v.add(list.substring(q1 + 1, q2)); |
314 |
|
315 |
if(q2 + 1 >= list.length()) break; |
316 |
|
317 |
if(list.charAt(q2 + 1) != ',') |
318 |
throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!")); |
319 |
q1 = q2 + 2; |
320 |
if(q1 >= list.length()) |
321 |
throw new LscpException(LscpI18n.getLogMsg("Parser.EOL!")); |
322 |
} |
323 |
|
324 |
return v.toArray(new String[v.size()]); |
325 |
} |
326 |
|
327 |
/** Helper function used by <code>parseListOfStringLists</code>. */ |
328 |
private static int |
329 |
getEndListIndex(int start, String list) throws LscpException { |
330 |
int i = start + 1; |
331 |
char q = list.charAt(0); // quote symbol |
332 |
if(list.charAt(start) != q) |
333 |
throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!")); |
334 |
|
335 |
if(list.charAt(i) == '\'') { // Check for empty list |
336 |
if(i == list.length() - 1 || list.charAt(i + 1) == ',') return i; |
337 |
} |
338 |
|
339 |
for(;;) { |
340 |
if(list.charAt(i) != q) |
341 |
throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!")); |
342 |
i = list.indexOf(q, i + 1); |
343 |
if(i == -1 || i == list.length() - 1) |
344 |
throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!")); |
345 |
|
346 |
if(list.charAt(i + 1) == q) return i + 1; |
347 |
|
348 |
if(list.charAt(i + 1) != ',') |
349 |
throw new LscpException(LscpI18n.getLogMsg("Parser.brokenList!")); |
350 |
i += 2; |
351 |
} |
352 |
} |
353 |
|
354 |
|
355 |
/** |
356 |
* Gets the type of the parameter represented by the specified result set. |
357 |
* @param resultSet A <code>String</code> array containing the information categories |
358 |
* of a multi-line result set. |
359 |
* @return The type of the parameter represented by the specified result set or |
360 |
* <code>null</code> if the specified result set does not contain |
361 |
* <code>TYPE</code> category. |
362 |
*/ |
363 |
protected static ParameterType |
364 |
parseType(String[] resultSet) { |
365 |
if(resultSet == null || resultSet.length == 0) return null; |
366 |
for(String s : resultSet) { |
367 |
if(s.startsWith("TYPE: ")) { |
368 |
String type = s.substring("TYPE: ".length(), s.length()); |
369 |
if(type.equals("BOOL")) return ParameterType.BOOL; |
370 |
if(type.equals("INT")) return ParameterType.INT; |
371 |
if(type.equals("FOAT")) return ParameterType.FLOAT; |
372 |
if(type.equals("STRING")) return ParameterType.STRING; |
373 |
} |
374 |
} |
375 |
return null; |
376 |
} |
377 |
|
378 |
/** |
379 |
* Determines whether the parameter represented by the specified result set allows |
380 |
* only one value or a list of values. |
381 |
* @param resultSet A <code>String</code> array containing the information categories |
382 |
* of a multi-line result set. |
383 |
* @return <code>false</code> if the parameter represented by the specified result set |
384 |
* allows only one value and <code>true</code> if allows a list of values. |
385 |
*/ |
386 |
protected static Boolean |
387 |
parseMultiplicity(String[] resultSet) { |
388 |
if(resultSet == null || resultSet.length == 0) return null; |
389 |
|
390 |
for(String s : resultSet) { |
391 |
if(s.startsWith("MULTIPLICITY: ")) return Boolean.parseBoolean ( |
392 |
s.substring("MULTIPLICITY: ".length(), s.length()) |
393 |
); |
394 |
} |
395 |
|
396 |
return null; |
397 |
} |
398 |
|
399 |
/** |
400 |
* Parses an empty result set and returns an appropriate <code>ResultSet</code> object. |
401 |
* Notice that the result set may be of type warning or error. |
402 |
* @param ln A <code>String</code> representing the single line result set to be parsed. |
403 |
* @return A <code>ResultSet</code> object. |
404 |
* @throws LscpException If LSCP protocol error occurs. |
405 |
* @throws LSException If the LinuxSampler instance returns error message. |
406 |
*/ |
407 |
protected static ResultSet |
408 |
parseEmptyResultSet(String ln) throws LscpException, LSException { |
409 |
ResultSet rs = new ResultSet(); |
410 |
|
411 |
if(ln.equals("OK")) { |
412 |
return rs; |
413 |
} else if(ln.startsWith("OK[") && ln.endsWith("]")) { |
414 |
ln = ln.substring("OK[".length(), ln.length() - 1); |
415 |
try { |
416 |
rs.setIndex(Integer.parseInt(ln)); |
417 |
return rs; |
418 |
} catch(NumberFormatException x) { |
419 |
throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"), x); |
420 |
} |
421 |
} else if(ln.startsWith("WRN")) { |
422 |
parseWarning(ln, rs); |
423 |
Client.getLogger().warning(rs.getMessage()); |
424 |
return rs; |
425 |
} else if(ln.startsWith("ERR:")) { |
426 |
parseError(ln, rs); |
427 |
throw new LSException(rs.getCode(), rs.getMessage()); |
428 |
} |
429 |
|
430 |
throw new LscpException(LscpI18n.getLogMsg("CommandFailed!") ); |
431 |
} |
432 |
|
433 |
/** |
434 |
* Parses warning message. |
435 |
* @param ln The warning message to be parsed. |
436 |
* @param rs A <code>ResultSet</code> instance where the warning must be stored. |
437 |
* @throws LscpException If LSCP protocol corruption occurs. |
438 |
*/ |
439 |
protected static void |
440 |
parseWarning(String ln, ResultSet rs) throws LscpException { |
441 |
if(!ln.startsWith("WRN")) |
442 |
throw new LscpException(LscpI18n.getLogMsg("CommandFailed!")); |
443 |
|
444 |
int i, j; |
445 |
rs.setWarning(true); |
446 |
|
447 |
if(ln.charAt(3) == '[') { |
448 |
i = ln.indexOf(']'); |
449 |
if(i == -1) throw new LscpException(LscpI18n.getLogMsg("CommandFailed!")); |
450 |
|
451 |
try { |
452 |
j = Integer.parseInt(ln.substring("WRN[".length(), i)); |
453 |
rs.setIndex(j); |
454 |
} catch(NumberFormatException x) { |
455 |
throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"), x); |
456 |
} |
457 |
|
458 |
if(ln.charAt(i + 1) != ':') |
459 |
throw new LscpException(LscpI18n.getLogMsg("CommandFailed!")); |
460 |
} |
461 |
|
462 |
i = ln.indexOf(':'); |
463 |
if(i == -1) throw new LscpException(LscpI18n.getLogMsg("CommandFailed!")); |
464 |
j = ln.indexOf(':', i + 1); |
465 |
if(j == -1) throw new LscpException(LscpI18n.getLogMsg("CommandFailed!")); |
466 |
|
467 |
try { rs.setCode(Integer.parseInt(ln.substring(i + 1, j))); } |
468 |
catch(NumberFormatException x) { |
469 |
throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"), x); |
470 |
} |
471 |
|
472 |
rs.setMessage(ln.substring(j + 1)); |
473 |
} |
474 |
|
475 |
/** |
476 |
* Parses error message. |
477 |
* @param ln The error message to be parsed. |
478 |
* @param rs A <code>ResultSet</code> instance where the error must be stored. |
479 |
* @throws LscpException If LSCP protocol corruption occurs. |
480 |
*/ |
481 |
protected static void |
482 |
parseError(String ln, ResultSet rs) throws LscpException { |
483 |
if(!ln.startsWith("ERR:")) |
484 |
throw new LscpException(LscpI18n.getLogMsg("CommandFailed!")); |
485 |
|
486 |
int i = ln.indexOf(':', "ERR:".length()); |
487 |
if(i == -1) throw new LscpException(LscpI18n.getLogMsg("CommandFailed!")); |
488 |
|
489 |
try { rs.setCode(Integer.parseInt(ln.substring("ERR:".length(), i))); } |
490 |
catch(NumberFormatException x) { |
491 |
throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"), x); |
492 |
} |
493 |
|
494 |
rs.setMessage(ln.substring(i + 1)); |
495 |
} |
496 |
|
497 |
/** |
498 |
* Gets the info character string to the specified information category. |
499 |
* @param resultSet A <code>String</code> array containing the information categories |
500 |
* of a multi-line result set. |
501 |
* @param category Specifies the category whose info character string to be returned. |
502 |
* @return The info character string to the specified information category or |
503 |
* <code>null</code> if the specified result set does not contain that category. |
504 |
*/ |
505 |
protected static String |
506 |
getCategoryInfo(String[] resultSet, String category) { |
507 |
String c = category + ": "; |
508 |
for(String s : resultSet) |
509 |
if(s.startsWith(c)) return s.substring(c.length(), s.length()); |
510 |
|
511 |
return null; |
512 |
} |
513 |
|
514 |
/** |
515 |
* Eliminates the quotation marks if the string is quoted. |
516 |
* @return New string without quotation marks if the string is quoted; else |
517 |
* the same string is returned. |
518 |
*/ |
519 |
protected static String |
520 |
removeQuotation(String s) { |
521 |
if(s == null || s.length() < 2) return s; |
522 |
char q = s.charAt(0); |
523 |
char q2 = s.charAt(s.length() - 1); |
524 |
if((q == '\'' && q2 == '\'') || (q == '"' && q2 == '"')) |
525 |
return s.substring(1, s.length() - 1); |
526 |
|
527 |
return s; |
528 |
} |
529 |
|
530 |
/** |
531 |
* Returns the provided string with added escape sequences where necessary. |
532 |
*/ |
533 |
public static String |
534 |
toEscapedString(Object obj) { |
535 |
String s = obj.toString(); |
536 |
StringBuffer sb = new StringBuffer(); |
537 |
for(int i = 0; i < s.length(); i++) { |
538 |
switch(s.charAt(i)) { |
539 |
case '\n': sb.append("\\n"); break; |
540 |
case '\r': sb.append("\\r"); break; |
541 |
case '\f': sb.append("\\f"); break; |
542 |
case '\t': sb.append("\\t"); break; |
543 |
case 0x0B: sb.append("\\v"); break; |
544 |
case '\'': sb.append("\\'"); break; |
545 |
case '\"': sb.append("\\\""); break; |
546 |
case '\\': sb.append("\\\\"); break; |
547 |
default : sb.append(s.charAt(i)); |
548 |
} |
549 |
} |
550 |
|
551 |
return sb.toString(); |
552 |
} |
553 |
|
554 |
/** |
555 |
* Returns the provided file name with added escape sequences where necessary. |
556 |
*/ |
557 |
public static String |
558 |
toEscapedFileName(Object obj) { |
559 |
String s = obj.toString(); |
560 |
StringBuffer sb = new StringBuffer(); |
561 |
for(int i = 0; i < s.length(); i++) { |
562 |
switch(s.charAt(i)) { |
563 |
case '/' : sb.append("\\x2f"); break; |
564 |
case '\n': sb.append("\\n"); break; |
565 |
case '\r': sb.append("\\r"); break; |
566 |
case '\f': sb.append("\\f"); break; |
567 |
case '\t': sb.append("\\t"); break; |
568 |
case 0x0B: sb.append("\\v"); break; |
569 |
case '\'': sb.append("\\'"); break; |
570 |
case '\"': sb.append("\\\""); break; |
571 |
case '\\': sb.append("\\\\"); break; |
572 |
default : sb.append(s.charAt(i)); |
573 |
} |
574 |
} |
575 |
|
576 |
return sb.toString(); |
577 |
} |
578 |
|
579 |
/** |
580 |
* Removes the escape sequences from the specified file name |
581 |
* @return The provided file name with removed escape sequences. |
582 |
*/ |
583 |
public static String |
584 |
toNonEscapedFileName(Object obj) { |
585 |
return toNonEscapedString(obj); |
586 |
} |
587 |
|
588 |
/** |
589 |
* Removes the escape sequences from the string <code>obj.toString()</code>. |
590 |
* @return The provided text with removed escape sequences. |
591 |
*/ |
592 |
public static String |
593 |
toNonEscapedString(Object obj) { |
594 |
String s = obj.toString(); |
595 |
StringBuffer sb = new StringBuffer(); |
596 |
for(int i = 0; i < s.length(); i++) { |
597 |
char c = s.charAt(i); |
598 |
if(c == '\\') { |
599 |
if(i >= s.length()) { |
600 |
Client.getLogger().info("Broken escape sequence!"); |
601 |
break; |
602 |
} |
603 |
char c2 = s.charAt(++i); |
604 |
if(c2 == '\'') sb.append('\''); |
605 |
else if(c2 == '"') sb.append('"'); |
606 |
else if(c2 == '\\') sb.append('\\'); |
607 |
else if(c2 == 'r') sb.append('\r'); |
608 |
else if(c2 == 'n') sb.append('\n'); |
609 |
else if(c2 == 'f') sb.append('\f'); |
610 |
else if(c2 == 't') sb.append('\t'); |
611 |
else if(c2 == 'v') sb.append((char)0x0B); |
612 |
else if(c2 == 'x') { |
613 |
Character ch = getHexEscapeSequence(s, i + 1); |
614 |
if(ch != null) sb.append(ch.charValue()); |
615 |
i += 2; |
616 |
} else if(c2 >= '0' && c2 <= '9') { |
617 |
Character ch = getOctEscapeSequence(s, i); |
618 |
if(ch != null) sb.append(ch.charValue()); |
619 |
i += 2; |
620 |
} else Client.getLogger().info("Unknown escape sequence \\" + c2); |
621 |
} else { |
622 |
sb.append(c); |
623 |
} |
624 |
} |
625 |
|
626 |
return sb.toString(); |
627 |
} |
628 |
|
629 |
private static Character |
630 |
getHexEscapeSequence(String s, int index) { |
631 |
Character c = null; |
632 |
|
633 |
if(index + 1 >= s.length()) { |
634 |
Client.getLogger().info("Broken escape sequence"); |
635 |
return c; |
636 |
} |
637 |
|
638 |
try { c = (char)Integer.parseInt(s.substring(index, index + 2), 16); } |
639 |
catch(Exception x) { Client.getLogger().info("Broken escape sequence!"); } |
640 |
|
641 |
return c; |
642 |
} |
643 |
|
644 |
private static Character |
645 |
getOctEscapeSequence(String s, int index) { |
646 |
Character c = null; |
647 |
|
648 |
if(index + 2 >= s.length()) { |
649 |
Client.getLogger().info("Broken escape sequence"); |
650 |
return c; |
651 |
} |
652 |
|
653 |
try { c = (char)Integer.parseInt(s.substring(index, index + 3), 8); } |
654 |
catch(Exception x) { Client.getLogger().info("Broken escape sequence!"); } |
655 |
|
656 |
return c; |
657 |
} |
658 |
|
659 |
/** |
660 |
* Determines whether the character at the specified position |
661 |
* is escaped with backslash. |
662 |
*/ |
663 |
public static boolean |
664 |
isEscaped(String s, int index) { |
665 |
if (index < 0 || index >= s.length()) return false; |
666 |
int count = 0; |
667 |
for (int i = index - 1; i >= 0; i--) { |
668 |
if (s.charAt(i) != '\\') break; |
669 |
count++; |
670 |
} |
671 |
return count % 2 != 0; |
672 |
} |
673 |
|
674 |
/** |
675 |
* Returns the index of the first occurrence of the specified non-escaped character |
676 |
* in the specified string, starting at <b>index</b>, or -1 if nothing is found. |
677 |
*/ |
678 |
private static int |
679 |
findNonEscapedChar(String s, int index, char c) { |
680 |
if(s == null) return -1; |
681 |
int pos = index; |
682 |
if (pos < 0 || pos >= s.length()) return -1; |
683 |
|
684 |
for(;;) { |
685 |
int i = s.indexOf(c, pos); |
686 |
if (i == -1) break; |
687 |
if (!isEscaped(s, i)) return i; |
688 |
pos = i + 1; |
689 |
if (pos >= s.length()) break; |
690 |
} |
691 |
|
692 |
return -1; |
693 |
} |
694 |
|
695 |
/** |
696 |
* Returns the index of the first occurrence of a file separator |
697 |
* in the specified escaped path, starting at <b>index</b>, or -1 if nothing is found. |
698 |
*/ |
699 |
private static int |
700 |
findFileSeparator(String path, int index) { |
701 |
return findNonEscapedChar(path, index, '/'); |
702 |
} |
703 |
|
704 |
/** |
705 |
* Gets the position of the last file separator in the specified |
706 |
* escaped path, or -1 if failed. |
707 |
*/ |
708 |
private static int |
709 |
getLastFileSeparator(String path) { |
710 |
if(path == null || path.length() == 0) return -1; |
711 |
int pos = path.length() - 1; |
712 |
|
713 |
for(;;) { |
714 |
pos = path.lastIndexOf('/', pos); |
715 |
if(pos == -1) return -1; |
716 |
if(!isEscaped(path, pos)) return pos; |
717 |
pos--; |
718 |
} |
719 |
} |
720 |
|
721 |
/** |
722 |
* Determines whether the specified escaped path ends with a file separator. |
723 |
*/ |
724 |
protected static boolean |
725 |
hasEndingFileSeparator(String path) { |
726 |
if(path == null || path.length() < 2) return false; |
727 |
|
728 |
int last = path.length() - 1; |
729 |
if(path.charAt(last) == '/' && !isEscaped(path, last)) return true; |
730 |
|
731 |
return false; |
732 |
} |
733 |
|
734 |
/** |
735 |
* If the specified escaped path ends with a file separator, |
736 |
* a new string is returned with the ending file separator removed. |
737 |
*/ |
738 |
protected static String |
739 |
removeEndingFileSeparator(String path) { |
740 |
if(path == null || path.length() < 2) return path; |
741 |
|
742 |
int last = path.length() - 1; |
743 |
if(path.charAt(last) == '/' && !isEscaped(path, last)) { |
744 |
path = path.substring(0, path.length() - 1); |
745 |
} |
746 |
|
747 |
return path; |
748 |
} |
749 |
|
750 |
/** |
751 |
* Gets the parent directory of the specified escaped path. |
752 |
*/ |
753 |
public static String |
754 |
getParentDirectory(String path) { |
755 |
if(path == null || path.length() == 0) return null; |
756 |
if(path.charAt(0) != '/') return null; |
757 |
if(path.length() == 1) return null; |
758 |
|
759 |
path = removeEndingFileSeparator(path); |
760 |
|
761 |
int i = getLastFileSeparator(path); |
762 |
if(i == 0) return "/"; |
763 |
return path.substring(0, i); |
764 |
} |
765 |
|
766 |
/** |
767 |
* Extracts the file name from the specified escaped path. |
768 |
* If the path does not ends with a file name, <code>null</code> is returned. |
769 |
*/ |
770 |
public static String |
771 |
getFileName(String path) { |
772 |
if(path == null || path.length() < 2) return null; |
773 |
int i = getLastFileSeparator(path); |
774 |
if(i == -1) return null; |
775 |
if(i == path.length() - 1) return null; |
776 |
return path.substring(i + 1); |
777 |
} |
778 |
|
779 |
/** |
780 |
* Returns an array containing all directories in the specified escaped path. |
781 |
*/ |
782 |
public static String[] |
783 |
getDirectoryList(String path) { |
784 |
if(path == null || path.length() == 0) return null; |
785 |
if(path.charAt(0) != '/') return null; |
786 |
Vector<String> v = new Vector<String>(); |
787 |
v.add("/"); |
788 |
if(path.length() == 1) return v.toArray(new String[v.size()]); |
789 |
|
790 |
if(!hasEndingFileSeparator(path)) path += "/"; |
791 |
int i = 1; |
792 |
int j = findFileSeparator(path, i); |
793 |
|
794 |
while(j != -1) { |
795 |
v.add(path.substring(i, j)); |
796 |
|
797 |
i = j + 1; |
798 |
if(i >= path.length()) return v.toArray(new String[v.size()]); |
799 |
j = findFileSeparator(path, i); |
800 |
} |
801 |
|
802 |
return null; |
803 |
} |
804 |
} |