Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.impl / src / main / java / org / gvsig / expressionevaluator / impl / SQLLexicalAnalyzer.java @ 44430

History | View | Annotate | Download (7.53 KB)

1
package org.gvsig.expressionevaluator.impl;
2

    
3
import org.gvsig.expressionevaluator.ExpressionSyntaxException;
4
import org.gvsig.expressionevaluator.spi.AbstractLexicalAnalyzer;
5
import org.gvsig.expressionevaluator.LexicalAnalyzer;
6

    
7
public class SQLLexicalAnalyzer extends AbstractLexicalAnalyzer {
8

    
9
    public SQLLexicalAnalyzer(String source) {
10
        super(source);
11
        this.tokens.put("null", Token.NULL);
12
        this.tokens.put("true", Token.TRUE);
13
        this.tokens.put("false", Token.FALSE);
14
        this.tokens.put("not", Token.OP_NOT);
15
        this.tokens.put("like", Token.PRED_LIKE);
16
        this.tokens.put("ilike", Token.PRED_ILIKE);
17
        this.tokens.put("is", Token.PRED_IS);
18
        this.tokens.put("between", Token.PRED_BETWEEN);
19
        this.tokens.put("and", Token.OP_AND);
20
        this.tokens.put("or", Token.OP_OR);
21
        this.tokens.put("isnull", Token.ISNULL);
22
        this.tokens.put("notnull", Token.NOTNULL);
23
    }
24

    
25
    public SQLLexicalAnalyzer() {
26
        this(null);
27
    }
28

    
29
    @Override
30
    public LexicalAnalyzer clone() throws CloneNotSupportedException {
31
        // As this implementation does not add state to the abstract class, we 
32
        // just call the super class.
33
        SQLLexicalAnalyzer other = (SQLLexicalAnalyzer) super.clone();
34

    
35
        return other;
36
    }
37

    
38
    private void skipcomments() {
39
        if (isEOF()) {
40
            return;
41
        }
42
        char ch = getch();
43
        while( ch == '-' ) {
44
            ch = lookch();
45
            if( ch == '-' ) {
46
                while( ch != EOF && ch != '\n' ) {
47
                    ch = getch();
48
                }
49
                ungetch();
50
                skipblanks();
51
                ch = getch();
52
            }
53
        }
54
        ungetch();
55
    }
56
    
57
    @Override
58
    protected Token getToken() {
59
        skipblanks();
60
        skipcomments();
61
        char ch = getch();
62
        switch( ch ) {
63
        case EOF:
64
            token.set(Token.EOF, null, null);
65
            return token;
66
        case '~':
67
            token.set(Token.OP_REGEXP, "~");
68
            return token;
69
        case '*':
70
            token.set(Token.OP_MULT, "*");
71
            return token;
72
        case '/':
73
            token.set(Token.OP_DIV, "/");
74
            return token;
75
        case '%':
76
            token.set(Token.OP_MOD, "%");
77
            return token;
78
        case '=':
79
            token.set(Token.OP_EQ, "=");
80
            return token;
81

    
82
        case '<':
83
            ch = getch();
84
            switch( ch ) {
85
            case EOF:
86
                throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this);
87
            case '>':
88
                token.set(Token.OP_NE, "<>");
89
                return token;
90
            case '=':
91
                token.set(Token.OP_LE, "<=");
92
                return token;
93
            }
94
            ungetch();
95
            token.set(Token.OP_LT, "<");
96
            return token;
97

    
98
        case '>':
99
            ch = getch();
100
            switch( ch ) {
101
            case EOF:
102
                throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this);
103
            case '=':
104
                token.set(Token.OP_GE, ">=");
105
                return token;
106
            }
107
            ungetch();
108
            token.set(Token.OP_GT, ">");
109
            return token;
110

    
111
        case '|':
112
            ch = getch();
113
            switch( ch ) {
114
            case EOF:
115
                throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this);
116
            case '|':
117
                token.set(Token.OP_CONCAT, "||");
118
                return token;
119
            }
120
            ungetch();
121
            token.set(Token.OP_GT, "|");
122
            return token;
123

    
124
        case '.': // SQL Extension to access object methods and attributes
125
            token.set(Token.OP_GETATTR, ".");
126
            return token;
127
        case ',':
128
            token.set(Token.COMA, ",");
129
            return token;
130
        case '(':
131
            token.set(Token.PARENTHESIS_OPEN, "(");
132
            return token;
133
        case ')':
134
            token.set(Token.PARENTHESIS_CLOSE, ")");
135
            return token;
136
        case '+':
137
            token.set(Token.OP_ADD, "+");
138
            return token;
139
//            ch = getch();
140
//            if( ch == EOF ) {
141
//                throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this);
142
//            }
143
//            if( !Character.isDigit(ch) ) {
144
//                ungetch();
145
//                token.set(Token.OP_ADD, "+");
146
//                return token;
147
//            }
148
//            ungetch();
149
//            parseNumber();
150
//            token.setLiteral("+" + token.getLiteral());
151
//            return token;
152
        case '-':
153
            ch = getch();
154
            switch( ch ) {
155
            case EOF:
156
                throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this);
157
            case '>':
158
                // SQL Extension to access object methods and attributes
159
                token.set(Token.OP_GETATTR, "->");
160
                return token;
161
            }
162
//            if( Character.isDigit(ch) ) {
163
//                ungetch();
164
//                ungetch();
165
//                parseNumber();
166
//                return token;
167
//            }
168
            ungetch();
169
            token.set(Token.OP_SUBST, "-");
170
            return token;
171

    
172
        case '"':
173
            buffer.clear();
174
            ch = getch();
175
            while( ch != '"' ) {
176
                if( ch == EOF ) {
177
                    throw new ExpressionSyntaxException(I18N.End_of_string_was_expected_and_end_of_source_was_found(), this);
178
                }
179
                buffer.add(ch);
180
                ch = getch();
181
            }
182
            if( buffer.length() < 1 ) {
183
                throw new ExpressionSyntaxException(I18N.Incorrect_string_length(), this);
184
            }
185
            token.set(Token.IDENTIFIER, buffer.toString());
186
            return token;
187

    
188
        case ']':
189
            token.set(Token.CLOSED_BRACKET, "]");
190
            return token;
191
        case '[':
192
            if( !this.useBracketsForIdentifiers ) {
193
                token.set(Token.OPEN_BRACKET, "[");
194
                return token;
195
            }
196
            buffer.clear();
197
            ch = getch();
198
            while( ch != ']' ) {
199
                if( ch == EOF ) {
200
                    throw new ExpressionSyntaxException(I18N.Closing_square_bracket_was_expected_and_end_of_source_was_found(), this);
201
                }
202
                buffer.add(ch);
203
                ch = getch();
204
            }
205
            if( buffer.length() < 1 ) {
206
                throw new ExpressionSyntaxException(I18N.Incorrect_identifier_length(), this);
207
            }
208
            token.set(Token.IDENTIFIER, buffer.toString());
209
            return token;
210

    
211
        case '\'':
212
            parseString();
213
            return token;
214
        
215
        case '@':
216
            ungetch();
217
            parseDMSNumber();
218
            return token;
219
        }
220
        
221
        if( Character.isDigit(ch) ) {
222
            ungetch();
223
            parseNumber();
224
            return token;
225
        }
226
        if( Character.isAlphabetic(ch) || ch =='$' ) {
227
            buffer.clear();
228
            while( Character.isLetterOrDigit(ch) || ch =='$' || ch == '_' ) {
229
                buffer.add(ch);
230
                ch = getch();
231
            }
232
            if( ch != EOF ) {
233
                ungetch();
234
            }
235
            String id = buffer.toString();
236
            int type = this.tokens.getOrDefault(id.toLowerCase(), Token.IDENTIFIER);
237
            token.set(type, id);
238
            return token;
239
        }
240

    
241
        token.set(Token.CHAR, Character.toString(ch), Character.toString(ch));
242
        return token;
243
    }
244

    
245
}