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);
74 content.getChars(0, fBufferLength, fBuffer, 0);
75 } catch (BadLocationException e) {
80 * Configures the scanner by providing access to the document range over which to scan.
82 * @param document the document to scan
83 * @param offset the offset of the document range to scan
84 * @param length the length of the document range to scan
86 public final void setRange(IDocument document, int offset, int length) {
92 String[] delimiters= document.getLegalLineDelimiters();
93 fDelimiters= new char[delimiters.length][];
94 for (int i= 0; i < delimiters.length; i++)
95 fDelimiters[i]= delimiters[i].toCharArray();
102 * @see ICharacterScanner#read()
104 public final int read() {
106 if (fOffset == fBufferLength) {
107 if (fBufferOffset + fBufferLength == fDocument.getLength())
110 updateBuffer(fBufferOffset + fBufferLength);
115 return fBuffer[fOffset++];
116 } catch (ArrayIndexOutOfBoundsException e) {
117 System.out.println("Offset:"+fOffset);
118 System.out.println("Buffer:"+fBuffer);
124 * @see ICharacterScanner#unread
126 public final void unread() {
129 if (fBufferOffset == fRangeOffset) {
132 updateBuffer(fBufferOffset - fBuffer.length);
133 fOffset= fBuffer.length - 1;
141 * @see ICharacterScanner#getColumn()
143 public final int getColumn() {
146 final int offset= fBufferOffset + fOffset;
147 final int line= fDocument.getLineOfOffset(offset);
148 final int start= fDocument.getLineOffset(line);
149 return offset - start;
150 } catch (BadLocationException e) {
157 * @see ICharacterScanner#getLegalLineDelimiters()
159 public final char[][] getLegalLineDelimiters() {