1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
13 import java.text.CharacterIterator;
15 import org.eclipse.jface.text.Assert;
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.IDocument;
20 * An <code>IDocument</code> based implementation of
21 * <code>CharacterIterator</code> and <code>CharSequence</code>. Note that
22 * the supplied document is not copied; if the document is modified during the
23 * lifetime of a <code>DocumentCharacterIterator</code>, the methods
24 * returning document content may not always return the same values. Also, if
25 * accessing the document fails with a {@link BadLocationException}, any of
26 * <code>CharacterIterator</code> methods as well as <code>charAt</code>may
27 * return {@link CharacterIterator#DONE}.
31 public class DocumentCharacterIterator implements CharacterIterator,
34 private int fIndex = -1;
36 private final IDocument fDocument;
38 private final int fFirst;
40 private final int fLast;
42 private void invariant() {
43 Assert.isTrue(fIndex >= fFirst);
44 Assert.isTrue(fIndex <= fLast);
48 * Creates an iterator for the entire document.
51 * the document backing this iterator
53 public DocumentCharacterIterator(IDocument document) {
58 * Creates an iterator, starting at offset <code>first</code>.
61 * the document backing this iterator
63 * the first character to consider
64 * @throws IllegalArgumentException
65 * if the indices are out of bounds
67 public DocumentCharacterIterator(IDocument document, int first)
68 throws IllegalArgumentException {
69 this(document, first, document.getLength());
73 * Creates an iterator for the document contents from <code>first</code>
74 * (inclusive) to <code>last</code> (exclusive).
77 * the document backing this iterator
79 * the first character to consider
81 * the last character index to consider
82 * @throws IllegalArgumentException
83 * if the indices are out of bounds
85 public DocumentCharacterIterator(IDocument document, int first, int last)
86 throws IllegalArgumentException {
88 throw new NullPointerException();
89 if (first < 0 || first > last)
90 throw new IllegalArgumentException();
91 if (last > document.getLength())
92 throw new IllegalArgumentException();
101 * @see java.text.CharacterIterator#first()
103 public char first() {
104 return setIndex(getBeginIndex());
108 * @see java.text.CharacterIterator#last()
112 return setIndex(getEndIndex());
114 return setIndex(getEndIndex() - 1);
118 * @see java.text.CharacterIterator#current()
120 public char current() {
121 if (fIndex >= fFirst && fIndex < fLast)
123 return fDocument.getChar(fIndex);
124 } catch (BadLocationException e) {
131 * @see java.text.CharacterIterator#next()
134 return setIndex(Math.min(fIndex + 1, getEndIndex()));
138 * @see java.text.CharacterIterator#previous()
140 public char previous() {
141 if (fIndex > getBeginIndex()) {
142 return setIndex(fIndex - 1);
149 * @see java.text.CharacterIterator#setIndex(int)
151 public char setIndex(int position) {
152 if (position >= getBeginIndex() && position <= getEndIndex())
155 throw new IllegalArgumentException();
162 * @see java.text.CharacterIterator#getBeginIndex()
164 public int getBeginIndex() {
169 * @see java.text.CharacterIterator#getEndIndex()
171 public int getEndIndex() {
176 * @see java.text.CharacterIterator#getIndex()
178 public int getIndex() {
183 * @see java.text.CharacterIterator#clone()
185 public Object clone() {
187 return super.clone();
188 } catch (CloneNotSupportedException e) {
189 throw new InternalError();
194 * @see java.lang.CharSequence#length()
196 public int length() {
197 return getEndIndex() - getBeginIndex();
203 * Note that, if the document is modified concurrently, this method may
204 * return {@link CharacterIterator#DONE} if a {@link BadLocationException}
205 * was thrown when accessing the backing document.
210 * @return {@inheritDoc}
212 public char charAt(int index) {
213 if (index >= 0 && index < length())
215 return fDocument.getChar(getBeginIndex() + index);
216 } catch (BadLocationException e) {
217 // ignore and return DONE
221 throw new IndexOutOfBoundsException();
225 * @see java.lang.CharSequence#subSequence(int, int)
227 public CharSequence subSequence(int start, int end) {
229 throw new IndexOutOfBoundsException();
231 throw new IndexOutOfBoundsException();
233 throw new IndexOutOfBoundsException();
234 return new DocumentCharacterIterator(fDocument,
235 getBeginIndex() + start, getBeginIndex() + end);