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