blob: 30e744b76b38dbe26a7951d9a434d54eb48ff004 [file] [log] [blame]
Stuart McCullochf3173222012-06-07 21:57:32 +00001package aQute.libg.qtokens;
2
3import java.util.*;
4
5import aQute.libg.generics.*;
6
7public 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 McCulloch4482c702012-06-15 13:27:53 +000016 public QuotedTokenizer(String string, String separators, boolean returnTokens) {
17 if (string == null)
Stuart McCullochf3173222012-06-07 21:57:32 +000018 throw new IllegalArgumentException("string argument must be not null");
19 this.string = string;
20 this.separators = separators;
21 this.returnTokens = returnTokens;
22 }
Stuart McCulloch4482c702012-06-15 13:27:53 +000023
Stuart McCullochf3173222012-06-07 21:57:32 +000024 public QuotedTokenizer(String string, String separators) {
Stuart McCulloch4482c702012-06-15 13:27:53 +000025 this(string, separators, false);
Stuart McCullochf3173222012-06-07 21:57:32 +000026 }
27
28 public String nextToken(String separators) {
29 separator = 0;
Stuart McCulloch4482c702012-06-15 13:27:53 +000030 if (peek != null) {
Stuart McCullochf3173222012-06-07 21:57:32 +000031 String tmp = peek;
32 peek = null;
33 return tmp;
34 }
Stuart McCulloch4482c702012-06-15 13:27:53 +000035
36 if (index == string.length())
Stuart McCullochf3173222012-06-07 21:57:32 +000037 return null;
Stuart McCulloch4482c702012-06-15 13:27:53 +000038
Stuart McCullochf3173222012-06-07 21:57:32 +000039 StringBuilder sb = new StringBuilder();
40
41 while (index < string.length()) {
42 char c = string.charAt(index++);
43
Stuart McCulloch4482c702012-06-15 13:27:53 +000044 if (Character.isWhitespace(c)) {
45 if (index == string.length())
Stuart McCullochf3173222012-06-07 21:57:32 +000046 break;
47 sb.append(c);
48 continue;
49 }
Stuart McCulloch4482c702012-06-15 13:27:53 +000050
Stuart McCullochf3173222012-06-07 21:57:32 +000051 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 McCulloch4482c702012-06-15 13:27:53 +000070 if (result.length() == 0 && index == string.length())
Stuart McCullochf3173222012-06-07 21:57:32 +000071 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 McCulloch2a0afd62012-09-06 18:28:06 +000085 if (c == '\\' && index < string.length() && string.charAt(index) == quote)
Stuart McCullochf3173222012-06-07 21:57:32 +000086 c = string.charAt(index++);
87 sb.append(c);
88 }
89 }
90
91 public String[] getTokens() {
92 return getTokens(0);
93 }
94
Stuart McCulloch4482c702012-06-15 13:27:53 +000095 private String[] getTokens(int cnt) {
Stuart McCullochf3173222012-06-07 21:57:32 +000096 String token = nextToken();
Stuart McCulloch4482c702012-06-15 13:27:53 +000097 if (token == null)
Stuart McCullochf3173222012-06-07 21:57:32 +000098 return new String[cnt];
Stuart McCulloch4482c702012-06-15 13:27:53 +000099
100 String result[] = getTokens(cnt + 1);
101 result[cnt] = token;
Stuart McCullochf3173222012-06-07 21:57:32 +0000102 return result;
103 }
104
Stuart McCulloch4482c702012-06-15 13:27:53 +0000105 public char getSeparator() {
106 return separator;
107 }
108
Stuart McCullochf3173222012-06-07 21:57:32 +0000109 public List<String> getTokenSet() {
110 List<String> list = Create.list();
111 String token = nextToken();
Stuart McCulloch4482c702012-06-15 13:27:53 +0000112 while (token != null) {
Stuart McCullochf3173222012-06-07 21:57:32 +0000113 list.add(token);
114 token = nextToken();
115 }
116 return list;
117 }
118}