1 package org.apache.torque.engine.sql;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.io.IOException;
58 import java.io.Reader;
59 import java.util.List;
60 import java.util.ArrayList;
61
62 /***
63 * A simple Scanner implementation that scans an
64 * sql file into usable tokens. Used by SQLToAppData.
65 *
66 * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
67 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
68 * @author <a href="mailto:andyhot@di.uoa.gr">Andreas Andreou</a>
69 * @version $Id: SQLScanner.java,v 1.3 2003/08/08 16:54:53 mpoeschl Exp $
70 */
71 public class SQLScanner
72 {
73 /*** white spaces */
74 private static final String WHITE = "\f\r\t\n ";
75 /*** alphabetic characters */
76 private static final String ALFA
77 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
78 /*** numbers */
79 private static final String NUMER = "0123456789";
80 /*** alphanumeric */
81 private static final String ALFANUM = ALFA + NUMER;
82 /*** special characters */
83 private static final String SPECIAL = ";(),'";
84 /*** comment */
85 private static final char COMMENT_POUND = '#';
86 /*** comment */
87 private static final char COMMENT_SLASH = '/';
88 /*** comment */
89 private static final char COMMENT_STAR = '*';
90 /*** comment */
91 private static final char COMMENT_DASH = '-';
92
93 /*** the input reader */
94 private Reader in;
95 /*** character */
96 private int chr;
97 /*** token */
98 private String token;
99 /*** list of tokens */
100 private List tokens;
101 /*** line */
102 private int line;
103 /*** column */
104 private int col;
105
106 /***
107 * Creates a new scanner with no Reader
108 */
109 public SQLScanner()
110 {
111 this(null);
112 }
113
114 /***
115 * Creates a new scanner with an Input Reader
116 *
117 * @param input the input reader
118 */
119 public SQLScanner(Reader input)
120 {
121 setInput(input);
122 }
123
124 /***
125 * Set the Input
126 *
127 * @param input the input reader
128 */
129 public void setInput(Reader input)
130 {
131 in = input;
132 }
133
134
135 /***
136 * Reads the next character and increments the line and column counters.
137 *
138 * @throws IOException If an I/O error occurs
139 */
140 private void readChar() throws IOException
141 {
142 boolean wasLine = (char) chr == '\r';
143 chr = in.read();
144 if ((char) chr == '\n' || (char) chr == '\r' || (char) chr == '\f')
145 {
146 col = 0;
147 if (!wasLine || (char) chr != '\n')
148 {
149 line++;
150 }
151 }
152 else
153 {
154 col++;
155 }
156 }
157
158 /***
159 * Scans an identifier.
160 *
161 * @throws IOException If an I/O error occurs
162 */
163 private void scanIdentifier () throws IOException
164 {
165 token = "";
166 char c = (char) chr;
167 while (chr != -1 && WHITE.indexOf(c) == -1 && SPECIAL.indexOf(c) == -1)
168 {
169 token = token + (char) chr;
170 readChar();
171 c = (char) chr;
172 }
173 int start = col - token.length();
174 tokens.add(new Token(token, line, start));
175 }
176
177 /***
178 * Scans an identifier which had started with the negative sign.
179 *
180 * @throws IOException If an I/O error occurs
181 */
182 private void scanNegativeIdentifier () throws IOException
183 {
184 token = "-";
185 char c = (char) chr;
186 while (chr != -1 && WHITE.indexOf(c) == -1 && SPECIAL.indexOf(c) == -1)
187 {
188 token = token + (char) chr;
189 readChar();
190 c = (char) chr;
191 }
192 int start = col - token.length();
193 tokens.add(new Token(token, line, start));
194 }
195
196 /***
197 * Scan the input Reader and returns a list of tokens.
198 *
199 * @return a list of tokens
200 * @throws IOException If an I/O error occurs
201 */
202 public List scan () throws IOException
203 {
204 line = 1;
205 col = 0;
206 boolean inComment = false;
207 boolean inCommentSlashStar = false;
208 boolean inCommentDash = false;
209
210 boolean inNegative;
211
212 tokens = new ArrayList();
213 readChar();
214 while (chr != -1)
215 {
216 char c = (char) chr;
217 inNegative = false;
218
219 if (c == COMMENT_DASH)
220 {
221 readChar();
222 if ((char) chr == COMMENT_DASH)
223 {
224 inCommentDash = true;
225 }
226 else
227 {
228 inNegative = true;
229 c = (char) chr;
230 }
231 }
232
233 if (inCommentDash)
234 {
235 if (c == '\n' || c == '\r')
236 {
237 inCommentDash = false;
238 }
239 readChar();
240 }
241 else if (c == COMMENT_POUND)
242 {
243 inComment = true;
244 readChar();
245 }
246 else if (c == COMMENT_SLASH)
247 {
248 readChar();
249 if ((char) chr == COMMENT_STAR)
250 {
251 inCommentSlashStar = true;
252 }
253 }
254 else if (inComment || inCommentSlashStar)
255 {
256 if (c == '*')
257 {
258 readChar();
259 if ((char) chr == COMMENT_SLASH)
260 {
261 inCommentSlashStar = false;
262 }
263 }
264 else if (c == '\n' || c == '\r')
265 {
266 inComment = false;
267 }
268 readChar();
269 }
270 else if (ALFANUM.indexOf(c) >= 0)
271 {
272 if (inNegative)
273 {
274 scanNegativeIdentifier();
275 }
276 else
277 {
278 scanIdentifier();
279 }
280 }
281 else if (SPECIAL.indexOf(c) >= 0)
282 {
283 tokens.add(new Token("" + c, line, col));
284 readChar();
285 }
286 else
287 {
288 readChar();
289 }
290 }
291 return tokens;
292 }
293 }
This page was automatically generated by Maven