1) Moved net.sourceforge.phpeclipse.ui\src\net\sourceforge\phpdt back to net.sourcefo...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / BufferedDocumentScanner.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
12
13 //incastrix
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;
19
20 /**
21  * A buffered document scanner. The buffer always contains a section of a fixed
22  * size of the document to be scanned.
23  */
24
25 public final class BufferedDocumentScanner implements ICharacterScanner {
26
27         /** The document being scanned. */
28         private IDocument fDocument;
29
30         /** The offset of the document range to scan. */
31         private int fRangeOffset;
32
33         /** The length of the document range to scan. */
34         private int fRangeLength;
35
36         /** The delimiters of the document. */
37         private char[][] fDelimiters;
38
39         /** The buffer. */
40         private final char[] fBuffer;
41
42         /** The offset of the buffer within the document. */
43         private int fBufferOffset;
44
45         /** The valid length of the buffer for access. */
46         private int fBufferLength;
47
48         /** The offset of the scanner within the buffer. */
49         private int fOffset;
50
51         /**
52          * Creates a new buffered document scanner. The buffer size is set to the
53          * given number of characters.
54          * 
55          * @param size
56          *            the buffer size
57          */
58         public BufferedDocumentScanner(int size) {
59                 Assert.isTrue(size >= 1);
60                 fBuffer = new char[size];
61         }
62
63         /**
64          * Fills the buffer with the contens of the document starting at the given
65          * offset.
66          * 
67          * @param offset
68          *            the document offset at which the buffer starts
69          */
70         private final void updateBuffer(int offset) {
71
72                 fBufferOffset = offset;
73
74                 if (fBufferOffset + fBuffer.length > fRangeOffset + fRangeLength)
75                         fBufferLength = fRangeLength - (fBufferOffset - fRangeOffset);
76                 else
77                         fBufferLength = fBuffer.length;
78
79                 try {
80                         final String content = fDocument.get(fBufferOffset, fBufferLength);
81                         if (content != null) {
82                                 content.getChars(0, fBufferLength, fBuffer, 0);
83                         }
84                 } catch (BadLocationException e) {
85                 }
86         }
87
88         /**
89          * Configures the scanner by providing access to the document range over
90          * which to scan.
91          * 
92          * @param document
93          *            the document to scan
94          * @param offset
95          *            the offset of the document range to scan
96          * @param length
97          *            the length of the document range to scan
98          */
99         public final void setRange(IDocument document, int offset, int length) {
100
101                 fDocument = document;
102                 fRangeOffset = offset;
103                 fRangeLength = length;
104
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();
109
110                 updateBuffer(offset);
111                 fOffset = 0;
112         }
113
114         /*
115          * @see ICharacterScanner#read()
116          */
117         public final int read() {
118
119                 if (fOffset == fBufferLength) {
120                         if (fBufferOffset + fBufferLength == fDocument.getLength())
121                                 return EOF;
122                         else {
123                                 updateBuffer(fBufferOffset + fBufferLength);
124                                 fOffset = 0;
125                         }
126                 }
127                 try {
128                         return fBuffer[fOffset++];
129                 } catch (ArrayIndexOutOfBoundsException e) {
130                         System.out.println("Offset:" + fOffset);
131                         System.out.println("Buffer:" + fBuffer.toString());
132                         throw e;
133                 }
134         }
135
136         /*
137          * @see ICharacterScanner#unread
138          */
139         public final void unread() {
140
141                 if (fOffset == 0) {
142                         if (fBufferOffset == fRangeOffset) {
143                                 // error: BOF
144                         } else {
145                                 updateBuffer(fBufferOffset - fBuffer.length);
146                                 fOffset = fBuffer.length - 1;
147                         }
148                 } else {
149                         --fOffset;
150                 }
151         }
152
153         /*
154          * @see ICharacterScanner#getColumn()
155          */
156         public final int getColumn() {
157
158                 try {
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) {
164                 }
165
166                 return -1;
167         }
168
169         /*
170          * @see ICharacterScanner#getLegalLineDelimiters()
171          */
172         public final char[][] getLegalLineDelimiters() {
173                 return fDelimiters;
174         }
175 }