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