1 21 22 package org.apache.derby.impl.tools.ij; 23 24 import java.io.IOException ; 25 import java.io.Reader ; 26 27 48 49 public class StatementFinder { 50 51 private Reader source; 52 private StringBuffer statement = new StringBuffer (); 53 private int state; 54 private boolean atEOF = false; 55 private boolean peekEOF = false; 56 private char peekChar; 57 private boolean peeked = false; 58 59 private static final int IN_STATEMENT = 0; 61 private static final int IN_STRING = 1; 62 private static final int IN_SQLCOMMENT = 2; 63 private static final int END_OF_STATEMENT = 3; 64 private static final int END_OF_INPUT = 4; 65 66 private static final char MINUS = '-'; 68 private static final char SINGLEQUOTE = '\''; 69 private static final char DOUBLEQUOTE = '\"'; 70 private static final char SEMICOLON = ';'; 71 private static final char NEWLINE = '\n'; 72 private static final char RETURN = '\r'; 73 private static final char SPACE = ' '; 74 private static final char TAB = '\t'; 75 private static final char FORMFEED = '\f'; 76 77 83 public StatementFinder(Reader s) { 84 source = s; 85 } 86 87 93 public void ReInit(Reader s) { 94 try { 95 source.close(); 96 } catch (IOException ioe) { 97 } 99 source = s; 100 state = IN_STATEMENT; 101 atEOF = false; 102 peekEOF = false; 103 peeked = false; 104 } 105 106 public void close() throws IOException { 107 source.close(); 108 } 109 110 117 public String nextStatement() { 118 boolean haveSemi = false; 119 char nextChar; 120 121 statement.setLength(0); 123 if (state == END_OF_INPUT) return null; 124 125 state = IN_STATEMENT; 126 127 nextChar = peekChar(); 129 if (peekEOF()) { 130 state = END_OF_INPUT; 131 return null; 132 } 133 if (whiteSpace(nextChar)) { 134 while (whiteSpace(peekChar()) && ! peekEOF()); 135 if (peekEOF()) { 136 state = END_OF_INPUT; 137 return null; 138 } 139 } 140 141 while (state != END_OF_STATEMENT && state != END_OF_INPUT) { 142 143 nextChar = readChar(); 145 if (atEOF()) { 146 state = END_OF_INPUT; 147 break; 148 } 149 150 switch(nextChar) { 151 case MINUS: 152 readSingleLineComment(nextChar); 153 break; 154 case SINGLEQUOTE: 155 case DOUBLEQUOTE: 156 readString(nextChar); 157 break; 158 case SEMICOLON: 159 haveSemi = true; 160 state = END_OF_STATEMENT; 161 break; 162 default: 163 break; 165 } 166 } 167 168 if (haveSemi) 169 statement.setLength(statement.length()-1); 170 return statement.toString(); 171 } 172 173 179 private boolean whiteSpace(char c) { 180 return (c == SPACE || 181 c == TAB || 182 c == RETURN || 183 c == NEWLINE || 184 c == FORMFEED); 185 } 186 187 199 private void readSingleLineComment(char commentChar) { 200 char nextChar; 201 202 nextChar = peekChar(); 203 if (peekEOF()) return; 205 206 if (nextChar != commentChar) return; 209 210 readChar(); 213 state = IN_SQLCOMMENT; 214 do { 215 nextChar = peekChar(); 216 if (peekEOF()) { 217 state = IN_STATEMENT; 219 return; 220 } 221 switch (nextChar) { 222 case NEWLINE: 223 case RETURN: 224 readChar(); state = IN_STATEMENT; 226 return; 227 default: 228 readChar(); break; 230 } 231 } while (state == IN_SQLCOMMENT); } 233 234 254 private void readString(char stringDelimiter) { 255 state = IN_STRING; 256 do { 257 char nextChar = readChar(); 258 259 if (atEOF()) { 260 state = END_OF_INPUT; 261 return; 262 } 263 264 if (nextChar == stringDelimiter) { 265 state = IN_STATEMENT; 267 return; 268 } 269 270 } while (state == IN_STRING); } 273 274 private boolean atEOF() { 275 return atEOF; 276 } 277 278 private boolean peekEOF() { 279 return peekEOF; 280 } 281 282 288 private char readChar() { 289 if (!peeked) peekChar(); 290 291 peeked = false; 292 atEOF = peekEOF; 293 294 if (!atEOF) statement.append(peekChar); 295 296 return peekChar; 297 } 298 299 305 private char peekChar() { 306 peeked = true; 307 char c = '\00'; 308 309 try { 310 int cInt; 311 312 cInt = source.read(); 316 peekEOF = (cInt == -1); 317 if (!peekEOF) c = (char)cInt; 318 } catch (IOException ie) { 319 throw ijException.iOException(ie); 320 } 321 322 peekChar = c; 323 return c; 324 } 325 } 326 | Popular Tags |