KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > BufferedDocumentScanner


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.text;
12
13
14 import org.eclipse.core.runtime.Assert;
15
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.rules.ICharacterScanner;
19
20 import org.eclipse.jdt.internal.ui.JavaPlugin;
21
22
23
24 /**
25  * A buffered document scanner. The buffer always contains a section
26  * of a fixed size of the document to be scanned.
27  */

28
29 public final class BufferedDocumentScanner implements ICharacterScanner {
30
31     /** The document being scanned. */
32     private IDocument fDocument;
33     /** The offset of the document range to scan. */
34     private int fRangeOffset;
35     /** The length of the document range to scan. */
36     private int fRangeLength;
37     /** The delimiters of the document. */
38     private char[][] fDelimiters;
39
40     /** The buffer. */
41     private final char[] fBuffer;
42     /** The offset of the buffer within the document. */
43     private int fBufferOffset;
44     /** The valid length of the buffer for access. */
45     private int fBufferLength;
46     /** The offset of the scanner within the buffer. */
47     private int fOffset;
48
49
50     /**
51      * Creates a new buffered document scanner.
52      * The buffer size is set to the given number of characters.
53      *
54      * @param size the buffer size
55      */

56     public BufferedDocumentScanner(int size) {
57         Assert.isTrue(size >= 1);
58         fBuffer= new char[size];
59     }
60
61     /**
62      * Fills the buffer with the contents of the document starting at the given offset.
63      *
64      * @param offset the document offset at which the buffer starts
65      */

66     private final void updateBuffer(int offset) {
67
68         fBufferOffset= offset;
69
70         if (fBufferOffset + fBuffer.length > fRangeOffset + fRangeLength)
71             fBufferLength= fRangeLength - (fBufferOffset - fRangeOffset);
72         else
73             fBufferLength= fBuffer.length;
74
75         try {
76             final String JavaDoc content= fDocument.get(fBufferOffset, fBufferLength);
77             content.getChars(0, fBufferLength, fBuffer, 0);
78         } catch (BadLocationException e) {
79         }
80     }
81
82     /**
83      * Configures the scanner by providing access to the document range over which to scan.
84      *
85      * @param document the document to scan
86      * @param offset the offset of the document range to scan
87      * @param length the length of the document range to scan
88      */

89     public final void setRange(IDocument document, int offset, int length) {
90
91         fDocument= document;
92         fRangeOffset= offset;
93         fRangeLength= length;
94
95         String JavaDoc[] delimiters= document.getLegalLineDelimiters();
96         fDelimiters= new char[delimiters.length][];
97         for (int i= 0; i < delimiters.length; i++)
98             fDelimiters[i]= delimiters[i].toCharArray();
99
100         updateBuffer(offset);
101         fOffset= 0;
102     }
103
104     /*
105      * @see ICharacterScanner#read()
106      */

107     public final int read() {
108
109         if (fOffset == fBufferLength) {
110             int end= fBufferOffset + fBufferLength;
111             if (end == fDocument.getLength() || end == fRangeOffset + fRangeLength)
112                 return EOF;
113             else {
114                 updateBuffer(fBufferOffset + fBufferLength);
115                 fOffset= 0;
116             }
117         }
118
119         try {
120             return fBuffer[fOffset++];
121         } catch (ArrayIndexOutOfBoundsException JavaDoc ex) {
122             StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
123             buf.append("Detailed state of 'BufferedDocumentScanner:'"); //$NON-NLS-1$
124
buf.append("\n\tfOffset= "); //$NON-NLS-1$
125
buf.append(fOffset);
126             buf.append("\n\tfBufferOffset= "); //$NON-NLS-1$
127
buf.append(fBufferOffset);
128             buf.append("\n\tfBufferLength= "); //$NON-NLS-1$
129
buf.append(fBufferLength);
130             buf.append("\n\tfRangeOffset= "); //$NON-NLS-1$
131
buf.append(fRangeOffset);
132             buf.append("\n\tfRangeLength= "); //$NON-NLS-1$
133
buf.append(fRangeLength);
134             JavaPlugin.logErrorMessage(buf.toString());
135             throw ex;
136         }
137     }
138
139     /*
140      * @see ICharacterScanner#unread
141      */

142     public final void unread() {
143
144         if (fOffset == 0) {
145             if (fBufferOffset == fRangeOffset) {
146                 // error: BOF
147
} else {
148                 updateBuffer(fBufferOffset - fBuffer.length);
149                 fOffset= fBuffer.length - 1;
150             }
151         } else {
152             --fOffset;
153         }
154     }
155
156     /*
157      * @see ICharacterScanner#getColumn()
158      */

159     public final int getColumn() {
160
161         try {
162             final int offset= fBufferOffset + fOffset;
163             final int line= fDocument.getLineOfOffset(offset);
164             final int start= fDocument.getLineOffset(line);
165             return offset - start;
166         } catch (BadLocationException e) {
167         }
168
169         return -1;
170     }
171
172     /*
173      * @see ICharacterScanner#getLegalLineDelimiters()
174      */

175     public final char[][] getLegalLineDelimiters() {
176         return fDelimiters;
177     }
178 }
179
Popular Tags