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