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 @ 44139

History | View | Annotate | Download (6.94 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
    
39
    @Override
40
    protected Token getToken() {
41
        skipblanks();
42
        char ch = getch();
43
        switch( ch ) {
44
        case EOF:
45
            token.set(Token.EOF, null, null);
46
            return token;
47
        case '~':
48
            token.set(Token.OP_REGEXP, "~");
49
            return token;
50
        case '*':
51
            token.set(Token.OP_MULT, "*");
52
            return token;
53
        case '/':
54
            token.set(Token.OP_DIV, "/");
55
            return token;
56
        case '%':
57
            token.set(Token.OP_MOD, "%");
58
            return token;
59
        case '=':
60
            token.set(Token.OP_EQ, "=");
61
            return token;
62

    
63
        case '<':
64
            ch = getch();
65
            switch( ch ) {
66
            case EOF:
67
                throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this);
68
            case '>':
69
                token.set(Token.OP_NE, "<>");
70
                return token;
71
            case '=':
72
                token.set(Token.OP_LE, "<=");
73
                return token;
74
            }
75
            ungetch();
76
            token.set(Token.OP_LT, "<");
77
            return token;
78

    
79
        case '>':
80
            ch = getch();
81
            switch( ch ) {
82
            case EOF:
83
                throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this);
84
            case '=':
85
                token.set(Token.OP_GE, ">=");
86
                return token;
87
            }
88
            ungetch();
89
            token.set(Token.OP_GT, ">");
90
            return token;
91

    
92
        case '|':
93
            ch = getch();
94
            switch( ch ) {
95
            case EOF:
96
                throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this);
97
            case '|':
98
                token.set(Token.OP_CONCAT, "||");
99
                return token;
100
            }
101
            ungetch();
102
            token.set(Token.OP_GT, "|");
103
            return token;
104

    
105
        case '.': // SQL Extension to access object methods and attributes
106
            token.set(Token.OP_GETATTR, ".");
107
            return token;
108
        case ',':
109
            token.set(Token.COMA, ",");
110
            return token;
111
        case '(':
112
            token.set(Token.PARENTHESIS_OPEN, "(");
113
            return token;
114
        case ')':
115
            token.set(Token.PARENTHESIS_CLOSE, ")");
116
            return token;
117
        case '+':
118
            token.set(Token.OP_ADD, "+");
119
            return token;
120
//            ch = getch();
121
//            if( ch == EOF ) {
122
//                throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this);
123
//            }
124
//            if( !Character.isDigit(ch) ) {
125
//                ungetch();
126
//                token.set(Token.OP_ADD, "+");
127
//                return token;
128
//            }
129
//            ungetch();
130
//            parseNumber();
131
//            token.setLiteral("+" + token.getLiteral());
132
//            return token;
133
        case '-':
134
            ch = getch();
135
            switch( ch ) {
136
            case EOF:
137
                throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this);
138
            case '>':
139
                // SQL Extension to access object methods and attributes
140
                token.set(Token.OP_GETATTR, "->");
141
                return token;
142
            }
143
//            if( Character.isDigit(ch) ) {
144
//                ungetch();
145
//                ungetch();
146
//                parseNumber();
147
//                return token;
148
//            }
149
            ungetch();
150
            token.set(Token.OP_SUBST, "-");
151
            return token;
152

    
153
        case '"':
154
            buffer.clear();
155
            ch = getch();
156
            while( ch != '"' ) {
157
                if( ch == EOF ) {
158
                    throw new ExpressionSyntaxException(I18N.End_of_string_was_expected_and_end_of_source_was_found(), this);
159
                }
160
                buffer.add(ch);
161
                ch = getch();
162
            }
163
            if( buffer.length() < 1 ) {
164
                throw new ExpressionSyntaxException(I18N.Incorrect_string_length(), this);
165
            }
166
            token.set(Token.IDENTIFIER, buffer.toString());
167
            return token;
168

    
169
        case ']':
170
            token.set(Token.CLOSED_BRACKET, "]");
171
            return token;
172
        case '[':
173
            if( !this.useBracketsForIdentifiers ) {
174
                token.set(Token.OPEN_BRACKET, "[");
175
                return token;
176
            }
177
            buffer.clear();
178
            ch = getch();
179
            while( ch != ']' ) {
180
                if( ch == EOF ) {
181
                    throw new ExpressionSyntaxException(I18N.Closing_square_bracket_was_expected_and_end_of_source_was_found(), this);
182
                }
183
                buffer.add(ch);
184
                ch = getch();
185
            }
186
            if( buffer.length() < 1 ) {
187
                throw new ExpressionSyntaxException(I18N.Incorrect_identifier_length(), this);
188
            }
189
            token.set(Token.IDENTIFIER, buffer.toString());
190
            return token;
191

    
192
        case '\'':
193
            parseString();
194
            return token;
195
        }
196
        if( Character.isDigit(ch) ) {
197
            ungetch();
198
            parseNumber();
199
            return token;
200
        }
201
        if( Character.isAlphabetic(ch) ) {
202
            buffer.clear();
203
            while( Character.isLetterOrDigit(ch) || ch == '_' ) {
204
                buffer.add(ch);
205
                ch = getch();
206
            }
207
            if( ch != EOF ) {
208
                ungetch();
209
            }
210
            String id = buffer.toString();
211
            int type = this.tokens.getOrDefault(id.toLowerCase(), Token.IDENTIFIER);
212
            token.set(type, id);
213
            return token;
214
        }
215

    
216
        token.set(Token.CHAR, Character.toString(ch), Character.toString(ch));
217
        return token;
218
    }
219

    
220
}