Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / console / jedit / SQLTokenMarker.java @ 40561

History | View | Annotate | Download (5.34 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.utils.console.jedit;
25
/*
26
 * SQLTokenMarker.java - Generic SQL token marker
27
 * Copyright (C) 1999 mike dillon
28
 *
29
 * You may use and modify this package for any purpose. Redistribution is
30
 * permitted, in both source and binary form, provided that this notice
31
 * remains intact in all source distributions of this package.
32
 */
33

    
34
import javax.swing.text.Segment;
35

    
36
/**
37
 * SQL token marker.
38
 *
39
 * @author mike dillon
40
 * @version $Id$
41
 */
42
public class SQLTokenMarker extends TokenMarker
43
{
44
        private int offset, lastOffset, lastKeyword, length;
45

    
46
        // public members
47
        public SQLTokenMarker(KeywordMap k)
48
        {
49
                this(k, false);
50
        }
51

    
52
        public SQLTokenMarker(KeywordMap k, boolean tsql)
53
        {
54
                keywords = k;
55
                isTSQL = tsql;
56
        }
57

    
58
        public byte markTokensImpl(byte token, Segment line, int lineIndex)
59
        {
60
                offset = lastOffset = lastKeyword = line.offset;
61
                length = line.count + offset;
62

    
63
loop:
64
                for(int i = offset; i < length; i++)
65
                {
66
                        switch(line.array[i])
67
                        {
68
                        case '*':
69
                                if(token == Token.COMMENT1 && length - i >= 1 && line.array[i+1] == '/')
70
                                {
71
                                        token = Token.NULL;
72
                                        i++;
73
                                        addToken((i + 1) - lastOffset,Token.COMMENT1);
74
                                        lastOffset = i + 1;
75
                                }
76
                                else if (token == Token.NULL)
77
                                {
78
                                        searchBack(line, i);
79
                                        addToken(1,Token.OPERATOR);
80
                                        lastOffset = i + 1;
81
                                }
82
                                break;
83
                        case '[':
84
                                if(token == Token.NULL)
85
                                {
86
                                        searchBack(line, i);
87
                                        token = Token.LITERAL1;
88
                                        literalChar = '[';
89
                                        lastOffset = i;
90
                                }
91
                                break;
92
                        case ']':
93
                                if(token == Token.LITERAL1 && literalChar == '[')
94
                                {
95
                                        token = Token.NULL;
96
                                        literalChar = 0;
97
                                        addToken((i + 1) - lastOffset,Token.LITERAL1);
98
                                        lastOffset = i + 1;
99
                                }
100
                                break;
101
                        case '.': case ',': case '(': case ')':
102
                                if (token == Token.NULL) {
103
                                        searchBack(line, i);
104
                                        addToken(1, Token.NULL);
105
                                        lastOffset = i + 1;
106
                                }
107
                                break;
108
                        case '+': case '%': case '&': case '|': case '^':
109
                        case '~': case '<': case '>': case '=':
110
                                if (token == Token.NULL) {
111
                                        searchBack(line, i);
112
                                        addToken(1,Token.OPERATOR);
113
                                        lastOffset = i + 1;
114
                                }
115
                                break;
116
                        case ' ': case '\t':
117
                                if (token == Token.NULL) {
118
                                        searchBack(line, i, false);
119
                                }
120
                                break;
121
                        case ':':
122
                                if(token == Token.NULL)
123
                                {
124
                                        addToken((i+1) - lastOffset,Token.LABEL);
125
                                        lastOffset = i + 1;
126
                                }
127
                                break;
128
                        case '/':
129
                                if(token == Token.NULL)
130
                                {
131
                                        if (length - i >= 2 && line.array[i + 1] == '*')
132
                                        {
133
                                                searchBack(line, i);
134
                                                token = Token.COMMENT1;
135
                                                lastOffset = i;
136
                                                i++;
137
                                        }
138
                                        else
139
                                        {
140
                                                searchBack(line, i);
141
                                                addToken(1,Token.OPERATOR);
142
                                                lastOffset = i + 1;
143
                                        }
144
                                }
145
                                break;
146
                        case '-':
147
                                if(token == Token.NULL)
148
                                {
149
                                        if (length - i >= 2 && line.array[i+1] == '-')
150
                                        {
151
                                                searchBack(line, i);
152
                                                addToken(length - i,Token.COMMENT1);
153
                                                lastOffset = length;
154
                                                break loop;
155
                                        }
156
                                        else
157
                                        {
158
                                                searchBack(line, i);
159
                                                addToken(1,Token.OPERATOR);
160
                                                lastOffset = i + 1;
161
                                        }
162
                                }
163
                                break;
164
                        case '!':
165
                                if(isTSQL && token == Token.NULL && length - i >= 2 &&
166
                                (line.array[i+1] == '=' || line.array[i+1] == '<' || line.array[i+1] == '>'))
167
                                {
168
                                        searchBack(line, i);
169
                                        addToken(1,Token.OPERATOR);
170
                                        lastOffset = i + 1;
171
                                }
172
                                break;
173
                        case '"': case '\'':
174
                                if(token == Token.NULL)
175
                                {
176
                                        token = Token.LITERAL1;
177
                                        literalChar = line.array[i];
178
                                        addToken(i - lastOffset,Token.NULL);
179
                                        lastOffset = i;
180
                                }
181
                                else if(token == Token.LITERAL1 && literalChar == line.array[i])
182
                                {
183
                                        token = Token.NULL;
184
                                        literalChar = 0;
185
                                        addToken((i + 1) - lastOffset,Token.LITERAL1);
186
                                        lastOffset = i + 1;
187
                                }
188
                                break;
189
                        default:
190
                                break;
191
                        }
192
                }
193
                if(token == Token.NULL)
194
                        searchBack(line, length, false);
195
                if(lastOffset != length)
196
                        addToken(length - lastOffset,token);
197
                return token;
198
        }
199

    
200
        // protected members
201
        protected boolean isTSQL = false;
202

    
203
        // private members
204
        private KeywordMap keywords;
205
        private char literalChar = 0;
206

    
207
        private void searchBack(Segment line, int pos)
208
        {
209
                searchBack(line, pos, true);
210
        }
211

    
212
        private void searchBack(Segment line, int pos, boolean padNull)
213
        {
214
                int len = pos - lastKeyword;
215
                byte id = keywords.lookup(line,lastKeyword,len);
216
                if(id != Token.NULL)
217
                {
218
                        if(lastKeyword != lastOffset)
219
                                addToken(lastKeyword - lastOffset,Token.NULL);
220
                        addToken(len,id);
221
                        lastOffset = pos;
222
                }
223
                lastKeyword = pos + 1;
224
                if (padNull && lastOffset < pos)
225
                        addToken(pos - lastOffset, Token.NULL);
226
        }
227
}