1 30 31 package de.susebox.jtopas; 32 33 import de.susebox.java.lang.ExtIndexOutOfBoundsException; 37 import de.susebox.jtopas.spi.DataProvider; 38 39 40 44 63 public class StandardTokenizer 64 extends AbstractTokenizer 65 implements Tokenizer, TokenizerPropertyListener 66 { 67 71 80 public StandardTokenizer() {} 81 82 89 public StandardTokenizer(TokenizerProperties properties) { 90 super.setTokenizerProperties(properties); 91 } 92 93 94 98 106 public int getRangeStart() { 107 return _rangeStart; 108 } 109 110 117 public void setSource(TokenizerSource source) { 118 super.setSource(source); 119 _hasBeenRead = false; 120 _rangeStart = 0; 121 try { 122 _charSequenceTokenizerSource = (CharSequenceTokenizerSource)getSource(); 123 _dataProvider = new StringDataProvider(_charSequenceTokenizerSource, 0, 0); 124 } catch (ClassCastException ex) { 125 _charSequenceTokenizerSource = null; 126 _dataProvider = new CharArrayDataProvider(_inputBuffer, 0, 0); 127 } 128 } 129 130 133 public void close() { 134 _inputBuffer = null; 135 _rangeStart = 0; 136 _hasBeenRead = false; 137 _charSequenceTokenizerSource = null; 138 _dataProvider = null; 139 super.close(); 140 } 141 142 143 147 153 protected DataProvider getDataProvider(int startPos, int length) { 154 _dataProvider.setDataRange(startPos - getRangeStart(), length); 155 return _dataProvider; 156 } 157 158 168 protected int readMoreData() throws TokenizerException { 169 if (_charSequenceTokenizerSource != null) { 170 if (_hasBeenRead || _charSequenceTokenizerSource.length() <= 0) { 172 return -1; 173 } else { 174 _hasBeenRead = true; 175 return _charSequenceTokenizerSource.length(); 176 } 177 178 } else { 179 if (_inputBuffer == null) { 181 if (isFlagSet(Flags.F_KEEP_DATA)) { 182 _inputBuffer = new char[LARGE_BUFFER_INITSIZE]; } else { 184 _inputBuffer = new char[SMALL_BUFFER_INITSIZE]; } 186 ((CharArrayDataProvider)_dataProvider).setData(_inputBuffer); 187 } 188 189 int readPos = getReadPosition() - getRangeStart(); 193 int writePos = currentlyAvailable(); 194 195 if ( ! isFlagSet(Flags.F_KEEP_DATA)) { 196 if ((readPos > _inputBuffer.length / 4) && (writePos > (3 * _inputBuffer.length) / 4)) { 197 reorganizeInputBuffer(_inputBuffer); 198 writePos = currentlyAvailable(); 199 } 200 } 201 202 if (writePos >= _inputBuffer.length) { 205 _inputBuffer = reorganizeInputBuffer(new char[_inputBuffer.length * 2]); 206 writePos = currentlyAvailable(); 207 ((CharArrayDataProvider)_dataProvider).setData(_inputBuffer); 208 } 209 210 int chars = 0; 212 213 while (chars == 0) { 214 try { 215 chars = getSource().read(_inputBuffer, writePos, _inputBuffer.length - writePos); 216 } catch (Exception ex) { 217 throw new TokenizerException(ex); 218 } 219 } 220 return chars; 221 } 222 } 223 224 227 private char[] reorganizeInputBuffer(char[] newBuffer) { 228 int readPos = getReadPosition() - getRangeStart(); 229 int writePos = currentlyAvailable(); 230 231 if ( ! isFlagSet(Flags.F_KEEP_DATA)) { 232 System.arraycopy(_inputBuffer, readPos, newBuffer, 0, writePos - readPos); 233 _rangeStart += readPos; 234 } else { 235 System.arraycopy(_inputBuffer, 0, newBuffer, 0, writePos); 236 } 237 return newBuffer; 238 } 239 240 241 245 249 private abstract class AbstractDataProvider implements DataProvider { 250 251 258 public AbstractDataProvider(int startPosition, int length) { 259 setDataRange(startPosition, length); 260 } 261 262 269 public int getStartPosition() { 270 return _startPosition; 271 } 272 273 281 public int getLength() { 282 return _length; 283 } 284 285 292 protected void setDataRange(int startPosition, int length) { 293 _startPosition = startPosition; 294 _length = length; 295 } 296 297 protected int _startPosition; 299 protected int _length; 300 } 301 302 306 private final class CharArrayDataProvider extends AbstractDataProvider implements DataProvider { 307 308 312 public CharArrayDataProvider(char[] data, int startPosition, int length) { 313 super(startPosition, length); 314 setData(data); 315 _dataAsString = null; 316 } 317 318 324 public char getCharAt(int index) { 325 return _data[_startPosition + index]; 326 } 327 328 333 public char[] getData() { 334 return _data; 335 } 336 337 344 public char[] getDataCopy() { 345 char[] copy = new char[getLength()]; 346 347 System.arraycopy(_data, getStartPosition(), copy, 0, copy.length); 348 return copy; 349 } 350 351 357 public String toString() { 358 if (_dataAsString == null) { 359 if (_data != null) { 360 _dataAsString = new String (_data, _startPosition, _length); 361 } else { 362 _dataAsString = ""; 363 } 364 } 365 return _dataAsString; 366 } 367 368 371 protected void setData(char[] data) { 372 _data = data; 373 } 374 375 382 protected void setDataRange(int startPosition, int length) { 383 super.setDataRange(startPosition, length); 384 _dataAsString = null; 385 } 386 387 private char[] _data; 389 private String _dataAsString; 390 } 391 392 393 397 private final class StringDataProvider extends AbstractDataProvider implements DataProvider { 398 399 403 public StringDataProvider(CharSequenceTokenizerSource source, int startPosition, int length) { 404 super(startPosition, length); 405 setData(source); 406 } 407 408 409 413 419 public char getCharAt(int index) { 420 return _source.charAt(_startPosition + index); 421 } 422 423 428 public char[] getData() { 429 return _source.toString().toCharArray(); 430 } 431 432 439 public char[] getDataCopy() { 440 return toString().toCharArray(); 441 } 442 443 449 public String toString() { 450 return _source.subSequence(_startPosition, _startPosition + _length).toString(); 451 } 452 453 456 protected void setData(CharSequenceTokenizerSource source) { 457 _source = source; 458 } 459 460 private CharSequenceTokenizerSource _source; 462 } 463 464 465 469 472 private static final int SMALL_BUFFER_INITSIZE = 0x2000; private static final int LARGE_BUFFER_INITSIZE = 0x10000; 475 476 480 484 protected char[] _inputBuffer = null; 485 486 490 protected int _rangeStart = 0; 491 492 495 protected boolean _hasBeenRead = false; 496 497 501 protected CharSequenceTokenizerSource _charSequenceTokenizerSource = null; 502 503 509 protected AbstractDataProvider _dataProvider = null; 510 } 511 | Popular Tags |