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