31 |
* This class contains only helper functions that are used from the other classes in this library. |
* This class contains only helper functions that are used from the other classes in this library. |
32 |
* @author Grigor Iliev |
* @author Grigor Iliev |
33 |
*/ |
*/ |
34 |
final class Parser { |
public final class Parser { |
35 |
/** Forbits the instantiatrion of this class */ |
/** Forbits the instantiatrion of this class */ |
36 |
private Parser() { } |
private Parser() { } |
37 |
|
|
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. |
* Parses a comma separated list whose items are encapsulated into apostrophes. |
224 |
* @param list The comma separated list. |
* @param list The comma separated list. |
225 |
* @return A <code>String</code> array containing all items in the list. |
* @return A <code>String</code> array containing all items in the list. |
530 |
/** |
/** |
531 |
* Returns the provided string with added escape sequences where necessary. |
* Returns the provided string with added escape sequences where necessary. |
532 |
*/ |
*/ |
533 |
protected static String |
public static String |
534 |
getEscapedString(String s) { |
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(); |
StringBuffer sb = new StringBuffer(); |
561 |
for(int i = 0; i < s.length(); i++) { |
for(int i = 0; i < s.length(); i++) { |
562 |
switch(s.charAt(i)) { |
switch(s.charAt(i)) { |
563 |
|
case '/' : sb.append("\\/"); break; |
564 |
case '\n': sb.append("\\n"); break; |
case '\n': sb.append("\\n"); break; |
565 |
case '\r': sb.append("\\r"); break; |
case '\r': sb.append("\\r"); break; |
566 |
case '\f': sb.append("\\f"); break; |
case '\f': sb.append("\\f"); break; |
575 |
|
|
576 |
return sb.toString(); |
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 |
|
String s = obj.toString(); |
586 |
|
StringBuffer sb = new StringBuffer(); |
587 |
|
for(int i = 0; i < s.length(); i++) { |
588 |
|
char c = s.charAt(i); |
589 |
|
if(c == '\\') { |
590 |
|
if(i >= s.length()) { |
591 |
|
Client.getLogger().info("Broken escape sequence"); |
592 |
|
break; |
593 |
|
} |
594 |
|
char c2 = s.charAt(++i); |
595 |
|
if(c2 == '\'') sb.append('\''); |
596 |
|
else if(c2 == '"') sb.append('"'); |
597 |
|
else if(c2 == '\\') sb.append('\\'); |
598 |
|
else if(c2 == 'r') sb.append('\r'); |
599 |
|
else if(c2 == 'n') sb.append('\n'); |
600 |
|
else if(c2 == '/') sb.append('/'); |
601 |
|
else Client.getLogger().info("Unknown escape sequence \\" + c2); |
602 |
|
} else { |
603 |
|
sb.append(c); |
604 |
|
} |
605 |
|
} |
606 |
|
|
607 |
|
return sb.toString(); |
608 |
|
} |
609 |
|
|
610 |
|
/** |
611 |
|
* Removes the escape sequences from the string <code>obj.toString()</code>. |
612 |
|
* @return The provided text with removed escape sequences. |
613 |
|
*/ |
614 |
|
public static String |
615 |
|
toNonEscapedText(Object obj) { |
616 |
|
String s = obj.toString(); |
617 |
|
StringBuffer sb = new StringBuffer(); |
618 |
|
for(int i = 0; i < s.length(); i++) { |
619 |
|
char c = s.charAt(i); |
620 |
|
if(c == '\\') { |
621 |
|
if(i >= s.length()) { |
622 |
|
Client.getLogger().info("Broken escape sequence"); |
623 |
|
break; |
624 |
|
} |
625 |
|
char c2 = s.charAt(++i); |
626 |
|
if(c2 == '\'') sb.append('\''); |
627 |
|
else if(c2 == '"') sb.append('"'); |
628 |
|
else if(c2 == '\\') sb.append('\\'); |
629 |
|
else if(c2 == 'r') sb.append('\r'); |
630 |
|
else if(c2 == 'n') sb.append('\n'); |
631 |
|
else Client.getLogger().info("Unknown escape sequence \\" + c2); |
632 |
|
} else { |
633 |
|
sb.append(c); |
634 |
|
} |
635 |
|
} |
636 |
|
|
637 |
|
return sb.toString(); |
638 |
|
} |
639 |
|
|
640 |
|
/** |
641 |
|
* Determines whether the character at the specified position |
642 |
|
* is escaped with backslash. |
643 |
|
*/ |
644 |
|
public static boolean |
645 |
|
isEscaped(String s, int index) { |
646 |
|
if (index < 0 || index >= s.length()) return false; |
647 |
|
int count = 0; |
648 |
|
for (int i = index - 1; i >= 0; i--) { |
649 |
|
if (s.charAt(i) != '\\') break; |
650 |
|
count++; |
651 |
|
} |
652 |
|
return count % 2 != 0; |
653 |
|
} |
654 |
|
|
655 |
|
/** |
656 |
|
* Returns the index of the first occurrence of the specified non-escaped character |
657 |
|
* in the specified string, starting at <b>index</b>, or -1 if nothing is found. |
658 |
|
*/ |
659 |
|
private static int |
660 |
|
findNonEscapedChar(String s, int index, char c) { |
661 |
|
if(s == null) return -1; |
662 |
|
int pos = index; |
663 |
|
if (pos < 0 || pos >= s.length()) return -1; |
664 |
|
|
665 |
|
for(;;) { |
666 |
|
int i = s.indexOf(c, pos); |
667 |
|
if (i == -1) break; |
668 |
|
if (!isEscaped(s, i)) return i; |
669 |
|
pos = i + 1; |
670 |
|
if (pos >= s.length()) break; |
671 |
|
} |
672 |
|
|
673 |
|
return -1; |
674 |
|
} |
675 |
|
|
676 |
|
/** |
677 |
|
* Returns the index of the first occurrence of a file separator |
678 |
|
* in the specified escaped path, starting at <b>index</b>, or -1 if nothing is found. |
679 |
|
*/ |
680 |
|
private static int |
681 |
|
findFileSeparator(String path, int index) { |
682 |
|
return findNonEscapedChar(path, index, '/'); |
683 |
|
} |
684 |
|
|
685 |
|
/** |
686 |
|
* Gets the position of the last file separator in the specified |
687 |
|
* escaped path, or -1 if failed. |
688 |
|
*/ |
689 |
|
private static int |
690 |
|
getLastFileSeparator(String path) { |
691 |
|
if(path == null || path.length() == 0) return -1; |
692 |
|
int pos = path.length() - 1; |
693 |
|
|
694 |
|
for(;;) { |
695 |
|
pos = path.lastIndexOf('/', pos); |
696 |
|
if(pos == -1) return -1; |
697 |
|
if(!isEscaped(path, pos)) return pos; |
698 |
|
pos--; |
699 |
|
} |
700 |
|
} |
701 |
|
|
702 |
|
/** |
703 |
|
* Determines whether the specified escaped path ends with a file separator. |
704 |
|
*/ |
705 |
|
protected static boolean |
706 |
|
hasEndingFileSeparator(String path) { |
707 |
|
if(path == null || path.length() < 2) return false; |
708 |
|
|
709 |
|
int last = path.length() - 1; |
710 |
|
if(path.charAt(last) == '/' && !isEscaped(path, last)) return true; |
711 |
|
|
712 |
|
return false; |
713 |
|
} |
714 |
|
|
715 |
|
/** |
716 |
|
* If the specified escaped path ends with a file separator, |
717 |
|
* a new string is returned with the ending file separator removed. |
718 |
|
*/ |
719 |
|
protected static String |
720 |
|
removeEndingFileSeparator(String path) { |
721 |
|
if(path == null || path.length() < 2) return path; |
722 |
|
|
723 |
|
int last = path.length() - 1; |
724 |
|
if(path.charAt(last) == '/' && !isEscaped(path, last)) { |
725 |
|
path = path.substring(0, path.length() - 1); |
726 |
|
} |
727 |
|
|
728 |
|
return path; |
729 |
|
} |
730 |
|
|
731 |
|
/** |
732 |
|
* Gets the parent directory of the specified escaped path. |
733 |
|
*/ |
734 |
|
public static String |
735 |
|
getParentDirectory(String path) { |
736 |
|
if(path == null || path.length() == 0) return null; |
737 |
|
if(path.charAt(0) != '/') return null; |
738 |
|
if(path.length() == 1) return null; |
739 |
|
|
740 |
|
path = removeEndingFileSeparator(path); |
741 |
|
|
742 |
|
int i = getLastFileSeparator(path); |
743 |
|
if(i == 0) return "/"; |
744 |
|
return path.substring(0, i); |
745 |
|
} |
746 |
|
|
747 |
|
/** |
748 |
|
* Extracts the file name from the specified escaped path. |
749 |
|
* If the path does not ends with a file name, <code>null</code> is returned. |
750 |
|
*/ |
751 |
|
public static String |
752 |
|
getFileName(String path) { |
753 |
|
if(path == null || path.length() < 2) return null; |
754 |
|
int i = getLastFileSeparator(path); |
755 |
|
if(i == -1) return null; |
756 |
|
if(i == path.length() - 1) return null; |
757 |
|
return path.substring(i + 1); |
758 |
|
} |
759 |
|
|
760 |
|
/** |
761 |
|
* Returns an array containing all directories in the specified escaped path. |
762 |
|
*/ |
763 |
|
public static String[] |
764 |
|
getDirectoryList(String path) { |
765 |
|
if(path == null || path.length() == 0) return null; |
766 |
|
if(path.charAt(0) != '/') return null; |
767 |
|
Vector<String> v = new Vector<String>(); |
768 |
|
v.add("/"); |
769 |
|
if(path.length() == 1) return v.toArray(new String[v.size()]); |
770 |
|
|
771 |
|
if(!hasEndingFileSeparator(path)) path += "/"; |
772 |
|
int i = 1; |
773 |
|
int j = findFileSeparator(path, i); |
774 |
|
|
775 |
|
while(j != -1) { |
776 |
|
v.add(path.substring(i, j)); |
777 |
|
|
778 |
|
i = j + 1; |
779 |
|
if(i >= path.length()) return v.toArray(new String[v.size()]); |
780 |
|
j = findFileSeparator(path, i); |
781 |
|
} |
782 |
|
|
783 |
|
return null; |
784 |
|
} |
785 |
} |
} |