1/* 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26/* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.0 */ 27package com.sun.xml.internal.xsom.impl.scd; 28 29/** 30 * An implementation of interface CharStream, where the stream is assumed to 31 * contain only ASCII characters (without unicode processing). 32 */ 33 34public class SimpleCharStream 35{ 36 public static final boolean staticFlag = false; 37 int bufsize; 38 int available; 39 int tokenBegin; 40 public int bufpos = -1; 41 protected int bufline[]; 42 protected int bufcolumn[]; 43 44 protected int column = 0; 45 protected int line = 1; 46 47 protected boolean prevCharIsCR = false; 48 protected boolean prevCharIsLF = false; 49 50 protected java.io.Reader inputStream; 51 52 protected char[] buffer; 53 protected int maxNextCharInd = 0; 54 protected int inBuf = 0; 55 protected int tabSize = 8; 56 57 protected void setTabSize(int i) { tabSize = i; } 58 protected int getTabSize(int i) { return tabSize; } 59 60 61 protected void ExpandBuff(boolean wrapAround) 62 { 63 char[] newbuffer = new char[bufsize + 2048]; 64 int newbufline[] = new int[bufsize + 2048]; 65 int newbufcolumn[] = new int[bufsize + 2048]; 66 67 try 68 { 69 if (wrapAround) 70 { 71 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 72 System.arraycopy(buffer, 0, newbuffer, 73 bufsize - tokenBegin, bufpos); 74 buffer = newbuffer; 75 76 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 77 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos); 78 bufline = newbufline; 79 80 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 81 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos); 82 bufcolumn = newbufcolumn; 83 84 maxNextCharInd = (bufpos += (bufsize - tokenBegin)); 85 } 86 else 87 { 88 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 89 buffer = newbuffer; 90 91 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 92 bufline = newbufline; 93 94 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 95 bufcolumn = newbufcolumn; 96 97 maxNextCharInd = (bufpos -= tokenBegin); 98 } 99 } 100 catch (Throwable t) 101 { 102 throw new Error(t.getMessage()); 103 } 104 105 106 bufsize += 2048; 107 available = bufsize; 108 tokenBegin = 0; 109 } 110 111 protected void FillBuff() throws java.io.IOException 112 { 113 if (maxNextCharInd == available) 114 { 115 if (available == bufsize) 116 { 117 if (tokenBegin > 2048) 118 { 119 bufpos = maxNextCharInd = 0; 120 available = tokenBegin; 121 } 122 else if (tokenBegin < 0) 123 bufpos = maxNextCharInd = 0; 124 else 125 ExpandBuff(false); 126 } 127 else if (available > tokenBegin) 128 available = bufsize; 129 else if ((tokenBegin - available) < 2048) 130 ExpandBuff(true); 131 else 132 available = tokenBegin; 133 } 134 135 int i; 136 try { 137 if ((i = inputStream.read(buffer, maxNextCharInd, 138 available - maxNextCharInd)) == -1) 139 { 140 inputStream.close(); 141 throw new java.io.IOException(); 142 } 143 else 144 maxNextCharInd += i; 145 return; 146 } 147 catch(java.io.IOException e) { 148 --bufpos; 149 backup(0); 150 if (tokenBegin == -1) 151 tokenBegin = bufpos; 152 throw e; 153 } 154 } 155 156 public char BeginToken() throws java.io.IOException 157 { 158 tokenBegin = -1; 159 char c = readChar(); 160 tokenBegin = bufpos; 161 162 return c; 163 } 164 165 protected void UpdateLineColumn(char c) 166 { 167 column++; 168 169 if (prevCharIsLF) 170 { 171 prevCharIsLF = false; 172 line += (column = 1); 173 } 174 else if (prevCharIsCR) 175 { 176 prevCharIsCR = false; 177 if (c == '\n') 178 { 179 prevCharIsLF = true; 180 } 181 else 182 line += (column = 1); 183 } 184 185 switch (c) 186 { 187 case '\r' : 188 prevCharIsCR = true; 189 break; 190 case '\n' : 191 prevCharIsLF = true; 192 break; 193 case '\t' : 194 column--; 195 column += (tabSize - (column % tabSize)); 196 break; 197 default : 198 break; 199 } 200 201 bufline[bufpos] = line; 202 bufcolumn[bufpos] = column; 203 } 204 205 public char readChar() throws java.io.IOException 206 { 207 if (inBuf > 0) 208 { 209 --inBuf; 210 211 if (++bufpos == bufsize) 212 bufpos = 0; 213 214 return buffer[bufpos]; 215 } 216 217 if (++bufpos >= maxNextCharInd) 218 FillBuff(); 219 220 char c = buffer[bufpos]; 221 222 UpdateLineColumn(c); 223 return (c); 224 } 225 226 /** 227 * @deprecated 228 * @see #getEndColumn 229 */ 230 231 public int getColumn() { 232 return bufcolumn[bufpos]; 233 } 234 235 /** 236 * @deprecated 237 * @see #getEndLine 238 */ 239 240 public int getLine() { 241 return bufline[bufpos]; 242 } 243 244 public int getEndColumn() { 245 return bufcolumn[bufpos]; 246 } 247 248 public int getEndLine() { 249 return bufline[bufpos]; 250 } 251 252 public int getBeginColumn() { 253 return bufcolumn[tokenBegin]; 254 } 255 256 public int getBeginLine() { 257 return bufline[tokenBegin]; 258 } 259 260 public void backup(int amount) { 261 262 inBuf += amount; 263 if ((bufpos -= amount) < 0) 264 bufpos += bufsize; 265 } 266 267 public SimpleCharStream(java.io.Reader dstream, int startline, 268 int startcolumn, int buffersize) 269 { 270 inputStream = dstream; 271 line = startline; 272 column = startcolumn - 1; 273 274 available = bufsize = buffersize; 275 buffer = new char[buffersize]; 276 bufline = new int[buffersize]; 277 bufcolumn = new int[buffersize]; 278 } 279 280 public SimpleCharStream(java.io.Reader dstream, int startline, 281 int startcolumn) 282 { 283 this(dstream, startline, startcolumn, 4096); 284 } 285 286 public SimpleCharStream(java.io.Reader dstream) 287 { 288 this(dstream, 1, 1, 4096); 289 } 290 public void ReInit(java.io.Reader dstream, int startline, 291 int startcolumn, int buffersize) 292 { 293 inputStream = dstream; 294 line = startline; 295 column = startcolumn - 1; 296 297 if (buffer == null || buffersize != buffer.length) 298 { 299 available = bufsize = buffersize; 300 buffer = new char[buffersize]; 301 bufline = new int[buffersize]; 302 bufcolumn = new int[buffersize]; 303 } 304 prevCharIsLF = prevCharIsCR = false; 305 tokenBegin = inBuf = maxNextCharInd = 0; 306 bufpos = -1; 307 } 308 309 public void ReInit(java.io.Reader dstream, int startline, 310 int startcolumn) 311 { 312 ReInit(dstream, startline, startcolumn, 4096); 313 } 314 315 public void ReInit(java.io.Reader dstream) 316 { 317 ReInit(dstream, 1, 1, 4096); 318 } 319 public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline, 320 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException 321 { 322 this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize); 323 } 324 325 public SimpleCharStream(java.io.InputStream dstream, int startline, 326 int startcolumn, int buffersize) 327 { 328 this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize); 329 } 330 331 public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline, 332 int startcolumn) throws java.io.UnsupportedEncodingException 333 { 334 this(dstream, encoding, startline, startcolumn, 4096); 335 } 336 337 public SimpleCharStream(java.io.InputStream dstream, int startline, 338 int startcolumn) 339 { 340 this(dstream, startline, startcolumn, 4096); 341 } 342 343 public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException 344 { 345 this(dstream, encoding, 1, 1, 4096); 346 } 347 348 public SimpleCharStream(java.io.InputStream dstream) 349 { 350 this(dstream, 1, 1, 4096); 351 } 352 353 public void ReInit(java.io.InputStream dstream, String encoding, int startline, 354 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException 355 { 356 ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize); 357 } 358 359 public void ReInit(java.io.InputStream dstream, int startline, 360 int startcolumn, int buffersize) 361 { 362 ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize); 363 } 364 365 public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException 366 { 367 ReInit(dstream, encoding, 1, 1, 4096); 368 } 369 370 public void ReInit(java.io.InputStream dstream) 371 { 372 ReInit(dstream, 1, 1, 4096); 373 } 374 public void ReInit(java.io.InputStream dstream, String encoding, int startline, 375 int startcolumn) throws java.io.UnsupportedEncodingException 376 { 377 ReInit(dstream, encoding, startline, startcolumn, 4096); 378 } 379 public void ReInit(java.io.InputStream dstream, int startline, 380 int startcolumn) 381 { 382 ReInit(dstream, startline, startcolumn, 4096); 383 } 384 public String GetImage() 385 { 386 if (bufpos >= tokenBegin) 387 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1); 388 else 389 return new String(buffer, tokenBegin, bufsize - tokenBegin) + 390 new String(buffer, 0, bufpos + 1); 391 } 392 393 public char[] GetSuffix(int len) 394 { 395 char[] ret = new char[len]; 396 397 if ((bufpos + 1) >= len) 398 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len); 399 else 400 { 401 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, 402 len - bufpos - 1); 403 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1); 404 } 405 406 return ret; 407 } 408 409 public void Done() 410 { 411 buffer = null; 412 bufline = null; 413 bufcolumn = null; 414 } 415 416 /** 417 * Method to adjust line and column numbers for the start of a token. 418 */ 419 public void adjustBeginLineColumn(int newLine, int newCol) 420 { 421 int start = tokenBegin; 422 int len; 423 424 if (bufpos >= tokenBegin) 425 { 426 len = bufpos - tokenBegin + inBuf + 1; 427 } 428 else 429 { 430 len = bufsize - tokenBegin + bufpos + 1 + inBuf; 431 } 432 433 int i = 0, j = 0, k = 0; 434 int nextColDiff = 0, columnDiff = 0; 435 436 while (i < len && 437 bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) 438 { 439 bufline[j] = newLine; 440 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j]; 441 bufcolumn[j] = newCol + columnDiff; 442 columnDiff = nextColDiff; 443 i++; 444 } 445 446 if (i < len) 447 { 448 bufline[j] = newLine++; 449 bufcolumn[j] = newCol + columnDiff; 450 451 while (i++ < len) 452 { 453 if (bufline[j = start % bufsize] != bufline[++start % bufsize]) 454 bufline[j] = newLine++; 455 else 456 bufline[j] = newLine; 457 } 458 } 459 460 line = bufline[j]; 461 column = bufcolumn[j]; 462 } 463 464} 465