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;
21 * An <code>IDocument</code> based implementation of
22 * <code>CharacterIterator</code> and <code>CharSequence</code>. Note that
23 * the supplied document is not copied; if the document is modified during the
24 * lifetime of a <code>DocumentCharacterIterator</code>, the methods
25 * returning document content may not always return the same values. Also, if
26 * accessing the document fails with a {@link BadLocationException}, any of
27 * <code>CharacterIterator</code> methods as well as <code>charAt</code>may
28 * return {@link CharacterIterator#DONE}.
32 public class DocumentCharacterIterator implements CharacterIterator, CharSequence {
34 private int fIndex= -1;
35 private final IDocument fDocument;
36 private final int fFirst;
37 private final int fLast;
39 private void invariant() {
40 Assert.isTrue(fIndex >= fFirst);
41 Assert.isTrue(fIndex <= fLast);
45 * Creates an iterator for the entire document.
47 * @param document the document backing this iterator
49 public DocumentCharacterIterator(IDocument document) {
54 * Creates an iterator, starting at offset <code>first</code>.
56 * @param document the document backing this iterator
57 * @param first the first character to consider
58 * @throws IllegalArgumentException if the indices are out of bounds
60 public DocumentCharacterIterator(IDocument document, int first) throws IllegalArgumentException {
61 this(document, first, document.getLength());
65 * Creates an iterator for the document contents from <code>first</code>
66 * (inclusive) to <code>last</code> (exclusive).
68 * @param document the document backing this iterator
69 * @param first the first character to consider
70 * @param last the last character index to consider
71 * @throws IllegalArgumentException if the indices are out of bounds
73 public DocumentCharacterIterator(IDocument document, int first, int last) throws IllegalArgumentException {
75 throw new NullPointerException();
76 if (first < 0 || first > last)
77 throw new IllegalArgumentException();
78 if (last > document.getLength())
79 throw new IllegalArgumentException();
88 * @see java.text.CharacterIterator#first()
91 return setIndex(getBeginIndex());
95 * @see java.text.CharacterIterator#last()
99 return setIndex(getEndIndex());
101 return setIndex(getEndIndex() - 1);
105 * @see java.text.CharacterIterator#current()
107 public char current() {
108 if (fIndex >= fFirst && fIndex < fLast)
110 return fDocument.getChar(fIndex);
111 } catch (BadLocationException e) {
118 * @see java.text.CharacterIterator#next()
121 return setIndex(Math.min(fIndex + 1, getEndIndex()));
125 * @see java.text.CharacterIterator#previous()
127 public char previous() {
128 if (fIndex > getBeginIndex()) {
129 return setIndex(fIndex - 1);
136 * @see java.text.CharacterIterator#setIndex(int)
138 public char setIndex(int position) {
139 if (position >= getBeginIndex() && position <= getEndIndex())
142 throw new IllegalArgumentException();
149 * @see java.text.CharacterIterator#getBeginIndex()
151 public int getBeginIndex() {
156 * @see java.text.CharacterIterator#getEndIndex()
158 public int getEndIndex() {
163 * @see java.text.CharacterIterator#getIndex()
165 public int getIndex() {
170 * @see java.text.CharacterIterator#clone()
172 public Object clone() {
174 return super.clone();
175 } catch (CloneNotSupportedException e) {
176 throw new InternalError();
181 * @see java.lang.CharSequence#length()
183 public int length() {
184 return getEndIndex() - getBeginIndex();
190 * Note that, if the document is modified concurrently, this method may
191 * return {@link CharacterIterator#DONE} if a {@link BadLocationException}
192 * was thrown when accessing the backing document.
195 * @param index {@inheritDoc}
196 * @return {@inheritDoc}
198 public char charAt(int index) {
199 if (index >= 0 && index < length())
201 return fDocument.getChar(getBeginIndex() + index);
202 } catch (BadLocationException e) {
203 // ignore and return DONE
207 throw new IndexOutOfBoundsException();
211 * @see java.lang.CharSequence#subSequence(int, int)
213 public CharSequence subSequence(int start, int end) {
215 throw new IndexOutOfBoundsException();
217 throw new IndexOutOfBoundsException();
219 throw new IndexOutOfBoundsException();
220 return new DocumentCharacterIterator(fDocument, getBeginIndex() + start, getBeginIndex() + end);