1 package org.apache.maven.jxr.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Collections;
23 import java.util.Vector;
24
25
26
27
28
29
30
31 public class SimpleWordTokenizer
32 {
33
34
35
36
37 public static final char[] BREAKERS = {'(', ')', '[', ' ', '{', '}'};
38
39
40
41
42 public static StringEntry[] tokenize( String line )
43 {
44
45
46
47
48
49 int start = getStart( line );
50
51
52
53 if ( line == null || line.length() == 0 || start == -1 )
54 {
55 return new StringEntry[0];
56 }
57
58 return tokenize( line, start );
59 }
60
61
62
63
64
65
66
67
68
69 public static StringEntry[] tokenize( String line, String find )
70 {
71
72 Vector v = new Vector();
73
74 StringEntry[] se = tokenize( line );
75
76 for ( int i = 0; i < se.length; ++i )
77 {
78
79 if ( se[i].toString().equals( find ) )
80 {
81 v.addElement( se[i] );
82 }
83
84 }
85
86 StringEntry[] found = new StringEntry[v.size()];
87 Collections.sort( v );
88 v.copyInto( found );
89 return found;
90 }
91
92
93
94
95 private static StringEntry[] tokenize( String line, int start )
96 {
97
98 Vector words = new Vector();
99
100
101
102
103 while ( true )
104 {
105
106 int next = getNextBreak( line, start );
107
108 if ( next < 0 || next <= start )
109 {
110 break;
111 }
112
113 String word = line.substring( start, next );
114
115 if ( isWord( word ) )
116 {
117 words.addElement( new StringEntry( word, start ) );
118 }
119
120 start = next + 1;
121 }
122
123 StringEntry[] found = new StringEntry[words.size()];
124 words.copyInto( found );
125 return found;
126 }
127
128
129
130
131
132
133 private static boolean isWord( String string )
134 {
135
136 if ( string == null || string.length() == 0 )
137 {
138
139 return false;
140 }
141
142 for ( int i = 0; i < string.length(); ++i )
143 {
144
145 char c = string.charAt( i );
146
147 if ( !Character.isJavaIdentifierPart( c ) && c != '.' )
148 {
149 return false;
150 }
151
152 }
153
154 return true;
155 }
156
157
158
159
160 private static int getNextBreak( String string, int start )
161 {
162
163 int breakPoint = -1;
164
165 for ( int i = 0; i < BREAKERS.length; ++i )
166 {
167
168 int next = string.indexOf( BREAKERS[i], start );
169
170 if ( breakPoint == -1 || next < breakPoint && next != -1 )
171 {
172
173 breakPoint = next;
174
175 }
176
177 }
178
179
180 if ( breakPoint == -1 )
181 {
182 breakPoint = string.length();
183 }
184
185 return breakPoint;
186 }
187
188
189
190
191 private static int getStart( String string )
192 {
193
194 for ( int i = 0; i < string.length(); ++i )
195 {
196
197 if ( isBreaker( string.charAt( i ) ) == false )
198 {
199 return i;
200 }
201
202 }
203
204 return -1;
205 }
206
207
208
209
210
211 private static boolean isBreaker( char c )
212 {
213
214 for ( int i = 0; i < BREAKERS.length; ++i )
215 {
216
217 if ( BREAKERS[i] == c )
218 {
219 return true;
220 }
221
222 }
223
224 return false;
225 }
226
227 }
228