1 19 package com.mysql.jdbc; 20 21 22 28 public class EscapeTokenizer { 29 private String 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 45 public EscapeTokenizer(String s) { 46 source = s; 47 sourceLength = s.length(); 48 pos = 0; 49 } 50 51 56 public synchronized boolean hasMoreTokens() { 57 return (pos < sourceLength); 58 } 59 60 65 public synchronized String nextToken() { 66 StringBuffer tokenBuf = new StringBuffer (); 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 |