KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.text.CharacterIterator JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.IDocument;
19
20
21 /**
22  * An <code>IDocument</code> based implementation of
23  * <code>CharacterIterator</code> and <code>CharSequence</code>. Note that
24  * the supplied document is not copied; if the document is modified during the
25  * lifetime of a <code>DocumentCharacterIterator</code>, the methods
26  * returning document content may not always return the same values. Also, if
27  * accessing the document fails with a {@link BadLocationException}, any of
28  * <code>CharacterIterator</code> methods as well as <code>charAt</code>may
29  * return {@link CharacterIterator#DONE}.
30  *
31  * @since 3.0
32  */

33 public class DocumentCharacterIterator implements CharacterIterator JavaDoc, CharSequence JavaDoc {
34
35     private int fIndex= -1;
36     private final IDocument fDocument;
37     private final int fFirst;
38     private final int fLast;
39
40     private void invariant() {
41         Assert.isTrue(fIndex >= fFirst);
42         Assert.isTrue(fIndex <= fLast);
43     }
44
45     /**
46      * Creates an iterator for the entire document.
47      *
48      * @param document the document backing this iterator
49      */

50     public DocumentCharacterIterator(IDocument document) {
51         this(document, 0);
52     }
53
54     /**
55      * Creates an iterator, starting at offset <code>first</code>.
56      *
57      * @param document the document backing this iterator
58      * @param first the first character to consider
59      * @throws IllegalArgumentException if the indices are out of bounds
60      */

61     public DocumentCharacterIterator(IDocument document, int first) throws IllegalArgumentException JavaDoc {
62         this(document, first, document.getLength());
63     }
64
65     /**
66      * Creates an iterator for the document contents from <code>first</code>
67      * (inclusive) to <code>last</code> (exclusive).
68      *
69      * @param document the document backing this iterator
70      * @param first the first character to consider
71      * @param last the last character index to consider
72      * @throws IllegalArgumentException if the indices are out of bounds
73      */

74     public DocumentCharacterIterator(IDocument document, int first, int last) throws IllegalArgumentException JavaDoc {
75         if (document == null)
76             throw new NullPointerException JavaDoc();
77         if (first < 0 || first > last)
78             throw new IllegalArgumentException JavaDoc();
79         if (last > document.getLength())
80             throw new IllegalArgumentException JavaDoc();
81         fDocument= document;
82         fFirst= first;
83         fLast= last;
84         fIndex= first;
85         invariant();
86     }
87
88     /*
89      * @see java.text.CharacterIterator#first()
90      */

91     public char first() {
92         return setIndex(getBeginIndex());
93     }
94
95     /*
96      * @see java.text.CharacterIterator#last()
97      */

98     public char last() {
99         if (fFirst == fLast)
100             return setIndex(getEndIndex());
101         else
102             return setIndex(getEndIndex() - 1);
103     }
104
105     /*
106      * @see java.text.CharacterIterator#current()
107      */

108     public char current() {
109         if (fIndex >= fFirst && fIndex < fLast)
110             try {
111                 return fDocument.getChar(fIndex);
112             } catch (BadLocationException e) {
113                 // ignore
114
}
115         return DONE;
116     }
117
118     /*
119      * @see java.text.CharacterIterator#next()
120      */

121     public char next() {
122         return setIndex(Math.min(fIndex + 1, getEndIndex()));
123     }
124
125     /*
126      * @see java.text.CharacterIterator#previous()
127      */

128     public char previous() {
129         if (fIndex > getBeginIndex()) {
130             return setIndex(fIndex - 1);
131         } else {
132             return DONE;
133         }
134     }
135
136     /*
137      * @see java.text.CharacterIterator#setIndex(int)
138      */

139     public char setIndex(int position) {
140         if (position >= getBeginIndex() && position <= getEndIndex())
141             fIndex= position;
142         else
143             throw new IllegalArgumentException JavaDoc();
144
145         invariant();
146         return current();
147     }
148
149     /*
150      * @see java.text.CharacterIterator#getBeginIndex()
151      */

152     public int getBeginIndex() {
153         return fFirst;
154     }
155
156     /*
157      * @see java.text.CharacterIterator#getEndIndex()
158      */

159     public int getEndIndex() {
160         return fLast;
161     }
162
163     /*
164      * @see java.text.CharacterIterator#getIndex()
165      */

166     public int getIndex() {
167         return fIndex;
168     }
169
170     /*
171      * @see java.text.CharacterIterator#clone()
172      */

173     public Object JavaDoc clone() {
174         try {
175             return super.clone();
176         } catch (CloneNotSupportedException JavaDoc e) {
177             throw new InternalError JavaDoc();
178         }
179     }
180
181     /*
182      * @see java.lang.CharSequence#length()
183      */

184     public int length() {
185         return getEndIndex() - getBeginIndex();
186     }
187
188     /**
189      * {@inheritDoc}
190      * <p>
191      * Note that, if the document is modified concurrently, this method may
192      * return {@link CharacterIterator#DONE} if a {@link BadLocationException}
193      * was thrown when accessing the backing document.
194      * </p>
195      *
196      * @param index {@inheritDoc}
197      * @return {@inheritDoc}
198      */

199     public char charAt(int index) {
200         if (index >= 0 && index < length())
201             try {
202                 return fDocument.getChar(getBeginIndex() + index);
203             } catch (BadLocationException e) {
204                 // ignore and return DONE
205
return DONE;
206             }
207         else
208             throw new IndexOutOfBoundsException JavaDoc();
209     }
210
211     /*
212      * @see java.lang.CharSequence#subSequence(int, int)
213      */

214     public CharSequence JavaDoc subSequence(int start, int end) {
215         if (start < 0)
216             throw new IndexOutOfBoundsException JavaDoc();
217         if (end < start)
218             throw new IndexOutOfBoundsException JavaDoc();
219         if (end > length())
220             throw new IndexOutOfBoundsException JavaDoc();
221         return new DocumentCharacterIterator(fDocument, getBeginIndex() + start, getBeginIndex() + end);
222     }
223 }
224
Popular Tags