KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > rules > BufferedRuleBasedScanner


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
12 package org.eclipse.jface.text.rules;
13
14 import org.eclipse.core.runtime.Assert;
15
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.IDocument;
18
19 /**
20  * A buffered rule based scanner. The buffer always contains a section
21  * of a fixed size of the document to be scanned. Completely adheres to
22  * the contract of <code>RuleBasedScanner</code>.
23  */

24 public class BufferedRuleBasedScanner extends RuleBasedScanner {
25
26     /** The default buffer size. Value = 500 */
27     private final static int DEFAULT_BUFFER_SIZE= 500;
28     /** The actual size of the buffer. Initially set to <code>DEFAULT_BUFFER_SIZE</code> */
29     private int fBufferSize= DEFAULT_BUFFER_SIZE;
30     /** The buffer */
31     private char[] fBuffer= new char[DEFAULT_BUFFER_SIZE];
32     /** The offset of the document at which the buffer starts */
33     private int fStart;
34     /** The offset of the document at which the buffer ends */
35     private int fEnd;
36     /** The cached length of the document */
37     private int fDocumentLength;
38
39
40     /**
41      * Creates a new buffered rule based scanner which does
42      * not have any rule and a default buffer size of 500 characters.
43      */

44     protected BufferedRuleBasedScanner() {
45         super();
46     }
47
48     /**
49      * Creates a new buffered rule based scanner which does
50      * not have any rule. The buffer size is set to the given
51      * number of characters.
52      *
53      * @param size the buffer size
54      */

55     public BufferedRuleBasedScanner(int size) {
56         super();
57         setBufferSize(size);
58     }
59
60     /**
61      * Sets the buffer to the given number of characters.
62      *
63      * @param size the buffer size
64      */

65     protected void setBufferSize(int size) {
66         Assert.isTrue(size > 0);
67         fBufferSize= size;
68         fBuffer= new char[size];
69     }
70
71     /**
72      * Shifts the buffer so that the buffer starts at the
73      * given document offset.
74      *
75      * @param offset the document offset at which the buffer starts
76      */

77     private void shiftBuffer(int offset) {
78
79         fStart= offset;
80         fEnd= fStart + fBufferSize;
81         if (fEnd > fDocumentLength)
82             fEnd= fDocumentLength;
83
84         try {
85
86             String JavaDoc content= fDocument.get(fStart, fEnd - fStart);
87             content.getChars(0, fEnd - fStart, fBuffer, 0);
88
89         } catch (BadLocationException x) {
90         }
91     }
92
93     /*
94      * @see RuleBasedScanner#setRange(IDocument, int, int)
95      */

96     public void setRange(IDocument document, int offset, int length) {
97
98         super.setRange(document, offset, length);
99
100         fDocumentLength= document.getLength();
101         shiftBuffer(offset);
102     }
103
104     /*
105      * @see RuleBasedScanner#read()
106      */

107     public int read() {
108
109         if (fOffset >= fRangeEnd) {
110             ++ fOffset;
111             return EOF;
112         }
113
114         if (fOffset == fEnd)
115             shiftBuffer(fEnd);
116         else if (fOffset < fStart || fEnd < fOffset)
117             shiftBuffer(fOffset);
118
119         return fBuffer[fOffset++ - fStart];
120     }
121
122     /*
123      * @see RuleBasedScanner#unread()
124      */

125     public void unread() {
126
127         if (fOffset == fStart)
128             shiftBuffer(Math.max(0, fStart - (fBufferSize / 2)));
129
130         -- fOffset;
131     }
132 }
133
134
135
Popular Tags