KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mysql > jdbc > EscapeTokenizer


1 /*
2    Copyright (C) 2002 MySQL AB
3
4       This program is free software; you can redistribute it and/or modify
5       it under the terms of the GNU General Public License as published by
6       the Free Software Foundation; either version 2 of the License, or
7       (at your option) any later version.
8
9       This program is distributed in the hope that it will be useful,
10       but WITHOUT ANY WARRANTY; without even the implied warranty of
11       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12       GNU General Public License for more details.
13
14       You should have received a copy of the GNU General Public License
15       along with this program; if not, write to the Free Software
16       Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18  */

19 package com.mysql.jdbc;
20
21
22 /**
23  * EscapeTokenizer breaks up an SQL statement into SQL and
24  * escape code parts.
25  *
26  * @author Mark Matthews
27  */

28 public class EscapeTokenizer {
29     private String JavaDoc source = null;
30     private boolean emittingEscapeCode = false;
31     private boolean inComment = false;
32     private boolean inQuotes = false;
33     private char lastChar = 0;
34     private char lastLastChar = 0;
35     private char quoteChar = 0;
36     private int bracesLevel = 0;
37     private int pos = 0;
38     private int sourceLength = 0;
39
40     /**
41      * Creates a new EscapeTokenizer object.
42      *
43      * @param s the string to tokenize
44      */

45     public EscapeTokenizer(String JavaDoc s) {
46         source = s;
47         sourceLength = s.length();
48         pos = 0;
49     }
50
51     /**
52      * Does this tokenizer have more tokens available?
53      *
54      * @return if this tokenizer has more tokens available
55      */

56     public synchronized boolean hasMoreTokens() {
57         return (pos < sourceLength);
58     }
59
60     /**
61      * Returns the next token
62      *
63      * @return the next token.
64      */

65     public synchronized String JavaDoc nextToken() {
66         StringBuffer JavaDoc tokenBuf = new StringBuffer JavaDoc();
67
68         if (emittingEscapeCode) {
69             tokenBuf.append("{");
70             emittingEscapeCode = false;
71         }
72
73         for (; pos < sourceLength; pos++) {
74             char c = source.charAt(pos);
75
76             if (c == '\'') {
77                 if (lastChar != '\\') {
78                     if (inQuotes) {
79                         if (quoteChar == c) {
80                             inQuotes = false;
81                         }
82                     } else {
83                         inQuotes = true;
84                         quoteChar = c;
85                     }
86                 } else if (lastLastChar == '\\') {
87                     if (inQuotes) {
88                         if (quoteChar == c) {
89                             inQuotes = false;
90                         }
91                     } else {
92                         inQuotes = true;
93                         quoteChar = c;
94                     }
95                 }
96
97                 tokenBuf.append(c);
98             } else if (c == '"') {
99                 if ((lastChar != '\\') && (lastChar != '"')) {
100                     if (inQuotes) {
101                         if (quoteChar == c) {
102                             inQuotes = false;
103                         }
104                     } else {
105                         inQuotes = true;
106                         quoteChar = c;
107                     }
108                 } else if (lastLastChar == '\\') {
109                     if (inQuotes) {
110                         if (quoteChar == c) {
111                             inQuotes = false;
112                         }
113                     } else {
114                         inQuotes = true;
115                         quoteChar = c;
116                     }
117                 }
118
119                 tokenBuf.append(c);
120             } else if (c == '-') {
121                 if ((lastChar == '-') && ((lastLastChar != '\\') & !inQuotes)) {
122                     inComment = true;
123                 }
124
125                 tokenBuf.append(c);
126             } else if ((c == '\n') || (c == '\r')) {
127                 inComment = false;
128
129                 tokenBuf.append(c);
130             } else if (c == '{') {
131                 if (inQuotes || inComment) {
132                     tokenBuf.append(c);
133                 } else {
134                     bracesLevel++;
135
136                     if (bracesLevel == 1) {
137                         pos++;
138                         emittingEscapeCode = true;
139
140                         return tokenBuf.toString();
141                     } else {
142                         tokenBuf.append(c);
143                     }
144                 }
145             } else if (c == '}') {
146                 tokenBuf.append(c);
147
148                 if (!inQuotes && !inComment) {
149                     lastChar = c;
150
151                     bracesLevel--;
152
153                     if (bracesLevel == 0) {
154                         pos++;
155
156                         return tokenBuf.toString();
157                     }
158                 }
159             } else {
160                 tokenBuf.append(c);
161             }
162
163             lastLastChar = lastChar;
164             lastChar = c;
165         }
166
167         return tokenBuf.toString();
168     }
169 }
170
Popular Tags