00001
00002 package org.objectweb.jonas_ejb.deployment.ejbql;
00003
00009 public class SimpleCharStream
00010 {
00011 public static final boolean staticFlag = false;
00012 int bufsize;
00013 int available;
00014 int tokenBegin;
00015 public int bufpos = -1;
00016 protected int bufline[];
00017 protected int bufcolumn[];
00018
00019 protected int column = 0;
00020 protected int line = 1;
00021
00022 protected boolean prevCharIsCR = false;
00023 protected boolean prevCharIsLF = false;
00024
00025 protected java.io.Reader inputStream;
00026
00027 protected char[] buffer;
00028 protected int maxNextCharInd = 0;
00029 protected int inBuf = 0;
00030
00031 protected void ExpandBuff(boolean wrapAround)
00032 {
00033 char[] newbuffer = new char[bufsize + 2048];
00034 int newbufline[] = new int[bufsize + 2048];
00035 int newbufcolumn[] = new int[bufsize + 2048];
00036
00037 try
00038 {
00039 if (wrapAround)
00040 {
00041 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
00042 System.arraycopy(buffer, 0, newbuffer,
00043 bufsize - tokenBegin, bufpos);
00044 buffer = newbuffer;
00045
00046 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
00047 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
00048 bufline = newbufline;
00049
00050 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
00051 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
00052 bufcolumn = newbufcolumn;
00053
00054 maxNextCharInd = (bufpos += (bufsize - tokenBegin));
00055 }
00056 else
00057 {
00058 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
00059 buffer = newbuffer;
00060
00061 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
00062 bufline = newbufline;
00063
00064 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
00065 bufcolumn = newbufcolumn;
00066
00067 maxNextCharInd = (bufpos -= tokenBegin);
00068 }
00069 }
00070 catch (Throwable t)
00071 {
00072 throw new Error(t.getMessage());
00073 }
00074
00075
00076 bufsize += 2048;
00077 available = bufsize;
00078 tokenBegin = 0;
00079 }
00080
00081 protected void FillBuff() throws java.io.IOException
00082 {
00083 if (maxNextCharInd == available)
00084 {
00085 if (available == bufsize)
00086 {
00087 if (tokenBegin > 2048)
00088 {
00089 bufpos = maxNextCharInd = 0;
00090 available = tokenBegin;
00091 }
00092 else if (tokenBegin < 0)
00093 bufpos = maxNextCharInd = 0;
00094 else
00095 ExpandBuff(false);
00096 }
00097 else if (available > tokenBegin)
00098 available = bufsize;
00099 else if ((tokenBegin - available) < 2048)
00100 ExpandBuff(true);
00101 else
00102 available = tokenBegin;
00103 }
00104
00105 int i;
00106 try {
00107 if ((i = inputStream.read(buffer, maxNextCharInd,
00108 available - maxNextCharInd)) == -1)
00109 {
00110 inputStream.close();
00111 throw new java.io.IOException();
00112 }
00113 else
00114 maxNextCharInd += i;
00115 return;
00116 }
00117 catch(java.io.IOException e) {
00118 --bufpos;
00119 backup(0);
00120 if (tokenBegin == -1)
00121 tokenBegin = bufpos;
00122 throw e;
00123 }
00124 }
00125
00126 public char BeginToken() throws java.io.IOException
00127 {
00128 tokenBegin = -1;
00129 char c = readChar();
00130 tokenBegin = bufpos;
00131
00132 return c;
00133 }
00134
00135 protected void UpdateLineColumn(char c)
00136 {
00137 column++;
00138
00139 if (prevCharIsLF)
00140 {
00141 prevCharIsLF = false;
00142 line += (column = 1);
00143 }
00144 else if (prevCharIsCR)
00145 {
00146 prevCharIsCR = false;
00147 if (c == '\n')
00148 {
00149 prevCharIsLF = true;
00150 }
00151 else
00152 line += (column = 1);
00153 }
00154
00155 switch (c)
00156 {
00157 case '\r' :
00158 prevCharIsCR = true;
00159 break;
00160 case '\n' :
00161 prevCharIsLF = true;
00162 break;
00163 case '\t' :
00164 column--;
00165 column += (8 - (column & 07));
00166 break;
00167 default :
00168 break;
00169 }
00170
00171 bufline[bufpos] = line;
00172 bufcolumn[bufpos] = column;
00173 }
00174
00175 public char readChar() throws java.io.IOException
00176 {
00177 if (inBuf > 0)
00178 {
00179 --inBuf;
00180
00181 if (++bufpos == bufsize)
00182 bufpos = 0;
00183
00184 return buffer[bufpos];
00185 }
00186
00187 if (++bufpos >= maxNextCharInd)
00188 FillBuff();
00189
00190 char c = buffer[bufpos];
00191
00192 UpdateLineColumn(c);
00193 return (c);
00194 }
00195
00201 public int getColumn() {
00202 return bufcolumn[bufpos];
00203 }
00204
00210 public int getLine() {
00211 return bufline[bufpos];
00212 }
00213
00214 public int getEndColumn() {
00215 return bufcolumn[bufpos];
00216 }
00217
00218 public int getEndLine() {
00219 return bufline[bufpos];
00220 }
00221
00222 public int getBeginColumn() {
00223 return bufcolumn[tokenBegin];
00224 }
00225
00226 public int getBeginLine() {
00227 return bufline[tokenBegin];
00228 }
00229
00230 public void backup(int amount) {
00231
00232 inBuf += amount;
00233 if ((bufpos -= amount) < 0)
00234 bufpos += bufsize;
00235 }
00236
00237 public SimpleCharStream(java.io.Reader dstream, int startline,
00238 int startcolumn, int buffersize)
00239 {
00240 inputStream = dstream;
00241 line = startline;
00242 column = startcolumn - 1;
00243
00244 available = bufsize = buffersize;
00245 buffer = new char[buffersize];
00246 bufline = new int[buffersize];
00247 bufcolumn = new int[buffersize];
00248 }
00249
00250 public SimpleCharStream(java.io.Reader dstream, int startline,
00251 int startcolumn)
00252 {
00253 this(dstream, startline, startcolumn, 4096);
00254 }
00255
00256 public SimpleCharStream(java.io.Reader dstream)
00257 {
00258 this(dstream, 1, 1, 4096);
00259 }
00260 public void ReInit(java.io.Reader dstream, int startline,
00261 int startcolumn, int buffersize)
00262 {
00263 inputStream = dstream;
00264 line = startline;
00265 column = startcolumn - 1;
00266
00267 if (buffer == null || buffersize != buffer.length)
00268 {
00269 available = bufsize = buffersize;
00270 buffer = new char[buffersize];
00271 bufline = new int[buffersize];
00272 bufcolumn = new int[buffersize];
00273 }
00274 prevCharIsLF = prevCharIsCR = false;
00275 tokenBegin = inBuf = maxNextCharInd = 0;
00276 bufpos = -1;
00277 }
00278
00279 public void ReInit(java.io.Reader dstream, int startline,
00280 int startcolumn)
00281 {
00282 ReInit(dstream, startline, startcolumn, 4096);
00283 }
00284
00285 public void ReInit(java.io.Reader dstream)
00286 {
00287 ReInit(dstream, 1, 1, 4096);
00288 }
00289 public SimpleCharStream(java.io.InputStream dstream, int startline,
00290 int startcolumn, int buffersize)
00291 {
00292 this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
00293 }
00294
00295 public SimpleCharStream(java.io.InputStream dstream, int startline,
00296 int startcolumn)
00297 {
00298 this(dstream, startline, startcolumn, 4096);
00299 }
00300
00301 public SimpleCharStream(java.io.InputStream dstream)
00302 {
00303 this(dstream, 1, 1, 4096);
00304 }
00305
00306 public void ReInit(java.io.InputStream dstream, int startline,
00307 int startcolumn, int buffersize)
00308 {
00309 ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
00310 }
00311
00312 public void ReInit(java.io.InputStream dstream)
00313 {
00314 ReInit(dstream, 1, 1, 4096);
00315 }
00316 public void ReInit(java.io.InputStream dstream, int startline,
00317 int startcolumn)
00318 {
00319 ReInit(dstream, startline, startcolumn, 4096);
00320 }
00321 public String GetImage()
00322 {
00323 if (bufpos >= tokenBegin)
00324 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
00325 else
00326 return new String(buffer, tokenBegin, bufsize - tokenBegin) +
00327 new String(buffer, 0, bufpos + 1);
00328 }
00329
00330 public char[] GetSuffix(int len)
00331 {
00332 char[] ret = new char[len];
00333
00334 if ((bufpos + 1) >= len)
00335 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
00336 else
00337 {
00338 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
00339 len - bufpos - 1);
00340 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
00341 }
00342
00343 return ret;
00344 }
00345
00346 public void Done()
00347 {
00348 buffer = null;
00349 bufline = null;
00350 bufcolumn = null;
00351 }
00352
00356 public void adjustBeginLineColumn(int newLine, int newCol)
00357 {
00358 int start = tokenBegin;
00359 int len;
00360
00361 if (bufpos >= tokenBegin)
00362 {
00363 len = bufpos - tokenBegin + inBuf + 1;
00364 }
00365 else
00366 {
00367 len = bufsize - tokenBegin + bufpos + 1 + inBuf;
00368 }
00369
00370 int i = 0, j = 0, k = 0;
00371 int nextColDiff = 0, columnDiff = 0;
00372
00373 while (i < len &&
00374 bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
00375 {
00376 bufline[j] = newLine;
00377 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
00378 bufcolumn[j] = newCol + columnDiff;
00379 columnDiff = nextColDiff;
00380 i++;
00381 }
00382
00383 if (i < len)
00384 {
00385 bufline[j] = newLine++;
00386 bufcolumn[j] = newCol + columnDiff;
00387
00388 while (i++ < len)
00389 {
00390 if (bufline[j = start % bufsize] != bufline[++start % bufsize])
00391 bufline[j] = newLine++;
00392 else
00393 bufline[j] = newLine;
00394 }
00395 }
00396
00397 line = bufline[j];
00398 column = bufcolumn[j];
00399 }
00400
00401 }