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