1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
14 import org.eclipse.jface.text.Assert;
15 import org.eclipse.jface.text.BadLocationException;
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.jface.text.rules.ICharacterScanner;
22 * A buffered document scanner. The buffer always contains a section
23 * of a fixed size of the document to be scanned.
26 public final class BufferedDocumentScanner implements ICharacterScanner {
28 /** The document being scanned. */
29 private IDocument fDocument;
30 /** The offset of the document range to scan. */
31 private int fRangeOffset;
32 /** The length of the document range to scan. */
33 private int fRangeLength;
34 /** The delimiters of the document. */
35 private char[][] fDelimiters;
38 private final char[] fBuffer;
39 /** The offset of the buffer within the document. */
40 private int fBufferOffset;
41 /** The valid length of the buffer for access. */
42 private int fBufferLength;
43 /** The offset of the scanner within the buffer. */
48 * Creates a new buffered document scanner.
49 * The buffer size is set to the given number of characters.
51 * @param size the buffer size
53 public BufferedDocumentScanner(int size) {
54 Assert.isTrue(size >= 1);
55 fBuffer= new char[size];
59 * Fills the buffer with the contens of the document starting at the given offset.
61 * @param offset the document offset at which the buffer starts
63 private final void updateBuffer(int offset) {
65 fBufferOffset= offset;
67 if (fBufferOffset + fBuffer.length > fRangeOffset + fRangeLength)
68 fBufferLength= fRangeLength - (fBufferOffset - fRangeOffset);
70 fBufferLength= fBuffer.length;
73 final String content= fDocument.get(fBufferOffset, fBufferLength);
75 content.getChars(0, fBufferLength, fBuffer, 0);
77 } catch (BadLocationException e) {
82 * Configures the scanner by providing access to the document range over which to scan.
84 * @param document the document to scan
85 * @param offset the offset of the document range to scan
86 * @param length the length of the document range to scan
88 public final void setRange(IDocument document, int offset, int length) {
94 String[] delimiters= document.getLegalLineDelimiters();
95 fDelimiters= new char[delimiters.length][];
96 for (int i= 0; i < delimiters.length; i++)
97 fDelimiters[i]= delimiters[i].toCharArray();
104 * @see ICharacterScanner#read()
106 public final int read() {
108 if (fOffset == fBufferLength) {
109 if (fBufferOffset + fBufferLength == fDocument.getLength())
112 updateBuffer(fBufferOffset + fBufferLength);
117 return fBuffer[fOffset++];
118 } catch (ArrayIndexOutOfBoundsException e) {
119 System.out.println("Offset:"+fOffset);
120 System.out.println("Buffer:"+fBuffer.toString());
126 * @see ICharacterScanner#unread
128 public final void unread() {
131 if (fBufferOffset == fRangeOffset) {
134 updateBuffer(fBufferOffset - fBuffer.length);
135 fOffset= fBuffer.length - 1;
143 * @see ICharacterScanner#getColumn()
145 public final int getColumn() {
148 final int offset= fBufferOffset + fOffset;
149 final int line= fDocument.getLineOfOffset(offset);
150 final int start= fDocument.getLineOffset(line);
151 return offset - start;
152 } catch (BadLocationException e) {
159 * @see ICharacterScanner#getLegalLineDelimiters()
161 public final char[][] getLegalLineDelimiters() {