Tokenizer.java
5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package org.emercit.model;
import java.io.*;
import java.util.*;
/**
* This class perform similar functionality to StringTokenizer class but faster.
* The difference is StringTokenizer doesn't count empty token, but this class does.
*
* @author Henry Zheng
* @url http://www.ireasoning.com
*/
public class Tokenizer
{
private Tokenizer()
{
}
/**
* It's different from the other parse method in that it checks left and right string first, which take higer
* priority than the delimiter. For example, if left and right is ", for string a:b:1"c:d"2:3 ,
* it returns { a, b, 1"c:d"2, 3 }
* @param left the openning tag of higher priority token
* @param right the closing tag of higher priority token
* @trimEachToken if true, each token will be trim by calling String.trim()
*/
public static String[] parse(String text, char delimiter, boolean trimEachToken, String left, String right)
{
if(text == null) return null;
int len = text.length();
StringArray tokens = new StringArray();
int pos1 = -1;
int pos2 = -1;
int firstPos = -1;
while(true)
{
pos2 = text.indexOf(delimiter, firstPos + 1);
if(pos2 < 0 )
{
String str = text.substring(pos1 + 1);
if(trimEachToken )
{
str = str.trim();
}
tokens.add(str);
break;
}
if(pos2 == pos1 + 1)
{
tokens.add("");
}
else
{
int tagPos1 = text.indexOf(left, firstPos + 1);
if(tagPos1 > 0 && tagPos1 < pos2 )
{
int tagPos2 = text.indexOf(right, tagPos1 + 1);
if(tagPos2 > 0)
{
firstPos = tagPos2;
continue;
}
}
String str = text.substring(pos1 + 1, pos2);
if(trimEachToken )
{
str = str.trim();
}
tokens.add(str);
}
pos1 = pos2;
firstPos = pos1;
}
String[] ret = tokens.toArray();
return ret;
}
/**
* @trimEachToken if true, each token will be trim by calling String.trim()
*/
public static String[] parse(String text, char delimiter, boolean trimEachToken)
{
return parse(text, delimiter, trimEachToken, false);
}
/**
* @trimEachToken if true, each token will be trim by calling String.trim()
*/
public static String[] parse(String text, char delimiter, boolean trimEachToken, boolean ignoreEmptyToken)
{
if(text == null) return null;
int len = text.length();
StringArray tokens = new StringArray();
int pos1 = -1;
int pos2 = -1;
while(true)
{
pos2 = text.indexOf(delimiter, pos1 + 1);
if(pos2 < 0 )
{
String str = text.substring(pos1 + 1);
if(trimEachToken )
{
str = str.trim();
}
if(ignoreEmptyToken)
{
if(str.length() != 0) tokens.add(str);
}
else
{
tokens.add(str);
}
break;
}
if(pos2 == pos1 + 1)
{
if(!ignoreEmptyToken) { tokens.add(""); }
}
else
{
String str = text.substring(pos1 + 1, pos2);
if(trimEachToken )
{
str = str.trim();
}
if(ignoreEmptyToken)
{
if(str.length() != 0) tokens.add(str);
}
else
{
tokens.add(str);
}
}
pos1 = pos2;
}
String[] ret = tokens.toArray();
return ret;
}
/**
* Does not trim each token.
* @see #parse(String, char, boolean)
*/
public static String[] parse(String text, char delimiter)
{
return parse(text, delimiter, false);
}
public static void main(String[] args)
{
String str = "1,\"2,\"ab\",ttt1,\"3,,a\"222\",4";
if(args.length > 0)
{
str = args[0];
}
// String [] tokens = Tokenizer.parse(str, ',');
//
//// System.out.println( "Text = (" + str + ")");
// // System.out.println( "------------------------------------------");
// for (int i = 0; i < tokens.length ; i++)
// {
// // System.out.println( "(" + tokens[i] + ")");
// }
// // System.out.println( "------------------------------------------");
// String [] tokens = Tokenizer.parse(str, ',', new String[]{"("}, new String[]{")"});
//
// // System.out.println( "Text = [" + str + "]");
// // System.out.println( "------------------------------------------");
// for (int i = 0; i < tokens.length ; i++)
// {
// // System.out.println( "[" + tokens[i] + "]");
// }
// // System.out.println( "------------------------------------------");
}
}//end of class Tokenizer