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.core.runtime.Assert;
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.rules.ICharacterScanner;
21 * A buffered document scanner. The buffer always contains a section of a fixed
22 * size of the document to be scanned.
25 public final class BufferedDocumentScanner implements ICharacterScanner {
27 /** The document being scanned. */
28 private IDocument fDocument;
30 /** The offset of the document range to scan. */
31 private int fRangeOffset;
33 /** The length of the document range to scan. */
34 private int fRangeLength;
36 /** The delimiters of the document. */
37 private char[][] fDelimiters;
40 private final char[] fBuffer;
42 /** The offset of the buffer within the document. */
43 private int fBufferOffset;
45 /** The valid length of the buffer for access. */
46 private int fBufferLength;
48 /** The offset of the scanner within the buffer. */
52 * Creates a new buffered document scanner. The buffer size is set to the
53 * given number of characters.
58 public BufferedDocumentScanner(int size) {
59 Assert.isTrue(size >= 1);
60 fBuffer = new char[size];
64 * Fills the buffer with the contens of the document starting at the given
68 * the document offset at which the buffer starts
70 private final void updateBuffer(int offset) {
72 fBufferOffset = offset;
74 if (fBufferOffset + fBuffer.length > fRangeOffset + fRangeLength)
75 fBufferLength = fRangeLength - (fBufferOffset - fRangeOffset);
77 fBufferLength = fBuffer.length;
80 final String content = fDocument.get(fBufferOffset, fBufferLength);
81 if (content != null) {
82 content.getChars(0, fBufferLength, fBuffer, 0);
84 } catch (BadLocationException e) {
89 * Configures the scanner by providing access to the document range over
93 * the document to scan
95 * the offset of the document range to scan
97 * the length of the document range to scan
99 public final void setRange(IDocument document, int offset, int length) {
101 fDocument = document;
102 fRangeOffset = offset;
103 fRangeLength = length;
105 String[] delimiters = document.getLegalLineDelimiters();
106 fDelimiters = new char[delimiters.length][];
107 for (int i = 0; i < delimiters.length; i++)
108 fDelimiters[i] = delimiters[i].toCharArray();
110 updateBuffer(offset);
115 * @see ICharacterScanner#read()
117 public final int read() {
119 if (fOffset == fBufferLength) {
120 if (fBufferOffset + fBufferLength == fDocument.getLength())
123 updateBuffer(fBufferOffset + fBufferLength);
128 return fBuffer[fOffset++];
129 } catch (ArrayIndexOutOfBoundsException e) {
130 System.out.println("Offset:" + fOffset);
131 System.out.println("Buffer:" + fBuffer.toString());
137 * @see ICharacterScanner#unread
139 public final void unread() {
142 if (fBufferOffset == fRangeOffset) {
145 updateBuffer(fBufferOffset - fBuffer.length);
146 fOffset = fBuffer.length - 1;
154 * @see ICharacterScanner#getColumn()
156 public final int getColumn() {
159 final int offset = fBufferOffset + fOffset;
160 final int line = fDocument.getLineOfOffset(offset);
161 final int start = fDocument.getLineOffset(line);
162 return offset - start;
163 } catch (BadLocationException e) {
170 * @see ICharacterScanner#getLegalLineDelimiters()
172 public final char[][] getLegalLineDelimiters() {