Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.libg.qtokens; |
| 2 | |
| 3 | import java.util.*; |
| 4 | |
| 5 | import aQute.libg.generics.*; |
| 6 | |
| 7 | public class QuotedTokenizer { |
| 8 | String string; |
| 9 | int index = 0; |
| 10 | String separators; |
| 11 | boolean returnTokens; |
| 12 | boolean ignoreWhiteSpace = true; |
| 13 | String peek; |
| 14 | char separator; |
| 15 | |
| 16 | public QuotedTokenizer(String string, String separators, boolean returnTokens ) { |
| 17 | if ( string == null ) |
| 18 | throw new IllegalArgumentException("string argument must be not null"); |
| 19 | this.string = string; |
| 20 | this.separators = separators; |
| 21 | this.returnTokens = returnTokens; |
| 22 | } |
| 23 | public QuotedTokenizer(String string, String separators) { |
| 24 | this(string,separators,false); |
| 25 | } |
| 26 | |
| 27 | public String nextToken(String separators) { |
| 28 | separator = 0; |
| 29 | if ( peek != null ) { |
| 30 | String tmp = peek; |
| 31 | peek = null; |
| 32 | return tmp; |
| 33 | } |
| 34 | |
| 35 | if ( index == string.length()) |
| 36 | return null; |
| 37 | |
| 38 | StringBuilder sb = new StringBuilder(); |
| 39 | |
| 40 | while (index < string.length()) { |
| 41 | char c = string.charAt(index++); |
| 42 | |
| 43 | if ( Character.isWhitespace(c)) { |
| 44 | if ( index == string.length()) |
| 45 | break; |
| 46 | sb.append(c); |
| 47 | continue; |
| 48 | } |
| 49 | |
| 50 | if (separators.indexOf(c) >= 0) { |
| 51 | if (returnTokens) |
| 52 | peek = Character.toString(c); |
| 53 | else |
| 54 | separator = c; |
| 55 | break; |
| 56 | } |
| 57 | |
| 58 | switch (c) { |
| 59 | case '"' : |
| 60 | case '\'' : |
| 61 | quotedString(sb, c); |
| 62 | break; |
| 63 | |
| 64 | default : |
| 65 | sb.append(c); |
| 66 | } |
| 67 | } |
| 68 | String result = sb.toString().trim(); |
| 69 | if ( result.length()==0 && index==string.length()) |
| 70 | return null; |
| 71 | return result; |
| 72 | } |
| 73 | |
| 74 | public String nextToken() { |
| 75 | return nextToken(separators); |
| 76 | } |
| 77 | |
| 78 | private void quotedString(StringBuilder sb, char c) { |
| 79 | char quote = c; |
| 80 | while (index < string.length()) { |
| 81 | c = string.charAt(index++); |
| 82 | if (c == quote) |
| 83 | break; |
| 84 | if (c == '\\' && index < string.length() |
| 85 | && string.charAt(index + 1) == quote) |
| 86 | c = string.charAt(index++); |
| 87 | sb.append(c); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | public String[] getTokens() { |
| 92 | return getTokens(0); |
| 93 | } |
| 94 | |
| 95 | private String [] getTokens(int cnt){ |
| 96 | String token = nextToken(); |
| 97 | if ( token == null ) |
| 98 | return new String[cnt]; |
| 99 | |
| 100 | String result[] = getTokens(cnt+1); |
| 101 | result[cnt]=token; |
| 102 | return result; |
| 103 | } |
| 104 | |
| 105 | public char getSeparator() { return separator; } |
| 106 | |
| 107 | public List<String> getTokenSet() { |
| 108 | List<String> list = Create.list(); |
| 109 | String token = nextToken(); |
| 110 | while ( token != null ) { |
| 111 | list.add(token); |
| 112 | token = nextToken(); |
| 113 | } |
| 114 | return list; |
| 115 | } |
| 116 | } |