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;
 
  13 import org.eclipse.jface.text.Assert;
 
  14 import org.eclipse.jface.text.BadLocationException;
 
  15 import org.eclipse.jface.text.IDocument;
 
  16 import org.eclipse.jface.text.rules.ICharacterScanner;
 
  19  * A buffered document scanner. The buffer always contains a section of a fixed
 
  20  * size of the document to be scanned.
 
  23 public final class BufferedDocumentScanner implements ICharacterScanner {
 
  25         /** The document being scanned. */
 
  26         private IDocument fDocument;
 
  28         /** The offset of the document range to scan. */
 
  29         private int fRangeOffset;
 
  31         /** The length of the document range to scan. */
 
  32         private int fRangeLength;
 
  34         /** The delimiters of the document. */
 
  35         private char[][] fDelimiters;
 
  38         private final char[] fBuffer;
 
  40         /** The offset of the buffer within the document. */
 
  41         private int fBufferOffset;
 
  43         /** The valid length of the buffer for access. */
 
  44         private int fBufferLength;
 
  46         /** The offset of the scanner within the buffer. */
 
  50          * Creates a new buffered document scanner. The buffer size is set to the
 
  51          * given number of characters.
 
  56         public BufferedDocumentScanner(int size) {
 
  57                 Assert.isTrue(size >= 1);
 
  58                 fBuffer = new char[size];
 
  62          * Fills the buffer with the contens of the document starting at the given
 
  66          *            the document offset at which the buffer starts
 
  68         private final void updateBuffer(int offset) {
 
  70                 fBufferOffset = offset;
 
  72                 if (fBufferOffset + fBuffer.length > fRangeOffset + fRangeLength)
 
  73                         fBufferLength = fRangeLength - (fBufferOffset - fRangeOffset);
 
  75                         fBufferLength = fBuffer.length;
 
  78                         final String content = fDocument.get(fBufferOffset, fBufferLength);
 
  79                         if (content != null) {
 
  80                                 content.getChars(0, fBufferLength, fBuffer, 0);
 
  82                 } catch (BadLocationException e) {
 
  87          * Configures the scanner by providing access to the document range over
 
  91          *            the document to scan
 
  93          *            the offset of the document range to scan
 
  95          *            the length of the document range to scan
 
  97         public final void setRange(IDocument document, int offset, int length) {
 
 100                 fRangeOffset = offset;
 
 101                 fRangeLength = length;
 
 103                 String[] delimiters = document.getLegalLineDelimiters();
 
 104                 fDelimiters = new char[delimiters.length][];
 
 105                 for (int i = 0; i < delimiters.length; i++)
 
 106                         fDelimiters[i] = delimiters[i].toCharArray();
 
 108                 updateBuffer(offset);
 
 113          * @see ICharacterScanner#read()
 
 115         public final int read() {
 
 117                 if (fOffset == fBufferLength) {
 
 118                         if (fBufferOffset + fBufferLength == fDocument.getLength())
 
 121                                 updateBuffer(fBufferOffset + fBufferLength);
 
 126                         return fBuffer[fOffset++];
 
 127                 } catch (ArrayIndexOutOfBoundsException e) {
 
 128                         System.out.println("Offset:" + fOffset);
 
 129                         System.out.println("Buffer:" + fBuffer.toString());
 
 135          * @see ICharacterScanner#unread
 
 137         public final void unread() {
 
 140                         if (fBufferOffset == fRangeOffset) {
 
 143                                 updateBuffer(fBufferOffset - fBuffer.length);
 
 144                                 fOffset = fBuffer.length - 1;
 
 152          * @see ICharacterScanner#getColumn()
 
 154         public final int getColumn() {
 
 157                         final int offset = fBufferOffset + fOffset;
 
 158                         final int line = fDocument.getLineOfOffset(offset);
 
 159                         final int start = fDocument.getLineOffset(line);
 
 160                         return offset - start;
 
 161                 } catch (BadLocationException e) {
 
 168          * @see ICharacterScanner#getLegalLineDelimiters()
 
 170         public final char[][] getLegalLineDelimiters() {