1 package net.sourceforge.phpdt.internal.ui.text;
4 * (c) Copyright IBM Corp. 2000, 2001.
8 import org.eclipse.jface.text.BadLocationException;
9 import org.eclipse.jface.text.IDocument;
10 import org.eclipse.jface.text.rules.ICharacterScanner;
11 import org.eclipse.jface.util.Assert;
16 * A buffered document scanner. The buffer always contains a section
17 * of a fixed size of the document to be scanned.
20 public final class BufferedDocumentScanner implements ICharacterScanner {
22 /** The document being scanned. */
23 private IDocument fDocument;
24 /** The offset of the document range to scan. */
25 private int fRangeOffset;
26 /** The length of the document range to scan. */
27 private int fRangeLength;
28 /** The delimiters of the document. */
29 private char[][] fDelimiters;
32 private final char[] fBuffer;
33 /** The offset of the buffer within the document. */
34 private int fBufferOffset;
35 /** The valid length of the buffer for access. */
36 private int fBufferLength;
37 /** The offset of the scanner within the buffer. */
42 * Creates a new buffered document scanner.
43 * The buffer size is set to the given number of characters.
45 * @param size the buffer size
47 public BufferedDocumentScanner(int size) {
48 Assert.isTrue(size >= 1);
49 fBuffer= new char[size];
53 * Fills the buffer with the contens of the document starting at the given offset.
55 * @param offset the document offset at which the buffer starts
57 private final void updateBuffer(int offset) {
59 fBufferOffset= offset;
61 if (fBufferOffset + fBuffer.length > fRangeOffset + fRangeLength)
62 fBufferLength= fRangeLength - (fBufferOffset - fRangeOffset);
64 fBufferLength= fBuffer.length;
67 final String content= fDocument.get(fBufferOffset, fBufferLength);
68 content.getChars(0, fBufferLength, fBuffer, 0);
69 } catch (BadLocationException e) {
74 * Configures the scanner by providing access to the document range over which to scan.
76 * @param document the document to scan
77 * @param offset the offset of the document range to scan
78 * @param length the length of the document range to scan
80 public final void setRange(IDocument document, int offset, int length) {
86 String[] delimiters= document.getLegalLineDelimiters();
87 fDelimiters= new char[delimiters.length][];
88 for (int i= 0; i < delimiters.length; i++)
89 fDelimiters[i]= delimiters[i].toCharArray();
96 * @see ICharacterScanner#read()
98 public final int read() {
100 if (fOffset == fBufferLength) {
101 if (fBufferOffset + fBufferLength == fDocument.getLength())
104 updateBuffer(fBufferOffset + fBufferLength);
109 return fBuffer[fOffset++];
113 * @see ICharacterScanner#unread
115 public final void unread() {
118 if (fBufferOffset == fRangeOffset) {
121 updateBuffer(fBufferOffset - fBuffer.length);
122 fOffset= fBuffer.length - 1;
130 * @see ICharacterScanner#getColumn()
132 public final int getColumn() {
135 final int offset= fBufferOffset + fOffset;
136 final int line= fDocument.getLineOfOffset(offset);
137 final int start= fDocument.getLineOffset(line);
138 return offset - start;
139 } catch (BadLocationException e) {
146 * @see ICharacterScanner#getLegalLineDelimiters()
148 public final char[][] getLegalLineDelimiters() {