1 2 3 21 22 23 package org.apache.cayenne.wocompat.parser; 24 25 29 30 public class SimpleCharStream 31 { 32 public static final boolean staticFlag = false; 33 int bufsize; 34 int available; 35 int tokenBegin; 36 public int bufpos = -1; 37 protected int bufline[]; 38 protected int bufcolumn[]; 39 40 protected int column = 0; 41 protected int line = 1; 42 43 protected boolean prevCharIsCR = false; 44 protected boolean prevCharIsLF = false; 45 46 protected java.io.Reader inputStream; 47 48 protected char[] buffer; 49 protected int maxNextCharInd = 0; 50 protected int inBuf = 0; 51 52 protected void ExpandBuff(boolean wrapAround) 53 { 54 char[] newbuffer = new char[bufsize + 2048]; 55 int newbufline[] = new int[bufsize + 2048]; 56 int newbufcolumn[] = new int[bufsize + 2048]; 57 58 try 59 { 60 if (wrapAround) 61 { 62 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 63 System.arraycopy(buffer, 0, newbuffer, 64 bufsize - tokenBegin, bufpos); 65 buffer = newbuffer; 66 67 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 68 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos); 69 bufline = newbufline; 70 71 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 72 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos); 73 bufcolumn = newbufcolumn; 74 75 maxNextCharInd = (bufpos += (bufsize - tokenBegin)); 76 } 77 else 78 { 79 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 80 buffer = newbuffer; 81 82 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 83 bufline = newbufline; 84 85 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 86 bufcolumn = newbufcolumn; 87 88 maxNextCharInd = (bufpos -= tokenBegin); 89 } 90 } 91 catch (Throwable t) 92 { 93 throw new Error (t.getMessage()); 94 } 95 96 97 bufsize += 2048; 98 available = bufsize; 99 tokenBegin = 0; 100 } 101 102 protected void FillBuff() throws java.io.IOException 103 { 104 if (maxNextCharInd == available) 105 { 106 if (available == bufsize) 107 { 108 if (tokenBegin > 2048) 109 { 110 bufpos = maxNextCharInd = 0; 111 available = tokenBegin; 112 } 113 else if (tokenBegin < 0) 114 bufpos = maxNextCharInd = 0; 115 else 116 ExpandBuff(false); 117 } 118 else if (available > tokenBegin) 119 available = bufsize; 120 else if ((tokenBegin - available) < 2048) 121 ExpandBuff(true); 122 else 123 available = tokenBegin; 124 } 125 126 int i; 127 try { 128 if ((i = inputStream.read(buffer, maxNextCharInd, 129 available - maxNextCharInd)) == -1) 130 { 131 inputStream.close(); 132 throw new java.io.IOException (); 133 } 134 else 135 maxNextCharInd += i; 136 return; 137 } 138 catch(java.io.IOException e) { 139 --bufpos; 140 backup(0); 141 if (tokenBegin == -1) 142 tokenBegin = bufpos; 143 throw e; 144 } 145 } 146 147 public char BeginToken() throws java.io.IOException 148 { 149 tokenBegin = -1; 150 char c = readChar(); 151 tokenBegin = bufpos; 152 153 return c; 154 } 155 156 protected void UpdateLineColumn(char c) 157 { 158 column++; 159 160 if (prevCharIsLF) 161 { 162 prevCharIsLF = false; 163 line += (column = 1); 164 } 165 else if (prevCharIsCR) 166 { 167 prevCharIsCR = false; 168 if (c == '\n') 169 { 170 prevCharIsLF = true; 171 } 172 else 173 line += (column = 1); 174 } 175 176 switch (c) 177 { 178 case '\r' : 179 prevCharIsCR = true; 180 break; 181 case '\n' : 182 prevCharIsLF = true; 183 break; 184 case '\t' : 185 column--; 186 column += (8 - (column & 07)); 187 break; 188 default : 189 break; 190 } 191 192 bufline[bufpos] = line; 193 bufcolumn[bufpos] = column; 194 } 195 196 public char readChar() throws java.io.IOException 197 { 198 if (inBuf > 0) 199 { 200 --inBuf; 201 202 if (++bufpos == bufsize) 203 bufpos = 0; 204 205 return buffer[bufpos]; 206 } 207 208 if (++bufpos >= maxNextCharInd) 209 FillBuff(); 210 211 char c = buffer[bufpos]; 212 213 UpdateLineColumn(c); 214 return (c); 215 } 216 217 221 222 public int getColumn() { 223 return bufcolumn[bufpos]; 224 } 225 226 230 231 public int getLine() { 232 return bufline[bufpos]; 233 } 234 235 public int getEndColumn() { 236 return bufcolumn[bufpos]; 237 } 238 239 public int getEndLine() { 240 return bufline[bufpos]; 241 } 242 243 public int getBeginColumn() { 244 return bufcolumn[tokenBegin]; 245 } 246 247 public int getBeginLine() { 248 return bufline[tokenBegin]; 249 } 250 251 public void backup(int amount) { 252 253 inBuf += amount; 254 if ((bufpos -= amount) < 0) 255 bufpos += bufsize; 256 } 257 258 public SimpleCharStream(java.io.Reader dstream, int startline, 259 int startcolumn, int buffersize) 260 { 261 inputStream = dstream; 262 line = startline; 263 column = startcolumn - 1; 264 265 available = bufsize = buffersize; 266 buffer = new char[buffersize]; 267 bufline = new int[buffersize]; 268 bufcolumn = new int[buffersize]; 269 } 270 271 public SimpleCharStream(java.io.Reader dstream, int startline, 272 int startcolumn) 273 { 274 this(dstream, startline, startcolumn, 4096); 275 } 276 277 public SimpleCharStream(java.io.Reader dstream) 278 { 279 this(dstream, 1, 1, 4096); 280 } 281 public void ReInit(java.io.Reader dstream, int startline, 282 int startcolumn, int buffersize) 283 { 284 inputStream = dstream; 285 line = startline; 286 column = startcolumn - 1; 287 288 if (buffer == null || buffersize != buffer.length) 289 { 290 available = bufsize = buffersize; 291 buffer = new char[buffersize]; 292 bufline = new int[buffersize]; 293 bufcolumn = new int[buffersize]; 294 } 295 prevCharIsLF = prevCharIsCR = false; 296 tokenBegin = inBuf = maxNextCharInd = 0; 297 bufpos = -1; 298 } 299 300 public void ReInit(java.io.Reader dstream, int startline, 301 int startcolumn) 302 { 303 ReInit(dstream, startline, startcolumn, 4096); 304 } 305 306 public void ReInit(java.io.Reader dstream) 307 { 308 ReInit(dstream, 1, 1, 4096); 309 } 310 public SimpleCharStream(java.io.InputStream dstream, int startline, 311 int startcolumn, int buffersize) 312 { 313 this(new java.io.InputStreamReader (dstream), startline, startcolumn, 4096); 314 } 315 316 public SimpleCharStream(java.io.InputStream dstream, int startline, 317 int startcolumn) 318 { 319 this(dstream, startline, startcolumn, 4096); 320 } 321 322 public SimpleCharStream(java.io.InputStream dstream) 323 { 324 this(dstream, 1, 1, 4096); 325 } 326 327 public void ReInit(java.io.InputStream dstream, int startline, 328 int startcolumn, int buffersize) 329 { 330 ReInit(new java.io.InputStreamReader (dstream), startline, startcolumn, 4096); 331 } 332 333 public void ReInit(java.io.InputStream dstream) 334 { 335 ReInit(dstream, 1, 1, 4096); 336 } 337 public void ReInit(java.io.InputStream dstream, int startline, 338 int startcolumn) 339 { 340 ReInit(dstream, startline, startcolumn, 4096); 341 } 342 public String GetImage() 343 { 344 if (bufpos >= tokenBegin) 345 return new String (buffer, tokenBegin, bufpos - tokenBegin + 1); 346 else 347 return new String (buffer, tokenBegin, bufsize - tokenBegin) + 348 new String (buffer, 0, bufpos + 1); 349 } 350 351 public char[] GetSuffix(int len) 352 { 353 char[] ret = new char[len]; 354 355 if ((bufpos + 1) >= len) 356 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len); 357 else 358 { 359 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, 360 len - bufpos - 1); 361 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1); 362 } 363 364 return ret; 365 } 366 367 public void Done() 368 { 369 buffer = null; 370 bufline = null; 371 bufcolumn = null; 372 } 373 374 377 public void adjustBeginLineColumn(int newLine, int newCol) 378 { 379 int start = tokenBegin; 380 int len; 381 382 if (bufpos >= tokenBegin) 383 { 384 len = bufpos - tokenBegin + inBuf + 1; 385 } 386 else 387 { 388 len = bufsize - tokenBegin + bufpos + 1 + inBuf; 389 } 390 391 int i = 0, j = 0, k = 0; 392 int nextColDiff = 0, columnDiff = 0; 393 394 while (i < len && 395 bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) 396 { 397 bufline[j] = newLine; 398 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j]; 399 bufcolumn[j] = newCol + columnDiff; 400 columnDiff = nextColDiff; 401 i++; 402 } 403 404 if (i < len) 405 { 406 bufline[j] = newLine++; 407 bufcolumn[j] = newCol + columnDiff; 408 409 while (i++ < len) 410 { 411 if (bufline[j = start % bufsize] != bufline[++start % bufsize]) 412 bufline[j] = newLine++; 413 else 414 bufline[j] = newLine; 415 } 416 } 417 418 line = bufline[j]; 419 column = bufcolumn[j]; 420 } 421 422 } 423 | Popular Tags |