1 17 18 19 20 package org.apache.lenya.lucene.html; 21 22 23 27 public final class SimpleCharStream { 28 public static final boolean staticFlag = false; 29 int bufsize; 30 int available; 31 int tokenBegin; 32 public int bufpos = -1; 33 private int[] bufline; 34 private int[] bufcolumn; 35 private int column = 0; 36 private int line = 1; 37 private boolean prevCharIsCR = false; 38 private boolean prevCharIsLF = false; 39 private java.io.Reader inputStream; 40 private char[] buffer; 41 private int maxNextCharInd = 0; 42 private int inBuf = 0; 43 44 52 public SimpleCharStream(java.io.Reader dstream, int startline, int startcolumn, int buffersize) { 53 inputStream = dstream; 54 line = startline; 55 column = startcolumn - 1; 56 57 available = bufsize = buffersize; 58 buffer = new char[buffersize]; 59 bufline = new int[buffersize]; 60 bufcolumn = new int[buffersize]; 61 } 62 63 70 public SimpleCharStream(java.io.Reader dstream, int startline, int startcolumn) { 71 this(dstream, startline, startcolumn, 4096); 72 } 73 74 79 public SimpleCharStream(java.io.Reader dstream) { 80 this(dstream, 1, 1, 4096); 81 } 82 83 91 public SimpleCharStream(java.io.InputStream dstream, int startline, int startcolumn, 92 int buffersize) { 93 this(new java.io.InputStreamReader (dstream), startline, startcolumn, 4096); 94 } 95 96 103 public SimpleCharStream(java.io.InputStream dstream, int startline, int startcolumn) { 104 this(dstream, startline, startcolumn, 4096); 105 } 106 107 112 public SimpleCharStream(java.io.InputStream dstream) { 113 this(dstream, 1, 1, 4096); 114 } 115 116 private final void ExpandBuff(boolean wrapAround) { 117 char[] newbuffer = new char[bufsize + 2048]; 118 int[] newbufline = new int[bufsize + 2048]; 119 int[] newbufcolumn = new int[bufsize + 2048]; 120 121 try { 122 if (wrapAround) { 123 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 124 System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos); 125 buffer = newbuffer; 126 127 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 128 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos); 129 bufline = newbufline; 130 131 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 132 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos); 133 bufcolumn = newbufcolumn; 134 135 maxNextCharInd = (bufpos += (bufsize - tokenBegin)); 136 } else { 137 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 138 buffer = newbuffer; 139 140 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 141 bufline = newbufline; 142 143 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 144 bufcolumn = newbufcolumn; 145 146 maxNextCharInd = (bufpos -= tokenBegin); 147 } 148 } catch (Throwable t) { 149 throw new Error (t.getMessage()); 150 } 151 152 bufsize += 2048; 153 available = bufsize; 154 tokenBegin = 0; 155 } 156 157 private final void FillBuff() throws java.io.IOException { 158 if (maxNextCharInd == available) { 159 if (available == bufsize) { 160 if (tokenBegin > 2048) { 161 bufpos = maxNextCharInd = 0; 162 available = tokenBegin; 163 } else if (tokenBegin < 0) { 164 bufpos = maxNextCharInd = 0; 165 } else { 166 ExpandBuff(false); 167 } 168 } else if (available > tokenBegin) { 169 available = bufsize; 170 } else if ((tokenBegin - available) < 2048) { 171 ExpandBuff(true); 172 } else { 173 available = tokenBegin; 174 } 175 } 176 177 int i; 178 179 try { 180 if ((i = inputStream.read(buffer, maxNextCharInd, available - maxNextCharInd)) == -1) { 181 inputStream.close(); 182 throw new java.io.IOException (); 183 } else { 184 maxNextCharInd += i; 185 } 186 187 return; 188 } catch (java.io.IOException e) { 189 --bufpos; 190 backup(0); 191 192 if (tokenBegin == -1) { 193 tokenBegin = bufpos; 194 } 195 196 throw e; 197 } 198 } 199 200 207 public final char BeginToken() throws java.io.IOException { 208 tokenBegin = -1; 209 210 char c = readChar(); 211 tokenBegin = bufpos; 212 213 return c; 214 } 215 216 private final void UpdateLineColumn(char c) { 217 column++; 218 219 if (prevCharIsLF) { 220 prevCharIsLF = false; 221 line += (column = 1); 222 } else if (prevCharIsCR) { 223 prevCharIsCR = false; 224 225 if (c == '\n') { 226 prevCharIsLF = true; 227 } else { 228 line += (column = 1); 229 } 230 } 231 232 switch (c) { 233 case '\r': 234 prevCharIsCR = true; 235 236 break; 237 238 case '\n': 239 prevCharIsLF = true; 240 241 break; 242 243 case '\t': 244 column--; 245 column += (8 - (column & 07)); 246 247 break; 248 249 default: 250 break; 251 } 252 253 bufline[bufpos] = line; 254 bufcolumn[bufpos] = column; 255 } 256 257 264 public final char readChar() throws java.io.IOException { 265 if (inBuf > 0) { 266 --inBuf; 267 268 if (++bufpos == bufsize) { 269 bufpos = 0; 270 } 271 272 return buffer[bufpos]; 273 } 274 275 if (++bufpos >= maxNextCharInd) { 276 FillBuff(); 277 } 278 279 char c = buffer[bufpos]; 280 281 UpdateLineColumn(c); 282 283 return (c); 284 } 285 286 294 public final int getColumn() { 295 return bufcolumn[bufpos]; 296 } 297 298 306 public final int getLine() { 307 return bufline[bufpos]; 308 } 309 310 315 public final int getEndColumn() { 316 return bufcolumn[bufpos]; 317 } 318 319 324 public final int getEndLine() { 325 return bufline[bufpos]; 326 } 327 328 333 public final int getBeginColumn() { 334 return bufcolumn[tokenBegin]; 335 } 336 337 342 public final int getBeginLine() { 343 return bufline[tokenBegin]; 344 } 345 346 351 public final void backup(int amount) { 352 inBuf += amount; 353 354 if ((bufpos -= amount) < 0) { 355 bufpos += bufsize; 356 } 357 } 358 359 367 public void ReInit(java.io.Reader dstream, int startline, int startcolumn, int buffersize) { 368 inputStream = dstream; 369 line = startline; 370 column = startcolumn - 1; 371 372 if ((buffer == null) || (buffersize != buffer.length)) { 373 available = bufsize = buffersize; 374 buffer = new char[buffersize]; 375 bufline = new int[buffersize]; 376 bufcolumn = new int[buffersize]; 377 } 378 379 prevCharIsLF = prevCharIsCR = false; 380 tokenBegin = inBuf = maxNextCharInd = 0; 381 bufpos = -1; 382 } 383 384 391 public void ReInit(java.io.Reader dstream, int startline, int startcolumn) { 392 ReInit(dstream, startline, startcolumn, 4096); 393 } 394 395 400 public void ReInit(java.io.Reader dstream) { 401 ReInit(dstream, 1, 1, 4096); 402 } 403 404 412 public void ReInit(java.io.InputStream dstream, int startline, int startcolumn, int buffersize) { 413 ReInit(new java.io.InputStreamReader (dstream), startline, startcolumn, 4096); 414 } 415 416 421 public void ReInit(java.io.InputStream dstream) { 422 ReInit(dstream, 1, 1, 4096); 423 } 424 425 432 public void ReInit(java.io.InputStream dstream, int startline, int startcolumn) { 433 ReInit(dstream, startline, startcolumn, 4096); 434 } 435 436 441 public final String GetImage() { 442 if (bufpos >= tokenBegin) { 443 return new String (buffer, tokenBegin, bufpos - tokenBegin + 1); 444 } else { 445 return new String (buffer, tokenBegin, bufsize - tokenBegin) + 446 new String (buffer, 0, bufpos + 1); 447 } 448 } 449 450 457 public final char[] GetSuffix(int len) { 458 char[] ret = new char[len]; 459 460 if ((bufpos + 1) >= len) { 461 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len); 462 } else { 463 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, len - bufpos - 1); 464 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1); 465 } 466 467 return ret; 468 } 469 470 473 public void Done() { 474 buffer = null; 475 bufline = null; 476 bufcolumn = null; 477 } 478 479 485 public void adjustBeginLineColumn(int newLine, int newCol) { 486 int start = tokenBegin; 487 int len; 488 489 if (bufpos >= tokenBegin) { 490 len = bufpos - tokenBegin + inBuf + 1; 491 } else { 492 len = bufsize - tokenBegin + bufpos + 1 + inBuf; 493 } 494 495 int i = 0; 496 int j = 0; 497 int k = 0; 498 int nextColDiff = 0; 499 int columnDiff = 0; 500 501 while ((i < len) && (bufline[j = start % bufsize] == bufline[k = ++start % bufsize])) { 502 bufline[j] = newLine; 503 nextColDiff = (columnDiff + bufcolumn[k]) - bufcolumn[j]; 504 bufcolumn[j] = newCol + columnDiff; 505 columnDiff = nextColDiff; 506 i++; 507 } 508 509 if (i < len) { 510 bufline[j] = newLine++; 511 bufcolumn[j] = newCol + columnDiff; 512 513 while (i++ < len) { 514 if (bufline[j = start % bufsize] != bufline[++start % bufsize]) { 515 bufline[j] = newLine++; 516 } else { 517 bufline[j] = newLine; 518 } 519 } 520 } 521 522 line = bufline[j]; 523 column = bufcolumn[j]; 524 } 525 } 526 | Popular Tags |