1 |
|
package org.apache.torque.engine.sql; |
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.io.Reader; |
24 |
|
import java.util.List; |
25 |
|
import java.util.ArrayList; |
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
@author |
32 |
|
@author |
33 |
|
@author |
34 |
|
@version |
35 |
|
|
|
|
| 0% |
Uncovered Elements: 116 (116) |
Complexity: 36 |
Complexity Density: 0.51 |
|
36 |
|
public class SQLScanner |
37 |
|
{ |
38 |
|
|
39 |
|
private static final String WHITE = "\f\r\t\n "; |
40 |
|
|
41 |
|
private static final String ALFA |
42 |
|
= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
43 |
|
|
44 |
|
private static final String NUMER = "0123456789"; |
45 |
|
|
46 |
|
private static final String ALFANUM = ALFA + NUMER; |
47 |
|
|
48 |
|
private static final String SPECIAL = ";(),'"; |
49 |
|
|
50 |
|
private static final char COMMENT_POUND = '#'; |
51 |
|
|
52 |
|
private static final char COMMENT_SLASH = '/'; |
53 |
|
|
54 |
|
private static final char COMMENT_STAR = '*'; |
55 |
|
|
56 |
|
private static final char COMMENT_DASH = '-'; |
57 |
|
|
58 |
|
|
59 |
|
private Reader in; |
60 |
|
|
61 |
|
private int chr; |
62 |
|
|
63 |
|
private String token; |
64 |
|
|
65 |
|
private List tokens; |
66 |
|
|
67 |
|
private int line; |
68 |
|
|
69 |
|
private int col; |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
74 |
0
|
public SQLScanner()... |
75 |
|
{ |
76 |
0
|
this(null); |
77 |
|
} |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
@param |
83 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
84 |
0
|
public SQLScanner(Reader input)... |
85 |
|
{ |
86 |
0
|
setInput(input); |
87 |
|
} |
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
@param |
93 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
94 |
0
|
public void setInput(Reader input)... |
95 |
|
{ |
96 |
0
|
in = input; |
97 |
|
} |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
@throws |
104 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 6 |
Complexity Density: 0.86 |
|
105 |
0
|
private void readChar() throws IOException... |
106 |
|
{ |
107 |
0
|
boolean wasLine = (char) chr == '\r'; |
108 |
0
|
chr = in.read(); |
109 |
0
|
if ((char) chr == '\n' || (char) chr == '\r' || (char) chr == '\f') |
110 |
|
{ |
111 |
0
|
col = 0; |
112 |
0
|
if (!wasLine || (char) chr != '\n') |
113 |
|
{ |
114 |
0
|
line++; |
115 |
|
} |
116 |
|
} |
117 |
|
else |
118 |
|
{ |
119 |
0
|
col++; |
120 |
|
} |
121 |
|
} |
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
@throws |
127 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 4 |
Complexity Density: 0.5 |
|
128 |
0
|
private void scanIdentifier () throws IOException... |
129 |
|
{ |
130 |
0
|
token = ""; |
131 |
0
|
char c = (char) chr; |
132 |
0
|
while (chr != -1 && WHITE.indexOf(c) == -1 && SPECIAL.indexOf(c) == -1) |
133 |
|
{ |
134 |
0
|
token = token + (char) chr; |
135 |
0
|
readChar(); |
136 |
0
|
c = (char) chr; |
137 |
|
} |
138 |
0
|
int start = col - token.length(); |
139 |
0
|
tokens.add(new Token(token, line, start)); |
140 |
|
} |
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
@throws |
146 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 4 |
Complexity Density: 0.5 |
|
147 |
0
|
private void scanNegativeIdentifier () throws IOException... |
148 |
|
{ |
149 |
0
|
token = "-"; |
150 |
0
|
char c = (char) chr; |
151 |
0
|
while (chr != -1 && WHITE.indexOf(c) == -1 && SPECIAL.indexOf(c) == -1) |
152 |
|
{ |
153 |
0
|
token = token + (char) chr; |
154 |
0
|
readChar(); |
155 |
0
|
c = (char) chr; |
156 |
|
} |
157 |
0
|
int start = col - token.length(); |
158 |
0
|
tokens.add(new Token(token, line, start)); |
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
@return |
165 |
|
@throws |
166 |
|
|
|
|
| 0% |
Uncovered Elements: 75 (75) |
Complexity: 19 |
Complexity Density: 0.42 |
|
167 |
0
|
public List scan () throws IOException... |
168 |
|
{ |
169 |
0
|
line = 1; |
170 |
0
|
col = 0; |
171 |
0
|
boolean inComment = false; |
172 |
0
|
boolean inCommentSlashStar = false; |
173 |
0
|
boolean inCommentDash = false; |
174 |
|
|
175 |
0
|
boolean inNegative; |
176 |
|
|
177 |
0
|
tokens = new ArrayList(); |
178 |
0
|
readChar(); |
179 |
0
|
while (chr != -1) |
180 |
|
{ |
181 |
0
|
char c = (char) chr; |
182 |
0
|
inNegative = false; |
183 |
|
|
184 |
0
|
if (c == COMMENT_DASH) |
185 |
|
{ |
186 |
0
|
readChar(); |
187 |
0
|
if ((char) chr == COMMENT_DASH) |
188 |
|
{ |
189 |
0
|
inCommentDash = true; |
190 |
|
} |
191 |
|
else |
192 |
|
{ |
193 |
0
|
inNegative = true; |
194 |
0
|
c = (char) chr; |
195 |
|
} |
196 |
|
} |
197 |
|
|
198 |
0
|
if (inCommentDash) |
199 |
|
{ |
200 |
0
|
if (c == '\n' || c == '\r') |
201 |
|
{ |
202 |
0
|
inCommentDash = false; |
203 |
|
} |
204 |
0
|
readChar(); |
205 |
|
} |
206 |
0
|
else if (c == COMMENT_POUND) |
207 |
|
{ |
208 |
0
|
inComment = true; |
209 |
0
|
readChar(); |
210 |
|
} |
211 |
0
|
else if (c == COMMENT_SLASH) |
212 |
|
{ |
213 |
0
|
readChar(); |
214 |
0
|
if ((char) chr == COMMENT_STAR) |
215 |
|
{ |
216 |
0
|
inCommentSlashStar = true; |
217 |
|
} |
218 |
|
} |
219 |
0
|
else if (inComment || inCommentSlashStar) |
220 |
|
{ |
221 |
0
|
if (c == '*') |
222 |
|
{ |
223 |
0
|
readChar(); |
224 |
0
|
if ((char) chr == COMMENT_SLASH) |
225 |
|
{ |
226 |
0
|
inCommentSlashStar = false; |
227 |
|
} |
228 |
|
} |
229 |
0
|
else if (c == '\n' || c == '\r') |
230 |
|
{ |
231 |
0
|
inComment = false; |
232 |
|
} |
233 |
0
|
readChar(); |
234 |
|
} |
235 |
0
|
else if (ALFANUM.indexOf(c) >= 0) |
236 |
|
{ |
237 |
0
|
if (inNegative) |
238 |
|
{ |
239 |
0
|
scanNegativeIdentifier(); |
240 |
|
} |
241 |
|
else |
242 |
|
{ |
243 |
0
|
scanIdentifier(); |
244 |
|
} |
245 |
|
} |
246 |
0
|
else if (SPECIAL.indexOf(c) >= 0) |
247 |
|
{ |
248 |
0
|
tokens.add(new Token("" + c, line, col)); |
249 |
0
|
readChar(); |
250 |
|
} |
251 |
|
else |
252 |
|
{ |
253 |
0
|
readChar(); |
254 |
|
} |
255 |
|
} |
256 |
0
|
return tokens; |
257 |
|
} |
258 |
|
} |