KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > source > sql > ParserHelper


1 package jimm.datavision.source.sql;
2
3 /**
4  * A helper class used by a SQL query while parsing the WHERE clause.
5  *
6  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
7  * @see SQLQuery
8  */

9 public class ParserHelper {
10
11 protected String JavaDoc str;
12 protected int startPos;
13 protected String JavaDoc prevToken;
14 protected int prevTokenStartPos;
15 protected int endBeforeToken;
16
17 public ParserHelper(String JavaDoc s, int pos) {
18     str = s;
19     startPos = pos;
20
21     findPreviousSQLToken();
22 }
23
24 /**
25  * Finds the token before the one that starts at <var>startPos</var> in
26  * <var>str</var>. The token will not include the whitespace surrounding
27  * it, if any.
28  *
29  * @return the token before the word that starts at <var>pos</var>; may be
30  * the empty string but will not be <code>null</code>
31  */

32 protected void findPreviousSQLToken() {
33     if (startPos == 0) {
34     prevToken = "";
35     prevTokenStartPos = 0;
36     endBeforeToken = 0;
37     return;
38     }
39
40     // Move backwards, skipping whitespace, to the end of the previous token.
41
prevTokenStartPos = startPos - 1;
42     while (prevTokenStartPos >= 0
43        && Character.isSpaceChar(str.charAt(prevTokenStartPos)))
44     --prevTokenStartPos;
45
46     int tokenEnd = prevTokenStartPos + 1;
47
48     char c = str.charAt(prevTokenStartPos);
49     boolean prevWordIsAlpha = (Character.isLetterOrDigit(c) || c == '_');
50
51     // Continue moving backwards, stopping when we get to whitespace or '}'
52
// or a char that is not of the same type.
53
while (prevTokenStartPos >= 0
54        && !Character.isSpaceChar(c = str.charAt(prevTokenStartPos))
55        && !(c == '}') // Always stop at '}'
56
&& (prevWordIsAlpha
57            ? (Character.isLetterOrDigit(c) || c == '_')
58            : !(Character.isLetterOrDigit(c) || c == '_')))
59     --prevTokenStartPos;
60     ++prevTokenStartPos;
61
62     // Set flag which tells us if there is a space before the previous token
63
// spaceBeforePrevToken = prevTokenStartPos > 0
64
// && Character.isSpaceChar(str.charAt(prevTokenStartPos - 1));
65

66     // Find the index of the character after the token before this token.
67
// That's the same as the beginning of this whitespace before this
68
// token or, if none, the beginning of this token.
69
if (prevTokenStartPos == 0)
70     endBeforeToken = 0;
71     else {
72     endBeforeToken = prevTokenStartPos - 1;
73     while (endBeforeToken >= 0
74            && Character.isSpaceChar(str.charAt(endBeforeToken)))
75         --endBeforeToken;
76     ++endBeforeToken;
77     }
78
79     // Finally, grab the token itself, sans whitespace.
80
prevToken = str.substring(prevTokenStartPos, tokenEnd);
81 }
82
83 /**
84  * Returns the previous token, sans whitespace.
85  *
86  * @return a possibly empty string
87  */

88 public String JavaDoc getPrevToken() { return prevToken; }
89
90 /**
91  * Returns the starting position of the previous token in the original
92  * string, not including any leading whitespace.
93  *
94  * @return a string index pointing to the start of the previous token
95  */

96 public int getPrevTokenStartPos() { return prevTokenStartPos; }
97
98 public int getEndBeforeToken() { return endBeforeToken; }
99
100 }
101
Popular Tags